Skip to content

Add new rule no-controllers - #661

Merged
bmish merged 1 commit into
ember-cli:masterfrom
bmish:no-controllers
Feb 11, 2020
Merged

bmish merged 1 commit into
ember-cli:masterfrom
bmish:no-controllers

Conversation

@bmish

@bmish bmish commented Feb 9, 2020

Copy link
Copy Markdown
Member

Some people may want to avoid the use of controllers in their applications.

Controllers are only truly necessary for specifying queryParams today, so this rule only allows controllers if queryParams are present.

While controllers are not currently deprecrated, the Ember 2019-2020 Roadmap RFC does mention:

Perhaps the longest-awaited simplification is eliminating controllers from the Ember programming model, which we will do this year. This will require us to find a new home for query parameters.

Fixes #660. CC: @mehulkar.

@mehulkar mehulkar left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Awesome!

@tracked category = null;
...
}
```

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would the rule prevent code other than queryParams and the category property?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Currently no. I'm enforcing the following simplified rule: If queryParams is defined inside the controller, then the controller and any other code in it is allowed. If queryParams is not defined inside the controller, then the controller is not allowed.

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);
    }
  }),
});

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

makes sense. what happens if queryParams = [] is defined?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The 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 queryParams = [...].

Comment thread docs/rules/no-controllers.md

rwjblue commented Feb 11, 2020

Copy link
Copy Markdown
Member

Seems good to me, though we shouldn't enable it by default.

@bmish
bmish merged commit 4896ef3 into ember-cli:master Feb 11, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

New Rule: prevent any code except queryParams in controllers

4 participants