-
-
Notifications
You must be signed in to change notification settings - Fork 215
Add new rule no-controllers
#661
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,35 @@ | ||
| # no-controllers | ||
|
|
||
| Some people may prefer to avoid the use of controllers in their applications, typically in favor of components which can be more portable and easier to test. | ||
|
|
||
| This rule disallows controller usage, except when the controller is used to define `queryParams`. | ||
|
|
||
| ## Examples | ||
|
|
||
| Examples of **incorrect** code for this rule: | ||
|
|
||
| ```js | ||
| import Controller from '@ember/controller'; | ||
| export default class ArticlesController extends Controller { | ||
| // Controller disallowed since it doesn't use `queryParams`. | ||
| ... | ||
| } | ||
| ``` | ||
|
|
||
| Examples of **correct** code for this rule: | ||
|
|
||
| ```js | ||
| import Controller from '@ember/controller'; | ||
| export default class ArticlesController extends Controller { | ||
| queryParams = ['category']; // Controller allowed for defining `queryParams`. | ||
| @tracked category = null; | ||
| ... | ||
| } | ||
| ``` | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Would the rule prevent code other than queryParams and the category property?
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Currently no. I'm enforcing the following simplified rule: If Why? Because it's non-trivial to determine if a particular line of code/function/property/observer/action handler/service injection/etc inside a controller is involved in or needed for reading or modifying a query parameter value. For example, all the code below is needed for managing a query parameter value, but it would be very difficult for the lint rule to determine that. export default Controller.extend({
queryParams: ['sortType'],
sortType: null,
hasDefaultSortType: equal('model.sortType', DEFAULT_SORT_TYPE),
sortObserver: observer('hasDefaultSortType', function() {
if (this.hasDefaultSortType) {
this.set('sortType', null);
} else {
this.set('sortType', this.model.sortType);
}
}),
});There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. makes sense. what happens if
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If you're asking if the rule handles native class syntax, then yes, it will allow a native class controller as long as it has |
||
|
|
||
| Note that this rule supports both classic and native class syntax. | ||
|
|
||
| ## Further Reading | ||
|
|
||
| - [Guide on controllers](https://guides.emberjs.com/release/routing/controllers/) | ||
| - [Guide on `queryParameters`](https://guides.emberjs.com/release/routing/query-params/) | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,67 @@ | ||
| const ember = require('../utils/ember'); | ||
| const types = require('../utils/types'); | ||
| const assert = require('assert'); | ||
|
|
||
| const ERROR_MESSAGE = 'Avoid using controllers except for specifying `queryParams`'; | ||
|
|
||
| module.exports = { | ||
| ERROR_MESSAGE, | ||
| meta: { | ||
| type: 'suggestion', | ||
| docs: { | ||
| description: 'disallow non-essential controllers', | ||
| category: 'Best Practices', | ||
| recommended: false, | ||
| url: | ||
| 'https://github.com/ember-cli/eslint-plugin-ember/tree/master/docs/rules/no-controllers.md', | ||
| }, | ||
| fixable: null, | ||
| schema: [], | ||
| }, | ||
|
|
||
| create: context => { | ||
| return { | ||
| ClassDeclaration(node) { | ||
| if ( | ||
| ember.isEmberController(context, node) && | ||
| (node.body.body.length === 0 || !classDeclarationHasProperty(node, 'queryParams')) | ||
| ) { | ||
| context.report(node, ERROR_MESSAGE); | ||
| } | ||
| }, | ||
|
|
||
| CallExpression(node) { | ||
| if ( | ||
| ember.isEmberController(context, node) && | ||
| (node.arguments.length === 0 || !callExpressionClassHasProperty(node, 'queryParams')) | ||
| ) { | ||
| context.report(node, ERROR_MESSAGE); | ||
| } | ||
| }, | ||
| }; | ||
| }, | ||
| }; | ||
|
|
||
| function classDeclarationHasProperty(classDeclaration, propertyName) { | ||
| assert(types.isClassDeclaration(classDeclaration)); | ||
| return ( | ||
| classDeclaration.body.body.length >= 1 && | ||
| classDeclaration.body.body.some( | ||
| item => | ||
| types.isClassProperty(item) && | ||
| types.isIdentifier(item.key) && | ||
| item.key.name === propertyName | ||
| ) | ||
| ); | ||
| } | ||
|
|
||
| function callExpressionClassHasProperty(callExpression, propertyName) { | ||
| assert(types.isCallExpression(callExpression)); | ||
| return ( | ||
| callExpression.arguments.length >= 1 && | ||
| types.isObjectExpression(callExpression.arguments[callExpression.arguments.length - 1]) && | ||
| callExpression.arguments[callExpression.arguments.length - 1].properties.some( | ||
| prop => types.isIdentifier(prop.key) && prop.key.name === propertyName | ||
| ) | ||
| ); | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,117 @@ | ||
| //------------------------------------------------------------------------------ | ||
| // Requirements | ||
| //------------------------------------------------------------------------------ | ||
|
|
||
| const rule = require('../../../lib/rules/no-controllers'); | ||
| const RuleTester = require('eslint').RuleTester; | ||
|
|
||
| const { ERROR_MESSAGE } = rule; | ||
|
|
||
| //------------------------------------------------------------------------------ | ||
| // Tests | ||
| //------------------------------------------------------------------------------ | ||
|
|
||
| const ruleTester = new RuleTester({ | ||
| parser: require.resolve('babel-eslint'), | ||
| parserOptions: { ecmaVersion: 6, sourceType: 'module' }, | ||
| }); | ||
| ruleTester.run('no-controllers', rule, { | ||
| valid: [ | ||
| ` | ||
| import Controller from '@ember/controller'; | ||
| export default Controller.extend({ | ||
| someFunction() {}, | ||
| query: null, | ||
| sortType: null, | ||
| sortOrder: null, | ||
| queryParams: ['query', 'sortType', 'sortOrder'] | ||
| }); | ||
| `, | ||
| ` | ||
| import Controller from '@ember/controller'; | ||
| export default class ArticlesController extends Controller { | ||
| get filteredArticles() {} | ||
| @tracked category = null; | ||
| queryParams = ['category']; | ||
| } | ||
| `, | ||
| ], | ||
|
|
||
| invalid: [ | ||
| // *************** | ||
| // Legacy classes: | ||
| // *************** | ||
|
|
||
| { | ||
| code: ` | ||
| import Controller from '@ember/controller'; | ||
| export default Controller.extend(); | ||
| `, | ||
| output: null, | ||
| errors: [{ type: 'CallExpression', message: ERROR_MESSAGE }], | ||
| }, | ||
| { | ||
| code: ` | ||
| import Controller from '@ember/controller'; | ||
| export default Controller.extend(SomeMixin); | ||
| `, | ||
| output: null, | ||
| errors: [{ type: 'CallExpression', message: ERROR_MESSAGE }], | ||
| }, | ||
| { | ||
| code: ` | ||
| import Controller from '@ember/controller'; | ||
| export default Controller.extend({}); | ||
| `, | ||
| output: null, | ||
| errors: [{ type: 'CallExpression', message: ERROR_MESSAGE }], | ||
| }, | ||
| { | ||
| code: ` | ||
| import Controller from '@ember/controller'; | ||
| export default Controller.extend({ | ||
| randomProperty: true, | ||
| randomFunction() {} | ||
| }); | ||
| `, | ||
| output: null, | ||
| errors: [{ type: 'CallExpression', message: ERROR_MESSAGE }], | ||
| }, | ||
| { | ||
| // With mixin. | ||
| code: ` | ||
| import Controller from '@ember/controller'; | ||
| export default Controller.extend(SomeMixin, { | ||
| randomProperty: true, | ||
| randomFunction() {} | ||
| }); | ||
| `, | ||
| output: null, | ||
| errors: [{ type: 'CallExpression', message: ERROR_MESSAGE }], | ||
| }, | ||
|
|
||
| // *************** | ||
| // Native classes: | ||
| // *************** | ||
|
|
||
| { | ||
| code: ` | ||
| import Controller from '@ember/controller'; | ||
| export default class ArticlesController extends Controller {} | ||
| `, | ||
| output: null, | ||
| errors: [{ type: 'ClassDeclaration', message: ERROR_MESSAGE }], | ||
| }, | ||
| { | ||
| code: ` | ||
| import Controller from '@ember/controller'; | ||
| export default class ArticlesController extends Controller { | ||
| randomProperty = true; | ||
| get filteredArticles() {} | ||
| } | ||
| `, | ||
| output: null, | ||
| errors: [{ type: 'ClassDeclaration', message: ERROR_MESSAGE }], | ||
| }, | ||
| ], | ||
| }); |
Uh oh!
There was an error while loading. Please reload this page.