Commit 9e91f814 authored by David Sveningsson's avatar David Sveningsson

feat(rules): add PascalCase and camelCase styles for `attr-case`

fixes #49
parent 18b14ad6
Pipeline #81690315 passed with stages
in 6 minutes and 45 seconds
......@@ -36,8 +36,10 @@ This rule takes an optional object:
### `style`
- `camelcase` requires all attribute names to be camelCase.
- `lowercase` requires all attribute names to be lowercase.
- `uppercase` requires all attribute names to be uppercase.
- `pascalcase` requires all attribute names to be PascalCase.
- `uppercase` requires all attribute names to be UPPERCASE.
### `ignoreForeign`
......
// Jest Snapshot v1, https://goo.gl/fbAQLP
exports[`rule attr-case configured with "camelcase" smoketest 1`] = `
Array [
Object {
"errorCount": 1,
"filePath": "test-files/rules/attr-case.html",
"messages": Array [
Object {
"column": 4,
"context": undefined,
"line": 2,
"message": "Attribute \\"ID\\" should be camelCase",
"offset": 23,
"ruleId": "attr-case",
"severity": 2,
"size": 2,
},
],
"source": "<p id=\\"foo\\">foo</p>
<p ID=\\"bar\\">bar</p>
<p clAss=\\"baz\\">baz</p>
",
"warningCount": 0,
},
]
`;
exports[`rule attr-case configured with "lowercase" smoketest 1`] = `
Array [
Object {
......@@ -36,6 +62,42 @@ Array [
]
`;
exports[`rule attr-case configured with "pascalcase" smoketest 1`] = `
Array [
Object {
"errorCount": 2,
"filePath": "test-files/rules/attr-case.html",
"messages": Array [
Object {
"column": 4,
"context": undefined,
"line": 1,
"message": "Attribute \\"id\\" should be PascalCase",
"offset": 3,
"ruleId": "attr-case",
"severity": 2,
"size": 2,
},
Object {
"column": 4,
"context": undefined,
"line": 3,
"message": "Attribute \\"clAss\\" should be PascalCase",
"offset": 43,
"ruleId": "attr-case",
"severity": 2,
"size": 5,
},
],
"source": "<p id=\\"foo\\">foo</p>
<p ID=\\"bar\\">bar</p>
<p clAss=\\"baz\\">baz</p>
",
"warningCount": 0,
},
]
`;
exports[`rule attr-case configured with "uppercase" smoketest 1`] = `
Array [
Object {
......
......@@ -91,6 +91,92 @@ describe("rule attr-case", () => {
});
});
describe('configured with "pascalcase"', () => {
beforeAll(() => {
htmlvalidate = new HtmlValidate({
rules: { "attr-case": ["error", { style: "pascalcase" }] },
});
});
it("should not report error when attributes is PascalCase", () => {
const report = htmlvalidate.validateString('<div FooBar="baz"></div>');
expect(report).toBeValid();
});
it("should not report error when attributes is UPPERCASE", () => {
const report = htmlvalidate.validateString('<div FOOBAR="baz"></div>');
expect(report).toBeValid();
});
it("should report error when attributes is lowercase", () => {
const report = htmlvalidate.validateString('<div foobar="baz"></div>');
expect(report).toBeInvalid();
expect(report).toHaveError(
"attr-case",
'Attribute "foobar" should be PascalCase'
);
});
it("should report error when attributes is camelCase", () => {
const report = htmlvalidate.validateString('<div fooBar="baz"></div>');
expect(report).toBeInvalid();
expect(report).toHaveError(
"attr-case",
'Attribute "fooBar" should be PascalCase'
);
});
it("smoketest", () => {
const report = htmlvalidate.validateFile(
"test-files/rules/attr-case.html"
);
expect(report.results).toMatchSnapshot();
});
});
describe('configured with "camelcase"', () => {
beforeAll(() => {
htmlvalidate = new HtmlValidate({
rules: { "attr-case": ["error", { style: "camelcase" }] },
});
});
it("should not report error when attributes is camelCase", () => {
const report = htmlvalidate.validateString('<div fooBar="baz"></div>');
expect(report).toBeValid();
});
it("should not report error when attributes is lowercase", () => {
const report = htmlvalidate.validateString('<div foobar="baz"></div>');
expect(report).toBeValid();
});
it("should report error when attributes is UPPERCASE", () => {
const report = htmlvalidate.validateString('<div FOOBAR="baz"></div>');
expect(report).toBeInvalid();
expect(report).toHaveError(
"attr-case",
'Attribute "FOOBAR" should be camelCase'
);
});
it("should report error when attributes is PascalCase", () => {
const report = htmlvalidate.validateString('<div FooBar="baz"></div>');
expect(report).toBeInvalid();
expect(report).toHaveError(
"attr-case",
'Attribute "FooBar" should be camelCase'
);
});
it("smoketest", () => {
const report = htmlvalidate.validateFile(
"test-files/rules/attr-case.html"
);
expect(report.results).toMatchSnapshot();
});
});
describe('configured with "ignoreForeign" true', () => {
beforeAll(() => {
htmlvalidate = new HtmlValidate({
......
......@@ -61,6 +61,10 @@ function parseStyle(style: string): [RegExp, string] {
return [/^[a-z]*$/, "lowercase"];
case "uppercase":
return [/^[A-Z]*$/, "uppercase"];
case "pascalcase":
return [/^[A-Z][A-Za-z]*$/, "PascalCase"];
case "camelcase":
return [/^[a-z][A-Za-z]*$/, "camelCase"];
default:
throw new Error(`Invalid style "${style}" for "attr-case" rule`);
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment