Skip to content

Commit

Permalink
Fix: one-var autofixing for export (fixes eslint#13834)
Browse files Browse the repository at this point in the history
  • Loading branch information
anikethsaha committed Nov 29, 2020
1 parent 110cf96 commit e460346
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 2 deletions.
4 changes: 2 additions & 2 deletions lib/rules/one-var.js
Expand Up @@ -341,11 +341,11 @@ module.exports = {

return fixer.replaceTextRange(
[tokenAfterDeclarator.range[0], lastComment.range[0]],
`;${sourceCode.text.slice(tokenAfterDeclarator.range[1], lastComment.range[0])}${declaration.kind} `
`;${sourceCode.text.slice(tokenAfterDeclarator.range[1], lastComment.range[0])}${declaration.parent.type === "ExportNamedDeclaration" ? "export " : ""}${declaration.kind} `
);
}

return fixer.replaceText(tokenAfterDeclarator, `; ${declaration.kind}`);
return fixer.replaceText(tokenAfterDeclarator, `; ${declaration.parent.type === "ExportNamedDeclaration" ? "export " : ""}${declaration.kind}`);
}).filter(x => x);
}

Expand Down
77 changes: 77 additions & 0 deletions tests/lib/rules/one-var.js
Expand Up @@ -1854,6 +1854,83 @@ ruleTester.run("one-var", rule, {
line: 2,
column: 1
}]
},
{
code: "export const foo=1, bar=2;",
output: "export const foo=1; export const bar=2;",
options: ["never"],
parserOptions: { ecmaVersion: 2021, sourceType: "module" },
errors: [{
messageId: "split",
data: { type: "const" },
type: "VariableDeclaration"
}]
},
{
code: "const foo=1,\n bar=2;",
output: "const foo=1;\n const bar=2;",
options: ["never"],
parserOptions: { ecmaVersion: 2021, sourceType: "module" },
errors: [{
messageId: "split",
data: { type: "const" },
type: "VariableDeclaration"
}]
},
{
code: "export const foo=1,\n bar=2;",
output: "export const foo=1;\n export const bar=2;",
options: ["never"],
parserOptions: { ecmaVersion: 2021, sourceType: "module" },
errors: [{
messageId: "split",
data: { type: "const" },
type: "VariableDeclaration"
}]
},
{
code: "export const foo=1\n, bar=2;",
output: "export const foo=1\n; export const bar=2;",
options: ["never"],
parserOptions: { ecmaVersion: 2021, sourceType: "module" },
errors: [{
messageId: "split",
data: { type: "const" },
type: "VariableDeclaration"
}]
},
{
code: "export const foo= a, bar=2;",
output: "export const foo= a; export const bar=2;",
options: ["never"],
parserOptions: { ecmaVersion: 2021, sourceType: "module" },
errors: [{
messageId: "split",
data: { type: "const" },
type: "VariableDeclaration"
}]
},
{
code: "export const foo=() => a, bar=2;",
output: "export const foo=() => a; export const bar=2;",
options: ["never"],
parserOptions: { ecmaVersion: 2021, sourceType: "module" },
errors: [{
messageId: "split",
data: { type: "const" },
type: "VariableDeclaration"
}]
},
{
code: "export const foo= a, bar=2, bar2=2;",
output: "export const foo= a; export const bar=2; export const bar2=2;",
options: ["never"],
parserOptions: { ecmaVersion: 2021, sourceType: "module" },
errors: [{
messageId: "split",
data: { type: "const" },
type: "VariableDeclaration"
}]
}
]
});

0 comments on commit e460346

Please sign in to comment.