Skip to content

Commit

Permalink
Merge pull request #577 from bmish/no-get-autofix
Browse files Browse the repository at this point in the history
Add autofixer to `no-get` rule
  • Loading branch information
bmish authored Nov 11, 2019
2 parents 5a59424 + d09a51c commit d9e7f9a
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 13 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ The `--fix` option on the command line automatically fixes problems reported by
| | [named-functions-in-promises](./docs/rules/named-functions-in-promises.md) | Enforces usage of named functions in promises |
| :white_check_mark: | [new-module-imports](./docs/rules/new-module-imports.md) | Use "New Module Imports" from Ember RFC #176 |
| :white_check_mark: | [no-function-prototype-extensions](./docs/rules/no-function-prototype-extensions.md) | Prevents usage of Ember's `function` prototype extensions |
| :car: | [no-get](./docs/rules/no-get.md) | Require ES5 getters instead of Ember's `get` / `getProperties` functions |
| :car::wrench: | [no-get](./docs/rules/no-get.md) | Require ES5 getters instead of Ember's `get` / `getProperties` functions |
| :white_check_mark: | [no-global-jquery](./docs/rules/no-global-jquery.md) | Prevents usage of global jQuery object |
| :car: | [no-jquery](./docs/rules/no-jquery.md) | Disallow any usage of jQuery |
| :white_check_mark: | [no-new-mixins](./docs/rules/no-new-mixins.md) | Prevents creation of new mixins |
Expand Down
27 changes: 25 additions & 2 deletions lib/rules/no-get.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ module.exports = {
octane: true,
url: 'https://github.com/ember-cli/eslint-plugin-ember/tree/master/docs/rules/no-get.md',
},
fixable: 'code',
schema: [
{
type: 'object',
Expand Down Expand Up @@ -109,7 +110,18 @@ module.exports = {
(!node.arguments[0].value.includes('.') || !ignoreNestedPaths)
) {
// Example: this.get('foo');
context.report(node, makeErrorMessageForGet(node.arguments[0].value), false);
const path = node.arguments[0].value;
context.report({
node,
message: makeErrorMessageForGet(path, false),
fix(fixer) {
if (path.includes('.')) {
// Not safe to autofix nested properties because some properties in the path might be null.
return null;
}
return fixer.replaceText(node, `this.${path}`);
},
});
}

if (
Expand All @@ -121,7 +133,18 @@ module.exports = {
(!node.arguments[1].value.includes('.') || !ignoreNestedPaths)
) {
// Example: get(this, 'foo');
context.report(node, makeErrorMessageForGet(node.arguments[1].value, true));
const path = node.arguments[1].value;
context.report({
node,
message: makeErrorMessageForGet(path, true),
fix(fixer) {
if (path.includes('.')) {
// Not safe to autofix nested properties because some properties in the path might be null.
return null;
}
return fixer.replaceText(node, `this.${path}`);
},
});
}

// **************************
Expand Down
56 changes: 46 additions & 10 deletions tests/lib/rules/no-get.js
Original file line number Diff line number Diff line change
Expand Up @@ -157,18 +157,18 @@ ruleTester.run('no-get', rule, {

{
code: "this.get('foo');",
output: null,
errors: [{ message: makeErrorMessageForGet('foo', false) }],
output: 'this.foo;',
errors: [{ message: makeErrorMessageForGet('foo', false), type: 'CallExpression' }],
},
{
code: "get(this, 'foo');",
output: null,
errors: [{ message: makeErrorMessageForGet('foo', true) }],
output: 'this.foo;',
errors: [{ message: makeErrorMessageForGet('foo', true), type: 'CallExpression' }],
},
{
code: "this.get('foo').someFunction();",
output: null,
errors: [{ message: makeErrorMessageForGet('foo', false) }],
output: 'this.foo.someFunction();',
errors: [{ message: makeErrorMessageForGet('foo', false), type: 'CallExpression' }],
},

// **************************
Expand Down Expand Up @@ -234,7 +234,16 @@ ruleTester.run('no-get', rule, {
});
this.get('propertyOutsideClass');
`,
output: null,
output: `
import ArrayProxy from '@ember/array/proxy';
export default ArrayProxy.extend({
someFunction() {
test();
console.log(this.get('propertyInsideProxyObject'));
}
});
this.propertyOutsideClass;
`,
errors: [{ message: makeErrorMessageForGet('propertyOutsideClass'), type: 'CallExpression' }],
},
{
Expand All @@ -249,7 +258,16 @@ ruleTester.run('no-get', rule, {
}
this.get('propertyOutsideClass');
`,
output: null,
output: `
import ArrayProxy from '@ember/array/proxy';
class MyProxy extends ArrayProxy {
someFunction() {
test();
console.log(this.get('propertyInsideProxyObject'));
}
}
this.propertyOutsideClass;
`,
errors: [{ message: makeErrorMessageForGet('propertyOutsideClass'), type: 'CallExpression' }],
},

Expand All @@ -265,7 +283,16 @@ ruleTester.run('no-get', rule, {
});
this.get('propertyOutsideClass');
`,
output: null,
output: `
import EmberObject from '@ember/object';
export default EmberObject.extend({
unknownProperty() {},
someFunction() {
console.log(this.get('propertyInsideClassWithUnknownProperty'));
}
});
this.propertyOutsideClass;
`,
errors: [{ message: makeErrorMessageForGet('propertyOutsideClass'), type: 'CallExpression' }],
},
{
Expand All @@ -280,7 +307,16 @@ ruleTester.run('no-get', rule, {
}
this.get('propertyOutsideClass');
`,
output: null,
output: `
import EmberObject from '@ember/object';
class MyClass extends EmberObject {
unknownProperty() {}
someFunction() {
console.log(this.get('propertyInsideClassWithUnknownProperty'));
}
}
this.propertyOutsideClass;
`,
errors: [{ message: makeErrorMessageForGet('propertyOutsideClass'), type: 'CallExpression' }],
},
],
Expand Down

0 comments on commit d9e7f9a

Please sign in to comment.