Skip to content

Commit

Permalink
fix: destructured access in no-controller-access-in-routes rule
Browse files Browse the repository at this point in the history
  • Loading branch information
bmish committed Aug 16, 2021
1 parent 516b699 commit 629bdf3
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 0 deletions.
18 changes: 18 additions & 0 deletions lib/rules/no-controller-access-in-routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,24 @@ module.exports = {
currentRouteNode = null;
}
},

VariableDeclarator(node) {
if (!currentRouteNode) {
return;
}

if (node.init.type !== 'ThisExpression' || node.id.type !== 'ObjectPattern') {
return;
}

const controllerProperty = node.id.properties.find(
(prop) => prop.key.type === 'Identifier' && prop.key.name === 'controller'
);
if (controllerProperty) {
// Example: const { controller } = this;
context.report({ node: controllerProperty, message: ERROR_MESSAGE });
}
},
};
},
};
Expand Down
25 changes: 25 additions & 0 deletions tests/lib/rules/no-controller-access-in-routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,17 @@ ruleTester.run('no-controller-access-in-routes', rule, {
}
});
`,
`
import Route from '@ember/routing/route';
export default Route.extend({
actions: {
myAction() {
const { foo } = this;
const { controller } = bar;
},
},
});
`,

`
import Component from '@ember/component';
Expand Down Expand Up @@ -277,5 +288,19 @@ ruleTester.run('no-controller-access-in-routes', rule, {
output: null,
errors: [{ message: ERROR_MESSAGE, type: 'CallExpression' }],
},
{
code: `
import Route from '@ember/routing/route';
import { action } from '@ember/object';
export default class MyRoute extends Route {
@action
myAction() {
const { controller } = this;
}
}
`,
output: null,
errors: [{ message: ERROR_MESSAGE, type: 'Property' }],
},
],
});

0 comments on commit 629bdf3

Please sign in to comment.