@virmator/lint 14.8.1 → 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);
@@ -29,28 +30,29 @@ function checkBlock(statements, context, sourceCode) {
29
30
  node: next,
30
31
  messageId: 'useIfElse',
31
32
  fix(fixer) {
32
- const fixes = [
33
- fixer.replaceTextRange([
34
- currRange[1],
35
- nextRange[0],
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);
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);
43
42
  const sourceText = sourceCode.getText();
44
- const lineStart = sourceText.lastIndexOf('\n', targetRange[0] - 1) + 1;
45
- const indent = sourceText.slice(lineStart, targetRange[0]);
43
+ const lastLineStart = sourceText.lastIndexOf('\n', lastStatementRange[0] - 1) + 1;
44
+ const indent = sourceText.slice(lastLineStart, lastStatementRange[0]);
46
45
  const commentText = commentsBetween
47
46
  .map((comment) => {
48
47
  return reconstructComment(comment);
49
48
  })
50
49
  .join('\n' + indent);
51
- fixes.push(fixer.insertTextBefore(targetStatement, commentText + '\n' + indent));
50
+ return [
51
+ fixer.insertTextAfter(lastStatement, '\n' + indent + commentText),
52
+ elseReplacement,
53
+ ];
52
54
  }
53
- return fixes;
55
+ return elseReplacement;
54
56
  },
55
57
  });
56
58
  });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@virmator/lint",
3
- "version": "14.8.1",
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.1",
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
 
@@ -50,26 +51,25 @@ function checkBlock(
50
51
  node: next,
51
52
  messageId: 'useIfElse',
52
53
  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);
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);
70
69
  const sourceText = sourceCode.getText();
71
- const lineStart = sourceText.lastIndexOf('\n', targetRange[0] - 1) + 1;
72
- const indent = sourceText.slice(lineStart, targetRange[0]);
70
+ const lastLineStart =
71
+ sourceText.lastIndexOf('\n', lastStatementRange[0] - 1) + 1;
72
+ const indent = sourceText.slice(lastLineStart, lastStatementRange[0]);
73
73
 
74
74
  const commentText = commentsBetween
75
75
  .map((comment) => {
@@ -77,12 +77,13 @@ function checkBlock(
77
77
  })
78
78
  .join('\n' + indent);
79
79
 
80
- fixes.push(
81
- fixer.insertTextBefore(targetStatement, commentText + '\n' + indent),
82
- );
80
+ return [
81
+ fixer.insertTextAfter(lastStatement, '\n' + indent + commentText),
82
+ elseReplacement,
83
+ ];
83
84
  }
84
85
 
85
- return fixes;
86
+ return elseReplacement;
86
87
  },
87
88
  });
88
89
  });