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

Fix: one-var shouldn't split declaration if it isn't in a statement list #13959

Merged
merged 1 commit into from Dec 30, 2020
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
34 changes: 30 additions & 4 deletions lib/rules/one-var.js
Expand Up @@ -5,6 +5,25 @@

"use strict";

//------------------------------------------------------------------------------
// Requirements
//------------------------------------------------------------------------------

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

//------------------------------------------------------------------------------
// Helpers
//------------------------------------------------------------------------------

/**
* Determines whether the given node is in a statement list.
* @param {ASTNode} node node to check
* @returns {boolean} `true` if the given node is in a statement list
*/
function isInStatementList(node) {
return astUtils.STATEMENT_LIST_PARENTS.has(node.parent.type);
}

//------------------------------------------------------------------------------
// Rule Definition
//------------------------------------------------------------------------------
Expand Down Expand Up @@ -268,8 +287,8 @@ module.exports = {

/**
* Fixer to join VariableDeclaration's into a single declaration
* @param {VariableDeclarator[]} declarations The `VariableDeclaration` to join
* @returns {Function} The fixer function
* @param {VariableDeclarator[]} declarations The `VariableDeclaration` to join
* @returns {Function} The fixer function
*/
function joinDeclarations(declarations) {
const declaration = declarations[0];
Expand Down Expand Up @@ -297,10 +316,17 @@ module.exports = {

/**
* Fixer to split a VariableDeclaration into individual declarations
* @param {VariableDeclaration} declaration The `VariableDeclaration` to split
* @returns {Function} The fixer function
* @param {VariableDeclaration} declaration The `VariableDeclaration` to split
* @returns {Function|null} The fixer function
*/
function splitDeclarations(declaration) {
const { parent } = declaration;

// don't autofix code such as: if (foo) var x, y;
if (!isInStatementList(parent.type === "ExportNamedDeclaration" ? parent : declaration)) {
return null;
}

return fixer => declaration.declarations.map(declarator => {
const tokenAfterDeclarator = sourceCode.getTokenAfter(declarator);

Expand Down
172 changes: 172 additions & 0 deletions tests/lib/rules/one-var.js
Expand Up @@ -492,6 +492,16 @@ ruleTester.run("one-var", rule, {
}
],
invalid: [
{
code: "var bar = true, baz = false;",
output: "var bar = true; var baz = false;",
options: ["never"],
errors: [{
messageId: "split",
data: { type: "var" },
type: "VariableDeclaration"
}]
},
{
code: "function foo() { var bar = true, baz = false; }",
output: "function foo() { var bar = true; var baz = false; }",
Expand All @@ -502,6 +512,36 @@ ruleTester.run("one-var", rule, {
type: "VariableDeclaration"
}]
},
{
code: "if (foo) { var bar = true, baz = false; }",
output: "if (foo) { var bar = true; var baz = false; }",
options: ["never"],
errors: [{
messageId: "split",
data: { type: "var" },
type: "VariableDeclaration"
}]
},
{
code: "switch (foo) { case bar: var baz = true, quux = false; }",
output: "switch (foo) { case bar: var baz = true; var quux = false; }",
options: ["never"],
errors: [{
messageId: "split",
data: { type: "var" },
type: "VariableDeclaration"
}]
},
{
code: "switch (foo) { default: var baz = true, quux = false; }",
output: "switch (foo) { default: var baz = true; var quux = false; }",
options: ["never"],
errors: [{
messageId: "split",
data: { type: "var" },
type: "VariableDeclaration"
}]
},
{
code: "function foo() { var bar = true; var baz = false; }",
output: "function foo() { var bar = true, baz = false; }",
Expand Down Expand Up @@ -1942,6 +1982,138 @@ ruleTester.run("one-var", rule, {
data: { type: "const" },
type: "VariableDeclaration"
}]
},

// "never" should not autofix declarations in a block position
{
code: "if (foo) var x, y;",
output: null,
options: ["never"],
errors: [{
messageId: "split",
data: { type: "var" },
type: "VariableDeclaration"
}]
},
{
code: "if (foo) var x, y;",
output: null,
options: [{ var: "never" }],
errors: [{
messageId: "split",
data: { type: "var" },
type: "VariableDeclaration"
}]
},
{
code: "if (foo) var x, y;",
output: null,
options: [{ uninitialized: "never" }],
errors: [{
messageId: "splitUninitialized",
data: { type: "var" },
type: "VariableDeclaration"
}]
},
{
code: "if (foo) var x = 1, y = 1;",
output: null,
options: [{ initialized: "never" }],
errors: [{
messageId: "splitInitialized",
data: { type: "var" },
type: "VariableDeclaration"
}]
},
{
code: "if (foo) {} else var x, y;",
output: null,
options: ["never"],
errors: [{
messageId: "split",
data: { type: "var" },
type: "VariableDeclaration"
}]
},
{
code: "while (foo) var x, y;",
output: null,
options: ["never"],
errors: [{
messageId: "split",
data: { type: "var" },
type: "VariableDeclaration"
}]
},
{
code: "do var x, y; while (foo);",
output: null,
options: ["never"],
errors: [{
messageId: "split",
data: { type: "var" },
type: "VariableDeclaration"
}]
},
{
code: "do var x = f(), y = b(); while (x < y);",
output: null,
options: ["never"],
errors: [{
messageId: "split",
data: { type: "var" },
type: "VariableDeclaration"
}]
},
{
code: "for (;;) var x, y;",
output: null,
options: ["never"],
errors: [{
messageId: "split",
data: { type: "var" },
type: "VariableDeclaration"
}]
},
{
code: "for (foo in bar) var x, y;",
output: null,
options: ["never"],
errors: [{
messageId: "split",
data: { type: "var" },
type: "VariableDeclaration"
}]
},
{
code: "for (foo of bar) var x, y;",
output: null,
options: ["never"],
errors: [{
messageId: "split",
data: { type: "var" },
type: "VariableDeclaration"
}]
},
{
code: "with (foo) var x, y;",
output: null,
options: ["never"],
errors: [{
messageId: "split",
data: { type: "var" },
type: "VariableDeclaration"
}]
},
{
code: "label: var x, y;",
output: null,
options: ["never"],
errors: [{
messageId: "split",
data: { type: "var" },
type: "VariableDeclaration"
}]
}
]
});