@spinnaker/eslint-plugin 3.0.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/.eslintignore +3 -0
- package/CHANGELOG.md +77 -0
- package/LICENSE.txt +203 -0
- package/README.md +259 -0
- package/babel.config.js +3 -0
- package/base.config.js +86 -0
- package/create-rule.js +67 -0
- package/eslint-plugin.ts +47 -0
- package/index.js +6 -0
- package/newrule.sh +88 -0
- package/none.config.js +18 -0
- package/package.json +51 -0
- package/rules/api-deprecation.spec.ts +79 -0
- package/rules/api-deprecation.ts +254 -0
- package/rules/api-no-slashes.spec.ts +84 -0
- package/rules/api-no-slashes.ts +148 -0
- package/rules/api-no-unused-chaining.spec.ts +26 -0
- package/rules/api-no-unused-chaining.ts +47 -0
- package/rules/import-from-alias-not-npm.spec.ts +22 -0
- package/rules/import-from-alias-not-npm.ts +53 -0
- package/rules/import-from-npm-not-alias.spec.ts +23 -0
- package/rules/import-from-npm-not-alias.ts +56 -0
- package/rules/import-from-npm-not-relative.spec.ts +22 -0
- package/rules/import-from-npm-not-relative.ts +57 -0
- package/rules/import-from-presentation-not-core.spec.ts +44 -0
- package/rules/import-from-presentation-not-core.ts +106 -0
- package/rules/import-relative-within-subpackage.spec.ts +50 -0
- package/rules/import-relative-within-subpackage.ts +71 -0
- package/rules/import-sort.spec.ts +85 -0
- package/rules/import-sort.ts +280 -0
- package/rules/migrate-to-mock-http-client.spec.ts +78 -0
- package/rules/migrate-to-mock-http-client.ts +122 -0
- package/rules/ng-no-component-class.spec.ts +45 -0
- package/rules/ng-no-component-class.ts +68 -0
- package/rules/ng-no-module-export.spec.ts +26 -0
- package/rules/ng-no-module-export.ts +117 -0
- package/rules/ng-no-require-angularjs.spec.ts +27 -0
- package/rules/ng-no-require-angularjs.ts +94 -0
- package/rules/ng-no-require-module-deps.spec.ts +33 -0
- package/rules/ng-no-require-module-deps.ts +211 -0
- package/rules/ng-strictdi.spec.ts +100 -0
- package/rules/ng-strictdi.ts +304 -0
- package/rules/prefer-promise-like.spec.ts +75 -0
- package/rules/prefer-promise-like.ts +108 -0
- package/rules/react2angular-with-error-boundary.spec.ts +29 -0
- package/rules/react2angular-with-error-boundary.ts +118 -0
- package/rules/rest-prefer-static-strings-in-initializer.spec.ts +34 -0
- package/rules/rest-prefer-static-strings-in-initializer.ts +89 -0
- package/template/template-rule.spec.ts +17 -0
- package/template/template-rule.ts +21 -0
- package/test.eslintrc +20 -0
- package/test_rule_against_deck_source.sh +18 -0
- package/tsconfig.json +17 -0
- package/utils/angular-rule/angular-rule.js +302 -0
- package/utils/angular-rule/false-values.js +6 -0
- package/utils/angular-rule/utils.js +624 -0
- package/utils/import-aliases.mock.ts +17 -0
- package/utils/import-aliases.ts +91 -0
- package/utils/mockModule.js +15 -0
- package/utils/ruleTester.js +8 -0
- package/utils/utils.ts +90 -0
|
@@ -0,0 +1,122 @@
|
|
|
1
|
+
import type { Rule } from 'eslint';
|
|
2
|
+
import type { FunctionExpression, ImportDeclaration, ImportSpecifier } from 'estree';
|
|
3
|
+
|
|
4
|
+
import { getProgram } from '../utils/utils';
|
|
5
|
+
|
|
6
|
+
const ruleModule: Rule.RuleModule = {
|
|
7
|
+
create(context) {
|
|
8
|
+
const text = (node) => context.getSourceCode().getText(node);
|
|
9
|
+
|
|
10
|
+
return {
|
|
11
|
+
CallExpression(node) {
|
|
12
|
+
/** it(() => {}) */
|
|
13
|
+
const isItBlock = node.callee.type === 'Identifier' && node.callee.name === 'it';
|
|
14
|
+
|
|
15
|
+
if (isItBlock) {
|
|
16
|
+
const itBlockText = text(node);
|
|
17
|
+
const testFunction = node.arguments[1] as FunctionExpression;
|
|
18
|
+
|
|
19
|
+
const doesFunctionIncludeHttpBackend = !!testFunction && itBlockText.includes('$httpBackend');
|
|
20
|
+
|
|
21
|
+
if (doesFunctionIncludeHttpBackend) {
|
|
22
|
+
const isFirstArgAFunction = ['FunctionExpression', 'ArrowFunctionExpression'].includes(testFunction.type);
|
|
23
|
+
|
|
24
|
+
if (isFirstArgAFunction) {
|
|
25
|
+
// Fix 1: make the test 'async'
|
|
26
|
+
if (testFunction.async !== true) {
|
|
27
|
+
return context.report({
|
|
28
|
+
node,
|
|
29
|
+
message: 'Migrate to MockHttpClient (step 1): make test function async',
|
|
30
|
+
fix: (fixer) => fixer.insertTextBefore(testFunction, 'async '),
|
|
31
|
+
});
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
// Fix 2: Add a 'http' variable
|
|
35
|
+
if (
|
|
36
|
+
testFunction.body.type === 'BlockStatement' &&
|
|
37
|
+
!text(testFunction.body.body[0]).includes('mockHttpClient')
|
|
38
|
+
) {
|
|
39
|
+
const program = getProgram(node);
|
|
40
|
+
const allImports = program.body.filter(
|
|
41
|
+
(item) => item.type === 'ImportDeclaration',
|
|
42
|
+
) as ImportDeclaration[];
|
|
43
|
+
|
|
44
|
+
const importSpecifiers = allImports
|
|
45
|
+
.map((decl) => decl.specifiers as ImportSpecifier[])
|
|
46
|
+
.reduce((acc, x) => acc.concat(x), []);
|
|
47
|
+
|
|
48
|
+
const mockHttpClientImport = importSpecifiers.find((specifier) => {
|
|
49
|
+
return specifier.imported && specifier.imported.name === 'mockHttpClient';
|
|
50
|
+
});
|
|
51
|
+
|
|
52
|
+
return context.report({
|
|
53
|
+
node,
|
|
54
|
+
message: 'Migrate to MockHttpClient (step 2): Create a MockHttpClient named "http"',
|
|
55
|
+
fix: (fixer) => {
|
|
56
|
+
const insertHttp = fixer.insertTextBefore(
|
|
57
|
+
testFunction.body.body[0],
|
|
58
|
+
'const http = mockHttpClient();\n',
|
|
59
|
+
);
|
|
60
|
+
|
|
61
|
+
let insertImport = fixer.insertTextBeforeRange(
|
|
62
|
+
[0, 0],
|
|
63
|
+
`import { mockHttpClient } from 'core/api/mock/jasmine';\n`,
|
|
64
|
+
);
|
|
65
|
+
|
|
66
|
+
// Put after 'use strict'
|
|
67
|
+
const sourcecode = text(program);
|
|
68
|
+
const [preamble] = /^['"]use strict['"];?/.exec(sourcecode) || [];
|
|
69
|
+
if (preamble) {
|
|
70
|
+
const insertPos = preamble.length;
|
|
71
|
+
insertImport = fixer.insertTextAfterRange(
|
|
72
|
+
[insertPos, insertPos],
|
|
73
|
+
`\nimport { mockHttpClient } from 'core/api/mock/jasmine';`,
|
|
74
|
+
);
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
if (mockHttpClientImport) {
|
|
78
|
+
return insertHttp;
|
|
79
|
+
} else {
|
|
80
|
+
return [insertHttp, insertImport];
|
|
81
|
+
}
|
|
82
|
+
},
|
|
83
|
+
});
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
// Fix 3:
|
|
87
|
+
// - replace "$httpBackend.when('GET'" with "$httpBackend.expectGET("
|
|
88
|
+
// - replace "$httpBackend.whenGET" with "$httpBackend.expectGET"
|
|
89
|
+
// - replace "$httpBackend" with "http"
|
|
90
|
+
return context.report({
|
|
91
|
+
node,
|
|
92
|
+
message: 'Migrate to MockHttpClient (step 3): replace $httpBackend with http',
|
|
93
|
+
fix: (fixer) => {
|
|
94
|
+
const newItBlockText = itBlockText
|
|
95
|
+
.replace(/(this\.)?\$httpBackend\.when(GET|POST|PUT|PATCH|DELETE)/g, '$httpBackend.expect$2')
|
|
96
|
+
.replace(
|
|
97
|
+
/(this\.)?\$httpBackend\.when\(['"](GET|POST|PUT|PATCH|DELETE)['"], /g,
|
|
98
|
+
'$httpBackend.expect$2(',
|
|
99
|
+
)
|
|
100
|
+
.replace(/(this\.)?\$httpBackend/g, 'http')
|
|
101
|
+
.replace(/http.flush/g, 'await http.flush')
|
|
102
|
+
.replace(/await await /g, 'await ');
|
|
103
|
+
|
|
104
|
+
return fixer.replaceText(node, newItBlockText);
|
|
105
|
+
},
|
|
106
|
+
});
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
},
|
|
111
|
+
};
|
|
112
|
+
},
|
|
113
|
+
meta: {
|
|
114
|
+
fixable: 'code',
|
|
115
|
+
type: 'problem',
|
|
116
|
+
docs: {
|
|
117
|
+
description: 'Do not import API',
|
|
118
|
+
},
|
|
119
|
+
},
|
|
120
|
+
};
|
|
121
|
+
|
|
122
|
+
export default ruleModule;
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
import ruleTester from '../utils/ruleTester';
|
|
2
|
+
import rule from './ng-no-component-class';
|
|
3
|
+
|
|
4
|
+
ruleTester.run('ng-no-component-class', rule, {
|
|
5
|
+
valid: [
|
|
6
|
+
{
|
|
7
|
+
code: `
|
|
8
|
+
const angular = require('angular');
|
|
9
|
+
angular.module('foo', [])
|
|
10
|
+
.component('componentName', componentObject);
|
|
11
|
+
|
|
12
|
+
const componentObject = {
|
|
13
|
+
controller: function() {},
|
|
14
|
+
template: 'a template'
|
|
15
|
+
}
|
|
16
|
+
`,
|
|
17
|
+
},
|
|
18
|
+
],
|
|
19
|
+
|
|
20
|
+
invalid: [
|
|
21
|
+
{
|
|
22
|
+
errors: [{ message: 'Use .component("foo", {}) instead of .component("foo", new FooComponentClass())' }],
|
|
23
|
+
code: `
|
|
24
|
+
import angular from 'angular';
|
|
25
|
+
angular.module('foo', [])
|
|
26
|
+
.component('componentName', new ComponentClass());
|
|
27
|
+
|
|
28
|
+
class ComponentClass {
|
|
29
|
+
controller = function() {};
|
|
30
|
+
template = 'a template';
|
|
31
|
+
}
|
|
32
|
+
`,
|
|
33
|
+
output: `
|
|
34
|
+
import angular from 'angular';
|
|
35
|
+
angular.module('foo', [])
|
|
36
|
+
.component('componentName', componentClass);
|
|
37
|
+
|
|
38
|
+
const componentClass = {
|
|
39
|
+
controller: function() {},
|
|
40
|
+
template: 'a template'
|
|
41
|
+
};
|
|
42
|
+
`,
|
|
43
|
+
},
|
|
44
|
+
],
|
|
45
|
+
});
|
|
@@ -0,0 +1,68 @@
|
|
|
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
|
+
import { isNewExpression } from '../utils/utils';
|
|
16
|
+
|
|
17
|
+
const useObjectLiteral = function (context: Rule.RuleContext) {
|
|
18
|
+
return {
|
|
19
|
+
'angular?component': function (callee, thisGuy) {
|
|
20
|
+
const node: Rule.Node = thisGuy.node;
|
|
21
|
+
const scope: Scope.Scope = thisGuy.scope;
|
|
22
|
+
if (isNewExpression(node)) {
|
|
23
|
+
const calleeName = 'name' in node.callee ? node.callee.name : undefined;
|
|
24
|
+
const fix = (fixer: Rule.RuleFixer) => {
|
|
25
|
+
const variable = scope.variables.find((x) => x.name === calleeName);
|
|
26
|
+
const classDef = variable.defs[0].node;
|
|
27
|
+
const name = classDef.id.name;
|
|
28
|
+
const camelCaseName = camelCase(name);
|
|
29
|
+
const rename = name !== camelCaseName;
|
|
30
|
+
|
|
31
|
+
const objProperties = classDef.body.body.map((node) => {
|
|
32
|
+
return node.key.name + ': ' + context.getSourceCode().getText(node.value);
|
|
33
|
+
});
|
|
34
|
+
|
|
35
|
+
const impls = (classDef.implements || []).map((impl) => context.getSourceCode().getText(impl));
|
|
36
|
+
const identifier = `const ${camelCaseName}${impls.length ? `: ${impls.join(' & ')}` : ''}`;
|
|
37
|
+
const objectLiteral = `${identifier} = {\n` + ` ${objProperties.join(',\n ')}\n` + `};`;
|
|
38
|
+
|
|
39
|
+
const otherReferences = findParentNodeByType(classDef, 'Program').tokens.filter((token) => {
|
|
40
|
+
const ignores = [classDef.id.range[0], node.callee.range[0]];
|
|
41
|
+
return (
|
|
42
|
+
token.type === 'Identifier' && token.value === name && ignores.every((pos) => pos !== token.range[0])
|
|
43
|
+
);
|
|
44
|
+
});
|
|
45
|
+
|
|
46
|
+
const otherFixes = rename ? [] : otherReferences.map((id) => fixer.replaceText(id, camelCaseName));
|
|
47
|
+
|
|
48
|
+
return [fixer.replaceText(classDef, objectLiteral), fixer.replaceText(node, camelCaseName), ...otherFixes];
|
|
49
|
+
};
|
|
50
|
+
|
|
51
|
+
const message = 'Use .component("foo", {}) instead of .component("foo", new FooComponentClass())';
|
|
52
|
+
context.report({ fix, node, message });
|
|
53
|
+
}
|
|
54
|
+
},
|
|
55
|
+
};
|
|
56
|
+
};
|
|
57
|
+
|
|
58
|
+
const ruleModule: Rule.RuleModule = {
|
|
59
|
+
meta: {
|
|
60
|
+
type: 'problem',
|
|
61
|
+
fixable: 'code',
|
|
62
|
+
docs: {
|
|
63
|
+
description: 'Prefer .component("foo", {}) over .component("foo", new FooComponentClass())',
|
|
64
|
+
},
|
|
65
|
+
},
|
|
66
|
+
create: angularRule(useObjectLiteral),
|
|
67
|
+
};
|
|
68
|
+
export default ruleModule;
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import ruleTester from '../utils/ruleTester';
|
|
2
|
+
import rule from './ng-no-module-export';
|
|
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
|
+
});
|
|
@@ -0,0 +1,117 @@
|
|
|
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 type { AssignmentExpression, CallExpression, MemberExpression } from 'estree';
|
|
7
|
+
import { isCallExpression, isIdentifier, isMemberExpression } from '../utils/utils';
|
|
8
|
+
|
|
9
|
+
const rule = function (context: Rule.RuleContext) {
|
|
10
|
+
function getSuggestedVariableNameForFile() {
|
|
11
|
+
const filename = context.getFilename();
|
|
12
|
+
if (filename.includes('/packages/')) {
|
|
13
|
+
return filename
|
|
14
|
+
.replace(/^.*\/packages\//g, '')
|
|
15
|
+
.replace(/\/src\//g, '/')
|
|
16
|
+
.replace(/\.[\w]*$/g, '')
|
|
17
|
+
.replace(/[^\w_]/g, '_')
|
|
18
|
+
.toUpperCase();
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
return {
|
|
23
|
+
AssignmentExpression: function (node: AssignmentExpression) {
|
|
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: CallExpression) {
|
|
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: Rule.Node): _callee is MemberExpression & Rule.NodeParentExtension {
|
|
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.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;
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import ruleTester from '../utils/ruleTester';
|
|
2
|
+
import rule from './ng-no-require-angularjs';
|
|
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
|
+
});
|
|
@@ -0,0 +1,94 @@
|
|
|
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 type { ImportDeclaration, MemberExpression } from 'estree';
|
|
12
|
+
import { getProgram, isMemberExpression } from '../utils/utils';
|
|
13
|
+
|
|
14
|
+
const rule = function (context: Rule.RuleContext) {
|
|
15
|
+
return {
|
|
16
|
+
'MemberExpression[object.name="angular"][property.name="module"]': function (
|
|
17
|
+
node: MemberExpression & Rule.NodeParentExtension,
|
|
18
|
+
) {
|
|
19
|
+
const angularVar = findAngularVariable(node, context);
|
|
20
|
+
const angularImport = findAngularImportStatement(node);
|
|
21
|
+
// Double check that there is only a single use of 'angular' variable and that it's 'angular.module()')
|
|
22
|
+
if (angularImport && angularVar && angularVar.references.length === 1) {
|
|
23
|
+
const { parent } = angularVar.references[0].identifier as Rule.Node;
|
|
24
|
+
if (isMemberExpression(parent)) {
|
|
25
|
+
if (
|
|
26
|
+
'name' in parent.object &&
|
|
27
|
+
parent.object.name === 'angular' &&
|
|
28
|
+
'name' in parent.property &&
|
|
29
|
+
parent.property.name === 'module'
|
|
30
|
+
) {
|
|
31
|
+
const message = "Prefer module('foo', []) to angular.module('foo', [])";
|
|
32
|
+
const fix = getFixForAngularModule(node, angularImport);
|
|
33
|
+
return context.report({ node, message, fix });
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
},
|
|
38
|
+
};
|
|
39
|
+
};
|
|
40
|
+
|
|
41
|
+
function findAngularVariable(_node, context): Scope.Variable {
|
|
42
|
+
const program = getProgram(_node);
|
|
43
|
+
|
|
44
|
+
const programScope = context.getSourceCode().scopeManager.acquire(program);
|
|
45
|
+
const moduleScope = programScope && programScope.childScopes.find((s) => s.type === 'module');
|
|
46
|
+
return moduleScope && moduleScope.variables.find((v) => v.name === 'angular');
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
function findAngularImportStatement(_node): ImportDeclaration {
|
|
50
|
+
let program = _node;
|
|
51
|
+
while (program && program.parent) {
|
|
52
|
+
program = program.parent;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
return program.body.find((node) => {
|
|
56
|
+
return (
|
|
57
|
+
node.type === 'ImportDeclaration' &&
|
|
58
|
+
node.source &&
|
|
59
|
+
node.source.type === 'Literal' &&
|
|
60
|
+
node.source.value === 'angular'
|
|
61
|
+
);
|
|
62
|
+
}) as ImportDeclaration;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
/*
|
|
66
|
+
Given:
|
|
67
|
+
angular.module('module', ['dep']);
|
|
68
|
+
|
|
69
|
+
Rewrites to:
|
|
70
|
+
import { module } from 'angular';
|
|
71
|
+
|
|
72
|
+
module('module', ['dep']);
|
|
73
|
+
*/
|
|
74
|
+
function getFixForAngularModule(angularDotModuleNode: Rule.Node, importStatement: ImportDeclaration) {
|
|
75
|
+
return function (fixer: Rule.RuleFixer) {
|
|
76
|
+
return [
|
|
77
|
+
fixer.replaceText(angularDotModuleNode, 'module'),
|
|
78
|
+
fixer.replaceText(importStatement, `import { module } from 'angular';`),
|
|
79
|
+
];
|
|
80
|
+
};
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
const ruleModule: Rule.RuleModule = {
|
|
84
|
+
meta: {
|
|
85
|
+
type: 'problem',
|
|
86
|
+
docs: {
|
|
87
|
+
description: `Prefer import { module } from 'angular' over angular.module()`,
|
|
88
|
+
},
|
|
89
|
+
fixable: 'code',
|
|
90
|
+
},
|
|
91
|
+
create: rule,
|
|
92
|
+
};
|
|
93
|
+
|
|
94
|
+
export default ruleModule;
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import ruleTester from '../utils/ruleTester';
|
|
2
|
+
import rule from './ng-no-require-module-deps';
|
|
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
|
+
});
|