From 39c8cd12738f0286931060a9db0d24eb5e67b4b7 Mon Sep 17 00:00:00 2001 From: "Nicholas C. Zakas" Date: Mon, 23 Aug 2021 11:04:57 -0700 Subject: [PATCH] Fixed several bugs --- lib/rules/semi.js | 32 ++++++++++-- tests/lib/rules/semi.js | 106 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 134 insertions(+), 4 deletions(-) diff --git a/lib/rules/semi.js b/lib/rules/semi.js index 8ec650f0678..3d7aa2dd5e5 100644 --- a/lib/rules/semi.js +++ b/lib/rules/semi.js @@ -183,7 +183,31 @@ module.exports = { return false; } - if (unsafeClassFieldNames.has(node.key.name)) { + // computed keys are always valid + if (node.computed) { + return false; + } + + // private identifiers don't have any constraints + if (node.key.type === "PrivateIdentifier") { + return false; + } + + /* + * If there is a static property that is named "static", + * that is considered safe because a third static would + * be an obvious syntax error. + */ + if (node.static && node.key.name === "static") { + return false; + } + + /* + * Certain names are problematic unless they also have an + * initializer to distinguish between keywords and property + * names. + */ + if (unsafeClassFieldNames.has(node.key.name) && !node.value) { return true; } @@ -259,12 +283,12 @@ module.exports = { * @returns {boolean} whether the semicolon is unnecessary. */ function canRemoveSemicolon(node) { - if (never && maybeClassFieldAsiHazard(node)) { - return false; - } if (isRedundantSemi(sourceCode.getLastToken(node))) { return true; // `;;` or `;}` } + if (never && maybeClassFieldAsiHazard(node)) { + return false; + } if (isOnSameLineWithNextToken(node)) { return false; // One liner. } diff --git a/tests/lib/rules/semi.js b/tests/lib/rules/semi.js index 3f18e11bdd5..2a049ab99c6 100644 --- a/tests/lib/rules/semi.js +++ b/tests/lib/rules/semi.js @@ -1787,6 +1787,112 @@ ruleTester.run("semi", rule, { endLine: 2, endColumn: 2 }] + }, + + // class fields + { + code: "class C { [get];\nfoo\n}", + output: "class C { [get]\nfoo\n}", + options: ["never"], + parserOptions: { ecmaVersion: 2022 }, + errors: [{ + messageId: "extraSemi", + line: 1, + column: 16, + endLine: 1, + endColumn: 17 + }] + }, + { + code: "class C { [set];\nfoo\n}", + output: "class C { [set]\nfoo\n}", + options: ["never"], + parserOptions: { ecmaVersion: 2022 }, + errors: [{ + messageId: "extraSemi", + line: 1, + column: 16, + endLine: 1, + endColumn: 17 + }] + }, + { + code: "class C { #get;\nfoo\n}", + output: "class C { #get\nfoo\n}", + options: ["never"], + parserOptions: { ecmaVersion: 2022 }, + errors: [{ + messageId: "extraSemi", + line: 1, + column: 15, + endLine: 1, + endColumn: 16 + }] + }, + { + code: "class C { #set;\nfoo\n}", + output: "class C { #set\nfoo\n}", + options: ["never"], + parserOptions: { ecmaVersion: 2022 }, + errors: [{ + messageId: "extraSemi", + line: 1, + column: 15, + endLine: 1, + endColumn: 16 + }] + }, + { + code: "class C { #static;\nfoo\n}", + output: "class C { #static\nfoo\n}", + options: ["never"], + parserOptions: { ecmaVersion: 2022 }, + errors: [{ + messageId: "extraSemi", + line: 1, + column: 18, + endLine: 1, + endColumn: 19 + }] + }, + { + code: "class C { get=1;\nfoo\n}", + output: "class C { get=1\nfoo\n}", + options: ["never"], + parserOptions: { ecmaVersion: 2022 }, + errors: [{ + messageId: "extraSemi", + line: 1, + column: 16, + endLine: 1, + endColumn: 17 + }] + }, + { + code: "class C { static static;\nfoo\n}", + output: "class C { static static\nfoo\n}", + options: ["never"], + parserOptions: { ecmaVersion: 2022 }, + errors: [{ + messageId: "extraSemi", + line: 1, + column: 24, + endLine: 1, + endColumn: 25 + }] + }, + { + code: "class C { static;\n}", + output: "class C { static\n}", + options: ["never"], + parserOptions: { ecmaVersion: 2022 }, + errors: [{ + messageId: "extraSemi", + line: 1, + column: 17, + endLine: 1, + endColumn: 18 + }] } ] });