Skip to content

Commit 08bfb79

Browse files
authored
Merge pull request #7 from heap-code/fix/throw-no-option
fix(config): thrown an error if an option is wrongly set
2 parents ceb0157 + 3bb0422 commit 08bfb79

File tree

2 files changed

+46
-3
lines changed

2 files changed

+46
-3
lines changed

src/rules/sort-attribute-content/sort-attribute-content.rule.ts

+10-3
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,16 @@ export const sortAttributeContentRule: Rule.RuleModule = {
4242
(context.options as [SortAttributeContentOptions])[0]
4343
);
4444

45+
if (ruleOptions.length === 0) {
46+
throw new Error("There is no options defined for this rule.");
47+
}
48+
if (ruleOptions.some(({ attributes }) => attributes.length === 0)) {
49+
throw new Error("At least one of the options have no attributes");
50+
}
51+
if (ruleOptions.some(({ separator }) => separator.length === 0)) {
52+
throw new Error("At least one of the options have an empty separator");
53+
}
54+
4555
/**
4656
* @param content The attribute value
4757
* @param options The sort options
@@ -54,9 +64,6 @@ export const sortAttributeContentRule: Rule.RuleModule = {
5464
nodeInfo: Pick<AttributeNode, "loc" | "range"> & { attribute: string }
5565
) {
5666
const { caseSensitive, direction, separator } = options;
57-
if (!separator.length) {
58-
throw new Error("Can not sort attribute content with an empty separator.");
59-
}
6067

6168
const parts = splitString(
6269
content,

test/e2e/e2e.spec.ts

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import { parseForESLint } from "@html-eslint/parser";
2+
import { Linter } from "eslint";
3+
4+
import {
5+
SORT_ATTRIBUTE_CONTENT_NAME,
6+
SortAttributeContentOptions,
7+
sortAttributeContentRule
8+
} from "../../src/rules";
9+
10+
describe("e2e", () => {
11+
const parser = "@html-eslint/parser";
12+
const linter = new Linter();
13+
14+
linter.defineParser(parser, { parseForESLint: parseForESLint as never });
15+
linter.defineRule(SORT_ATTRIBUTE_CONTENT_NAME, sortAttributeContentRule);
16+
17+
const verify = (options: SortAttributeContentOptions) =>
18+
linter.verify("<div></div>", {
19+
parser,
20+
rules: {
21+
[SORT_ATTRIBUTE_CONTENT_NAME]: ["error", options]
22+
}
23+
});
24+
25+
it("should throw an error when no option is set", () => {
26+
expect(() => verify([])).toThrow("no options");
27+
});
28+
29+
it("should throw an error when `attributes` is empty", () => {
30+
expect(() => verify([{ attributes: [] }])).toThrow("no attributes");
31+
});
32+
33+
it("should throw an error when the `separator` is an empty string", () => {
34+
expect(() => verify([{ attributes: "class", separator: "" }])).toThrow("empty separator");
35+
});
36+
});

0 commit comments

Comments
 (0)