-
-
Notifications
You must be signed in to change notification settings - Fork 202
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add rule to enforce consistent computed property style
Defaults to `never` allow getters in computed properties, has the option to `always` require them.
- Loading branch information
Showing
4 changed files
with
251 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
# Rule name: `computed-property-getters` | ||
|
||
## Enforce the consistent use of getters in computed properties | ||
|
||
Computed properties may be created with or without a `get` method. This rule ensures that the choice | ||
is consistent. | ||
|
||
## Options | ||
|
||
This rule takes a single string option | ||
|
||
String option: | ||
|
||
* `"never"` (default) getters are *never allowed* in computed properties | ||
* `"always"` getters are *required* in computed properties | ||
|
||
### never | ||
|
||
```javascript | ||
/// GOOD | ||
Ember.Object.extend({ | ||
fullName: computed('firstName', 'lastName', function() { | ||
//... | ||
}) | ||
}); | ||
|
||
// BAD | ||
Ember.Object.extend({ | ||
fullName: computed('firstName', 'lastName', { | ||
get() { | ||
//... | ||
} | ||
}) | ||
}); | ||
``` | ||
|
||
### always | ||
|
||
```javascript | ||
/// GOOD | ||
Ember.Object.extend({ | ||
fullName: computed('firstName', 'lastName', { | ||
get() { | ||
//... | ||
} | ||
}) | ||
}); | ||
|
||
// BAD | ||
Ember.Object.extend({ | ||
fullName: computed('firstName', 'lastName', function() { | ||
//... | ||
}) | ||
}); | ||
``` | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
'use strict'; | ||
|
||
const ember = require('../utils/ember'); | ||
const utils = require('../utils/utils'); | ||
|
||
//------------------------------------------------------------------------------ | ||
// General rule - Prevent using a getter inside computed properties. | ||
//------------------------------------------------------------------------------ | ||
|
||
module.exports = { | ||
meta: { | ||
docs: { | ||
description: 'Warns about getters in computed properties', | ||
category: 'Possible Errors', | ||
recommended: false, | ||
url: 'https://github.com/ember-cli/eslint-plugin-ember/tree/master/docs/rules/computed-property-getters.md' | ||
}, | ||
fixable: null, // or "code" or "whitespace" | ||
schema: [ | ||
{ | ||
enum: ['always', 'never'] | ||
}, | ||
], | ||
}, | ||
|
||
create(context) { | ||
const requireGetters = context.options[0] || 'never'; | ||
const message = 'Do not use a getter inside computed properties.'; | ||
|
||
const report = function (node) { | ||
context.report(node, message); | ||
}; | ||
|
||
const preventGetterInComputedProperty = function (node) { | ||
const objectExpressions = node.arguments.filter(arg => utils.isObjectExpression(arg)); | ||
if ( | ||
objectExpressions.length | ||
) { | ||
report(node); | ||
} | ||
}; | ||
|
||
const requireGetterInComputedProperty = function (node) { | ||
const functionExpressions = node.arguments.filter(arg => utils.isFunctionExpression(arg)); | ||
if ( | ||
functionExpressions.length | ||
) { | ||
report(node); | ||
} | ||
}; | ||
|
||
return { | ||
CallExpression(node) { | ||
if ( | ||
ember.isComputedProp(node) && | ||
node.arguments.length | ||
) { | ||
if (requireGetters === 'always') { | ||
requireGetterInComputedProperty(node); | ||
} else { | ||
preventGetterInComputedProperty(node); | ||
} | ||
} | ||
} | ||
}; | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,126 @@ | ||
'use strict'; | ||
|
||
//------------------------------------------------------------------------------ | ||
// Requirements | ||
//------------------------------------------------------------------------------ | ||
|
||
const rule = require('../../../lib/rules/computed-property-getters'); | ||
const RuleTester = require('eslint').RuleTester; | ||
|
||
|
||
//------------------------------------------------------------------------------ | ||
// Tests | ||
//------------------------------------------------------------------------------ | ||
|
||
const ruleTester = new RuleTester(); | ||
const parserOptions = { ecmaVersion: 2018, sourceType: 'module' }; | ||
|
||
const codeWithoutGetters = [ | ||
` | ||
{ | ||
foo: computed('model', function() {}) | ||
}`, | ||
`{ | ||
foo: computed('model', function() {}).volatile() | ||
}`, | ||
`{ | ||
foo: computed(function() {}) | ||
}`, | ||
`{ | ||
foo: computed(async function() {}) | ||
}`, | ||
`{ | ||
foo: computed('model', async function() {}) | ||
}` | ||
]; | ||
|
||
const codeWithGetters = [ | ||
`{ | ||
foo: computed({ | ||
get() { | ||
return true; | ||
} | ||
}).readonly() | ||
}`, | ||
`{ | ||
foo: computed({ | ||
get() { | ||
return true; | ||
} | ||
}) | ||
}`, | ||
`{ | ||
foo: computed('model.foo', { | ||
get() { | ||
return true; | ||
} | ||
}).readonly() | ||
}`, | ||
`{ | ||
foo: computed('model.foo', { | ||
get() { | ||
return true; | ||
} | ||
}) | ||
}`, | ||
`{ | ||
foo: computed('model.foo', { | ||
get() {} | ||
}) | ||
}` | ||
]; | ||
|
||
|
||
const validWithDefaultOptions = codeWithoutGetters.map(code => ({ code, parserOptions })); | ||
|
||
const validWithNeverOption = codeWithoutGetters.map((code) => { | ||
const options = ['never']; | ||
return { code, parserOptions, options }; | ||
}); | ||
|
||
const validWithAlwaysOption = codeWithGetters.map((code) => { | ||
const options = ['always']; | ||
return { code, parserOptions, options }; | ||
}); | ||
|
||
const inValidWithDefaultOptions = codeWithGetters.map(code => ( | ||
{ | ||
code, | ||
parserOptions, | ||
output: null, | ||
errors: [{ | ||
message: rule.meta.message, | ||
}] | ||
} | ||
)); | ||
|
||
const inValidWithNeverOption = codeWithGetters.map((code) => { | ||
const options = ['never']; | ||
return { | ||
code, | ||
parserOptions, | ||
options, | ||
output: null, | ||
errors: [{ | ||
message: rule.meta.message, | ||
}] | ||
}; | ||
}); | ||
|
||
const inValidWithAlwaysOption = codeWithoutGetters.map((code) => { | ||
const options = ['always']; | ||
return { | ||
code, | ||
parserOptions, | ||
options, | ||
output: null, | ||
errors: [{ | ||
message: rule.meta.message, | ||
}] | ||
}; | ||
}); | ||
|
||
ruleTester.run('computed-property-getters', rule, { | ||
valid: [...validWithDefaultOptions, ...validWithNeverOption, ...validWithAlwaysOption], | ||
invalid: [...inValidWithDefaultOptions, ...inValidWithNeverOption, ...inValidWithAlwaysOption], | ||
}); |