-
-
Notifications
You must be signed in to change notification settings - Fork 197
/
no-get.js
51 lines (47 loc) · 1.53 KB
/
no-get.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
'use strict';
const utils = require('../utils/utils');
function makeErrorMessage(property, isImportedGet) {
return isImportedGet
? `Use \`this.${property}\` instead of \`get(this, '${property}')\``
: `Use \`this.${property}\` instead of \`this.get('${property}')\``;
}
module.exports = {
makeErrorMessage,
meta: {
docs: {
description: "Disallow unnecessary usage of Ember's `get` function",
category: 'Best Practices',
recommended: false
}
},
create(context) {
return {
CallExpression(node) {
if (
utils.isMemberExpression(node.callee) &&
utils.isThisExpression(node.callee.object) &&
node.callee.property.name === 'get' &&
node.arguments.length === 1 &&
utils.isLiteral(node.arguments[0]) &&
typeof node.arguments[0].value === 'string' &&
!node.arguments[0].value.includes('.')
) {
// Example: this.get('foo');
context.report(node, makeErrorMessage(node.arguments[0].value), false);
}
if (
utils.isIdentifier(node.callee) &&
node.callee.name === 'get' &&
node.arguments.length === 2 &&
utils.isThisExpression(node.arguments[0]) &&
utils.isLiteral(node.arguments[1]) &&
typeof node.arguments[1].value === 'string' &&
!node.arguments[1].value.includes('.')
) {
// Example: get(this, 'foo');
context.report(node, makeErrorMessage(node.arguments[1].value, true));
}
}
};
}
};