@spinnaker/eslint-plugin 2026.2.2 → 2026.3.0
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/base.config.js +0 -9
- package/eslint-plugin.spec.ts +34 -0
- package/eslint-plugin.ts +1 -16
- package/legacy.config.js +72 -0
- package/none.config.js +0 -1
- package/package.json +5 -5
- package/rules/api-no-unused-chaining.spec.ts +5 -1
- package/rules/api-no-unused-chaining.ts +1 -12
- package/rules/import-sort.spec.ts +5 -5
- package/rules/import-sort.ts +2 -2
- package/test.eslintrc +12 -14
- package/utils/utils.spec.ts +16 -0
- package/utils/utils.ts +3 -1
- package/rules/migrate-to-mock-http-client.spec.ts +0 -78
- package/rules/migrate-to-mock-http-client.ts +0 -121
- package/rules/ng-no-component-class.spec.ts +0 -45
- package/rules/ng-no-component-class.ts +0 -67
- package/rules/ng-no-module-export.spec.ts +0 -26
- package/rules/ng-no-module-export.ts +0 -117
- package/rules/ng-no-require-angularjs.spec.ts +0 -27
- package/rules/ng-no-require-angularjs.ts +0 -92
- package/rules/ng-no-require-module-deps.spec.ts +0 -33
- package/rules/ng-no-require-module-deps.ts +0 -211
- package/rules/ng-strictdi.spec.ts +0 -100
- package/rules/ng-strictdi.ts +0 -304
- package/rules/prefer-promise-like.spec.ts +0 -75
- package/rules/prefer-promise-like.ts +0 -108
- package/rules/react2angular-with-error-boundary.spec.ts +0 -29
- package/rules/react2angular-with-error-boundary.ts +0 -119
- package/utils/angular-rule/angular-rule.js +0 -302
- package/utils/angular-rule/false-values.js +0 -6
- package/utils/angular-rule/utils.js +0 -625
|
@@ -1,67 +0,0 @@
|
|
|
1
|
-
import type { Rule, Scope } from 'eslint';
|
|
2
|
-
import camelCase from 'lodash/camelCase';
|
|
3
|
-
|
|
4
|
-
const findParentNodeByType = (node: Rule.Node, type: string) =>
|
|
5
|
-
!node ? null : node.type === type ? node : findParentNodeByType(node.parent, type);
|
|
6
|
-
|
|
7
|
-
/**
|
|
8
|
-
* Use object literal when declaring AngularJS components
|
|
9
|
-
* Do not use new ComponentClass()
|
|
10
|
-
*
|
|
11
|
-
* @version 0.1.0
|
|
12
|
-
* @category conventions
|
|
13
|
-
*/
|
|
14
|
-
import angularRule from '../utils/angular-rule/angular-rule';
|
|
15
|
-
|
|
16
|
-
const useObjectLiteral = function (context: Rule.RuleContext) {
|
|
17
|
-
return {
|
|
18
|
-
'angular?component': function (callee, thisGuy) {
|
|
19
|
-
const node: Rule.Node = thisGuy.node;
|
|
20
|
-
const scope: Scope.Scope = thisGuy.scope;
|
|
21
|
-
if (node.type === 'NewExpression') {
|
|
22
|
-
const calleeName = 'name' in node.callee ? node.callee.name : undefined;
|
|
23
|
-
const fix = (fixer: Rule.RuleFixer) => {
|
|
24
|
-
const variable = scope.variables.find((x) => x.name === calleeName);
|
|
25
|
-
const classDef = variable.defs[0].node;
|
|
26
|
-
const name = classDef.id.name;
|
|
27
|
-
const camelCaseName = camelCase(name);
|
|
28
|
-
const rename = name !== camelCaseName;
|
|
29
|
-
|
|
30
|
-
const objProperties = classDef.body.body.map((node) => {
|
|
31
|
-
return node.key.name + ': ' + context.getSourceCode().getText(node.value);
|
|
32
|
-
});
|
|
33
|
-
|
|
34
|
-
const impls = (classDef.implements || []).map((impl) => context.getSourceCode().getText(impl));
|
|
35
|
-
const identifier = `const ${camelCaseName}${impls.length ? `: ${impls.join(' & ')}` : ''}`;
|
|
36
|
-
const objectLiteral = `${identifier} = {\n` + ` ${objProperties.join(',\n ')}\n` + `};`;
|
|
37
|
-
|
|
38
|
-
const otherReferences = findParentNodeByType(classDef, 'Program').tokens.filter((token) => {
|
|
39
|
-
const ignores = [classDef.id.range[0], node.callee.range[0]];
|
|
40
|
-
return (
|
|
41
|
-
token.type === 'Identifier' && token.value === name && ignores.every((pos) => pos !== token.range[0])
|
|
42
|
-
);
|
|
43
|
-
});
|
|
44
|
-
|
|
45
|
-
const otherFixes = rename ? [] : otherReferences.map((id) => fixer.replaceText(id, camelCaseName));
|
|
46
|
-
|
|
47
|
-
return [fixer.replaceText(classDef, objectLiteral), fixer.replaceText(node, camelCaseName), ...otherFixes];
|
|
48
|
-
};
|
|
49
|
-
|
|
50
|
-
const message = 'Use .component("foo", {}) instead of .component("foo", new FooComponentClass())';
|
|
51
|
-
context.report({ fix, node, message });
|
|
52
|
-
}
|
|
53
|
-
},
|
|
54
|
-
};
|
|
55
|
-
};
|
|
56
|
-
|
|
57
|
-
const ruleModule: Rule.RuleModule = {
|
|
58
|
-
meta: {
|
|
59
|
-
type: 'problem',
|
|
60
|
-
fixable: 'code',
|
|
61
|
-
docs: {
|
|
62
|
-
description: 'Prefer .component("foo", {}) over .component("foo", new FooComponentClass())',
|
|
63
|
-
},
|
|
64
|
-
},
|
|
65
|
-
create: angularRule(useObjectLiteral),
|
|
66
|
-
};
|
|
67
|
-
export default ruleModule;
|
|
@@ -1,26 +0,0 @@
|
|
|
1
|
-
import rule from './ng-no-module-export';
|
|
2
|
-
import ruleTester from '../utils/ruleTester';
|
|
3
|
-
|
|
4
|
-
ruleTester.run('ng-no-module-export', rule, {
|
|
5
|
-
valid: [
|
|
6
|
-
{
|
|
7
|
-
code: `
|
|
8
|
-
const angular = require('angular');
|
|
9
|
-
export const MODULE_NAME = 'foo';
|
|
10
|
-
angular.module(MODULE_NAME, [])
|
|
11
|
-
.component('componentName', {});
|
|
12
|
-
`,
|
|
13
|
-
},
|
|
14
|
-
],
|
|
15
|
-
|
|
16
|
-
invalid: [
|
|
17
|
-
{
|
|
18
|
-
errors: [{ message: 'Prefer exporting the AngularJS module name instead of the entire module' }],
|
|
19
|
-
code: `
|
|
20
|
-
const angular = require('angular');
|
|
21
|
-
module.exports = angular.module('foo', [])
|
|
22
|
-
.component('componentName', {});
|
|
23
|
-
`,
|
|
24
|
-
},
|
|
25
|
-
],
|
|
26
|
-
});
|
|
@@ -1,117 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Prefer exporting a module's NAME instead of the entire angular.module()
|
|
3
|
-
*/
|
|
4
|
-
|
|
5
|
-
import type { AST, Rule } from 'eslint';
|
|
6
|
-
import { isCallExpression, isIdentifier, isMemberExpression } from '../utils/utils';
|
|
7
|
-
|
|
8
|
-
const rule = function (context: Rule.RuleContext) {
|
|
9
|
-
function getSuggestedVariableNameForFile() {
|
|
10
|
-
const filename = context.getFilename();
|
|
11
|
-
if (filename.includes('/packages/')) {
|
|
12
|
-
return filename
|
|
13
|
-
.replace(/^.*\/packages\//g, '')
|
|
14
|
-
.replace(/\/src\//g, '/')
|
|
15
|
-
.replace(/\.[\w]*$/g, '')
|
|
16
|
-
.replace(/[^\w_]/g, '_')
|
|
17
|
-
.toUpperCase();
|
|
18
|
-
}
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
return {
|
|
22
|
-
AssignmentExpression: function (_node: any) {
|
|
23
|
-
const node = _node;
|
|
24
|
-
const left = node.left;
|
|
25
|
-
const right = node.right;
|
|
26
|
-
const isModuleExports = isModuleExportMemberExpression(left);
|
|
27
|
-
const moduleNameNode = getAngularModuleNameNode(right);
|
|
28
|
-
if (isModuleExports && moduleNameNode) {
|
|
29
|
-
const message = 'Prefer exporting the AngularJS module name instead of the entire module';
|
|
30
|
-
const variableName = getSuggestedVariableNameForFile();
|
|
31
|
-
|
|
32
|
-
const fix = (fixer: Rule.RuleFixer) => {
|
|
33
|
-
const assignmentRange = [left?.range[0], right?.range[0]] as AST.Range;
|
|
34
|
-
const exportModuleVariable = `export const ${variableName} = ${moduleNameNode.raw};\n`;
|
|
35
|
-
const exportNameVariable = `export const name = ${variableName}; // for backwards compatibility\n`;
|
|
36
|
-
return [
|
|
37
|
-
// Insert 'export const FOO = 'foo';
|
|
38
|
-
fixer.insertTextBefore(node, exportModuleVariable + exportNameVariable),
|
|
39
|
-
// Remove 'module.exports = '
|
|
40
|
-
fixer.replaceTextRange(assignmentRange, ''),
|
|
41
|
-
// Replace 'angular.module("foo"' with 'angular.module(FOO'
|
|
42
|
-
fixer.replaceText(moduleNameNode, variableName),
|
|
43
|
-
];
|
|
44
|
-
};
|
|
45
|
-
|
|
46
|
-
if (variableName) {
|
|
47
|
-
context.report({ node, message, fix });
|
|
48
|
-
} else {
|
|
49
|
-
context.report({ node, message });
|
|
50
|
-
}
|
|
51
|
-
}
|
|
52
|
-
},
|
|
53
|
-
};
|
|
54
|
-
};
|
|
55
|
-
|
|
56
|
-
function isModuleExportMemberExpression(node) {
|
|
57
|
-
const object = node.object;
|
|
58
|
-
const property = node.property;
|
|
59
|
-
const isModuleExports = node.type === 'MemberExpression' && object.name === 'module' && property.name === 'exports';
|
|
60
|
-
const isBareExports = node.type === 'Identifier' && node.name === 'exports';
|
|
61
|
-
return isModuleExports || isBareExports;
|
|
62
|
-
}
|
|
63
|
-
|
|
64
|
-
function getAngularModuleNameNode(node) {
|
|
65
|
-
if (!isCallExpression(node)) return false;
|
|
66
|
-
const callee = node.callee as Rule.Node;
|
|
67
|
-
|
|
68
|
-
function angularModuleNameNode(callExpression: any) {
|
|
69
|
-
const isLiteral =
|
|
70
|
-
callExpression.arguments && callExpression.arguments[0] && callExpression.arguments[0].type === 'Literal';
|
|
71
|
-
return isLiteral ? callExpression.arguments[0] : undefined;
|
|
72
|
-
}
|
|
73
|
-
|
|
74
|
-
function isChainedCallExpression(_callee: any): boolean {
|
|
75
|
-
if (isMemberExpression(_callee)) {
|
|
76
|
-
return _callee.object && _callee.object.type === 'CallExpression';
|
|
77
|
-
}
|
|
78
|
-
}
|
|
79
|
-
|
|
80
|
-
function isAngularModuleCall(_callee) {
|
|
81
|
-
if (isMemberExpression(_callee)) {
|
|
82
|
-
return (
|
|
83
|
-
_callee.object &&
|
|
84
|
-
_callee.object.type === 'Identifier' &&
|
|
85
|
-
_callee.object.name === 'angular' &&
|
|
86
|
-
_callee.property &&
|
|
87
|
-
'name' in _callee.property &&
|
|
88
|
-
_callee.property.name === 'module'
|
|
89
|
-
);
|
|
90
|
-
}
|
|
91
|
-
}
|
|
92
|
-
|
|
93
|
-
function isRawModuleCall(_callee) {
|
|
94
|
-
return isIdentifier(_callee) && _callee.name === 'module';
|
|
95
|
-
}
|
|
96
|
-
|
|
97
|
-
if (isChainedCallExpression(callee)) {
|
|
98
|
-
return getAngularModuleNameNode((callee as any).object);
|
|
99
|
-
} else if (isRawModuleCall(callee)) {
|
|
100
|
-
if (node.arguments && node.arguments[0] && node.arguments[0].type === 'Literal') return angularModuleNameNode(node);
|
|
101
|
-
} else if (isAngularModuleCall(callee)) {
|
|
102
|
-
return angularModuleNameNode(node);
|
|
103
|
-
}
|
|
104
|
-
}
|
|
105
|
-
|
|
106
|
-
const ruleModule: Rule.RuleModule = {
|
|
107
|
-
meta: {
|
|
108
|
-
type: 'problem',
|
|
109
|
-
docs: {
|
|
110
|
-
description: 'Instead of exporting the angular.module(), export the modules string identifier',
|
|
111
|
-
},
|
|
112
|
-
fixable: 'code',
|
|
113
|
-
},
|
|
114
|
-
create: rule,
|
|
115
|
-
};
|
|
116
|
-
|
|
117
|
-
export default ruleModule;
|
|
@@ -1,27 +0,0 @@
|
|
|
1
|
-
import rule from './ng-no-require-angularjs';
|
|
2
|
-
import ruleTester from '../utils/ruleTester';
|
|
3
|
-
|
|
4
|
-
ruleTester.run('ng-no-require-angularjs', rule, {
|
|
5
|
-
valid: [
|
|
6
|
-
{
|
|
7
|
-
code: `
|
|
8
|
-
import { module } from 'angular';
|
|
9
|
-
module('foo', []);
|
|
10
|
-
`,
|
|
11
|
-
},
|
|
12
|
-
],
|
|
13
|
-
|
|
14
|
-
invalid: [
|
|
15
|
-
{
|
|
16
|
-
errors: [{ message: "Prefer module('foo', []) to angular.module('foo', [])" }],
|
|
17
|
-
code: `
|
|
18
|
-
import angular from 'angular';
|
|
19
|
-
angular.module('foo', []);
|
|
20
|
-
`,
|
|
21
|
-
output: `
|
|
22
|
-
import { module } from 'angular';
|
|
23
|
-
module('foo', []);
|
|
24
|
-
`,
|
|
25
|
-
},
|
|
26
|
-
],
|
|
27
|
-
});
|
|
@@ -1,92 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Prefer:
|
|
3
|
-
* import { module } from 'angular';
|
|
4
|
-
* module('mymodule', [])
|
|
5
|
-
*
|
|
6
|
-
* over:
|
|
7
|
-
* import * as angular from 'angular';
|
|
8
|
-
* angular.module('mymodule', [])
|
|
9
|
-
*/
|
|
10
|
-
import type { Rule, Scope } from 'eslint';
|
|
11
|
-
import { getProgram, isMemberExpression } from '../utils/utils';
|
|
12
|
-
|
|
13
|
-
const rule = function (context: Rule.RuleContext) {
|
|
14
|
-
return {
|
|
15
|
-
'MemberExpression[object.name="angular"][property.name="module"]': function (_node: any) {
|
|
16
|
-
const node = _node;
|
|
17
|
-
const angularVar = findAngularVariable(node, context);
|
|
18
|
-
const angularImport = findAngularImportStatement(node);
|
|
19
|
-
// Double check that there is only a single use of 'angular' variable and that it's 'angular.module()')
|
|
20
|
-
if (angularImport && angularVar && angularVar.references.length === 1) {
|
|
21
|
-
const { parent } = angularVar.references[0].identifier as any;
|
|
22
|
-
if (isMemberExpression(parent)) {
|
|
23
|
-
if (
|
|
24
|
-
'name' in parent.object &&
|
|
25
|
-
parent.object.name === 'angular' &&
|
|
26
|
-
'name' in parent.property &&
|
|
27
|
-
parent.property.name === 'module'
|
|
28
|
-
) {
|
|
29
|
-
const message = "Prefer module('foo', []) to angular.module('foo', [])";
|
|
30
|
-
const fix = getFixForAngularModule(node, angularImport);
|
|
31
|
-
return context.report({ node, message, fix });
|
|
32
|
-
}
|
|
33
|
-
}
|
|
34
|
-
}
|
|
35
|
-
},
|
|
36
|
-
};
|
|
37
|
-
};
|
|
38
|
-
|
|
39
|
-
function findAngularVariable(_node, context): Scope.Variable {
|
|
40
|
-
const program = getProgram(_node);
|
|
41
|
-
|
|
42
|
-
const programScope = context.getSourceCode().scopeManager.acquire(program);
|
|
43
|
-
const moduleScope = programScope && programScope.childScopes.find((s) => s.type === 'module');
|
|
44
|
-
return moduleScope && moduleScope.variables.find((v) => v.name === 'angular');
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
function findAngularImportStatement(_node): any {
|
|
48
|
-
let program = _node;
|
|
49
|
-
while (program && program.parent) {
|
|
50
|
-
program = program.parent;
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
return program.body.find((node) => {
|
|
54
|
-
return (
|
|
55
|
-
node.type === 'ImportDeclaration' &&
|
|
56
|
-
node.source &&
|
|
57
|
-
node.source.type === 'Literal' &&
|
|
58
|
-
node.source.value === 'angular'
|
|
59
|
-
);
|
|
60
|
-
});
|
|
61
|
-
}
|
|
62
|
-
|
|
63
|
-
/*
|
|
64
|
-
Given:
|
|
65
|
-
angular.module('module', ['dep']);
|
|
66
|
-
|
|
67
|
-
Rewrites to:
|
|
68
|
-
import { module } from 'angular';
|
|
69
|
-
|
|
70
|
-
module('module', ['dep']);
|
|
71
|
-
*/
|
|
72
|
-
function getFixForAngularModule(angularDotModuleNode: Rule.Node, importStatement: any) {
|
|
73
|
-
return function (fixer: Rule.RuleFixer) {
|
|
74
|
-
return [
|
|
75
|
-
fixer.replaceText(angularDotModuleNode, 'module'),
|
|
76
|
-
fixer.replaceText(importStatement, `import { module } from 'angular';`),
|
|
77
|
-
];
|
|
78
|
-
};
|
|
79
|
-
}
|
|
80
|
-
|
|
81
|
-
const ruleModule: Rule.RuleModule = {
|
|
82
|
-
meta: {
|
|
83
|
-
type: 'problem',
|
|
84
|
-
docs: {
|
|
85
|
-
description: `Prefer import { module } from 'angular' over angular.module()`,
|
|
86
|
-
},
|
|
87
|
-
fixable: 'code',
|
|
88
|
-
},
|
|
89
|
-
create: rule,
|
|
90
|
-
};
|
|
91
|
-
|
|
92
|
-
export default ruleModule;
|
|
@@ -1,33 +0,0 @@
|
|
|
1
|
-
import rule from './ng-no-require-module-deps';
|
|
2
|
-
import ruleTester from '../utils/ruleTester';
|
|
3
|
-
|
|
4
|
-
ruleTester.run('ng-no-require-module-deps', rule, {
|
|
5
|
-
valid: [
|
|
6
|
-
{
|
|
7
|
-
code: `
|
|
8
|
-
import { module } from 'angular';
|
|
9
|
-
import FOO_MODULE from './foo';
|
|
10
|
-
module('foo', [FOO_MODULE]);
|
|
11
|
-
`,
|
|
12
|
-
},
|
|
13
|
-
],
|
|
14
|
-
|
|
15
|
-
invalid: [
|
|
16
|
-
{
|
|
17
|
-
errors: [
|
|
18
|
-
{
|
|
19
|
-
message: 'Prefer \'import ANGULARJS_LIBRARY from "angularjs-library"\' over \'require("angularjs-library")\'',
|
|
20
|
-
},
|
|
21
|
-
],
|
|
22
|
-
code: `
|
|
23
|
-
import angular from 'angular';
|
|
24
|
-
angular.module('foo', [ require('./foo') ]);
|
|
25
|
-
`,
|
|
26
|
-
output: `
|
|
27
|
-
import angular from 'angular';
|
|
28
|
-
import __FOO from './foo';
|
|
29
|
-
angular.module('foo', [ __FOO ]);
|
|
30
|
-
`,
|
|
31
|
-
},
|
|
32
|
-
],
|
|
33
|
-
});
|
|
@@ -1,211 +0,0 @@
|
|
|
1
|
-
import type { Rule } from 'eslint';
|
|
2
|
-
import fs from 'fs';
|
|
3
|
-
import path from 'path';
|
|
4
|
-
|
|
5
|
-
/**
|
|
6
|
-
* Prefer exporting a module's NAME instead of the entire angular.module()
|
|
7
|
-
*
|
|
8
|
-
* @version 0.1.0
|
|
9
|
-
* @category conventions
|
|
10
|
-
* @sinceAngularVersion 1.x
|
|
11
|
-
*/
|
|
12
|
-
const rule = function (context: Rule.RuleContext) {
|
|
13
|
-
return {
|
|
14
|
-
ArrayExpression: function (_node: any) {
|
|
15
|
-
const node = _node;
|
|
16
|
-
if (isInAngularModuleCall(node)) {
|
|
17
|
-
const requireDotNames = node.elements.map((element) => getRequireDotNameNode(element)).filter((x) => !!x);
|
|
18
|
-
requireDotNames.forEach(([_node, relativePath]) => {
|
|
19
|
-
const message = `Prefer 'import { ANGULARJS_MODULE } from "./module"' over 'require("./module").name'`;
|
|
20
|
-
const fix = getFixForRequireDotName(_node, context.getFilename(), relativePath);
|
|
21
|
-
context.report({ node: _node, message, fix });
|
|
22
|
-
});
|
|
23
|
-
|
|
24
|
-
const requireDotAnythings = node.elements
|
|
25
|
-
.map((element) => getRequireDotAnythingNode(element))
|
|
26
|
-
.filter((x) => !!x);
|
|
27
|
-
requireDotAnythings.forEach(([_node, requiredString, propertyName]) => {
|
|
28
|
-
const message = `Prefer 'import { default as ANGULARJS_MODULE } from "./module"' over 'require("./module").default'`;
|
|
29
|
-
const fix = getFixForRequireDotAnything(_node, requiredString, propertyName);
|
|
30
|
-
context.report({ node: _node, message, fix });
|
|
31
|
-
});
|
|
32
|
-
|
|
33
|
-
const bareRequires = node.elements.map((element) => getBareRequireNode(element)).filter((x) => !!x);
|
|
34
|
-
bareRequires.forEach(([_node, requiredString]) => {
|
|
35
|
-
const message = `Prefer 'import ANGULARJS_LIBRARY from "angularjs-library"' over 'require("angularjs-library")'`;
|
|
36
|
-
const fix = getFixForBareRequire(_node, requiredString);
|
|
37
|
-
context.report({ node: _node, message, fix });
|
|
38
|
-
});
|
|
39
|
-
}
|
|
40
|
-
},
|
|
41
|
-
};
|
|
42
|
-
};
|
|
43
|
-
|
|
44
|
-
/*
|
|
45
|
-
Given:
|
|
46
|
-
angular.module('module', [
|
|
47
|
-
require('angular-ui-bootstrap')
|
|
48
|
-
]);
|
|
49
|
-
|
|
50
|
-
Rewrites to:
|
|
51
|
-
import ANGULAR_UI_BOOTSTRAP from 'angular-ui-bootstrap';
|
|
52
|
-
|
|
53
|
-
angular.module('module', [
|
|
54
|
-
ANGULAR_UI_BOOTSTRAP
|
|
55
|
-
]);
|
|
56
|
-
*/
|
|
57
|
-
function getFixForBareRequire(node: any, requiredString: string) {
|
|
58
|
-
return function (fixer: Rule.RuleFixer) {
|
|
59
|
-
const variableName = requiredString.replace(/[^\w_]/g, '_').toUpperCase();
|
|
60
|
-
const lastImport = findLastImportStatement(node);
|
|
61
|
-
const importStatement = `\nimport ${variableName} from '${requiredString}';`;
|
|
62
|
-
if (lastImport) {
|
|
63
|
-
return [fixer.replaceText(node, variableName), fixer.insertTextAfter(lastImport, importStatement)];
|
|
64
|
-
} else {
|
|
65
|
-
return [fixer.replaceText(node, variableName), fixer.insertTextBeforeRange([0, 0], importStatement)];
|
|
66
|
-
}
|
|
67
|
-
};
|
|
68
|
-
}
|
|
69
|
-
|
|
70
|
-
/*
|
|
71
|
-
Given:
|
|
72
|
-
angular.module('module', [
|
|
73
|
-
require('some/require/string').default
|
|
74
|
-
]);
|
|
75
|
-
|
|
76
|
-
Rewrites to:
|
|
77
|
-
import { default as SOME_REQUIRE_STRING } from 'some/require/string';
|
|
78
|
-
|
|
79
|
-
angular.module('module', [
|
|
80
|
-
SOME_REQUIRE_STRING
|
|
81
|
-
]);
|
|
82
|
-
*/
|
|
83
|
-
function getFixForRequireDotAnything(node: any, requiredString: string, property: string) {
|
|
84
|
-
return function (fixer: Rule.RuleFixer) {
|
|
85
|
-
const variableName = requiredString
|
|
86
|
-
.replace(/^[^\w_]*/g, '')
|
|
87
|
-
.replace(/[^\w_]/g, '_')
|
|
88
|
-
.toUpperCase();
|
|
89
|
-
const lastImport = findLastImportStatement(node);
|
|
90
|
-
const importStatement = `\nimport { ${property} as ${variableName} } from '${requiredString}';`;
|
|
91
|
-
if (lastImport) {
|
|
92
|
-
return [fixer.replaceText(node, variableName), fixer.insertTextAfter(lastImport, importStatement)];
|
|
93
|
-
} else {
|
|
94
|
-
return [fixer.replaceText(node, variableName), fixer.insertTextBeforeRange([0, 0], importStatement)];
|
|
95
|
-
}
|
|
96
|
-
};
|
|
97
|
-
}
|
|
98
|
-
|
|
99
|
-
/*
|
|
100
|
-
Given:
|
|
101
|
-
angular.module('module', [
|
|
102
|
-
require('./path/to/dependency').name
|
|
103
|
-
]);
|
|
104
|
-
|
|
105
|
-
Rewrites to:
|
|
106
|
-
import { DEPENDENCY_SYMBOL } from './path/to/dependency';
|
|
107
|
-
|
|
108
|
-
angular.module('module', [
|
|
109
|
-
DEPENDENCY_SYMBOL
|
|
110
|
-
]);
|
|
111
|
-
*/
|
|
112
|
-
function getFixForRequireDotName(node: any, filename: string, relativePath: string) {
|
|
113
|
-
const modulesPath = filename.replace(/modules\/.*/, 'modules/');
|
|
114
|
-
|
|
115
|
-
const path1 = path.resolve(filename, '..', relativePath);
|
|
116
|
-
const path2 = path.resolve(modulesPath, relativePath.replace(/^([a-zA-Z]+)\//, '$1/src/'));
|
|
117
|
-
const path3 = path.resolve(modulesPath, relativePath);
|
|
118
|
-
|
|
119
|
-
for (const path of [path1, path2, path3]) {
|
|
120
|
-
for (const extension of ['.ts', '.js']) {
|
|
121
|
-
if (fs.existsSync(path + extension)) {
|
|
122
|
-
const fileSource = fs.readFileSync(path + extension).toString();
|
|
123
|
-
const match = /export const name = ([\w_]*);/.exec(fileSource);
|
|
124
|
-
if (match) {
|
|
125
|
-
const variableName = match[1];
|
|
126
|
-
return function (fixer) {
|
|
127
|
-
const lastImport = findLastImportStatement(node);
|
|
128
|
-
const importStatement = `\nimport { ${variableName} } from '${relativePath}';`;
|
|
129
|
-
if (lastImport) {
|
|
130
|
-
return [fixer.replaceText(node, variableName), fixer.insertTextAfter(lastImport, importStatement)];
|
|
131
|
-
} else {
|
|
132
|
-
return [fixer.replaceText(node, variableName), fixer.insertTextBeforeRange([0, 0], importStatement)];
|
|
133
|
-
}
|
|
134
|
-
};
|
|
135
|
-
}
|
|
136
|
-
}
|
|
137
|
-
}
|
|
138
|
-
}
|
|
139
|
-
}
|
|
140
|
-
|
|
141
|
-
function findLastImportStatement(_node: any) {
|
|
142
|
-
let program = _node as Rule.Node;
|
|
143
|
-
while (program && program.parent) {
|
|
144
|
-
program = program.parent;
|
|
145
|
-
}
|
|
146
|
-
|
|
147
|
-
if (program && program.type === 'Program') {
|
|
148
|
-
const imports = program.body.filter((node) => node.type === 'ImportDeclaration');
|
|
149
|
-
if (imports.length) {
|
|
150
|
-
return imports[imports.length - 1];
|
|
151
|
-
}
|
|
152
|
-
}
|
|
153
|
-
}
|
|
154
|
-
|
|
155
|
-
// require('./some/nested/angularjs/module').name
|
|
156
|
-
function getRequireDotNameNode(node: any): [any, string] {
|
|
157
|
-
if (node.type !== 'MemberExpression') return undefined;
|
|
158
|
-
if (node.property.type !== 'Identifier' || node.property.name !== 'name') return undefined;
|
|
159
|
-
if (node.object.type !== 'CallExpression' || (node.object.callee as any).name !== 'require') return undefined;
|
|
160
|
-
if (node.object.arguments.length !== 1 || node.object.arguments[0].type !== 'Literal') return undefined;
|
|
161
|
-
|
|
162
|
-
// [node, './some/nested/angularjs/module']
|
|
163
|
-
return [node, node.object.arguments[0].value as string];
|
|
164
|
-
}
|
|
165
|
-
|
|
166
|
-
// require('something').anything
|
|
167
|
-
function getRequireDotAnythingNode(node: any): [any, string, string] {
|
|
168
|
-
if (node.type !== 'MemberExpression') return undefined;
|
|
169
|
-
if (node.property.type !== 'Identifier') return undefined;
|
|
170
|
-
if (node.object.type !== 'CallExpression' || (node.object.callee as any).name !== 'require') return undefined;
|
|
171
|
-
if (node.object.arguments.length !== 1 || node.object.arguments[0].type !== 'Literal') return undefined;
|
|
172
|
-
|
|
173
|
-
// [node, 'something', 'anything']
|
|
174
|
-
return [node, node.object.arguments[0].value as string, node.property.name];
|
|
175
|
-
}
|
|
176
|
-
|
|
177
|
-
// require('something')
|
|
178
|
-
function getBareRequireNode(node): [any, string] {
|
|
179
|
-
if (node.type !== 'CallExpression' || node.callee.name !== 'require') return undefined;
|
|
180
|
-
if (node.arguments.length !== 1 || node.arguments[0].type !== 'Literal') return undefined;
|
|
181
|
-
|
|
182
|
-
// [node, 'something']
|
|
183
|
-
return [node, node.arguments[0].value as string];
|
|
184
|
-
}
|
|
185
|
-
|
|
186
|
-
function isInAngularModuleCall(arrayExpression) {
|
|
187
|
-
const { parent = {} } = arrayExpression;
|
|
188
|
-
if (parent.type === 'CallExpression') {
|
|
189
|
-
const { callee = {} } = parent;
|
|
190
|
-
const { type, object, property } = callee;
|
|
191
|
-
const isAngularModule =
|
|
192
|
-
type === 'MemberExpression' && object && object.name === 'angular' && property && property.name === 'module';
|
|
193
|
-
const isRawModule = type === 'Identifier' && callee.name === 'module';
|
|
194
|
-
|
|
195
|
-
return isAngularModule || isRawModule;
|
|
196
|
-
}
|
|
197
|
-
return false;
|
|
198
|
-
}
|
|
199
|
-
|
|
200
|
-
const ruleModule: Rule.RuleModule = {
|
|
201
|
-
meta: {
|
|
202
|
-
type: 'problem',
|
|
203
|
-
docs: {
|
|
204
|
-
description: 'Import an angular module name symbol instead of using require("../foo").name',
|
|
205
|
-
},
|
|
206
|
-
fixable: 'code',
|
|
207
|
-
},
|
|
208
|
-
create: rule,
|
|
209
|
-
};
|
|
210
|
-
|
|
211
|
-
export default ruleModule;
|
|
@@ -1,100 +0,0 @@
|
|
|
1
|
-
import rule from './ng-strictdi';
|
|
2
|
-
import ruleTester from '../utils/ruleTester';
|
|
3
|
-
ruleTester.run('ng-strictdi', rule, {
|
|
4
|
-
valid: [
|
|
5
|
-
{
|
|
6
|
-
code: `
|
|
7
|
-
import { module } from 'angular';
|
|
8
|
-
module('foo', [])
|
|
9
|
-
.directive('myDirective', ['$scope', function($scope) {}]);
|
|
10
|
-
`,
|
|
11
|
-
},
|
|
12
|
-
{
|
|
13
|
-
code: `
|
|
14
|
-
import { module } from 'angular';
|
|
15
|
-
class Controller {
|
|
16
|
-
static $inject = ['$scope'];
|
|
17
|
-
constructor($scope) {
|
|
18
|
-
}
|
|
19
|
-
}
|
|
20
|
-
module('foo', [])
|
|
21
|
-
.directive('myDirective', ['$scope', function($scope) {}]);
|
|
22
|
-
`,
|
|
23
|
-
},
|
|
24
|
-
{
|
|
25
|
-
code: `
|
|
26
|
-
import { module } from 'angular';
|
|
27
|
-
module('foo', [])
|
|
28
|
-
.directive('myDirective', ['$scope', function($scope) {}]);
|
|
29
|
-
`,
|
|
30
|
-
},
|
|
31
|
-
],
|
|
32
|
-
|
|
33
|
-
invalid: [
|
|
34
|
-
{
|
|
35
|
-
errors: [{ message: 'The injected function has 1 parameter(s): ["$scope"], but no annotation was found' }],
|
|
36
|
-
code: `
|
|
37
|
-
const angular = require('angular');
|
|
38
|
-
angular.module('foo', [])
|
|
39
|
-
.controller('myController', function($scope) {});
|
|
40
|
-
`,
|
|
41
|
-
output: `
|
|
42
|
-
const angular = require('angular');
|
|
43
|
-
angular.module('foo', [])
|
|
44
|
-
.controller('myController', ['$scope', function($scope) {}]);
|
|
45
|
-
`,
|
|
46
|
-
},
|
|
47
|
-
{
|
|
48
|
-
errors: [
|
|
49
|
-
{ message: 'The injected function \'MyClass\' has 1 parameter(s): ["$scope"], but no annotation was found' },
|
|
50
|
-
],
|
|
51
|
-
code: `
|
|
52
|
-
import { module } from 'angular';
|
|
53
|
-
// Do not rename this to MyClassController or the checkDi rule will fire twice
|
|
54
|
-
class MyClass {
|
|
55
|
-
constructor($scope) {
|
|
56
|
-
}
|
|
57
|
-
}
|
|
58
|
-
module('foo', []).controller('myClassController', MyClass);
|
|
59
|
-
`,
|
|
60
|
-
output: `
|
|
61
|
-
import { module } from 'angular';
|
|
62
|
-
// Do not rename this to MyClassController or the checkDi rule will fire twice
|
|
63
|
-
class MyClass {
|
|
64
|
-
constructor($scope) {
|
|
65
|
-
}
|
|
66
|
-
}
|
|
67
|
-
MyClass.$inject = ['$scope'];
|
|
68
|
-
module('foo', []).controller('myClassController', MyClass);
|
|
69
|
-
`,
|
|
70
|
-
},
|
|
71
|
-
{
|
|
72
|
-
errors: [{ message: 'The injected function has 1 parameter(s): ["$scope"], but no annotation was found' }],
|
|
73
|
-
code: `
|
|
74
|
-
const angular = require('angular');
|
|
75
|
-
angular.module('foo', [])
|
|
76
|
-
.directive('myDirective', { controller: function namedFunction($scope) {} });
|
|
77
|
-
`,
|
|
78
|
-
output: `
|
|
79
|
-
const angular = require('angular');
|
|
80
|
-
angular.module('foo', [])
|
|
81
|
-
.directive('myDirective', { controller: ['$scope', function namedFunction($scope) {}] });
|
|
82
|
-
`,
|
|
83
|
-
},
|
|
84
|
-
{
|
|
85
|
-
errors: [
|
|
86
|
-
{ message: 'The injected function has 2 parameter(s): ["$scope","$location"], but no annotation was found' },
|
|
87
|
-
],
|
|
88
|
-
code: `
|
|
89
|
-
const angular = require('angular');
|
|
90
|
-
angular.module('foo', [])
|
|
91
|
-
.directive('myDirective', { controller: function ($scope, $location) {} });
|
|
92
|
-
`,
|
|
93
|
-
output: `
|
|
94
|
-
const angular = require('angular');
|
|
95
|
-
angular.module('foo', [])
|
|
96
|
-
.directive('myDirective', { controller: ['$scope', '$location', function ($scope, $location) {}] });
|
|
97
|
-
`,
|
|
98
|
-
},
|
|
99
|
-
],
|
|
100
|
-
});
|