Skip to content

Commit

Permalink
Update: add fixer for no-undef-init (#7210)
Browse files Browse the repository at this point in the history
  • Loading branch information
not-an-aardvark authored and nzakas committed Sep 30, 2016
1 parent 876d747 commit 2f171f3
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 8 deletions.
2 changes: 2 additions & 0 deletions docs/rules/no-undef-init.md
@@ -1,5 +1,7 @@
# Disallow Initializing to undefined (no-undef-init)

(fixable) The `--fix` option on the [command line](../user-guide/command-line-interface#fix) automatically fixes problems reported by this rule.

In JavaScript, a variable that is declared and not initialized to any value automatically gets the value of `undefined`. For example:

```js
Expand Down
33 changes: 27 additions & 6 deletions lib/rules/no-undef-init.js
Expand Up @@ -5,6 +5,8 @@

"use strict";

const astUtils = require("../ast-utils");

//------------------------------------------------------------------------------
// Rule Definition
//------------------------------------------------------------------------------
Expand All @@ -17,19 +19,38 @@ module.exports = {
recommended: false
},

schema: []
schema: [],

fixable: "code"
},

create(context) {

const sourceCode = context.getSourceCode();

return {

VariableDeclarator(node) {
const name = node.id.name,
init = node.init && node.init.name;

if (init === "undefined" && node.parent.kind !== "const") {
context.report(node, "It's not necessary to initialize '{{name}}' to undefined.", { name });
const name = sourceCode.getText(node.id),
init = node.init && node.init.name,
scope = context.getScope(),
undefinedVar = astUtils.getVariableByName(scope, "undefined"),
shadowed = undefinedVar && undefinedVar.defs.length > 0;

if (init === "undefined" && node.parent.kind !== "const" && !shadowed) {
context.report({
node,
message: "It's not necessary to initialize '{{name}}' to undefined.",
data: {name},
fix(fixer) {
if (node.id.type === "ArrayPattern" || node.id.type === "ObjectPattern") {

// Don't fix destructuring assignment to `undefined`.
return null;
}
return fixer.removeRange([node.id.range[1], node.range[1]]);
}
});
}
}
};
Expand Down
31 changes: 29 additions & 2 deletions tests/lib/rules/no-undef-init.js
Expand Up @@ -21,9 +21,36 @@ const ruleTester = new RuleTester();
ruleTester.run("no-undef-init", rule, {
valid: [
"var a;",
{ code: "const foo = undefined", parserOptions: { ecmaVersion: 6 } }
{ code: "const foo = undefined", parserOptions: { ecmaVersion: 6 } },
"var undefined = 5; var foo = undefined;"
],
invalid: [
{ code: "var a = undefined;", errors: [{ message: "It's not necessary to initialize 'a' to undefined.", type: "VariableDeclarator"}] }
{
code: "var a = undefined;",
output: "var a;",
errors: [{ message: "It's not necessary to initialize 'a' to undefined.", type: "VariableDeclarator"}]
},
{
code: "var a = undefined, b = 1;",
output: "var a, b = 1;",
errors: [{ message: "It's not necessary to initialize 'a' to undefined.", type: "VariableDeclarator"}]
},
{
code: "var a = 1, b = undefined, c = 5;",
output: "var a = 1, b, c = 5;",
errors: [{ message: "It's not necessary to initialize 'b' to undefined.", type: "VariableDeclarator"}]
},
{
code: "var [a] = undefined;",
output: "var [a] = undefined;",
parserOptions: { ecmaVersion: 6 },
errors: [{ message: "It's not necessary to initialize '[a]' to undefined.", type: "VariableDeclarator"}]
},
{
code: "var {a} = undefined;",
output: "var {a} = undefined;",
parserOptions: { ecmaVersion: 6 },
errors: [{ message: "It's not necessary to initialize '{a}' to undefined.", type: "VariableDeclarator"}]
}
]
});

0 comments on commit 2f171f3

Please sign in to comment.