eslint-plugin-crisp 1.7.10 → 1.7.11

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -230,6 +230,7 @@ Each item has emojis denoting:
230
230
  | [crisp/vue-header-check](https://github.com/crisp-oss/eslint-plugin-crisp/blob/master/rules/vue-header-check.js) | Ensures `script`, `template` and `style` tags start with corresponding comment block | | 🟢 |
231
231
  | [crisp/vue-html-indent](https://github.com/crisp-oss/eslint-plugin-crisp/blob/master/rules/vue-html-indent.js) | Enforces consistent indentation in `template` (supports for Pug) | | 🟢 |
232
232
  | [crisp/vue-html-quotes](https://github.com/crisp-oss/eslint-plugin-crisp/blob/master/rules/vue-html-quotes.js) | Enforces HTML attributes to be enclosed with double quotes | | 🟢 |
233
+ | [crisp/vue-methods-separator](https://github.com/crisp-oss/eslint-plugin-crisp/blob/master/rules/vue-methods-separator.js) | Requires method section comments to be ordered as actions, helpers, then event listeners | | 🟢 |
233
234
  | [crisp/vue-no-component-class-shorthand](https://github.com/crisp-oss/eslint-plugin-crisp/blob/master/rules/vue-no-component-class-shorthand.js) | Disallows Pug class shorthand on Vue components | | 🟢 |
234
235
  | [crisp/vue-no-mixed-class](https://github.com/crisp-oss/eslint-plugin-crisp/blob/master/rules/vue-no-mixed-class.js) | Disallows using both `class` and `:class` on the same element | | 🟢 |
235
236
  | [crisp/vue-no-name-option](https://github.com/crisp-oss/eslint-plugin-crisp/blob/master/rules/vue-no-name-option.js) | Disallows explicit component names in Vue single-file components | | 🟢 |
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "eslint-plugin-crisp",
3
- "version": "1.7.10",
3
+ "version": "1.7.11",
4
4
  "description": "Custom ESLint Rules for Crisp",
5
5
  "author": "Crisp IM SAS",
6
6
  "main": "index.js",
@@ -1,65 +1,121 @@
1
+ const SEPARATORS = [
2
+ "--> ACTIONS <--",
3
+ "--> HELPERS <--",
4
+ "--> EVENT LISTENERS <--"
5
+ ];
6
+
7
+ const SEPARATOR_ORDER = new Map(
8
+ SEPARATORS.map((separator, index) => [separator, index])
9
+ );
10
+ const SEPARATOR_LIKE_PATTERN = /^--> .+ <--$/;
11
+
1
12
  export default {
2
13
  meta: {
3
14
  type: "suggestion",
4
15
  docs: {
5
- description: "enforce at least one separator comment in Vue methods block",
16
+ description:
17
+ "enforce separator comments and their order in Vue methods block",
6
18
  category: "Stylistic Issues",
7
19
  recommended: false
8
20
  },
9
21
  fixable: null,
10
- schema: []
22
+ schema: [],
23
+ messages: {
24
+ duplicateSeparator:
25
+ "Duplicate methods separator comment '// {{separator}}'.",
26
+ invalidSeparator:
27
+ "Invalid separator comment. Must be '// --> ACTIONS <--', " +
28
+ "'// --> HELPERS <--', or '// --> EVENT LISTENERS <--'.",
29
+ missingSeparator:
30
+ "Methods block must contain at least one separator comment: " +
31
+ "'// --> ACTIONS <--', '// --> HELPERS <--', " +
32
+ "or '// --> EVENT LISTENERS <--'.",
33
+ unorderedSeparator:
34
+ "Methods separator comments must be ordered: '// --> ACTIONS <--', " +
35
+ "then '// --> HELPERS <--', then '// --> EVENT LISTENERS <--'."
36
+ }
11
37
  },
12
38
 
13
39
  create(context) {
14
- const SEPARATOR_PATTERN = /^--> (ACTIONS|HELPERS|EVENT LISTENERS) <--$/;
15
- const SEPARATOR_LIKE_PATTERN = /^--> .+ <--$/;
40
+ const sourceCode = context.sourceCode;
16
41
 
17
- return {
18
- 'ExportDefaultDeclaration Property[key.name="methods"]'(node) {
19
- const properties = node.value.properties;
42
+ function checkMethods(node) {
43
+ const properties = node.value.properties;
44
+
45
+ if (!properties || properties.length === 0) {
46
+ return;
47
+ }
48
+
49
+ const comments = sourceCode.getCommentsInside(node.value).filter(
50
+ (comment) => {
51
+ return !properties.some((property) => {
52
+ return (
53
+ comment.range[0] >= property.range[0] &&
54
+ comment.range[1] <= property.range[1]
55
+ );
56
+ });
57
+ }
58
+ );
59
+ const seenSeparators = new Set();
60
+ let lastSeparatorOrder = -1;
61
+ let separatorCount = 0;
20
62
 
21
- if (!properties || properties.length === 0) {
63
+ comments.forEach((comment) => {
64
+ if (comment.type !== "Line") {
22
65
  return;
23
66
  }
24
67
 
25
- const sourceCode = context.sourceCode;
26
- const comments = sourceCode.getCommentsInside(node);
68
+ const separator = comment.value.trim();
69
+ const separatorOrder = SEPARATOR_ORDER.get(separator);
27
70
 
28
- const hasSeparator = comments.some((comment) => {
29
- return (
30
- comment.type === "Line" &&
31
- SEPARATOR_PATTERN.test(comment.value.trim())
32
- );
33
- });
71
+ if (separatorOrder === undefined) {
72
+ if (SEPARATOR_LIKE_PATTERN.test(separator)) {
73
+ context.report({
74
+ node: comment,
75
+ messageId: "invalidSeparator"
76
+ });
77
+ }
78
+
79
+ return;
80
+ }
81
+
82
+ separatorCount++;
34
83
 
35
- if (!hasSeparator) {
84
+ if (seenSeparators.has(separator)) {
36
85
  context.report({
37
- node,
38
- message:
39
- "Methods block must contain at least one separator comment: " +
40
- "'// --> ACTIONS <--', '// --> HELPERS <--', " +
41
- "or '// --> EVENT LISTENERS <--'."
86
+ node: comment,
87
+ messageId: "duplicateSeparator",
88
+ data: { separator }
42
89
  });
90
+
91
+ return;
43
92
  }
44
93
 
45
- comments.forEach((comment) => {
46
- if (comment.type === "Line") {
47
- const trimmed = comment.value.trim();
48
-
49
- if (
50
- SEPARATOR_LIKE_PATTERN.test(trimmed) &&
51
- !SEPARATOR_PATTERN.test(trimmed)
52
- ) {
53
- context.report({
54
- node: comment,
55
- message:
56
- "Invalid separator comment. Must be '// --> ACTIONS <--', " +
57
- "'// --> HELPERS <--', or '// --> EVENT LISTENERS <--'."
58
- });
59
- }
60
- }
94
+ seenSeparators.add(separator);
95
+
96
+ if (separatorOrder < lastSeparatorOrder) {
97
+ context.report({
98
+ node: comment,
99
+ messageId: "unorderedSeparator"
100
+ });
101
+ } else {
102
+ lastSeparatorOrder = separatorOrder;
103
+ }
104
+ });
105
+
106
+ if (separatorCount === 0) {
107
+ context.report({
108
+ node,
109
+ messageId: "missingSeparator"
61
110
  });
62
111
  }
112
+ }
113
+
114
+ return {
115
+ 'ExportDefaultDeclaration > ObjectExpression > Property[key.name="methods"]':
116
+ checkMethods,
117
+ 'ExportDefaultDeclaration > CallExpression[callee.name="defineComponent"] > ObjectExpression > Property[key.name="methods"]':
118
+ checkMethods
63
119
  };
64
120
  }
65
121
  };
@@ -1,7 +1,7 @@
1
1
  import rule from "../../rules/vue-methods-separator.js";
2
- import createRuleTester from "../rule-tester.js";
2
+ import { createVueRuleTester } from "../rule-tester.js";
3
3
 
4
- const ruleTester = createRuleTester();
4
+ const ruleTester = createVueRuleTester();
5
5
 
6
6
  ruleTester.run("vue-methods-separator", rule, {
7
7
  valid: [
@@ -17,6 +17,98 @@ ruleTester.run("vue-methods-separator", rule, {
17
17
  }
18
18
  };
19
19
  `
20
+ },
21
+ {
22
+ code: `
23
+ export default {
24
+ methods: {
25
+ // --> HELPERS <--
26
+ format() {}
27
+ }
28
+ };
29
+ `
30
+ },
31
+ {
32
+ code: `
33
+ export default {
34
+ methods: {
35
+ // --> EVENT LISTENERS <--
36
+ onSave() {}
37
+ }
38
+ };
39
+ `
40
+ },
41
+ {
42
+ code: `
43
+ export default {
44
+ methods: {
45
+ // --> ACTIONS <--
46
+ save() {},
47
+ // --> EVENT LISTENERS <--
48
+ onSave() {}
49
+ }
50
+ };
51
+ `
52
+ },
53
+ {
54
+ code: `
55
+ export default defineComponent({
56
+ methods: {
57
+ // --> ACTIONS <--
58
+ save() {},
59
+ // --> HELPERS <--
60
+ format() {},
61
+ // --> EVENT LISTENERS <--
62
+ onSave() {}
63
+ }
64
+ });
65
+ `
66
+ },
67
+ {
68
+ code: `
69
+ export default {
70
+ options: {
71
+ methods: {
72
+ save() {}
73
+ }
74
+ }
75
+ };
76
+ `
77
+ },
78
+ {
79
+ code: `
80
+ export default {
81
+ methods: {
82
+ // --> ACTIONS <--
83
+ save() {
84
+ // --> EVENT LISTENERS <--
85
+ const status = "saved";
86
+
87
+ return status;
88
+ },
89
+ // --> HELPERS <--
90
+ format() {}
91
+ }
92
+ };
93
+ `
94
+ },
95
+ {
96
+ filename: "Component.vue",
97
+ code: `
98
+ <template><div /></template>
99
+ <script>
100
+ export default defineComponent({
101
+ methods: {
102
+ // --> ACTIONS <--
103
+ save() {},
104
+ // --> HELPERS <--
105
+ format() {},
106
+ // --> EVENT LISTENERS <--
107
+ onSave() {}
108
+ }
109
+ });
110
+ </script>
111
+ `
20
112
  }
21
113
  ],
22
114
 
@@ -25,10 +117,7 @@ ruleTester.run("vue-methods-separator", rule, {
25
117
  code: "export default { methods: { save() {} } };",
26
118
  errors: [
27
119
  {
28
- message:
29
- "Methods block must contain at least one separator comment: " +
30
- "'// --> ACTIONS <--', '// --> HELPERS <--', " +
31
- "or '// --> EVENT LISTENERS <--'."
120
+ messageId: "missingSeparator"
32
121
  }
33
122
  ]
34
123
  },
@@ -45,9 +134,95 @@ ruleTester.run("vue-methods-separator", rule, {
45
134
  `,
46
135
  errors: [
47
136
  {
48
- message:
49
- "Invalid separator comment. Must be '// --> ACTIONS <--', " +
50
- "'// --> HELPERS <--', or '// --> EVENT LISTENERS <--'."
137
+ messageId: "invalidSeparator"
138
+ }
139
+ ]
140
+ },
141
+ {
142
+ code: `
143
+ export default {
144
+ methods: {
145
+ // --> HELPERS <--
146
+ format() {},
147
+ // --> ACTIONS <--
148
+ save() {}
149
+ }
150
+ };
151
+ `,
152
+ errors: [
153
+ {
154
+ messageId: "unorderedSeparator"
155
+ }
156
+ ]
157
+ },
158
+ {
159
+ code: `
160
+ export default {
161
+ methods: {
162
+ // --> ACTIONS <--
163
+ save() {},
164
+ // --> ACTIONS <--
165
+ publish() {}
166
+ }
167
+ };
168
+ `,
169
+ errors: [
170
+ {
171
+ messageId: "duplicateSeparator",
172
+ data: { separator: "--> ACTIONS <--" }
173
+ }
174
+ ]
175
+ },
176
+ {
177
+ code: `
178
+ export default defineComponent({
179
+ methods: {
180
+ // --> EVENT LISTENERS <--
181
+ onSave() {},
182
+ // --> HELPERS <--
183
+ format() {}
184
+ }
185
+ });
186
+ `,
187
+ errors: [
188
+ {
189
+ messageId: "unorderedSeparator"
190
+ }
191
+ ]
192
+ },
193
+ {
194
+ code: `
195
+ export default {
196
+ methods: {
197
+ save() {
198
+ // --> ACTIONS <--
199
+ }
200
+ }
201
+ };
202
+ `,
203
+ errors: [
204
+ {
205
+ messageId: "missingSeparator"
206
+ }
207
+ ]
208
+ },
209
+ {
210
+ filename: "Component.vue",
211
+ code: `
212
+ <script>
213
+ export default defineComponent({
214
+ methods: {
215
+ // --> EVENT LISTENERS <--
216
+ onSave() {},
217
+ // --> ACTIONS <--
218
+ save() {}
219
+ }
220
+ });
221
+ </script>
222
+ `,
223
+ errors: [
224
+ {
225
+ messageId: "unorderedSeparator"
51
226
  }
52
227
  ]
53
228
  }