Skip to content

Commit 1883735

Browse files
authored
Merge pull request #880 from bmish/no-get-check-import-name
Use imported name of `get` and `getProperties` in `no-get` rule
2 parents b09c30b + 589895f commit 1883735

File tree

2 files changed

+91
-28
lines changed

2 files changed

+91
-28
lines changed

lib/rules/no-get.js

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ const types = require('../utils/types');
44
const emberUtils = require('../utils/ember');
55
const utils = require('../utils/utils');
66
const assert = require('assert');
7+
const { getImportIdentifier } = require('../utils/import');
78

89
function makeErrorMessageForGet(
910
property,
@@ -115,6 +116,9 @@ module.exports = {
115116
let currentProxyObject = null;
116117
let currentClassWithUnknownPropertyMethod = null;
117118

119+
let importedGetName;
120+
let importedGetPropertiesName;
121+
118122
const filename = context.getFilename();
119123

120124
// Skip mirage directory
@@ -123,6 +127,15 @@ module.exports = {
123127
}
124128

125129
return {
130+
ImportDeclaration(node) {
131+
if (node.source.value === '@ember/object') {
132+
importedGetName = importedGetName || getImportIdentifier(node, '@ember/object', 'get');
133+
importedGetPropertiesName =
134+
importedGetPropertiesName ||
135+
getImportIdentifier(node, '@ember/object', 'getProperties');
136+
}
137+
},
138+
126139
'CallExpression:exit'(node) {
127140
if (currentProxyObject === node) {
128141
currentProxyObject = null;
@@ -193,7 +206,7 @@ module.exports = {
193206

194207
if (
195208
types.isIdentifier(node.callee) &&
196-
node.callee.name === 'get' &&
209+
node.callee.name === importedGetName &&
197210
node.arguments.length === 2 &&
198211
types.isThisExpression(node.arguments[0]) &&
199212
types.isStringLiteral(node.arguments[1]) &&
@@ -230,7 +243,7 @@ module.exports = {
230243

231244
if (
232245
types.isIdentifier(node.callee) &&
233-
node.callee.name === 'getProperties' &&
246+
node.callee.name === importedGetPropertiesName &&
234247
types.isThisExpression(node.arguments[0]) &&
235248
validateGetPropertiesArguments(node.arguments.slice(1), ignoreNestedPaths)
236249
) {

tests/lib/rules/no-get.js

Lines changed: 76 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -20,59 +20,72 @@ ruleTester.run('no-get', rule, {
2020

2121
// Nested property path.
2222
{ code: "this.get('foo.bar');", options: [{ ignoreNestedPaths: true }] },
23-
{ code: "get(this, 'foo.bar');", options: [{ ignoreNestedPaths: true }] },
23+
{
24+
code: "import { get } from '@ember/object'; get(this, 'foo.bar');",
25+
options: [{ ignoreNestedPaths: true }],
26+
},
2427

2528
// Template literals.
2629
{
2730
code: 'this.get(`foo`);',
2831
parserOptions: { ecmaVersion: 6 },
2932
},
3033
{
31-
code: 'get(this, `foo`);',
34+
code: "import { get } from '@ember/object'; get(this, `foo`);",
3235
parserOptions: { ecmaVersion: 6 },
3336
},
3437

3538
// Not `this`.
3639
"foo.get('bar');",
37-
"get(foo, 'bar');",
40+
"import { get } from '@ember/object'; get(foo, 'bar');",
3841

3942
// Not `get`.
4043
"this.foo('bar');",
4144
"foo(this, 'bar');",
4245

4346
// Unknown extra argument.
4447
"this.get('foo', 'bar');",
45-
"get(this, 'foo', 'bar');",
48+
"import { get } from '@ember/object'; get(this, 'foo', 'bar');",
4649

4750
// Non-string parameter.
4851
'this.get(5);',
4952
'this.get(MY_PROP);',
50-
'get(this, 5);',
51-
'get(this, MY_PROP);',
53+
"import { get } from '@ember/object'; get(this, 5);",
54+
"import { get } from '@ember/object'; get(this, MY_PROP);",
5255

5356
// Unknown sub-function call:
5457
"this.get.foo('bar');",
55-
"get.foo(this, 'bar');",
58+
"import { get } from '@ember/object'; get.foo(this, 'bar');",
5659

5760
// In mirage directory
5861
{
5962
code: 'this.get("/resources")',
6063
filename: path.join('app', 'mirage', 'config.js'),
6164
},
6265

66+
// Missing import:
67+
"get(this, 'foo');",
68+
6369
// **************************
6470
// getProperties
6571
// **************************
6672

6773
// Nested property path.
6874
{ code: "this.getProperties('foo', 'bar.baz');", options: [{ ignoreNestedPaths: true }] },
6975
{ code: "this.getProperties(['foo', 'bar.baz']);", options: [{ ignoreNestedPaths: true }] }, // With parameters in array.
70-
{ code: "getProperties(this, 'foo', 'bar.baz');", options: [{ ignoreNestedPaths: true }] },
71-
{ code: "getProperties(this, ['foo', 'bar.baz']);", options: [{ ignoreNestedPaths: true }] }, // With parameters in array.
76+
{
77+
code: "import { getProperties } from '@ember/object'; getProperties(this, 'foo', 'bar.baz');",
78+
options: [{ ignoreNestedPaths: true }],
79+
},
80+
{
81+
code:
82+
"import { getProperties } from '@ember/object'; getProperties(this, ['foo', 'bar.baz']);",
83+
options: [{ ignoreNestedPaths: true }],
84+
}, // With parameters in array.
7285

7386
// Template literals.
7487
'this.getProperties(`prop1`, `prop2`);',
75-
'getProperties(this, `prop1`, `prop2`);',
88+
"import { getProperties } from '@ember/object'; getProperties(this, `prop1`, `prop2`);",
7689

7790
// Not `this`.
7891
"myObject.getProperties('prop1', 'prop2');",
@@ -84,13 +97,16 @@ ruleTester.run('no-get', rule, {
8497
'this.getProperties(MY_PROP);',
8598
'this.getProperties(...MY_PROPS);',
8699
'this.getProperties([MY_PROP]);',
87-
'getProperties(this, MY_PROP);',
88-
'getProperties(this, ...MY_PROPS);',
89-
'getProperties(this, [MY_PROP]);',
100+
"import { getProperties } from '@ember/object'; getProperties(this, MY_PROP);",
101+
"import { getProperties } from '@ember/object'; getProperties(this, ...MY_PROPS);",
102+
"import { getProperties } from '@ember/object'; getProperties(this, [MY_PROP]);",
90103

91104
// Unknown sub-function call:
92105
"this.getProperties.foo('prop1', 'prop2');",
93106

107+
// Missing import:
108+
"getProperties(this, 'prop1', 'prop2');",
109+
94110
// With ignoreGetProperties: true
95111
{
96112
code: "this.getProperties('prop1', 'prop2');",
@@ -101,11 +117,12 @@ ruleTester.run('no-get', rule, {
101117
options: [{ ignoreGetProperties: true }],
102118
},
103119
{
104-
code: "getProperties(this, 'prop1', 'prop2');",
120+
code: "import { getProperties } from '@ember/object'; getProperties(this, 'prop1', 'prop2');",
105121
options: [{ ignoreGetProperties: true }],
106122
},
107123
{
108-
code: "getProperties(this, ['prop1', 'prop2']);", // With parameters in array.
124+
code:
125+
"import { getProperties } from '@ember/object'; getProperties(this, ['prop1', 'prop2']);", // With parameters in array.
109126
options: [{ ignoreGetProperties: true }],
110127
},
111128

@@ -173,11 +190,32 @@ ruleTester.run('no-get', rule, {
173190
errors: [{ message: "Use `this.foo` instead of `this.get('foo')`", type: 'CallExpression' }],
174191
},
175192
{
176-
code: "get(this, 'foo');",
177-
output: 'this.foo;',
193+
code: `
194+
import { get } from '@ember/object';
195+
import { somethingElse } from '@ember/object';
196+
import { random } from 'random';
197+
get(this, 'foo');
198+
`,
199+
output: `
200+
import { get } from '@ember/object';
201+
import { somethingElse } from '@ember/object';
202+
import { random } from 'random';
203+
this.foo;
204+
`,
178205
// Error message intentionally written out to ensure it looks right.
179206
errors: [{ message: "Use `this.foo` instead of `get(this, 'foo')`", type: 'CallExpression' }],
180207
},
208+
{
209+
// With renamed import:
210+
code: "import { get as g } from '@ember/object'; g(this, 'foo');",
211+
output: "import { get as g } from '@ember/object'; this.foo;",
212+
errors: [
213+
{
214+
message: makeErrorMessageForGet('foo', { isImportedGet: true }),
215+
type: 'CallExpression',
216+
},
217+
],
218+
},
181219
{
182220
code: "this.get('foo').someFunction();",
183221
output: 'this.foo.someFunction();',
@@ -201,7 +239,7 @@ ruleTester.run('no-get', rule, {
201239
},
202240
{
203241
// With invalid JS variable name:
204-
code: "get(this, 'foo-bar');",
242+
code: "import { get } from '@ember/object'; get(this, 'foo-bar');",
205243
output: null,
206244
errors: [
207245
{
@@ -226,12 +264,24 @@ ruleTester.run('no-get', rule, {
226264
errors: [{ message: ERROR_MESSAGE_GET_PROPERTIES, type: 'CallExpression' }],
227265
},
228266
{
229-
code: "getProperties(this, 'prop1', 'prop2');",
267+
code: `
268+
import { getProperties } from '@ember/object';
269+
import { somethingElse } from '@ember/object';
270+
import { random } from 'random';
271+
getProperties(this, 'prop1', 'prop2');
272+
`,
230273
output: null,
231274
errors: [{ message: ERROR_MESSAGE_GET_PROPERTIES, type: 'CallExpression' }],
232275
},
233276
{
234-
code: "getProperties(this, ['prop1', 'prop2']);", // With parameters in array.
277+
// With renamed import:
278+
code: "import { getProperties as gp } from '@ember/object'; gp(this, 'prop1', 'prop2');",
279+
output: null,
280+
errors: [{ message: ERROR_MESSAGE_GET_PROPERTIES, type: 'CallExpression' }],
281+
},
282+
{
283+
code:
284+
"import { getProperties } from '@ember/object'; getProperties(this, ['prop1', 'prop2']);", // With parameters in array.
235285
output: null,
236286
errors: [{ message: ERROR_MESSAGE_GET_PROPERTIES, type: 'CallExpression' }],
237287
},
@@ -248,7 +298,7 @@ ruleTester.run('no-get', rule, {
248298
],
249299
},
250300
{
251-
code: "get(this, 'foo.bar');",
301+
code: "import { get } from '@ember/object'; get(this, 'foo.bar');",
252302
output: null,
253303
errors: [
254304
{
@@ -263,7 +313,7 @@ ruleTester.run('no-get', rule, {
263313
errors: [{ message: ERROR_MESSAGE_GET_PROPERTIES, type: 'CallExpression' }],
264314
},
265315
{
266-
code: "getProperties(this, 'foo.bar');",
316+
code: "import { getProperties } from '@ember/object'; getProperties(this, 'foo.bar');",
267317
output: null,
268318
errors: [{ message: ERROR_MESSAGE_GET_PROPERTIES, type: 'CallExpression' }],
269319
},
@@ -296,9 +346,9 @@ ruleTester.run('no-get', rule, {
296346
],
297347
},
298348
{
299-
code: "get(this, 'foo.bar');",
349+
code: "import { get } from '@ember/object'; get(this, 'foo.bar');",
300350
options: [{ useOptionalChaining: true }],
301-
output: 'this.foo?.bar;',
351+
output: "import { get } from '@ember/object'; this.foo?.bar;",
302352
errors: [
303353
{
304354
message: makeErrorMessageForGet('foo.bar', {
@@ -310,9 +360,9 @@ ruleTester.run('no-get', rule, {
310360
],
311361
},
312362
{
313-
code: "get(this, 'very.long.path');",
363+
code: "import { get } from '@ember/object'; get(this, 'very.long.path');",
314364
options: [{ useOptionalChaining: true }],
315-
output: 'this.very?.long?.path;',
365+
output: "import { get } from '@ember/object'; this.very?.long?.path;",
316366
errors: [
317367
{
318368
message: makeErrorMessageForGet('very.long.path', {

0 commit comments

Comments
 (0)