Skip to content
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

fix: ignore mirage directory for avoid-leaking-state-in-ember-objects #1004

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions docs/rules/avoid-leaking-state-in-ember-objects.md
Expand Up @@ -2,6 +2,8 @@

:white_check_mark: The `"extends": "plugin:ember/recommended"` property in a configuration file enables this rule.

In addition, all files in the `mirage` directory will be excluded from this rule.

Don't use arrays and objects as default properties. More info here: <https://dockyard.com/blog/2015/09/18/ember-best-practices-avoid-leaking-state-into-factories>

## Examples
Expand Down
7 changes: 7 additions & 0 deletions lib/rules/avoid-leaking-state-in-ember-objects.js
Expand Up @@ -70,6 +70,13 @@ module.exports = {
DEFAULT_IGNORED_PROPERTIES,

create(context) {
const filename = context.getFilename();

// Skip mirage directory
if (ember.isMirageDirectory(filename)) {
return {};
}

const ignoredProperties = context.options[0] || DEFAULT_IGNORED_PROPERTIES;

const report = function (node) {
Expand Down
8 changes: 8 additions & 0 deletions tests/lib/rules/avoid-leaking-state-in-ember-objects.js
Expand Up @@ -66,6 +66,14 @@ eslintTester.run('avoid-leaking-state-in-ember-objects', rule, {
'export default Foo.extend({ foo: condition ? "foo" : "bar" });',
'export default Foo.extend({ foo: "foo" && "bar" });',
'export default Foo.extend({ foo: "foo" || "bar" });',
{
filename: 'example-app/mirage/models/foo.js',
code: 'export default Model.extend({bar: belongsTo()});',
},
{
filename: 'example-app/mirage/factories/foo.js',
code: 'export default Factory.extend({bar: []});',
},
],
invalid: [
{
Expand Down
5 changes: 5 additions & 0 deletions tests/lib/utils/ember-test.js
Expand Up @@ -113,12 +113,17 @@ describe('isMirageDirectory', () => {
it('detects the mirage directory', () => {
expect(emberUtils.isMirageDirectory('example-app/mirage/config.js')).toBeTruthy();
expect(emberUtils.isMirageDirectory('example-app/mirage/scenarios/foo.js')).toBeTruthy();
expect(emberUtils.isMirageDirectory('example-addon/tests/dummy/mirage/config.js')).toBeTruthy();
expect(
emberUtils.isMirageDirectory('example-addon/tests/dummy/mirage/scenarios/foo.js')
).toBeTruthy();
});

it('does not detect other directories', () => {
expect(emberUtils.isMirageDirectory('example-app/app/app.js')).toBeFalsy();
expect(emberUtils.isMirageDirectory('example-app/tests/test.js')).toBeFalsy();
expect(emberUtils.isMirageDirectory('example-addon/addon/addon.js')).toBeFalsy();
expect(emberUtils.isMirageDirectory('example-addon/tests/dummy/app/app.js')).toBeFalsy();
});
});

Expand Down