Commit be7d6928 authored by David Sveningsson's avatar David Sveningsson

feat(rules): support pascalcase and camelcase for `element-case` rule

parent 0dcfea14
......@@ -35,5 +35,7 @@ This rule takes an optional object:
### Style
- `camelcase` requires all element names to be camelCase.
- `lowercase` requires all element names to be lowercase.
- `uppercase` requires all element names to be uppercase.
- `pascalcase` requires all element names to be PascalCase.
- `uppercase` requires all element names to be UPPERCASE.
......@@ -90,6 +90,50 @@ describe("rule element-case", () => {
});
});
describe('configured with "pascalcase"', () => {
beforeAll(() => {
htmlvalidate = new HtmlValidate({
rules: { "element-case": ["error", { style: "pascalcase" }] },
});
});
it("should report error when element is lowercase", () => {
const report = htmlvalidate.validateString("<foo-bar></foo-bar>");
expect(report).toBeInvalid();
expect(report).toHaveError(
"element-case",
'Element "foo-bar" should be PascalCase'
);
});
it("should not report error when element is pascalcase", () => {
const report = htmlvalidate.validateString("<FooBar></FooBar>");
expect(report).toBeValid();
});
});
describe('configured with "camelcase"', () => {
beforeAll(() => {
htmlvalidate = new HtmlValidate({
rules: { "element-case": ["error", { style: "camelcase" }] },
});
});
it("should report error when element is pascalcase", () => {
const report = htmlvalidate.validateString("<FooBar></FooBar>");
expect(report).toBeInvalid();
expect(report).toHaveError(
"element-case",
'Element "FooBar" should be camelCase'
);
});
it("should not report error when element is camelcase", () => {
const report = htmlvalidate.validateString("<fooBar></fooBar>");
expect(report).toBeValid();
});
});
it("should throw error if configured with invalid value", () => {
htmlvalidate = new HtmlValidate({
rules: { "element-case": ["error", { style: "foobar" }] },
......
......@@ -43,6 +43,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 "element-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