eslint-plugin-crisp 1.7.10 → 1.7.12

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
@@ -227,9 +227,11 @@ Each item has emojis denoting:
227
227
  | [crisp/vue-computed-order](https://github.com/crisp-oss/eslint-plugin-crisp/blob/master/rules/vue-computed-order.js) | Ensures computed properties are alphabetically ordered | | 🟢 |
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
+ | [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` | | 🟢 |
230
231
  | [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
232
  | [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
233
  | [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 | | 🟢 |
234
+ | [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
235
  | [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
236
  | [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
237
  | [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/index.js CHANGED
@@ -50,6 +50,7 @@ import ruleVueComputedOrder from "./rules/vue-computed-order.js";
50
50
  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
+ import ruleVueEventListenerJsdoc from "./rules/vue-event-listener-jsdoc.js";
53
54
  import ruleVueHeaderCheck from "./rules/vue-header-check.js";
54
55
  import ruleVueHtmlIndent from "./rules/vue-html-indent.js";
55
56
  import ruleVueHtmlQuotes from "./rules/vue-html-quotes.js";
@@ -129,6 +130,7 @@ const plugin = {
129
130
  "vue-data-comment": ruleVueDataComment,
130
131
  "vue-define-component": ruleVueDefineComponent,
131
132
  "vue-emits-order": ruleVueEmitsOrder,
133
+ "vue-event-listener-jsdoc": ruleVueEventListenerJsdoc,
132
134
  "vue-header-check": ruleVueHeaderCheck,
133
135
  "vue-html-indent": ruleVueHtmlIndent,
134
136
  "vue-html-quotes": ruleVueHtmlQuotes,
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.12",
4
4
  "description": "Custom ESLint Rules for Crisp",
5
5
  "author": "Crisp IM SAS",
6
6
  "main": "index.js",
@@ -385,6 +385,7 @@ export default function configRecommendedVue(pluginCrisp) {
385
385
  "crisp/vue-data-comment": "error",
386
386
  "crisp/vue-define-component": "error",
387
387
  "crisp/vue-emits-order": "error",
388
+ "crisp/vue-event-listener-jsdoc": "error",
388
389
  "crisp/vue-header-check": "error",
389
390
  "crisp/vue-html-indent": "error",
390
391
  "crisp/vue-html-quotes": "error",
@@ -0,0 +1,87 @@
1
+ import doctrine from "doctrine";
2
+
3
+ const EVENT_LISTENERS_SEPARATOR = "--> EVENT LISTENERS <--";
4
+ const DESCRIPTION_PREFIX = "Triggers on ";
5
+
6
+ export default {
7
+ meta: {
8
+ type: "suggestion",
9
+ docs: {
10
+ description:
11
+ "require event listener method descriptions to start with 'Triggers on'",
12
+ category: "Stylistic Issues",
13
+ recommended: false
14
+ },
15
+ fixable: null,
16
+ schema: [],
17
+ messages: {
18
+ invalidDescription:
19
+ "Event listener method JSDoc description must start with " +
20
+ "'Triggers on ...'.",
21
+ missingJsdoc: "Event listener methods must have a JSDoc comment."
22
+ }
23
+ },
24
+
25
+ create(context) {
26
+ const sourceCode = context.sourceCode;
27
+
28
+ function checkMethods(node) {
29
+ const properties = node.value.properties;
30
+
31
+ if (!properties) {
32
+ return;
33
+ }
34
+
35
+ let eventListenersStarted = false;
36
+
37
+ properties.forEach((property) => {
38
+ const comments = sourceCode.getCommentsBefore(property);
39
+
40
+ if (!eventListenersStarted) {
41
+ eventListenersStarted = comments.some((comment) => {
42
+ return (
43
+ comment.type === "Line" &&
44
+ comment.value.trim() === EVENT_LISTENERS_SEPARATOR
45
+ );
46
+ });
47
+ }
48
+
49
+ if (!eventListenersStarted || property.type !== "Property") {
50
+ return;
51
+ }
52
+
53
+ const jsdoc = [...comments].reverse().find((comment) => {
54
+ return comment.type === "Block" && comment.value.startsWith("*");
55
+ });
56
+
57
+ if (!jsdoc) {
58
+ context.report({
59
+ node: property,
60
+ messageId: "missingJsdoc"
61
+ });
62
+
63
+ return;
64
+ }
65
+
66
+ const description = doctrine.parse(
67
+ `/*${jsdoc.value}*/`,
68
+ { unwrap: true }
69
+ ).description.trim();
70
+
71
+ if (!description.startsWith(DESCRIPTION_PREFIX)) {
72
+ context.report({
73
+ node: jsdoc,
74
+ messageId: "invalidDescription"
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
+ };
@@ -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
  };
@@ -0,0 +1,174 @@
1
+ import rule from "../../rules/vue-event-listener-jsdoc.js";
2
+ import { createVueRuleTester } from "../rule-tester.js";
3
+
4
+ const ruleTester = createVueRuleTester();
5
+
6
+ ruleTester.run("vue-event-listener-jsdoc", rule, {
7
+ valid: [
8
+ {
9
+ code: "export default { methods: {} };"
10
+ },
11
+ {
12
+ code: `
13
+ export default {
14
+ methods: {
15
+ // --> HELPERS <--
16
+ /**
17
+ * Handles formatting
18
+ */
19
+ format() {}
20
+ }
21
+ };
22
+ `
23
+ },
24
+ {
25
+ code: `
26
+ export default {
27
+ methods: {
28
+ // --> EVENT LISTENERS <--
29
+ /**
30
+ * Triggers on load more
31
+ * @public
32
+ * @param {object} $state
33
+ */
34
+ onLoadMore($state) {}
35
+ }
36
+ };
37
+ `
38
+ },
39
+ {
40
+ code: `
41
+ export default defineComponent({
42
+ methods: {
43
+ // --> ACTIONS <--
44
+ /**
45
+ * Saves content
46
+ */
47
+ save() {},
48
+ // --> HELPERS <--
49
+ /**
50
+ * Formats content
51
+ */
52
+ format() {},
53
+ // --> EVENT LISTENERS <--
54
+ /**
55
+ * Triggers on save
56
+ */
57
+ onSave() {},
58
+ /**
59
+ * Triggers on close
60
+ */
61
+ onClose: function() {}
62
+ }
63
+ });
64
+ `
65
+ },
66
+ {
67
+ filename: "Component.vue",
68
+ code: `
69
+ <script>
70
+ export default defineComponent({
71
+ methods: {
72
+ // --> EVENT LISTENERS <--
73
+ /**
74
+ * Triggers on load more
75
+ * @public
76
+ */
77
+ onLoadMore() {
78
+ this.loadMore(() => {
79
+ /**
80
+ * Handles a nested callback
81
+ */
82
+ return true;
83
+ });
84
+ }
85
+ }
86
+ });
87
+ </script>
88
+ `
89
+ }
90
+ ],
91
+
92
+ invalid: [
93
+ {
94
+ code: `
95
+ export default {
96
+ methods: {
97
+ // --> EVENT LISTENERS <--
98
+ /**
99
+ * Handles load more
100
+ * @public
101
+ */
102
+ onLoadMore() {}
103
+ }
104
+ };
105
+ `,
106
+ errors: [
107
+ {
108
+ messageId: "invalidDescription"
109
+ }
110
+ ]
111
+ },
112
+ {
113
+ code: `
114
+ export default {
115
+ methods: {
116
+ // --> EVENT LISTENERS <--
117
+ onLoadMore() {}
118
+ }
119
+ };
120
+ `,
121
+ errors: [
122
+ {
123
+ messageId: "missingJsdoc"
124
+ }
125
+ ]
126
+ },
127
+ {
128
+ code: `
129
+ export default {
130
+ methods: {
131
+ // --> EVENT LISTENERS <--
132
+ /**
133
+ * Triggers on
134
+ * @public
135
+ */
136
+ onLoadMore() {}
137
+ }
138
+ };
139
+ `,
140
+ errors: [
141
+ {
142
+ messageId: "invalidDescription"
143
+ }
144
+ ]
145
+ },
146
+ {
147
+ filename: "Component.vue",
148
+ code: `
149
+ <script>
150
+ export default defineComponent({
151
+ methods: {
152
+ // --> EVENT LISTENERS <--
153
+ /**
154
+ * Triggers when content is saved
155
+ * @public
156
+ */
157
+ onSave() {},
158
+ /**
159
+ * Triggers on close
160
+ * @public
161
+ */
162
+ onClose() {}
163
+ }
164
+ });
165
+ </script>
166
+ `,
167
+ errors: [
168
+ {
169
+ messageId: "invalidDescription"
170
+ }
171
+ ]
172
+ }
173
+ ]
174
+ });
@@ -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
  }