forked from ember-cli/eslint-plugin-ember
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclassic-decorator-no-classic-methods.js
More file actions
72 lines (64 loc) · 1.93 KB
/
classic-decorator-no-classic-methods.js
File metadata and controls
72 lines (64 loc) · 1.93 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
const disallowedMethods = new Set([
'get',
'set',
'getProperties',
'setProperties',
'getWithDefault',
'incrementProperty',
'decrementProperty',
'toggleProperty',
'addObserver',
'removeObserver',
'notifyPropertyChange',
'cacheFor',
'proto',
]);
function disallowedMethodErrorMessage(name) {
return `The this.${name}() method is a classic ember object method, and can't be used in octane classes. You can refactor this usage to use a utility version instead (e.g. get(this, 'foo')), or to use native/modern syntax instead. Alternatively, you can add the @classic decorator to this class to continue using classic APIs.`;
}
/** @type {import('eslint').Rule.RuleModule} */
module.exports = {
disallowedMethodErrorMessage,
meta: {
type: 'problem',
docs: {
description:
"disallow usage of classic APIs such as `get`/`set` in classes that aren't explicitly decorated with `@classic`",
category: 'Ember Octane',
recommended: true,
url: 'https://github.com/ember-cli/eslint-plugin-ember/tree/master/docs/rules/classic-decorator-no-classic-methods.md',
},
fixable: null,
schema: [],
},
create(context) {
let inClassExtends = false;
return {
ClassDeclaration(node) {
if (
node.superClass &&
!(node.decorators && node.decorators.some((d) => d.expression.name === 'classic'))
) {
inClassExtends = true;
}
},
'ClassDeclaration:exit'() {
inClassExtends = false;
},
MemberExpression(node) {
if (!inClassExtends) {
return;
}
if (node.object.type !== 'ThisExpression' || node.property.type === 'PrivateIdentifier') {
return;
}
if (disallowedMethods.has(node.property.name)) {
context.report({
node,
message: disallowedMethodErrorMessage(node.property.name),
});
}
},
};
},
};