Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
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 cbc57fb
Show file tree
Hide file tree
Showing 2 changed files with 93 additions and 3 deletions.
8 changes: 5 additions & 3 deletions lib/rules/one-var.js
Expand Up @@ -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} `);
}

/*
Expand All @@ -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);
}

Expand Down
88 changes: 88 additions & 0 deletions tests/lib/rules/one-var.js
Expand Up @@ -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.