eslint-plugin-crisp 1.6.4 → 1.6.6

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.6.4",
3
+ "version": "1.6.6",
4
4
  "description": "Custom ESLint Rules for Crisp",
5
5
  "author": "Crisp IM SAS",
6
6
  "main": "index.js",
@@ -2,6 +2,12 @@ const COMMENT_HEADER_FORMAT = (section) => {
2
2
  return `/**************************************************************************\n * ${section.toUpperCase()}\n ***************************************************************************/`;
3
3
  };
4
4
 
5
+ const HEADER_COMMENT_REGEX = /^\*+\n \* [A-Z][A-Z0-9 _:-]*\n \*+$/;
6
+
7
+ function isHeaderComment(comment) {
8
+ return comment.type === "Block" && HEADER_COMMENT_REGEX.test(comment.value);
9
+ }
10
+
5
11
  function isTypeComment(comment) {
6
12
  if (comment.type !== "Block" || !comment.value.startsWith("*")) {
7
13
  return false;
@@ -109,7 +115,7 @@ export default {
109
115
  category: "Best Practices",
110
116
  recommended: false,
111
117
  },
112
- fixable: null,
118
+ fixable: "whitespace",
113
119
  },
114
120
 
115
121
  create(context) {
@@ -224,6 +230,30 @@ export default {
224
230
  }
225
231
  }
226
232
 
233
+ // Checks that a header comment block is followed by a blank line
234
+ function checkBlankLineAfterHeaders() {
235
+ for (const comment of allBlockComments) {
236
+ if (!isHeaderComment(comment)) {
237
+ continue;
238
+ }
239
+
240
+ const next = sourceCode.getTokenAfter(comment, { includeComments: true });
241
+
242
+ if (!next || next.loc.start.line - comment.loc.end.line >= 2) {
243
+ continue;
244
+ }
245
+
246
+ const isOnNextLine = next.loc.start.line - comment.loc.end.line === 1;
247
+
248
+ context.report({
249
+ loc: comment.loc,
250
+ message: "Header comment block must be followed by a blank line",
251
+
252
+ fix: isOnNextLine ? (fixer) => fixer.insertTextAfter(comment, "\n") : null,
253
+ });
254
+ }
255
+ }
256
+
227
257
  return {
228
258
  ":statement": (node) => {
229
259
  if (node.parent.type !== "Program") {
@@ -256,6 +286,9 @@ export default {
256
286
 
257
287
  // Check type comments group
258
288
  checkTypeCommentsGroup();
289
+
290
+ // Check blank line after header comments
291
+ checkBlankLineAfterHeaders();
259
292
  }
260
293
  };
261
294
  }
@@ -12,17 +12,17 @@ ruleTester.run("header-comments-check", rule, {
12
12
  valid: [
13
13
  {
14
14
  filename: "/project/src/example.js",
15
- code: `${header("IMPORTS")}\nimport value from "package";`
15
+ code: `${header("IMPORTS")}\n\nimport value from "package";`
16
16
  },
17
17
  {
18
18
  filename: "/project/src/example.ts",
19
- code: `${header("CONSTANTS")}\nconst MAX_RETRIES = 3;\n\n${header("INSTANCES")}\nconst pattern = /value/;`
19
+ code: `${header("CONSTANTS")}\n\nconst MAX_RETRIES = 3;\n\n${header("INSTANCES")}\n\nconst pattern = /value/;`
20
20
  },
21
21
  {
22
22
  filename: "/project/src/example.js",
23
23
  code:
24
- `${header("TYPES")}\n/** @typedef {string} Identifier */\n\n` +
25
- `${header("EXPORTS")}\nexport default {};`
24
+ `${header("TYPES")}\n\n/** @typedef {string} Identifier */\n\n` +
25
+ `${header("EXPORTS")}\n\nexport default {};`
26
26
  },
27
27
  {
28
28
  filename: "/project/src/example.js",
@@ -30,12 +30,12 @@ ruleTester.run("header-comments-check", rule, {
30
30
  },
31
31
  {
32
32
  filename: "/project/src/example.js",
33
- code: `${header("MIXIN TO MANAGE HANDOFF")}\nexport default {};`
33
+ code: `${header("MIXIN TO MANAGE HANDOFF")}\n\nexport default {};`
34
34
  },
35
35
  {
36
36
  filename: "/project/src/example.js",
37
37
  code:
38
- `${header("IMPORTS")}\nimport first from "first";\n` +
38
+ `${header("IMPORTS")}\n\nimport first from "first";\n` +
39
39
  "import second from \"second\";"
40
40
  },
41
41
  {
@@ -84,6 +84,26 @@ ruleTester.run("header-comments-check", rule, {
84
84
  message: "Type comments group must be preceded by a 'TYPES' comment block"
85
85
  }
86
86
  ]
87
+ },
88
+ {
89
+ filename: "/project/src/example.js",
90
+ code: `${header("CONSTANTS")}\nconst MAX_RETRIES = 3;`,
91
+ output: `${header("CONSTANTS")}\n\nconst MAX_RETRIES = 3;`,
92
+ errors: [
93
+ {
94
+ message: "Header comment block must be followed by a blank line"
95
+ }
96
+ ]
97
+ },
98
+ {
99
+ filename: "/project/src/example.js",
100
+ code: `${header("IMPORTS")}\n// NPM\nimport value from "package";`,
101
+ output: `${header("IMPORTS")}\n\n// NPM\nimport value from "package";`,
102
+ errors: [
103
+ {
104
+ message: "Header comment block must be followed by a blank line"
105
+ }
106
+ ]
87
107
  }
88
108
  ]
89
109
  });