💼 This rule is enabled in the ✅ recommended config.
🔧 This rule is automatically fixable by the --fix CLI option.
Ember 3.26 introduced a deprecation for using transitionTo and replaceWith in Routes or transitionToRoute and replaceRoute in Controllers. These methods should be replaced with an injected router service and calls to this.router.transitionTo and this.router.replaceWith instead.
This rule checks for uses of transitionTo and replaceWith in Routes or transitionToRoute and replaceRoute in Controllers.
Examples of incorrect code for this rule:
// app/routes/settings.js
import Route from '@ember/routing/route';
import { inject as service } from '@ember/service';
export default class SettingsRoute extends Route {
@service session;
beforeModel() {
if (!this.session.isAuthenticated) {
this.transitionTo('login');
}
}
}// app/controllers/new-post.js
import Controller from '@ember/controller';
import { action } from '@ember/object';
export default class NewPostController extends Controller {
@action
async save({ title, text }) {
const post = this.store.createRecord('post', { title, text });
await post.save();
return this.transitionToRoute('post', post.id);
}
}Examples of correct code for this rule:
// app/routes/settings.js
import Route from '@ember/routing/route';
import { inject as service } from '@ember/service';
export default class SettingsRoute extends Route {
@service('router') router;
@service('session') session;
beforeModel() {
if (!this.session.isAuthenticated) {
this.router.transitionTo('login');
}
}
}// app/controllers/new-post.js
import Controller from '@ember/controller';
import { action } from '@ember/object';
import { inject as service } from '@ember/service';
export default class NewPostController extends Controller {
@service('router') router;
@action
async save({ title, text }) {
const post = this.store.createRecord('post', { title, text });
await post.save();
return this.router.transitionTo('post', post.id);
}
}The autofixer for this rule will update method calls to use the router service, and will inject the router service as needed.