File tree 2 files changed +46
-3
lines changed
src/rules/sort-attribute-content
2 files changed +46
-3
lines changed Original file line number Diff line number Diff line change @@ -42,6 +42,16 @@ export const sortAttributeContentRule: Rule.RuleModule = {
42
42
( context . options as [ SortAttributeContentOptions ] ) [ 0 ]
43
43
) ;
44
44
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
+
45
55
/**
46
56
* @param content The attribute value
47
57
* @param options The sort options
@@ -54,9 +64,6 @@ export const sortAttributeContentRule: Rule.RuleModule = {
54
64
nodeInfo : Pick < AttributeNode , "loc" | "range" > & { attribute : string }
55
65
) {
56
66
const { caseSensitive, direction, separator } = options ;
57
- if ( ! separator . length ) {
58
- throw new Error ( "Can not sort attribute content with an empty separator." ) ;
59
- }
60
67
61
68
const parts = splitString (
62
69
content ,
Original file line number Diff line number Diff line change
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
+ } ) ;
You can’t perform that action at this time.
0 commit comments