Skip to content

Commit

Permalink
Fixed several bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
nzakas committed Aug 23, 2021
1 parent c67c7c0 commit 39c8cd1
Show file tree
Hide file tree
Showing 2 changed files with 134 additions and 4 deletions.
32 changes: 28 additions & 4 deletions lib/rules/semi.js
Expand Up @@ -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;
}

Expand Down Expand Up @@ -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.
}
Expand Down
106 changes: 106 additions & 0 deletions tests/lib/rules/semi.js
Expand Up @@ -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
}]
}
]
});

0 comments on commit 39c8cd1

Please sign in to comment.