Skip to content

Commit fe793d7

Browse files
authored
Merge pull request #21 from JoaoDsv/fix-no-const-outside-module
Add `--fix` support to `no-const-outside-module` rule
2 parents 7ee3feb + fd83bcb commit fe793d7

File tree

2 files changed

+68
-22
lines changed

2 files changed

+68
-22
lines changed
Lines changed: 35 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,43 @@
11
'use strict';
22

3-
module.exports = function(context) {
4-
return {
5-
VariableDeclaration: function(node) {
6-
if (node.kind !== 'const') {
7-
return;
8-
}
3+
module.exports = {
4+
meta: { fixable: 'code', schema: [] },
5+
create(context) {
6+
return {
7+
VariableDeclaration: function(node) {
8+
if (node.kind !== 'const') {
9+
return;
10+
}
911

10-
if (node.parent && node.parent.type === 'Program') {
11-
// Declaration is in root of module.
12-
return;
13-
}
12+
if (node.parent && node.parent.type === 'Program') {
13+
// Declaration is in root of module.
14+
return;
15+
}
1416

15-
if (node.parent && node.parent.type === 'ExportNamedDeclaration' &&
16-
node.parent.parent && node.parent.parent.type === 'Program') {
17-
// Declaration is a `export const foo = 'asdf'` in root of the module.
18-
return;
19-
}
17+
if (node.parent && node.parent.type === 'ExportNamedDeclaration' &&
18+
node.parent.parent && node.parent.parent.type === 'Program') {
19+
// Declaration is a `export const foo = 'asdf'` in root of the module.
20+
return;
21+
}
2022

21-
context.report({
22-
node: node,
23-
message: '`const` should only be used in module scope (not inside functions/blocks).'
24-
});
25-
}
26-
};
23+
context.report({
24+
node: node,
25+
message: '`const` should only be used in module scope (not inside functions/blocks).',
26+
fix(fixer) {
27+
// Get node's text content
28+
let nodeSourceCode = context.getSourceCode();
29+
let nodeText = nodeSourceCode.getText(node);
30+
31+
// Transform the `const` to a `let` declaration
32+
let fixed = nodeText.replace('const', 'let')
33+
34+
// // Apply and return the fix
35+
return fixer.replaceText(node, fixed);
36+
}
37+
});
38+
}
39+
};
40+
}
2741
};
2842

2943
module.exports.schema = []; // no options

tests/lib/rules/no-const-outside-module-scope.js

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,46 @@ var ruleTester = new RuleTester({
2222
ruleTester.run('no-const-outside-module-scope', rule, {
2323
valid: [
2424
'const FOOBAR = 5;',
25-
'export const FOOBAR = 5;'
25+
'export const FOOBAR = 5;',
26+
`
27+
import Controller from '@ember/controller';
28+
const COUNT = 5;
29+
export default class extends Controller {
30+
get count() {
31+
let listCount = this.module.scope ? this.list.length : COUNT;
32+
return listCount;
33+
}
34+
}
35+
`
2636
],
2737

2838
invalid: [{
2939
code: '{ const FOOBAR = 5; }',
40+
output: '{ let FOOBAR = 5; }',
3041
errors: [{ message: '`const` should only be used in module scope (not inside functions/blocks).' }]
3142
}, {
3243
code: 'function foobar() { const FOOBAR = 5; return FOOBAR; }',
44+
output: 'function foobar() { let FOOBAR = 5; return FOOBAR; }',
45+
errors: [{ message: '`const` should only be used in module scope (not inside functions/blocks).' }]
46+
}, {
47+
code: `
48+
import Controller from '@ember/controller';
49+
export default class extends Controller {
50+
get isControllerScope() {
51+
const { scope } = this
52+
return Boolean(scope)
53+
}
54+
}
55+
`,
56+
output: `
57+
import Controller from '@ember/controller';
58+
export default class extends Controller {
59+
get isControllerScope() {
60+
let { scope } = this
61+
return Boolean(scope)
62+
}
63+
}
64+
`,
3365
errors: [{ message: '`const` should only be used in module scope (not inside functions/blocks).' }]
3466
}]
3567
});

0 commit comments

Comments
 (0)