@virmator/lint 14.9.1 → 14.10.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,6 +10,7 @@ import {existsSync} from 'node:fs';
10
10
  import {join} from 'node:path';
11
11
  import tsEslint from 'typescript-eslint';
12
12
  import preferIfElseChainRule from '../src/rules/prefer-if-else-chain.lint.js';
13
+ import preferParseUrlRule from '../src/rules/prefer-parse-url.lint.js';
13
14
 
14
15
  export const globalVars = {
15
16
  ...globals.node,
@@ -86,6 +87,7 @@ export function defineEslintConfig(repoDir: string) {
86
87
  '@virmator': {
87
88
  rules: {
88
89
  'prefer-if-else-chain': preferIfElseChainRule,
90
+ 'prefer-parse-url': preferParseUrlRule,
89
91
  },
90
92
  },
91
93
  },
@@ -187,6 +189,7 @@ export function defineEslintConfig(repoDir: string) {
187
189
  '@typescript-eslint/await-thenable': 'error',
188
190
  '@typescript-eslint/no-unused-vars': 'error',
189
191
  '@virmator/prefer-if-else-chain': 'error',
192
+ '@virmator/prefer-parse-url': 'error',
190
193
  'no-lonely-if': 'error',
191
194
  curly: 'error',
192
195
  'object-shorthand': 'error',
@@ -0,0 +1,3 @@
1
+ import { type Rule } from 'eslint';
2
+ declare const rule: Rule.RuleModule;
3
+ export default rule;
@@ -0,0 +1,77 @@
1
+ function isPassedToInterface(node) {
2
+ const parent = node.parent;
3
+ return (((parent.type === 'CallExpression' || parent.type === 'NewExpression') &&
4
+ 'arguments' in parent &&
5
+ Array.isArray(parent.arguments) &&
6
+ parent.arguments.includes(node)) ||
7
+ parent.type === 'ReturnStatement' ||
8
+ (parent.type === 'ArrowFunctionExpression' && parent.body === node));
9
+ }
10
+ function addParseUrlImport(fixer, sourceCode) {
11
+ const importDeclarations = sourceCode.ast.body.filter((statement) => statement.type === 'ImportDeclaration');
12
+ const existingUrlVirImport = importDeclarations.find((imp) => imp.source.value === 'url-vir');
13
+ if (existingUrlVirImport) {
14
+ const hasParseUrl = existingUrlVirImport.specifiers.some((specifier) => specifier.type === 'ImportSpecifier' &&
15
+ specifier.imported.type === 'Identifier' &&
16
+ specifier.imported.name === 'parseUrl');
17
+ if (!hasParseUrl) {
18
+ const lastSpecifier = existingUrlVirImport.specifiers.at(-1);
19
+ if (lastSpecifier) {
20
+ return fixer.insertTextAfter(lastSpecifier, ', parseUrl');
21
+ }
22
+ }
23
+ return undefined;
24
+ }
25
+ const lastImport = importDeclarations.at(-1);
26
+ if (lastImport) {
27
+ return fixer.insertTextAfter(lastImport, "\nimport {parseUrl} from 'url-vir';");
28
+ }
29
+ const firstNode = sourceCode.ast.body[0];
30
+ if (firstNode) {
31
+ return fixer.insertTextBefore(firstNode, "import {parseUrl} from 'url-vir';\n\n");
32
+ }
33
+ return undefined;
34
+ }
35
+ const rule = {
36
+ meta: {
37
+ type: 'suggestion',
38
+ fixable: 'code',
39
+ messages: {
40
+ useParseUrl: 'Use parseUrl from url-vir instead of new URL for URL parsing.',
41
+ },
42
+ },
43
+ create(context) {
44
+ const sourceCode = context.sourceCode;
45
+ return {
46
+ NewExpression(node) {
47
+ if (node.callee.type !== 'Identifier' ||
48
+ node.callee.name !== 'URL' ||
49
+ node.arguments.length !== 1 ||
50
+ isPassedToInterface(node)) {
51
+ return;
52
+ }
53
+ context.report({
54
+ node,
55
+ messageId: 'useParseUrl',
56
+ fix(fixer) {
57
+ const arg = node.arguments[0];
58
+ if (!arg) {
59
+ return null;
60
+ }
61
+ const argText = sourceCode.getText(arg);
62
+ const nodeRange = sourceCode.getRange(node);
63
+ const fixes = [
64
+ fixer.replaceTextRange(nodeRange, `parseUrl(${argText})`),
65
+ ];
66
+ const importFix = addParseUrlImport(fixer, sourceCode);
67
+ if (importFix) {
68
+ fixes.push(importFix);
69
+ }
70
+ return fixes;
71
+ },
72
+ });
73
+ },
74
+ };
75
+ },
76
+ };
77
+ export default rule;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@virmator/lint",
3
- "version": "14.9.1",
3
+ "version": "14.10.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.68.1",
36
36
  "@augment-vir/common": "^31.68.1",
37
37
  "@augment-vir/node": "^31.68.1",
38
- "@virmator/core": "^14.9.1",
38
+ "@virmator/core": "^14.10.1",
39
39
  "jiti": "^2.6.1",
40
40
  "mri": "^1.2.0"
41
41
  },
@@ -59,7 +59,7 @@
59
59
  "eslint-plugin-sonarjs": "^4.0.2",
60
60
  "eslint-plugin-unicorn": "^63.0.0",
61
61
  "istanbul-smart-text-reporter": "^1.1.5",
62
- "typescript-eslint": "^8.57.0"
62
+ "typescript-eslint": "8.57.1"
63
63
  },
64
64
  "engines": {
65
65
  "node": ">=22"
@@ -0,0 +1,109 @@
1
+ import {type Rule, type SourceCode} from 'eslint';
2
+ import type {ImportDeclaration, NewExpression} from 'estree';
3
+
4
+ function isPassedToInterface(node: NewExpression & Rule.NodeParentExtension): boolean {
5
+ const parent = node.parent;
6
+
7
+ return (
8
+ ((parent.type === 'CallExpression' || parent.type === 'NewExpression') &&
9
+ 'arguments' in parent &&
10
+ Array.isArray(parent.arguments) &&
11
+ parent.arguments.includes(node)) ||
12
+ parent.type === 'ReturnStatement' ||
13
+ (parent.type === 'ArrowFunctionExpression' && parent.body === node)
14
+ );
15
+ }
16
+
17
+ function addParseUrlImport(
18
+ fixer: Rule.RuleFixer,
19
+ sourceCode: Readonly<SourceCode>,
20
+ ): Rule.Fix | undefined {
21
+ const importDeclarations = sourceCode.ast.body.filter(
22
+ (statement): statement is ImportDeclaration => statement.type === 'ImportDeclaration',
23
+ );
24
+
25
+ const existingUrlVirImport = importDeclarations.find((imp) => imp.source.value === 'url-vir');
26
+
27
+ if (existingUrlVirImport) {
28
+ const hasParseUrl = existingUrlVirImport.specifiers.some(
29
+ (specifier) =>
30
+ specifier.type === 'ImportSpecifier' &&
31
+ specifier.imported.type === 'Identifier' &&
32
+ specifier.imported.name === 'parseUrl',
33
+ );
34
+
35
+ if (!hasParseUrl) {
36
+ const lastSpecifier = existingUrlVirImport.specifiers.at(-1);
37
+ if (lastSpecifier) {
38
+ return fixer.insertTextAfter(lastSpecifier, ', parseUrl');
39
+ }
40
+ }
41
+
42
+ return undefined;
43
+ }
44
+
45
+ const lastImport = importDeclarations.at(-1);
46
+ if (lastImport) {
47
+ return fixer.insertTextAfter(lastImport, "\nimport {parseUrl} from 'url-vir';");
48
+ }
49
+
50
+ const firstNode = sourceCode.ast.body[0];
51
+ if (firstNode) {
52
+ return fixer.insertTextBefore(firstNode, "import {parseUrl} from 'url-vir';\n\n");
53
+ }
54
+
55
+ return undefined;
56
+ }
57
+
58
+ const rule: Rule.RuleModule = {
59
+ meta: {
60
+ type: 'suggestion',
61
+ fixable: 'code',
62
+ messages: {
63
+ useParseUrl: 'Use parseUrl from url-vir instead of new URL for URL parsing.',
64
+ },
65
+ },
66
+ create(context) {
67
+ const sourceCode = context.sourceCode;
68
+
69
+ return {
70
+ NewExpression(node: NewExpression & Rule.NodeParentExtension) {
71
+ if (
72
+ node.callee.type !== 'Identifier' ||
73
+ node.callee.name !== 'URL' ||
74
+ node.arguments.length !== 1 ||
75
+ isPassedToInterface(node)
76
+ ) {
77
+ return;
78
+ }
79
+
80
+ context.report({
81
+ node,
82
+ messageId: 'useParseUrl',
83
+ fix(fixer) {
84
+ const arg = node.arguments[0];
85
+ if (!arg) {
86
+ return null;
87
+ }
88
+
89
+ const argText = sourceCode.getText(arg);
90
+ const nodeRange = sourceCode.getRange(node);
91
+
92
+ const fixes: Rule.Fix[] = [
93
+ fixer.replaceTextRange(nodeRange, `parseUrl(${argText})`),
94
+ ];
95
+
96
+ const importFix = addParseUrlImport(fixer, sourceCode);
97
+ if (importFix) {
98
+ fixes.push(importFix);
99
+ }
100
+
101
+ return fixes;
102
+ },
103
+ });
104
+ },
105
+ };
106
+ },
107
+ };
108
+
109
+ export default rule;