-
-
Notifications
You must be signed in to change notification settings - Fork 197
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #397 from jrjohnson/344-computed-style
Add rule to enforce computed property style
- Loading branch information
Showing
4 changed files
with
438 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,102 @@ | ||
# 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: | ||
|
||
* `"always-with-setter"` (default) getters are *required* when computed property has a setter | ||
* `"always"` getters are *required* in computed properties | ||
* `"never"` getters are *never allowed* in computed properties | ||
|
||
|
||
### always-with-setter | ||
|
||
```javascript | ||
/// BAD | ||
Ember.Object.extend({ | ||
fullName: computed('firstName', 'lastName', { | ||
get() { | ||
//... | ||
} | ||
}) | ||
}); | ||
|
||
// GOOD | ||
Ember.Object.extend({ | ||
fullName: computed('firstName', 'lastName', function() { | ||
//... | ||
}) | ||
}); | ||
|
||
// GOOD | ||
Ember.Object.extend({ | ||
fullName: computed('firstName', 'lastName', { | ||
set() { | ||
//... | ||
}, | ||
get() { | ||
//... | ||
} | ||
}) | ||
}); | ||
``` | ||
|
||
|
||
### always | ||
|
||
```javascript | ||
/// GOOD | ||
Ember.Object.extend({ | ||
fullName: computed('firstName', 'lastName', { | ||
get() { | ||
//... | ||
} | ||
}) | ||
}); | ||
|
||
// BAD | ||
Ember.Object.extend({ | ||
fullName: computed('firstName', 'lastName', function() { | ||
//... | ||
}) | ||
}); | ||
``` | ||
|
||
### never | ||
|
||
```javascript | ||
/// GOOD | ||
Ember.Object.extend({ | ||
fullName: computed('firstName', 'lastName', function() { | ||
//... | ||
}) | ||
}); | ||
|
||
// BAD | ||
Ember.Object.extend({ | ||
fullName: computed('firstName', 'lastName', { | ||
get() { | ||
//... | ||
} | ||
}) | ||
}); | ||
|
||
// BAD | ||
Ember.Object.extend({ | ||
fullName: computed('firstName', 'lastName', { | ||
get() { | ||
//... | ||
}, | ||
set() { | ||
//... | ||
} | ||
}) | ||
}); | ||
``` |
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,99 @@ | ||
'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-with-setter', 'always', 'never'] | ||
}, | ||
], | ||
}, | ||
|
||
create(context) { | ||
const requireGetters = context.options[0] || 'always-with-setter'; | ||
const message = 'Do not use a getter inside computed properties.'; | ||
|
||
const report = function (node) { | ||
context.report(node, message); | ||
}; | ||
|
||
const requireGetterOnlyWithASetterInComputedProperty = function (node) { | ||
const objectExpressions = node.arguments.filter(arg => utils.isObjectExpression(arg)); | ||
if ( | ||
objectExpressions.length | ||
) { | ||
const { properties } = objectExpressions[0]; | ||
const getters = properties.filter(prop => prop.key && prop.key.name && prop.key.name === 'get'); | ||
const setters = properties.filter(prop => prop.key && prop.key.name && prop.key.name === 'set'); | ||
if ( | ||
(setters.length > 0 && getters.length === 0) || | ||
(getters.length > 0 && setters.length === 0) | ||
) { | ||
report(node); | ||
} | ||
} | ||
}; | ||
|
||
const preventGetterInComputedProperty = function (node) { | ||
const objectExpressions = node.arguments.filter(arg => utils.isObjectExpression(arg)); | ||
if ( | ||
objectExpressions.length | ||
) { | ||
const { properties } = objectExpressions[0]; | ||
const getters = properties.filter(prop => prop.key && prop.key.name && prop.key.name === 'get'); | ||
const setters = properties.filter(prop => prop.key && prop.key.name && prop.key.name === 'set'); | ||
if (getters.length > 0 || setters.length > 0) { | ||
report(node); | ||
} | ||
} | ||
}; | ||
|
||
const requireGetterInComputedProperty = function (node) { | ||
const objectExpressions = node.arguments.filter(arg => utils.isObjectExpression(arg)); | ||
if ( | ||
objectExpressions.length | ||
) { | ||
const { properties } = objectExpressions[0]; | ||
const getters = properties.filter(prop => prop.key && prop.key.name && prop.key.name === 'get'); | ||
if (getters.length === 0) { | ||
report(node); | ||
} | ||
} else { | ||
report(node); | ||
} | ||
}; | ||
|
||
return { | ||
CallExpression(node) { | ||
if ( | ||
ember.isComputedProp(node) && | ||
node.arguments.length | ||
) { | ||
if (requireGetters === 'always-with-setter') { | ||
requireGetterOnlyWithASetterInComputedProperty(node); | ||
} | ||
if (requireGetters === 'always') { | ||
requireGetterInComputedProperty(node); | ||
} | ||
if (requireGetters === 'never') { | ||
preventGetterInComputedProperty(node); | ||
} | ||
} | ||
} | ||
}; | ||
} | ||
}; |
Oops, something went wrong.