|
1 | 1 | 'use strict'; |
2 | 2 |
|
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 | + } |
9 | 11 |
|
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 | + } |
14 | 16 |
|
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 | + } |
20 | 22 |
|
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 | + } |
27 | 41 | }; |
28 | 42 |
|
29 | 43 | module.exports.schema = []; // no options |
0 commit comments