forked from ember-cli/eslint-plugin-ember
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathno-duplicate-dependent-keys.js
More file actions
83 lines (70 loc) · 2.4 KB
/
no-duplicate-dependent-keys.js
File metadata and controls
83 lines (70 loc) · 2.4 KB
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
'use strict';
const emberUtils = require('../utils/ember');
const types = require('../utils/types');
const fixerUtils = require('../utils/fixer');
const { getImportIdentifier } = require('../utils/import');
const ERROR_MESSAGE = 'Dependent keys should not be repeated';
//------------------------------------------------------------------------------
// Rule Definition
//------------------------------------------------------------------------------
/** @type {import('eslint').Rule.RuleModule} */
module.exports = {
meta: {
type: 'problem',
docs: {
description: 'disallow repeating computed property dependent keys',
category: 'Computed Properties',
recommended: true,
url: 'https://github.com/ember-cli/eslint-plugin-ember/tree/master/docs/rules/no-duplicate-dependent-keys.md',
},
fixable: 'code',
schema: [],
},
ERROR_MESSAGE,
create(context) {
let importedEmberName;
let importedComputedName;
return {
ImportDeclaration(node) {
if (node.source.value === 'ember') {
importedEmberName = importedEmberName || getImportIdentifier(node, 'ember');
}
if (node.source.value === '@ember/object') {
importedComputedName =
importedComputedName || getImportIdentifier(node, '@ember/object', 'computed');
}
},
CallExpression(node) {
if (emberUtils.hasDuplicateDependentKeys(node, importedEmberName, importedComputedName)) {
context.report({
node,
message: ERROR_MESSAGE,
fix(fixer) {
const sourceCode = context.sourceCode ?? context.getSourceCode();
const stringNodes = node.arguments.filter((arg) => types.isStringLiteral(arg));
const duplicateNodes = findDuplicateStringNodes(stringNodes);
if (duplicateNodes.length === 0) {
return null;
}
return duplicateNodes.flatMap((duplicateNode) =>
fixerUtils.removeCommaSeparatedNode(duplicateNode, sourceCode, fixer)
);
},
});
}
},
};
},
};
function findDuplicateStringNodes(stringNodes) {
const seenNodes = new Set();
const duplicateNodes = [];
for (const node of stringNodes) {
if (seenNodes.has(node.value)) {
duplicateNodes.push(node);
} else {
seenNodes.add(node.value);
}
}
return duplicateNodes;
}