@virmator/lint 14.8.0 → 14.8.2

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.
@@ -1,3 +1,4 @@
1
+ import { assertWrap } from '@augment-vir/assert';
1
2
  function isTerminating(node) {
2
3
  const body = node.consequent.type === 'BlockStatement' ? node.consequent.body : [node.consequent];
3
4
  const last = body.at(-1);
@@ -10,26 +11,49 @@ function isConsecutiveTerminatingIfs(curr, next) {
10
11
  isTerminating(curr) &&
11
12
  isTerminating(next));
12
13
  }
14
+ function reconstructComment(comment) {
15
+ if (comment.type === 'Block') {
16
+ return `/*${comment.value}*/`;
17
+ }
18
+ return `//${comment.value}`;
19
+ }
13
20
  function checkBlock(statements, context, sourceCode) {
14
21
  statements.forEach((curr, index) => {
15
22
  const next = statements[index + 1];
16
- if (!next || !isConsecutiveTerminatingIfs(curr, next)) {
23
+ if (!next || !isConsecutiveTerminatingIfs(curr, next) || next.type !== 'IfStatement') {
17
24
  return;
18
25
  }
19
26
  const currRange = sourceCode.getRange(curr);
20
27
  const nextRange = sourceCode.getRange(next);
21
- const hasCommentsBetween = sourceCode.commentsExistBetween(curr, next);
28
+ const commentsBetween = sourceCode.getCommentsBefore(next);
22
29
  context.report({
23
30
  node: next,
24
31
  messageId: 'useIfElse',
25
- fix: hasCommentsBetween
26
- ? null
27
- : (fixer) => {
28
- return fixer.replaceTextRange([
29
- currRange[1],
30
- nextRange[0],
31
- ], ' else ');
32
- },
32
+ fix(fixer) {
33
+ const elseReplacement = fixer.replaceTextRange([
34
+ currRange[1],
35
+ nextRange[0],
36
+ ], ' else ');
37
+ if (commentsBetween.length > 0 &&
38
+ curr.consequent.type === 'BlockStatement' &&
39
+ curr.consequent.body.length > 0) {
40
+ const lastStatement = assertWrap.isDefined(curr.consequent.body.at(-1));
41
+ const lastStatementRange = sourceCode.getRange(lastStatement);
42
+ const sourceText = sourceCode.getText();
43
+ const lastLineStart = sourceText.lastIndexOf('\n', lastStatementRange[0] - 1) + 1;
44
+ const indent = sourceText.slice(lastLineStart, lastStatementRange[0]);
45
+ const commentText = commentsBetween
46
+ .map((comment) => {
47
+ return reconstructComment(comment);
48
+ })
49
+ .join('\n' + indent);
50
+ return [
51
+ fixer.insertTextAfter(lastStatement, '\n' + indent + commentText),
52
+ elseReplacement,
53
+ ];
54
+ }
55
+ return elseReplacement;
56
+ },
33
57
  });
34
58
  });
35
59
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@virmator/lint",
3
- "version": "14.8.0",
3
+ "version": "14.8.2",
4
4
  "description": "Default ESLint linting plugin for virmator.",
5
5
  "keywords": [
6
6
  "virmator",
@@ -35,7 +35,7 @@
35
35
  "@augment-vir/assert": "^31.67.1",
36
36
  "@augment-vir/common": "^31.67.1",
37
37
  "@augment-vir/node": "^31.67.1",
38
- "@virmator/core": "^14.8.0",
38
+ "@virmator/core": "^14.8.2",
39
39
  "jiti": "^2.6.1",
40
40
  "mri": "^1.2.0"
41
41
  },
@@ -1,3 +1,4 @@
1
+ import {assertWrap} from '@augment-vir/assert';
1
2
  import {type Rule, type SourceCode} from 'eslint';
2
3
  import type {Directive, IfStatement, ModuleDeclaration, Statement} from 'estree';
3
4
 
@@ -23,6 +24,13 @@ function isConsecutiveTerminatingIfs(
23
24
  );
24
25
  }
25
26
 
27
+ function reconstructComment(comment: Readonly<{type: string; value: string}>): string {
28
+ if (comment.type === 'Block') {
29
+ return `/*${comment.value}*/`;
30
+ }
31
+ return `//${comment.value}`;
32
+ }
33
+
26
34
  function checkBlock(
27
35
  statements: ReadonlyArray<AnyStatement>,
28
36
  context: Readonly<Rule.RuleContext>,
@@ -31,28 +39,52 @@ function checkBlock(
31
39
  statements.forEach((curr, index) => {
32
40
  const next = statements[index + 1];
33
41
 
34
- if (!next || !isConsecutiveTerminatingIfs(curr, next)) {
42
+ if (!next || !isConsecutiveTerminatingIfs(curr, next) || next.type !== 'IfStatement') {
35
43
  return;
36
44
  }
37
45
 
38
46
  const currRange = sourceCode.getRange(curr);
39
47
  const nextRange = sourceCode.getRange(next);
40
- const hasCommentsBetween = sourceCode.commentsExistBetween(curr, next);
48
+ const commentsBetween = sourceCode.getCommentsBefore(next);
41
49
 
42
50
  context.report({
43
51
  node: next,
44
52
  messageId: 'useIfElse',
45
- fix: hasCommentsBetween
46
- ? null
47
- : (fixer) => {
48
- return fixer.replaceTextRange(
49
- [
50
- currRange[1],
51
- nextRange[0],
52
- ],
53
- ' else ',
54
- );
55
- },
53
+ fix(fixer) {
54
+ const elseReplacement = fixer.replaceTextRange(
55
+ [
56
+ currRange[1],
57
+ nextRange[0],
58
+ ],
59
+ ' else ',
60
+ );
61
+
62
+ if (
63
+ commentsBetween.length > 0 &&
64
+ curr.consequent.type === 'BlockStatement' &&
65
+ curr.consequent.body.length > 0
66
+ ) {
67
+ const lastStatement = assertWrap.isDefined(curr.consequent.body.at(-1));
68
+ const lastStatementRange = sourceCode.getRange(lastStatement);
69
+ const sourceText = sourceCode.getText();
70
+ const lastLineStart =
71
+ sourceText.lastIndexOf('\n', lastStatementRange[0] - 1) + 1;
72
+ const indent = sourceText.slice(lastLineStart, lastStatementRange[0]);
73
+
74
+ const commentText = commentsBetween
75
+ .map((comment) => {
76
+ return reconstructComment(comment);
77
+ })
78
+ .join('\n' + indent);
79
+
80
+ return [
81
+ fixer.insertTextAfter(lastStatement, '\n' + indent + commentText),
82
+ elseReplacement,
83
+ ];
84
+ }
85
+
86
+ return elseReplacement;
87
+ },
56
88
  });
57
89
  });
58
90
  }