forked from ember-cli/eslint-plugin-ember
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathno-get-with-default.js
More file actions
155 lines (144 loc) · 5.27 KB
/
no-get-with-default.js
File metadata and controls
155 lines (144 loc) · 5.27 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
'use strict';
const types = require('../utils/types');
const { getImportIdentifier } = require('../utils/import');
const ERROR_MESSAGE = 'Use `||` or the ternary operator instead of `getWithDefault()`';
/** @type {import('eslint').Rule.RuleModule} */
module.exports = {
ERROR_MESSAGE,
meta: {
type: 'suggestion',
docs: {
description: "disallow usage of the Ember's `getWithDefault` function",
category: 'Ember Object',
recommended: true,
url: 'https://github.com/ember-cli/eslint-plugin-ember/tree/master/docs/rules/no-get-with-default.md',
},
fixable: 'code',
schema: [
{
type: 'object',
properties: {
catchSafeObjects: {
type: 'boolean',
default: true,
description:
"Whether the rule should catch non-`this` imported usages like `getWithDefault(person, 'name', '')`.",
},
catchUnsafeObjects: {
type: 'boolean',
default: true,
description:
"Whether the rule should catch non-`this` usages like `person.getWithDefault('name', '')` even though we don't know for sure if `person` is an Ember object.",
},
},
additionalProperties: false,
},
],
},
create(context) {
let importedGetName;
let importedGetWithDefaultName;
const catchSafeObjects = !context.options[0] || context.options[0].catchSafeObjects;
const catchUnsafeObjects = !context.options[0] || context.options[0].catchUnsafeObjects;
return {
ImportDeclaration(node) {
if (node.source.value === '@ember/object') {
importedGetName = importedGetName || getImportIdentifier(node, '@ember/object', 'get');
importedGetWithDefaultName =
importedGetWithDefaultName ||
getImportIdentifier(node, '@ember/object', 'getWithDefault');
}
},
CallExpression(node) {
if (
types.isMemberExpression(node.callee) &&
(types.isThisExpression(node.callee.object) || catchUnsafeObjects) &&
types.isIdentifier(node.callee.property) &&
node.callee.property.name === 'getWithDefault' &&
node.arguments.length === 2
) {
// Example: this.getWithDefault('foo', 'bar');
context.report({
node,
message: ERROR_MESSAGE,
fix(fixer) {
return fix({
fixer,
context,
node,
nodeObject: node.callee.object,
nodeProperty: node.arguments[0],
nodeDefault: node.arguments[1],
isImported: false,
importedGetName,
});
},
});
}
if (
types.isIdentifier(node.callee) &&
node.callee.name === importedGetWithDefaultName &&
node.arguments.length === 3 &&
(types.isThisExpression(node.arguments[0]) || catchSafeObjects)
) {
// Example: getWithDefault(this, 'foo', 'bar');
context.report({
node,
message: ERROR_MESSAGE,
fix(fixer) {
return fix({
fixer,
context,
node,
nodeObject: node.arguments[0],
nodeProperty: node.arguments[1],
nodeDefault: node.arguments[2],
isImported: true,
importedGetName,
});
},
});
}
},
};
},
};
/**
* @param {fixer} fixer
* @param {context} context
* @param {node} node - node with: this.getWithDefault('foo', 'bar');
* @param {node} nodeObject - node with: 'this'
* @param {node} nodeProperty - node with: 'foo'
* @param {node} nodeDefault - node with: 'bar'
* @param {boolean} isImported - whether we are dealing with the imported version of `getWithDefault`
* @param {string} importedGetName - name that `get` is imported under (if at all)
*/
function fix({
fixer,
context,
node,
nodeObject,
nodeProperty,
nodeDefault,
isImported,
importedGetName,
}) {
const sourceCode = context.sourceCode ?? context.getSourceCode();
const nodeObjectSourceText = sourceCode.getText(nodeObject);
const nodePropertySourceText = sourceCode.getText(nodeProperty);
const nodeDefaultSourceText = sourceCode.getText(nodeDefault);
// We convert it to use `this.get('property')` here for safety in case of nested paths.
// The `no-get` rule can then convert it to ES5 getters (`this.property`) if safe.
// eslint-disable-next-line unicorn/prefer-logical-operator-over-ternary
const getName = importedGetName ? importedGetName : 'get';
const fixed = isImported
? `(${getName}(${nodeObjectSourceText}, ${nodePropertySourceText}) === undefined ? ${nodeDefaultSourceText} : ${getName}(${nodeObjectSourceText}, ${nodePropertySourceText}))`
: `(${nodeObjectSourceText}.get(${nodePropertySourceText}) === undefined ? ${nodeDefaultSourceText} : ${nodeObjectSourceText}.get(${nodePropertySourceText}))`;
return !isImported || importedGetName
? fixer.replaceText(node, fixed)
: [
// Need to add import statement for `get`.
fixer.insertTextBefore(sourceCode.ast, "import { get } from '@ember/object';\n"),
fixer.replaceText(node, fixed),
];
}