Skip to content

Commit

Permalink
Merge pull request #866 from bmish/no-incorrect-computed-macros-fix-m…
Browse files Browse the repository at this point in the history
…issing-import

Fix missing import statement in autofix for `no-incorrect-computed-macros` rule
  • Loading branch information
bmish committed Jun 23, 2020
2 parents 73016a9 + 988a6a0 commit cd6c930
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 15 deletions.
38 changes: 26 additions & 12 deletions lib/rules/no-incorrect-computed-macros.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ const types = require('../utils/types');

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

const COMPUTED_MACROS_AND_OR = ['and', 'or'];

module.exports = {
meta: {
type: 'problem',
Expand All @@ -24,22 +22,27 @@ module.exports = {
ERROR_MESSAGE_AND_OR,

create(context) {
let macroIdentifiersAndOr = [];
let importNameAnd = undefined;
let importNameOr = undefined;
let importNameReadOnly = undefined;

return {
ImportDeclaration(node) {
if (node.source.value !== '@ember/object/computed') {
return;
if (node.source.value === '@ember/object/computed') {
// Gather the identifiers that these macros are imported under.
importNameAnd =
importNameAnd || getImportIdentifier(node, '@ember/object/computed', 'and');
importNameOr = importNameOr || getImportIdentifier(node, '@ember/object/computed', 'or');
importNameReadOnly =
importNameReadOnly || getImportIdentifier(node, '@ember/object/computed', 'readOnly');
}

// Gather the identifiers that these macros are imported under.
macroIdentifiersAndOr = COMPUTED_MACROS_AND_OR.map((fn) =>
getImportIdentifier(node, '@ember/object/computed', fn)
);
},

CallExpression(node) {
if (types.isIdentifier(node.callee) && macroIdentifiersAndOr.includes(node.callee.name)) {
if (
types.isIdentifier(node.callee) &&
[importNameAnd, importNameOr].includes(node.callee.name)
) {
if (
node.arguments.length === 1 &&
types.isStringLiteral(node.arguments[0]) &&
Expand All @@ -49,7 +52,18 @@ module.exports = {
node: node.callee,
message: ERROR_MESSAGE_AND_OR,
fix(fixer) {
return fixer.replaceText(node.callee, 'readOnly');
if (importNameReadOnly) {
return fixer.replaceText(node.callee, importNameReadOnly);
} else {
const sourceCode = context.getSourceCode();
return [
fixer.insertTextBefore(
sourceCode.ast,
"import { readOnly } from '@ember/object/computed';\n"
),
fixer.replaceText(node.callee, 'readOnly'),
];
}
},
});
} else if (node.arguments.length === 0) {
Expand Down
9 changes: 6 additions & 3 deletions tests/lib/rules/no-incorrect-computed-macros.js
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,8 @@ ruleTester.run('no-incorrect-computed-macros', rule, {
import { and } from '@ember/object/computed';
and('someProperty')`,
output: `
import { and } from '@ember/object/computed';
import { readOnly } from '@ember/object/computed';
import { and } from '@ember/object/computed';
readOnly('someProperty')`,
errors: [
{
Expand All @@ -106,7 +107,8 @@ ruleTester.run('no-incorrect-computed-macros', rule, {
import { or } from '@ember/object/computed';
or('someProperty')`,
output: `
import { or } from '@ember/object/computed';
import { readOnly } from '@ember/object/computed';
import { or } from '@ember/object/computed';
readOnly('someProperty')`,
errors: [
{
Expand All @@ -121,7 +123,8 @@ ruleTester.run('no-incorrect-computed-macros', rule, {
import { and } from '@ember/object/computed';
class Test { @and('someProperty') prop }`,
output: `
import { and } from '@ember/object/computed';
import { readOnly } from '@ember/object/computed';
import { and } from '@ember/object/computed';
class Test { @readOnly('someProperty') prop }`,
errors: [
{
Expand Down

0 comments on commit cd6c930

Please sign in to comment.