forked from ember-cli/eslint-plugin-ember
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathno-restricted-property-modifications.js
More file actions
185 lines (170 loc) · 5.81 KB
/
no-restricted-property-modifications.js
File metadata and controls
185 lines (170 loc) · 5.81 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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
'use strict';
const { isStringLiteral } = require('../utils/types');
const { getImportIdentifier } = require('../utils/import');
const { isThisSet: isThisSetNative } = require('../utils/property-setter');
const { nodeToDependentKey } = require('../utils/property-getter');
/** @type {import('eslint').Rule.RuleModule} */
module.exports = {
meta: {
type: 'problem',
docs: {
description: 'disallow modifying the specified properties',
category: 'Miscellaneous',
url: 'https://github.com/ember-cli/eslint-plugin-ember/tree/master/docs/rules/no-restricted-property-modifications.md',
},
fixable: 'code',
schema: {
type: 'array',
minItems: 1,
maxItems: 1,
items: [
{
type: 'object',
properties: {
properties: {
type: 'array',
items: {
type: 'string',
},
minItems: 1,
uniqueItems: true,
description:
'Array of names of properties that should not be modified (modifying child/nested/sub-properties of these is also not allowed).',
},
},
required: ['properties'],
additionalProperties: false,
},
],
},
messages: {
doNotUseAssignment: 'Do not use assignment on properties that should not be modified.',
doNotUseSet: 'Do not call `set` on properties that should not be modified.',
useReadOnlyMacro:
'Use the `readOnly` computed property macro for properties that should not be modified.',
},
},
create(context) {
let importedComputedName;
let importedReadsName;
let importedAliasName;
const readOnlyProperties = context.options[0].properties;
return {
ImportDeclaration(node) {
if (node.source.value === '@ember/object') {
importedComputedName =
importedComputedName || getImportIdentifier(node, '@ember/object', 'computed');
} else if (node.source.value === '@ember/object/computed') {
importedReadsName =
importedReadsName || getImportIdentifier(node, '@ember/object/computed', 'reads');
importedAliasName =
importedAliasName || getImportIdentifier(node, '@ember/object/computed', 'alias');
}
},
AssignmentExpression(node) {
if (!isThisSetNative(node)) {
return;
}
const dependentKey = nodeToDependentKey(node.left, context);
if (
readOnlyProperties.includes(dependentKey) ||
readOnlyProperties.some((property) => dependentKey.startsWith(`${property}.`))
) {
context.report({
node,
messageId: 'doNotUseAssignment',
});
}
},
CallExpression(node) {
if (
isReadOnlyPropertyUsingAliasOrReads(
node,
readOnlyProperties,
importedComputedName,
importedAliasName,
importedReadsName
)
) {
context.report({
node,
messageId: 'useReadOnlyMacro',
fix(fixer) {
const argumentText0 = (context.sourceCode ?? context.getSourceCode()).getText(
node.arguments[0]
);
return node.callee.type === 'MemberExpression'
? fixer.replaceText(node, `${importedComputedName}.readOnly(${argumentText0})`)
: fixer.replaceText(node, `readOnly(${argumentText0})`);
},
});
} else if (isThisSetReadOnlyProperty(node, readOnlyProperties)) {
context.report({ node, messageId: 'doNotUseSet' });
}
},
};
},
};
function isReadOnlyPropertyUsingAliasOrReads(
node,
readOnlyProperties,
importedComputedName,
importedAliasName,
importedReadsName
) {
// Looks for: reads('readOnlyProperty') or alias('readOnlyProperty')
return (
(isAliasComputedProperty(node, importedComputedName, importedAliasName) ||
isReadsComputedProperty(node, importedComputedName, importedReadsName)) &&
node.arguments.length === 1 &&
isStringLiteral(node.arguments[0]) &&
isReadOnlyProperty(node.arguments[0].value, readOnlyProperties)
);
}
function isThisSet(node) {
// Looks for: this.set('readOnlyProperty...', ...);
return (
node.callee.type === 'MemberExpression' &&
node.callee.object.type === 'ThisExpression' &&
node.callee.property.type === 'Identifier' &&
node.callee.property.name === 'set' &&
node.arguments.length === 2 &&
isStringLiteral(node.arguments[0])
);
}
function isThisSetReadOnlyProperty(node, readOnlyProperties) {
return isThisSet(node) && isReadOnlyProperty(node.arguments[0].value, readOnlyProperties);
}
function isAliasComputedProperty(node, importedComputedName, importedAliasName) {
return (
isIdentifierCall(node, importedAliasName) ||
isMemberExpressionCall(node, importedComputedName, 'alias')
);
}
function isReadsComputedProperty(node, importedComputedName, importedReadsName) {
return (
isIdentifierCall(node, importedReadsName) ||
isMemberExpressionCall(node, importedComputedName, 'reads')
);
}
function isIdentifierCall(node, name) {
return (
node.type === 'CallExpression' && node.callee.type === 'Identifier' && node.callee.name === name
);
}
function isMemberExpressionCall(node, object, name) {
return (
node.type === 'CallExpression' &&
node.callee.type === 'MemberExpression' &&
node.callee.object.type === 'Identifier' &&
node.callee.object.name === object &&
node.callee.property.type === 'Identifier' &&
node.callee.property.name === name
);
}
function isReadOnlyProperty(property, readOnlyProperties) {
return (
readOnlyProperties.includes(property) ||
readOnlyProperties.some((propertyCurrent) => property.startsWith(`${propertyCurrent}.`))
);
}