eslint-plugin-crisp 1.7.11 → 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,6 +227,7 @@ 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 | | 🟢 |
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.11",
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
+ };
@@ -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
+ });