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

Add new rule no-leaking-state-in-program-scope #447

Open
wants to merge 1 commit 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
1 change: 1 addition & 0 deletions README.md
Expand Up @@ -119,6 +119,7 @@ The `--fix` option on the command line automatically fixes problems reported by
| :wrench: | [no-ember-super-in-es-classes](./docs/rules/no-ember-super-in-es-classes.md) | Prevents use of `this._super` in ES class methods |
| :white_check_mark: | [no-ember-testing-in-module-scope](./docs/rules/no-ember-testing-in-module-scope.md) | Prevents use of Ember.testing in module scope |
| | [no-invalid-debug-function-arguments](./docs/rules/no-invalid-debug-function-arguments.md) | Catch usages of Ember's `assert()` / `warn()` / `deprecate()` functions that have the arguments passed in the wrong order. |
| | [no-leaking-state-in-program-scope](./docs/rules/no-leaking-state-in-program-scope.md) | Do not allow anything else than constants in global module namespace. |
| :white_check_mark: | [no-side-effects](./docs/rules/no-side-effects.md) | Warns about unexpected side effects in computed properties |
| | [require-return-from-computed](./docs/rules/require-return-from-computed.md) | Warns about missing return statements in computed properties |
| :white_check_mark: | [require-super-in-init](./docs/rules/require-super-in-init.md) | Enforces super calls in init hooks |
Expand Down
54 changes: 54 additions & 0 deletions docs/rules/no-leaking-state-in-program-scope.md
@@ -0,0 +1,54 @@
# Do not allow anything else than constants in global module namespace. (no-leaking-state-in-program-scope)

This rule attempts to catch any possible hidden state thanks to program scope variables.


## Rule Details

Program scope variables that can store value retrieved at runtime can potentially introduce bugs in tests where they might pollute environments between two tests.

Consider following service:

```js
var cachedFoo = null;

export default Service.extend({
fetchFoo(count) {
if(!cachedFoo) {
fetch(`./api/?results=${count}`).then(function(response) {
cachedFoo = response;
});
}
}

getFoo() {
return cachedFoo;
}
});
```

If there are two tests calling this service with different value for `count` when calling `fetchFoo`, then calls to `getFoo` will always return the response of the first test.

Examples of **incorrect** code for this rule:

```js
var foo = 'bar';

export default Service.extend({});
```

Examples of **correct** code for this rule:

```js
const foo = 'bar';

export default Service.extend({});
```

```js
export default Service.extend({
init() {
this.set('foo', 'bar');
}
});
```
1 change: 1 addition & 0 deletions lib/recommended-rules.js
Expand Up @@ -31,6 +31,7 @@ module.exports = {
"ember/no-global-jquery": "error",
"ember/no-invalid-debug-function-arguments": "off",
"ember/no-jquery": "off",
"ember/no-leaking-state-in-program-scope": "off",
"ember/no-new-mixins": "off",
"ember/no-observers": "error",
"ember/no-old-shims": "error",
Expand Down
28 changes: 28 additions & 0 deletions lib/rules/no-leaking-state-in-program-scope.js
@@ -0,0 +1,28 @@
'use strict';

const message = 'Use const on global scope';

module.exports = {
meta: {
docs: {
description: 'Do not allow anything else than constants in global module namespace.',
category: 'Possible Errors',
recommended: false,
},
fixable: null,
},

create(context) {
const report = function(node) {
context.report(node, message);
};

return {
VariableDeclaration(node) {
if (node.parent.type === 'Program' && node.kind !== 'const') {
report(node, message);
}
},
};
},
};
41 changes: 41 additions & 0 deletions tests/lib/rules/no-leaking-state-in-module-scope.js
@@ -0,0 +1,41 @@
'use strict';

//------------------------------------------------------------------------------
// Requirements
//------------------------------------------------------------------------------

const rule = require('../../../lib/rules/no-leaking-state-in-program-scope');
const RuleTester = require('eslint').RuleTester;

//------------------------------------------------------------------------------
// Tests
//------------------------------------------------------------------------------

let ruleTester = new RuleTester({
parserOptions: {
ecmaVersion: 2018,
sourceType: 'module',
},
});

ruleTester.run('no-leaking-state-in-program-scope', rule, {
valid: [
`const foo = "bar";`,
`export default Ember.Service.extend({
init() {
var foo = "bar";
}
});`,
],

invalid: [
{
code: `var foo = "bar";`,
errors: [{ message: 'Use const on global scope', type: 'VariableDeclaration' }],
},
{
code: `let foo = "bar";`,
errors: [{ message: 'Use const on global scope', type: 'VariableDeclaration' }],
},
],
});