Skip to content

Commit 60de719

Browse files
author
Joao
committed
Add --fix support to the rule
Running the --fix option will replace the invalid `const` declarations by `let ` declarations
1 parent 372fc54 commit 60de719

File tree

1 file changed

+12
-1
lines changed

1 file changed

+12
-1
lines changed

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

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,18 @@ module.exports = {
2222

2323
context.report({
2424
node: node,
25-
message: '`const` should only be used in module scope (not inside functions/blocks).'
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+
}
2637
});
2738
}
2839
};

0 commit comments

Comments
 (0)