eslint-plugin-crisp 1.7.12 → 1.7.14

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
@@ -228,6 +228,7 @@ Each item has emojis denoting:
228
228
  | [crisp/vue-define-component](https://github.com/crisp-oss/eslint-plugin-crisp/blob/master/rules/vue-define-component.js) | Enforces mixins and components to use the `defineComponent` wrapper | | 🟢 |
229
229
  | [crisp/vue-emits-order](https://github.com/crisp-oss/eslint-plugin-crisp/blob/master/rules/vue-emits-order.js) | Ensures emits properties are alphabetically ordered | | 🟢 |
230
230
  | [crisp/vue-event-listener-jsdoc](https://github.com/crisp-oss/eslint-plugin-crisp/blob/master/rules/vue-event-listener-jsdoc.js) | Requires event listener method JSDoc descriptions to start with `Triggers on` | | 🟢 |
231
+ | [crisp/vue-event-listener-section](https://github.com/crisp-oss/eslint-plugin-crisp/blob/master/rules/vue-event-listener-section.js) | Requires methods named `on` followed by an uppercase letter to be in the event listeners section | | 🟢 |
231
232
  | [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 | | 🟢 |
232
233
  | [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) | | 🟢 |
233
234
  | [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 | | 🟢 |
package/index.js CHANGED
@@ -51,6 +51,7 @@ import ruleVueDataComment from "./rules/vue-data-comment.js";
51
51
  import ruleVueDefineComponent from "./rules/vue-define-component.js";
52
52
  import ruleVueEmitsOrder from "./rules/vue-emits-order.js";
53
53
  import ruleVueEventListenerJsdoc from "./rules/vue-event-listener-jsdoc.js";
54
+ import ruleVueEventListenerSection from "./rules/vue-event-listener-section.js";
54
55
  import ruleVueHeaderCheck from "./rules/vue-header-check.js";
55
56
  import ruleVueHtmlIndent from "./rules/vue-html-indent.js";
56
57
  import ruleVueHtmlQuotes from "./rules/vue-html-quotes.js";
@@ -131,6 +132,7 @@ const plugin = {
131
132
  "vue-define-component": ruleVueDefineComponent,
132
133
  "vue-emits-order": ruleVueEmitsOrder,
133
134
  "vue-event-listener-jsdoc": ruleVueEventListenerJsdoc,
135
+ "vue-event-listener-section": ruleVueEventListenerSection,
134
136
  "vue-header-check": ruleVueHeaderCheck,
135
137
  "vue-html-indent": ruleVueHtmlIndent,
136
138
  "vue-html-quotes": ruleVueHtmlQuotes,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "eslint-plugin-crisp",
3
- "version": "1.7.12",
3
+ "version": "1.7.14",
4
4
  "description": "Custom ESLint Rules for Crisp",
5
5
  "author": "Crisp IM SAS",
6
6
  "main": "index.js",
@@ -386,6 +386,7 @@ export default function configRecommendedVue(pluginCrisp) {
386
386
  "crisp/vue-define-component": "error",
387
387
  "crisp/vue-emits-order": "error",
388
388
  "crisp/vue-event-listener-jsdoc": "error",
389
+ "crisp/vue-event-listener-section": "error",
389
390
  "crisp/vue-header-check": "error",
390
391
  "crisp/vue-html-indent": "error",
391
392
  "crisp/vue-html-quotes": "error",
@@ -0,0 +1,87 @@
1
+ const EVENT_LISTENERS_SEPARATOR = "--> EVENT LISTENERS <--";
2
+ const EVENT_LISTENER_NAME_PATTERN = /^on[A-Z]/;
3
+
4
+ export default {
5
+ meta: {
6
+ type: "suggestion",
7
+ docs: {
8
+ description:
9
+ "require on-prefixed Vue methods to be in the event listeners section",
10
+ category: "Stylistic Issues",
11
+ recommended: false
12
+ },
13
+ fixable: null,
14
+ schema: [],
15
+ messages: {
16
+ wrongSection:
17
+ "Method '{{name}}' must be declared in the EVENT LISTENERS section."
18
+ }
19
+ },
20
+
21
+ create(context) {
22
+ const sourceCode = context.sourceCode;
23
+
24
+ function getPropertyName(property) {
25
+ if (property.type !== "Property" || property.computed) {
26
+ return null;
27
+ }
28
+
29
+ if (property.key.type === "Identifier") {
30
+ return property.key.name;
31
+ }
32
+
33
+ if (
34
+ property.key.type === "Literal" &&
35
+ typeof property.key.value === "string"
36
+ ) {
37
+ return property.key.value;
38
+ }
39
+
40
+ return null;
41
+ }
42
+
43
+ function checkMethods(node) {
44
+ const properties = node.value.properties;
45
+
46
+ if (!properties) {
47
+ return;
48
+ }
49
+
50
+ let eventListenersStarted = false;
51
+
52
+ properties.forEach((property) => {
53
+ const comments = sourceCode.getCommentsBefore(property);
54
+
55
+ if (!eventListenersStarted) {
56
+ eventListenersStarted = comments.some((comment) => {
57
+ return (
58
+ comment.type === "Line" &&
59
+ comment.value.trim() === EVENT_LISTENERS_SEPARATOR
60
+ );
61
+ });
62
+ }
63
+
64
+ if (eventListenersStarted) {
65
+ return;
66
+ }
67
+
68
+ const name = getPropertyName(property);
69
+
70
+ if (name && EVENT_LISTENER_NAME_PATTERN.test(name)) {
71
+ context.report({
72
+ node: property.key,
73
+ messageId: "wrongSection",
74
+ data: { name }
75
+ });
76
+ }
77
+ });
78
+ }
79
+
80
+ return {
81
+ 'ExportDefaultDeclaration > ObjectExpression > Property[key.name="methods"]':
82
+ checkMethods,
83
+ 'ExportDefaultDeclaration > CallExpression[callee.name="defineComponent"] > ObjectExpression > Property[key.name="methods"]':
84
+ checkMethods
85
+ };
86
+ }
87
+ };
@@ -18,7 +18,7 @@ export default {
18
18
  category: "Stylistic Issues",
19
19
  recommended: false
20
20
  },
21
- fixable: null,
21
+ fixable: "code",
22
22
  schema: [],
23
23
  messages: {
24
24
  duplicateSeparator:
@@ -39,6 +39,114 @@ export default {
39
39
  create(context) {
40
40
  const sourceCode = context.sourceCode;
41
41
 
42
+ function createOrderFix(node, separatorEntries, properties) {
43
+ const objectEnd = node.value.range[1] - 1;
44
+ const sectionStarts = separatorEntries.map(({ comment }) => {
45
+ return sourceCode.getIndexFromLoc({
46
+ line: comment.loc.start.line,
47
+ column: 0
48
+ });
49
+ });
50
+
51
+ const hasInlineSeparator = separatorEntries.some(
52
+ ({ comment }, index) => {
53
+ return sourceCode.text
54
+ .slice(sectionStarts[index], comment.range[0])
55
+ .trim() !== "";
56
+ }
57
+ );
58
+
59
+ if (hasInlineSeparator) {
60
+ return null;
61
+ }
62
+
63
+ const sections = separatorEntries.map((entry, index) => {
64
+ const start = sectionStarts[index];
65
+ const end = sectionStarts[index + 1] ?? objectEnd;
66
+ const rawText = sourceCode.text.slice(start, end);
67
+ const text = rawText.trimEnd();
68
+ const nextEntry = separatorEntries[index + 1];
69
+ const sectionProperties = properties.filter((property) => {
70
+ return (
71
+ property.range[0] > entry.comment.range[1] &&
72
+ (!nextEntry || property.range[0] < nextEntry.comment.range[0])
73
+ );
74
+ });
75
+
76
+ return {
77
+ ...entry,
78
+ start,
79
+ end,
80
+ text,
81
+ trailingWhitespace: rawText.slice(text.length),
82
+ properties: sectionProperties
83
+ };
84
+ });
85
+
86
+ if (sections.some((section) => section.properties.length === 0)) {
87
+ return null;
88
+ }
89
+
90
+ const sectionGap = sections[0].trailingWhitespace;
91
+
92
+ if (!/[\r\n]/.test(sectionGap)) {
93
+ return null;
94
+ }
95
+
96
+ const suffix = sections.at(-1).trailingWhitespace;
97
+ const lastProperty = properties.at(-1);
98
+ const tokenAfterLastProperty = sourceCode.getTokenAfter(lastProperty);
99
+ const preserveTrailingComma = (
100
+ tokenAfterLastProperty?.value === "," &&
101
+ tokenAfterLastProperty.range[0] < objectEnd
102
+ );
103
+ const orderedSections = [...sections].sort((first, second) => {
104
+ return first.order - second.order;
105
+ });
106
+
107
+ const fixedSections = orderedSections.map((section, index) => {
108
+ const lastSectionProperty = section.properties.at(-1);
109
+ const tokenAfterProperty = sourceCode.getTokenAfter(
110
+ lastSectionProperty
111
+ );
112
+ const hasComma = (
113
+ tokenAfterProperty?.value === "," &&
114
+ tokenAfterProperty.range[0] < section.end
115
+ );
116
+ const needsComma = (
117
+ index < orderedSections.length - 1 ||
118
+ preserveTrailingComma
119
+ );
120
+ const propertyEnd = lastSectionProperty.range[1] - section.start;
121
+
122
+ if (needsComma && !hasComma) {
123
+ return (
124
+ section.text.slice(0, propertyEnd) +
125
+ "," +
126
+ section.text.slice(propertyEnd)
127
+ );
128
+ }
129
+
130
+ if (!needsComma && hasComma) {
131
+ const commaStart = tokenAfterProperty.range[0] - section.start;
132
+
133
+ return (
134
+ section.text.slice(0, commaStart) +
135
+ section.text.slice(commaStart + 1)
136
+ );
137
+ }
138
+
139
+ return section.text;
140
+ });
141
+
142
+ return (fixer) => {
143
+ return fixer.replaceTextRange(
144
+ [sectionStarts[0], objectEnd],
145
+ fixedSections.join(sectionGap) + suffix
146
+ );
147
+ };
148
+ }
149
+
42
150
  function checkMethods(node) {
43
151
  const properties = node.value.properties;
44
152
 
@@ -57,8 +165,10 @@ export default {
57
165
  }
58
166
  );
59
167
  const seenSeparators = new Set();
60
- let lastSeparatorOrder = -1;
168
+ const separatorEntries = [];
61
169
  let separatorCount = 0;
170
+ let hasDuplicateSeparator = false;
171
+ let hasInvalidSeparator = false;
62
172
 
63
173
  comments.forEach((comment) => {
64
174
  if (comment.type !== "Line") {
@@ -70,6 +180,8 @@ export default {
70
180
 
71
181
  if (separatorOrder === undefined) {
72
182
  if (SEPARATOR_LIKE_PATTERN.test(separator)) {
183
+ hasInvalidSeparator = true;
184
+
73
185
  context.report({
74
186
  node: comment,
75
187
  messageId: "invalidSeparator"
@@ -82,6 +194,8 @@ export default {
82
194
  separatorCount++;
83
195
 
84
196
  if (seenSeparators.has(separator)) {
197
+ hasDuplicateSeparator = true;
198
+
85
199
  context.report({
86
200
  node: comment,
87
201
  messageId: "duplicateSeparator",
@@ -92,15 +206,37 @@ export default {
92
206
  }
93
207
 
94
208
  seenSeparators.add(separator);
209
+ separatorEntries.push({
210
+ comment,
211
+ order: separatorOrder
212
+ });
213
+ });
95
214
 
96
- if (separatorOrder < lastSeparatorOrder) {
97
- context.report({
98
- node: comment,
99
- messageId: "unorderedSeparator"
100
- });
101
- } else {
102
- lastSeparatorOrder = separatorOrder;
215
+ let lastSeparatorOrder = -1;
216
+ const unorderedEntries = separatorEntries.filter((entry) => {
217
+ if (entry.order < lastSeparatorOrder) {
218
+ return true;
103
219
  }
220
+
221
+ lastSeparatorOrder = entry.order;
222
+
223
+ return false;
224
+ });
225
+
226
+ const orderFix = (
227
+ unorderedEntries.length > 0 &&
228
+ !hasDuplicateSeparator &&
229
+ !hasInvalidSeparator
230
+ )
231
+ ? createOrderFix(node, separatorEntries, properties)
232
+ : null;
233
+
234
+ unorderedEntries.forEach((entry, index) => {
235
+ context.report({
236
+ node: entry.comment,
237
+ messageId: "unorderedSeparator",
238
+ ...(index === 0 && orderFix ? { fix: orderFix } : {})
239
+ });
104
240
  });
105
241
 
106
242
  if (separatorCount === 0) {
@@ -0,0 +1,145 @@
1
+ import rule from "../../rules/vue-event-listener-section.js";
2
+ import { createVueRuleTester } from "../rule-tester.js";
3
+
4
+ const ruleTester = createVueRuleTester();
5
+
6
+ ruleTester.run("vue-event-listener-section", rule, {
7
+ valid: [
8
+ {
9
+ code: "export default { methods: {} };"
10
+ },
11
+ {
12
+ code: `
13
+ export default {
14
+ methods: {
15
+ // --> ACTIONS <--
16
+ save() {},
17
+ // --> HELPERS <--
18
+ onlyOnce() {},
19
+ onload() {},
20
+ on_save() {},
21
+ on2Factor() {}
22
+ }
23
+ };
24
+ `
25
+ },
26
+ {
27
+ code: `
28
+ export default defineComponent({
29
+ methods: {
30
+ // --> EVENT LISTENERS <--
31
+ onSave() {},
32
+ onURLChange() {},
33
+ "onClose"() {}
34
+ }
35
+ });
36
+ `
37
+ },
38
+ {
39
+ code: `
40
+ export default {
41
+ methods: {
42
+ // --> HELPERS <--
43
+ createHandlers() {
44
+ return {
45
+ onSave() {}
46
+ };
47
+ }
48
+ }
49
+ };
50
+ `
51
+ },
52
+ {
53
+ filename: "Component.vue",
54
+ code: `
55
+ <script>
56
+ export default defineComponent({
57
+ methods: {
58
+ // --> ACTIONS <--
59
+ save() {},
60
+ // --> EVENT LISTENERS <--
61
+ onLoadMore() {}
62
+ }
63
+ });
64
+ </script>
65
+ `
66
+ }
67
+ ],
68
+
69
+ invalid: [
70
+ {
71
+ code: `
72
+ export default {
73
+ methods: {
74
+ // --> ACTIONS <--
75
+ onSave() {}
76
+ }
77
+ };
78
+ `,
79
+ errors: [
80
+ {
81
+ messageId: "wrongSection",
82
+ data: { name: "onSave" }
83
+ }
84
+ ]
85
+ },
86
+ {
87
+ code: `
88
+ export default {
89
+ methods: {
90
+ // --> HELPERS <--
91
+ onLoadMore() {}
92
+ }
93
+ };
94
+ `,
95
+ errors: [
96
+ {
97
+ messageId: "wrongSection",
98
+ data: { name: "onLoadMore" }
99
+ }
100
+ ]
101
+ },
102
+ {
103
+ code: `
104
+ export default {
105
+ methods: {
106
+ "onClose": function() {}
107
+ }
108
+ };
109
+ `,
110
+ errors: [
111
+ {
112
+ messageId: "wrongSection",
113
+ data: { name: "onClose" }
114
+ }
115
+ ]
116
+ },
117
+ {
118
+ filename: "Component.vue",
119
+ code: `
120
+ <script>
121
+ export default defineComponent({
122
+ methods: {
123
+ // --> ACTIONS <--
124
+ onSave() {},
125
+ // --> HELPERS <--
126
+ onFormatComplete() {},
127
+ // --> EVENT LISTENERS <--
128
+ onClose() {}
129
+ }
130
+ });
131
+ </script>
132
+ `,
133
+ errors: [
134
+ {
135
+ messageId: "wrongSection",
136
+ data: { name: "onSave" }
137
+ },
138
+ {
139
+ messageId: "wrongSection",
140
+ data: { name: "onFormatComplete" }
141
+ }
142
+ ]
143
+ }
144
+ ]
145
+ });
@@ -149,12 +149,94 @@ ruleTester.run("vue-methods-separator", rule, {
149
149
  }
150
150
  };
151
151
  `,
152
+ output: `
153
+ export default {
154
+ methods: {
155
+ // --> ACTIONS <--
156
+ save() {},
157
+ // --> HELPERS <--
158
+ format() {}
159
+ }
160
+ };
161
+ `,
152
162
  errors: [
153
163
  {
154
164
  messageId: "unorderedSeparator"
155
165
  }
156
166
  ]
157
167
  },
168
+ {
169
+ code: `
170
+ export default {
171
+ methods: {
172
+ // --> EVENT LISTENERS <--
173
+
174
+ /**
175
+ * Triggers on save
176
+ */
177
+ onSave() {},
178
+
179
+ /**
180
+ * Triggers on close
181
+ */
182
+ onClose() {},
183
+
184
+ // --> ACTIONS <--
185
+
186
+ /**
187
+ * Saves content
188
+ */
189
+ save() {},
190
+
191
+ // --> HELPERS <--
192
+
193
+ /**
194
+ * Formats content
195
+ */
196
+ format() {}
197
+ }
198
+ };
199
+ `,
200
+ output: `
201
+ export default {
202
+ methods: {
203
+ // --> ACTIONS <--
204
+
205
+ /**
206
+ * Saves content
207
+ */
208
+ save() {},
209
+
210
+ // --> HELPERS <--
211
+
212
+ /**
213
+ * Formats content
214
+ */
215
+ format() {},
216
+
217
+ // --> EVENT LISTENERS <--
218
+
219
+ /**
220
+ * Triggers on save
221
+ */
222
+ onSave() {},
223
+
224
+ /**
225
+ * Triggers on close
226
+ */
227
+ onClose() {}
228
+ }
229
+ };
230
+ `,
231
+ errors: [
232
+ {
233
+ messageId: "unorderedSeparator"
234
+ },
235
+ {
236
+ messageId: "unorderedSeparator"
237
+ }
238
+ ]
239
+ },
158
240
  {
159
241
  code: `
160
242
  export default {
@@ -184,6 +266,16 @@ ruleTester.run("vue-methods-separator", rule, {
184
266
  }
185
267
  });
186
268
  `,
269
+ output: `
270
+ export default defineComponent({
271
+ methods: {
272
+ // --> HELPERS <--
273
+ format() {},
274
+ // --> EVENT LISTENERS <--
275
+ onSave() {}
276
+ }
277
+ });
278
+ `,
187
279
  errors: [
188
280
  {
189
281
  messageId: "unorderedSeparator"
@@ -220,6 +312,18 @@ ruleTester.run("vue-methods-separator", rule, {
220
312
  });
221
313
  </script>
222
314
  `,
315
+ output: `
316
+ <script>
317
+ export default defineComponent({
318
+ methods: {
319
+ // --> ACTIONS <--
320
+ save() {},
321
+ // --> EVENT LISTENERS <--
322
+ onSave() {}
323
+ }
324
+ });
325
+ </script>
326
+ `,
223
327
  errors: [
224
328
  {
225
329
  messageId: "unorderedSeparator"