From e460346c270021ba4d23990d4e984623d7f7b292 Mon Sep 17 00:00:00 2001 From: Anix Date: Sat, 28 Nov 2020 16:17:13 +0000 Subject: [PATCH 1/2] Fix: one-var autofixing for export (fixes #13834) --- lib/rules/one-var.js | 4 +- tests/lib/rules/one-var.js | 77 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 79 insertions(+), 2 deletions(-) diff --git a/lib/rules/one-var.js b/lib/rules/one-var.js index c31a0d2b13c..162e464536f 100644 --- a/lib/rules/one-var.js +++ b/lib/rules/one-var.js @@ -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); } diff --git a/tests/lib/rules/one-var.js b/tests/lib/rules/one-var.js index f17c0b3c7d7..dd90454da47 100644 --- a/tests/lib/rules/one-var.js +++ b/tests/lib/rules/one-var.js @@ -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" + }] } ] }); From 99416d3a8d161304dda0d9bd2b17c82967a43ddd Mon Sep 17 00:00:00 2001 From: Anix Date: Mon, 30 Nov 2020 05:06:33 +0000 Subject: [PATCH 2/2] Chore: added export placement for adjacent tokens --- lib/rules/one-var.js | 8 +++++--- tests/lib/rules/one-var.js | 11 +++++++++++ 2 files changed, 16 insertions(+), 3 deletions(-) diff --git a/lib/rules/one-var.js b/lib/rules/one-var.js index 162e464536f..b370c6d5e19 100644 --- a/lib/rules/one-var.js +++ b/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.parent.type === "ExportNamedDeclaration" ? "export " : ""}${declaration.kind} ` + `;${sourceCode.text.slice(tokenAfterDeclarator.range[1], lastComment.range[0])}${exportPlacement}${declaration.kind} ` ); } - return fixer.replaceText(tokenAfterDeclarator, `; ${declaration.parent.type === "ExportNamedDeclaration" ? "export " : ""}${declaration.kind}`); + return fixer.replaceText(tokenAfterDeclarator, `; ${exportPlacement}${declaration.kind}`); }).filter(x => x); } diff --git a/tests/lib/rules/one-var.js b/tests/lib/rules/one-var.js index dd90454da47..026862e10a7 100644 --- a/tests/lib/rules/one-var.js +++ b/tests/lib/rules/one-var.js @@ -1931,6 +1931,17 @@ ruleTester.run("one-var", rule, { 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" + }] } ] });