Skip to content

Commit cd6c930

Browse files
authored
Merge pull request #866 from bmish/no-incorrect-computed-macros-fix-missing-import
Fix missing import statement in autofix for `no-incorrect-computed-macros` rule
2 parents 73016a9 + 988a6a0 commit cd6c930

File tree

2 files changed

+32
-15
lines changed

2 files changed

+32
-15
lines changed

lib/rules/no-incorrect-computed-macros.js

Lines changed: 26 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@ const types = require('../utils/types');
55

66
const ERROR_MESSAGE_AND_OR = 'Computed property macro should be used with 2+ arguments';
77

8-
const COMPUTED_MACROS_AND_OR = ['and', 'or'];
9-
108
module.exports = {
119
meta: {
1210
type: 'problem',
@@ -24,22 +22,27 @@ module.exports = {
2422
ERROR_MESSAGE_AND_OR,
2523

2624
create(context) {
27-
let macroIdentifiersAndOr = [];
25+
let importNameAnd = undefined;
26+
let importNameOr = undefined;
27+
let importNameReadOnly = undefined;
2828

2929
return {
3030
ImportDeclaration(node) {
31-
if (node.source.value !== '@ember/object/computed') {
32-
return;
31+
if (node.source.value === '@ember/object/computed') {
32+
// Gather the identifiers that these macros are imported under.
33+
importNameAnd =
34+
importNameAnd || getImportIdentifier(node, '@ember/object/computed', 'and');
35+
importNameOr = importNameOr || getImportIdentifier(node, '@ember/object/computed', 'or');
36+
importNameReadOnly =
37+
importNameReadOnly || getImportIdentifier(node, '@ember/object/computed', 'readOnly');
3338
}
34-
35-
// Gather the identifiers that these macros are imported under.
36-
macroIdentifiersAndOr = COMPUTED_MACROS_AND_OR.map((fn) =>
37-
getImportIdentifier(node, '@ember/object/computed', fn)
38-
);
3939
},
4040

4141
CallExpression(node) {
42-
if (types.isIdentifier(node.callee) && macroIdentifiersAndOr.includes(node.callee.name)) {
42+
if (
43+
types.isIdentifier(node.callee) &&
44+
[importNameAnd, importNameOr].includes(node.callee.name)
45+
) {
4346
if (
4447
node.arguments.length === 1 &&
4548
types.isStringLiteral(node.arguments[0]) &&
@@ -49,7 +52,18 @@ module.exports = {
4952
node: node.callee,
5053
message: ERROR_MESSAGE_AND_OR,
5154
fix(fixer) {
52-
return fixer.replaceText(node.callee, 'readOnly');
55+
if (importNameReadOnly) {
56+
return fixer.replaceText(node.callee, importNameReadOnly);
57+
} else {
58+
const sourceCode = context.getSourceCode();
59+
return [
60+
fixer.insertTextBefore(
61+
sourceCode.ast,
62+
"import { readOnly } from '@ember/object/computed';\n"
63+
),
64+
fixer.replaceText(node.callee, 'readOnly'),
65+
];
66+
}
5367
},
5468
});
5569
} else if (node.arguments.length === 0) {

tests/lib/rules/no-incorrect-computed-macros.js

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,8 @@ ruleTester.run('no-incorrect-computed-macros', rule, {
9292
import { and } from '@ember/object/computed';
9393
and('someProperty')`,
9494
output: `
95-
import { and } from '@ember/object/computed';
95+
import { readOnly } from '@ember/object/computed';
96+
import { and } from '@ember/object/computed';
9697
readOnly('someProperty')`,
9798
errors: [
9899
{
@@ -106,7 +107,8 @@ ruleTester.run('no-incorrect-computed-macros', rule, {
106107
import { or } from '@ember/object/computed';
107108
or('someProperty')`,
108109
output: `
109-
import { or } from '@ember/object/computed';
110+
import { readOnly } from '@ember/object/computed';
111+
import { or } from '@ember/object/computed';
110112
readOnly('someProperty')`,
111113
errors: [
112114
{
@@ -121,7 +123,8 @@ ruleTester.run('no-incorrect-computed-macros', rule, {
121123
import { and } from '@ember/object/computed';
122124
class Test { @and('someProperty') prop }`,
123125
output: `
124-
import { and } from '@ember/object/computed';
126+
import { readOnly } from '@ember/object/computed';
127+
import { and } from '@ember/object/computed';
125128
class Test { @readOnly('someProperty') prop }`,
126129
errors: [
127130
{

0 commit comments

Comments
 (0)