eslint-plugin-crisp 1.7.12 → 1.7.13

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "eslint-plugin-crisp",
3
- "version": "1.7.12",
3
+ "version": "1.7.13",
4
4
  "description": "Custom ESLint Rules for Crisp",
5
5
  "author": "Crisp IM SAS",
6
6
  "main": "index.js",
@@ -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) {
@@ -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"