forked from ember-cli/eslint-plugin-ember
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathno-invalid-dependent-keys.js
More file actions
170 lines (148 loc) · 5.55 KB
/
no-invalid-dependent-keys.js
File metadata and controls
170 lines (148 loc) · 5.55 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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
'use strict';
const types = require('../utils/types');
const ember = require('../utils/ember');
const { getImportIdentifier } = require('../utils/import');
//------------------------------------------------------------------------------
// General rule - Dependent keys used for computed properties have to be valid.
//------------------------------------------------------------------------------
const ERROR_MESSAGE_UNBALANCED_BRACES = 'Found unbalanced braces in dependent key';
const ERROR_MESSAGE_UNNECESSARY_BRACES = 'Found unnecessary braces in dependent key';
const ERROR_MESSAGE_TERMINAL_AT_EACH = 'Found terminal `@each`, use `[]` instead';
const ERROR_MESSAGE_MIDDLE_BRACKETS = '`[]` should only be used at the end of the dependent key';
const ERROR_MESSAGE_LEADING_TRAILING_PERIOD = 'Found leading or trailing period in dependent key';
const ERROR_MESSAGE_INVALID_CHARACTER = 'Found invalid character in dependent key';
function hasUnbalancedBraces(str) {
const foundBraces = str.match(/[{}]/g) || [];
const openBraces = foundBraces.filter((c) => c === '{').length;
const closeBraces = foundBraces.filter((c) => c === '}').length;
return openBraces !== closeBraces;
}
const REGEXP_UNNECESSARY_BRACES = /{([^,.]+)}/g;
function hasUnnecessaryBraces(str) {
return str.match(REGEXP_UNNECESSARY_BRACES);
}
function hasTerminalAtEach(str) {
return str.endsWith('.@each');
}
function hasMiddleBrackets(str) {
return str.includes('[].');
}
/** @type {import('eslint').Rule.RuleModule} */
module.exports = {
meta: {
type: 'problem',
docs: {
description: 'disallow invalid dependent keys in computed properties',
category: 'Computed Properties',
recommended: true,
url: 'https://github.com/ember-cli/eslint-plugin-ember/tree/master/docs/rules/no-invalid-dependent-keys.md',
},
fixable: 'code',
schema: [],
},
ERROR_MESSAGE_UNBALANCED_BRACES,
ERROR_MESSAGE_UNNECESSARY_BRACES,
ERROR_MESSAGE_TERMINAL_AT_EACH,
ERROR_MESSAGE_MIDDLE_BRACKETS,
ERROR_MESSAGE_LEADING_TRAILING_PERIOD,
ERROR_MESSAGE_INVALID_CHARACTER,
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 (!ember.isComputedProp(node, importedEmberName, importedComputedName)) {
return;
}
const stringArgs = node.arguments.filter(
(arg) => types.isLiteral(arg) && typeof arg.value === 'string'
);
const sourceCode = context.sourceCode ?? context.getSourceCode();
for (const node of stringArgs) {
if (hasTerminalAtEach(node.value)) {
context.report({
node,
message: ERROR_MESSAGE_TERMINAL_AT_EACH,
fix(fixer) {
const nodeText = sourceCode.getText(node);
return fixer.replaceText(node, nodeText.replace('.@each', '.[]'));
},
});
}
if (hasUnbalancedBraces(node.value)) {
context.report({
node,
message: ERROR_MESSAGE_UNBALANCED_BRACES,
});
}
if (hasUnnecessaryBraces(node.value)) {
context.report({
node,
message: ERROR_MESSAGE_UNNECESSARY_BRACES,
fix(fixer) {
const nodeText = sourceCode.getText(node);
return fixer.replaceText(
node,
nodeText.replaceAll(REGEXP_UNNECESSARY_BRACES, '$1')
);
},
});
}
if (hasMiddleBrackets(node.value)) {
context.report({
node,
message: ERROR_MESSAGE_MIDDLE_BRACKETS,
fix(fixer) {
const nodeText = sourceCode.getText(node);
return fixer.replaceText(node, nodeText.replace('[].', '@each.'));
},
});
}
if (node.value.startsWith('.')) {
context.report({
node,
message: ERROR_MESSAGE_LEADING_TRAILING_PERIOD,
fix(fixer) {
const nodeText = sourceCode.getText(node);
return fixer.replaceText(node, nodeText.replace('.', '')); // Remove first period.
},
});
}
if (node.value.endsWith('.')) {
context.report({
node,
message: ERROR_MESSAGE_LEADING_TRAILING_PERIOD,
fix(fixer) {
const nodeText = sourceCode.getText(node);
return fixer.replaceText(node, removeLastOccurrenceOf(nodeText, '.'));
},
});
}
if (node.value.includes(' ')) {
context.report({
node,
message: ERROR_MESSAGE_INVALID_CHARACTER,
fix(fixer) {
const nodeText = sourceCode.getText(node);
return fixer.replaceText(node, nodeText.replaceAll(' ', '')); // Remove all spaces.
},
});
}
}
},
};
},
};
function removeLastOccurrenceOf(strToSearch, strToRemove) {
const posToRemove = strToSearch.lastIndexOf(strToRemove);
return strToSearch.slice(0, posToRemove) + strToSearch.slice(posToRemove + 1);
}