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

Update: add fixer for no-undef-init #7210

Merged
merged 1 commit into from
Sep 30, 2016
Merged
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
2 changes: 2 additions & 0 deletions docs/rules/no-undef-init.md
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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"}]
}
]
});