@virmator/lint 14.8.0 → 14.8.1

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