forked from ember-cli/eslint-plugin-ember
-
Notifications
You must be signed in to change notification settings - Fork 0
/
no-unnecessary-service-injection-argument.js
99 lines (88 loc) · 3.1 KB
/
no-unnecessary-service-injection-argument.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
'use strict';
const types = require('../utils/types');
const emberUtils = require('../utils/ember');
const { getImportIdentifier } = require('../utils/import');
//------------------------------------------------------------------------------
// Rule Definition
//------------------------------------------------------------------------------
const ERROR_MESSAGE =
"Don't specify injected service name as an argument when it matches the property name.";
/** @type {import('eslint').Rule.RuleModule} */
module.exports = {
meta: {
type: 'suggestion',
docs: {
description: 'disallow unnecessary argument when injecting services',
category: 'Services',
recommended: false,
url: 'https://github.com/ember-cli/eslint-plugin-ember/tree/master/docs/rules/no-unnecessary-service-injection-argument.md',
},
fixable: 'code',
schema: [],
},
ERROR_MESSAGE,
create(context) {
let importedInjectName;
let importedEmberName;
// Handle either ClassProperty (ESLint v7) or PropertyDefinition (ESLint v8).
function visitClassPropertyOrPropertyDefinition(node) {
if (
!emberUtils.isInjectedServiceProp(node, importedEmberName, importedInjectName) ||
node.decorators.length !== 1 ||
!types.isCallExpression(node.decorators[0].expression) ||
node.decorators[0].expression.arguments.length !== 1 ||
!types.isStringLiteral(node.decorators[0].expression.arguments[0])
) {
return;
}
const keyName = node.key.name || node.key.value;
const firstArg = node.decorators[0].expression.arguments[0];
const firstArgValue = firstArg.value;
if (keyName === firstArgValue) {
context.report({
node: firstArg,
message: ERROR_MESSAGE,
fix(fixer) {
return fixer.remove(firstArg);
},
});
}
}
return {
ImportDeclaration(node) {
if (node.source.value === 'ember') {
importedEmberName = importedEmberName || getImportIdentifier(node, 'ember');
}
if (node.source.value === '@ember/service') {
importedInjectName =
importedInjectName ||
getImportIdentifier(node, '@ember/service', 'inject') ||
getImportIdentifier(node, '@ember/service', 'service');
}
},
Property(node) {
if (
!emberUtils.isInjectedServiceProp(node, importedEmberName, importedInjectName) ||
node.value.arguments.length !== 1 ||
!types.isStringLiteral(node.value.arguments[0])
) {
return;
}
const keyName = node.key.name || node.key.value;
const firstArg = node.value.arguments[0];
const firstArgValue = firstArg.value;
if (keyName === firstArgValue) {
context.report({
node: firstArg,
message: ERROR_MESSAGE,
fix(fixer) {
return fixer.remove(firstArg);
},
});
}
},
ClassProperty: visitClassPropertyOrPropertyDefinition,
PropertyDefinition: visitClassPropertyOrPropertyDefinition,
};
},
};