Skip to content

Commit

Permalink
Merge pull request #534 from NullVoxPopuli/no-arrow-function-computed…
Browse files Browse the repository at this point in the history
…-properties-allow-arrows-when-no-this

Add `onlyThisContexts` option to `no-arrow-function-computed-properties` rule
  • Loading branch information
bmish authored Oct 17, 2019
2 parents 2817e26 + cdff503 commit 6b7f671
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 9 deletions.
6 changes: 6 additions & 0 deletions docs/rules/no-arrow-function-computed-properties.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,12 @@ const Person = EmberObject.extend({
});
```

## Configuration

This rule takes an optional object containing:

* `boolean` -- `onlyThisContexts` -- whether the rule should allow or disallow computed properties where the arrow function body does not contain a `this` reference (default: `false`)

## References

* [Arrow function spec](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/Arrow_functions)
Expand Down
28 changes: 24 additions & 4 deletions lib/rules/no-arrow-function-computed-properties.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,33 @@ module.exports = {
},

create(context) {
const options = context.options[0] || {};
const onlyThisContexts = options.onlyThisContexts || false;

let isThisPresent = false;

return {
CallExpression(node) {
if (
ThisExpression() {
isThisPresent = true;
},
CallExpression() {
isThisPresent = false;
},
'CallExpression:exit'(node) {
const isComputedArrow =
emberUtils.isComputedProp(node) &&
node.arguments.length > 0 &&
types.isArrowFunctionExpression(node.arguments[node.arguments.length - 1])
) {
types.isArrowFunctionExpression(node.arguments[node.arguments.length - 1]);

if (!isComputedArrow) {
return;
}

if (onlyThisContexts) {
if (isThisPresent) {
context.report(node.arguments[node.arguments.length - 1], ERROR_MESSAGE);
}
} else {
context.report(node.arguments[node.arguments.length - 1], ERROR_MESSAGE);
}
},
Expand Down
47 changes: 42 additions & 5 deletions tests/lib/rules/no-arrow-function-computed-properties.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,18 +21,41 @@ ruleTester.run('no-arrow-function-computed-properties', rule, {
valid: [
'computed()',
'computed(function() { return 123; })',
"computed('prop', function() { return this.prop; })",
"computed('prop', function() { return this.prop; }).volatile()",
"computed.map('products', function(product) { return someFunction(product); })",
'computed("prop", function() { return this.prop; })',
'computed("prop", function() { return this.prop; }).volatile()',
'computed.map("products", function(product) { return someFunction(product); })',
'other(() => {})',
'other.computed(() => {})',
{
code: `computed('prop', function() { return this.prop; });`,
options: [{ onlyThisContexts: true }],
},
{
code: `computed('prop', function() { return this.prop; }).volatile();`,
options: [{ onlyThisContexts: true }],
},
{
code: `computed(() => { return 123; });`,
options: [{ onlyThisContexts: true }],
},
{
code: `computed(() => { return "string stuff"; });`,
options: [{ onlyThisContexts: true }],
},
{
code: `computed(() => []);`,
options: [{ onlyThisContexts: true }],
},
{
code: `computed.map('products', product => { return someFunction(product); });`,
options: [{ onlyThisContexts: true }],
},
],
invalid: [
{
code: 'computed(() => { return 123; })',
errors: [{ message: ERROR_MESSAGE, type: 'ArrowFunctionExpression' }],
},

{
code: "computed('prop', () => { return this.prop; })",
errors: [{ message: ERROR_MESSAGE, type: 'ArrowFunctionExpression' }],
Expand All @@ -42,10 +65,24 @@ ruleTester.run('no-arrow-function-computed-properties', rule, {
code: "computed('prop', () => { return this.prop; }).volatile()",
errors: [{ message: ERROR_MESSAGE, type: 'ArrowFunctionExpression' }],
},

{
code: "computed.map('products', product => { return someFunction(product); })",
errors: [{ message: ERROR_MESSAGE, type: 'ArrowFunctionExpression' }],
},
{
code: 'computed(() => { return 123; })',
errors: [],
options: [{ onlyThisContexts: true }],
},
{
code: "computed('prop', () => { return this.prop; })",
errors: [{ message: ERROR_MESSAGE, type: 'ArrowFunctionExpression' }],
options: [{ onlyThisContexts: true }],
},
{
code: "computed('prop', () => { return this.prop; }).volatile()",
errors: [{ message: ERROR_MESSAGE, type: 'ArrowFunctionExpression' }],
options: [{ onlyThisContexts: true }],
},
],
});

0 comments on commit 6b7f671

Please sign in to comment.