Skip to content

Commit

Permalink
wip: address pr feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
rtablada committed Dec 29, 2022
1 parent 4f4969c commit 75faccf
Show file tree
Hide file tree
Showing 3 changed files with 104 additions and 32 deletions.
3 changes: 2 additions & 1 deletion docs/rules/no-deprecated-router-transition-methods.md
Expand Up @@ -84,8 +84,9 @@ export default class NewPostController extends Controller {

## Migration

The autofixer for this rule will update classes and add injections for the configured services.
The autofixer for this rule will update method calls to use the router service, and will inject the router service as needed.

## References

- [Deprecation](https://deprecations.emberjs.com/v3.x/#toc_routing-transition-methods)
- [Router Service](https://api.emberjs.com/ember/release/classes/RouterService)
5 changes: 0 additions & 5 deletions lib/rules/no-deprecated-router-transition-methods.js
Expand Up @@ -67,11 +67,6 @@ module.exports = {
// State being tracked for the current class we're inside.
const classStack = new Stack();

// This rule does not apply to test files or non class modules
if (emberUtils.isTestFile(context.getFilename())) {
return {};
}

return {
ImportDeclaration(node) {
if (node.source.value === '@ember/service') {
Expand Down
128 changes: 102 additions & 26 deletions tests/lib/rules/no-deprecated-router-transition-methods.js
Expand Up @@ -22,11 +22,13 @@ const ruleTester = new RuleTester({

ruleTester.run('no-deprecated-router-transition-methods', rule, {
valid: [
// Route Uses RouterService.transitionTo
{
filename: 'routes/index.js',
code: `
import Route from '@ember/routing/route';
import { inject as service } from '@ember/service';
export default class SettingsRoute extends Route {
@service('router') router;
@service session;
Expand All @@ -37,11 +39,68 @@ ruleTester.run('no-deprecated-router-transition-methods', rule, {
}
}`,
},
// Route Uses RouterService.replaceWith
{
filename: 'routes/index.js',
code: `
import Route from '@ember/routing/route';
import { inject as service } from '@ember/service';
export default class SettingsRoute extends Route {
@service('router') router;
@service session;
beforeModel() {
if (!this.session.isAuthenticated) {
this.router.replaceWith('login');
}
}
}`,
},
// Controller Uses RouterService.transitionTo
{
filename: 'controllers/index.js',
code: `
import Controller from '@ember/controller';
import { inject as service } from '@ember/service';
import { action } from '@ember/object';
export default class NewPostController1 extends Controller {
@service('router') router;
@action
async save({ title, text }) {
let post = this.store.createRecord('post', { title, text });
await post.save();
return this.router.transitionTo('post', post.id);
}
}`,
},
// Controller Uses RouterService.replaceWith
{
filename: 'controllers/index.js',
code: `
import Controller from '@ember/controller';
import { inject as service } from '@ember/service';
import { action } from '@ember/object';
export default class NewPostController1 extends Controller {
@service('router') router;
@action
async save({ title, text }) {
let post = this.store.createRecord('post', { title, text });
await post.save();
return this.router.replaceWith('post', post.id);
}
}`,
},
// Route Uses RouterService.transitionTo on service injected without serviceName
{
filename: 'routes/index.js',
code: `
import Route from '@ember/routing/route';
import { inject as service } from '@ember/service';
export default class SettingsRoute extends Route {
@service() router;
@service session;
Expand All @@ -55,32 +114,49 @@ ruleTester.run('no-deprecated-router-transition-methods', rule, {
{
filename: 'routes/index.js',
code: `
import Route from '@ember/routing/route';
import { inject as service } from '@ember/service';
export class SettingsIndexRoute extends Route {
model() {
return [];
}
}
export class SettingsDetailRoute extends Route {
@service('settings') settingsService;
async model(id) {
return new Setting(await this.settingsService.find(id));
}
}
export class SettingsRoute extends Route {
@service() router;
@service session;
beforeModel() {
if (!this.session.isAuthenticated) {
this.router.transitionTo('login');
}
}
}`,
import Route from '@ember/routing/route';
import { inject as service } from '@ember/service';
export default class SettingsRoute extends Route {
@service router;
@service session;
beforeModel() {
if (!this.session.isAuthenticated) {
this.router.transitionTo('login');
}
}
}`,
},
// Test Multiple Classes in One File
{
filename: 'routes/index.js',
code: `
import Route from '@ember/routing/route';
import { inject as service } from '@ember/service';
export class SettingsIndexRoute extends Route {
model() {
return [];
}
}
export class SettingsDetailRoute extends Route {
@service('settings') settingsService;
async model(id) {
return new Setting(await this.settingsService.find(id));
}
}
export class SettingsRoute extends Route {
@service() router;
@service session;
beforeModel() {
if (!this.session.isAuthenticated) {
this.router.transitionTo('login');
}
}
}`,
},
],
invalid: [
Expand Down

0 comments on commit 75faccf

Please sign in to comment.