Skip to content
Permalink
Browse files
Fix: one-var autofixing for export (fixes #13834) (#13891)
* Fix: one-var autofixing for export (fixes #13834)

* Chore: added export placement for adjacent tokens
  • Loading branch information
anikethsaha committed Dec 5, 2020
1 parent 110cf96 commit cbc57fb7d07c00663ed5781f5e6bc8f534cc2d76
Showing with 93 additions and 3 deletions.
  1. +5 −3 lib/rules/one-var.js
  2. +88 −0 tests/lib/rules/one-var.js
@@ -314,12 +314,14 @@ module.exports = {
return null;
}

const exportPlacement = declaration.parent.type === "ExportNamedDeclaration" ? "export " : "";

/*
* `var x,y`
* tokenAfterDeclarator ^^ afterComma
*/
if (afterComma.range[0] === tokenAfterDeclarator.range[1]) {
return fixer.replaceText(tokenAfterDeclarator, `; ${declaration.kind} `);
return fixer.replaceText(tokenAfterDeclarator, `; ${exportPlacement}${declaration.kind} `);
}

/*
@@ -341,11 +343,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])}${exportPlacement}${declaration.kind} `
);
}

return fixer.replaceText(tokenAfterDeclarator, `; ${declaration.kind}`);
return fixer.replaceText(tokenAfterDeclarator, `; ${exportPlacement}${declaration.kind}`);
}).filter(x => x);
}

@@ -1854,6 +1854,94 @@ 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"
}]
},
{
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"
}]
}
]
});

0 comments on commit cbc57fb

Please sign in to comment.