Skip to content

Commit

Permalink
Ensure beforeStatementContinuationChars does not apply to class fields
Browse files Browse the repository at this point in the history
  • Loading branch information
nzakas committed Aug 25, 2021
1 parent 39c8cd1 commit d0d46a8
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 54 deletions.
18 changes: 18 additions & 0 deletions docs/rules/semi.md
Expand Up @@ -76,6 +76,8 @@ Object option (when `"never"`):
* `"beforeStatementContinuationChars": "always"` requires semicolons at the end of statements if the next line starts with `[`, `(`, `/`, `+`, or `-`.
* `"beforeStatementContinuationChars": "never"` disallows semicolons as the end of statements if it doesn't make ASI hazard even if the next line starts with `[`, `(`, `/`, `+`, or `-`.

**Note:** `beforeStatementContinuationChars` does not apply to class fields because class fields are not statements.

### always

Examples of **incorrect** code for this rule with the default `"always"` option:
Expand All @@ -88,6 +90,10 @@ var name = "ESLint"
object.method = function() {
// ...
}

class Foo {
bar = 1
}
```

Examples of **correct** code for this rule with the default `"always"` option:
Expand All @@ -100,6 +106,10 @@ var name = "ESLint";
object.method = function() {
// ...
};

class Foo {
bar = 1;
}
```

### never
Expand All @@ -114,6 +124,10 @@ var name = "ESLint";
object.method = function() {
// ...
};

class Foo {
bar = 1;
}
```

Examples of **correct** code for this rule with the `"never"` option:
Expand Down Expand Up @@ -142,6 +156,10 @@ import b from "b"
;(function() {
// ...
})()

class Foo {
bar = 1
}
```

#### omitLastInOneLineBlock
Expand Down
14 changes: 12 additions & 2 deletions lib/rules/semi.js
Expand Up @@ -292,7 +292,13 @@ module.exports = {
if (isOnSameLineWithNextToken(node)) {
return false; // One liner.
}
if (beforeStatementContinuationChars === "never" && !maybeAsiHazardAfter(node)) {

// continuation characters should not apply to class fields
if (
node.type !== "PropertyDefinition" &&
beforeStatementContinuationChars === "never" &&
!maybeAsiHazardAfter(node)
) {
return true; // ASI works. This statement doesn't connect to the next.
}
if (!maybeAsiHazardBefore(sourceCode.getTokenAfter(node))) {
Expand Down Expand Up @@ -332,7 +338,11 @@ module.exports = {
if (never) {
if (isSemi && canRemoveSemicolon(node)) {
report(node, true);
} else if (!isSemi && beforeStatementContinuationChars === "always" && maybeAsiHazardBefore(sourceCode.getTokenAfter(node))) {
} else if (
!isSemi && beforeStatementContinuationChars === "always" &&
node.type !== "PropertyDefinition" &&
maybeAsiHazardBefore(sourceCode.getTokenAfter(node))
) {
report(node);
}
} else {
Expand Down
104 changes: 52 additions & 52 deletions tests/lib/rules/semi.js
Expand Up @@ -336,6 +336,58 @@ ruleTester.run("semi", rule, {
code: "class C { a=b;\ninstanceof }",
options: ["never"],
parserOptions: { ecmaVersion: 2022 }
},
{
code: `
class C {
x
[foo]
x;
[foo]
x = "a";
[foo]
}
`,
options: ["never", { beforeStatementContinuationChars: "never" }],
parserOptions: { ecmaVersion: 2022 }
},
{
code: `
class C {
x
[foo]
x;
[foo]
x = 1;
[foo]
}
`,
options: ["never", { beforeStatementContinuationChars: "always" }],
parserOptions: { ecmaVersion: 2022 }
},
{
code: "class C { foo\n[bar] }",
options: ["never", { beforeStatementContinuationChars: "always" }],
parserOptions: { ecmaVersion: 2022 }
},
{
code: "class C { foo = () => {}\n[bar] }",
options: ["never", { beforeStatementContinuationChars: "always" }],
parserOptions: { ecmaVersion: 2022 }
},
{
code: "class C { foo\n;[bar] }",
options: ["never", { beforeStatementContinuationChars: "never" }],
parserOptions: { ecmaVersion: 2022 }
},
{
code: "class C { foo = () => {}\n;[bar] }",
options: ["never", { beforeStatementContinuationChars: "never" }],
parserOptions: { ecmaVersion: 2022 }
}
],
invalid: [
Expand Down Expand Up @@ -1736,58 +1788,6 @@ ruleTester.run("semi", rule, {
endColumn: 1
}]
},
{
code: "class C { foo\n[bar] }",
output: "class C { foo;\n[bar] }",
options: ["never", { beforeStatementContinuationChars: "always" }],
parserOptions: { ecmaVersion: 2022 },
errors: [{
messageId: "missingSemi",
line: 1,
column: 14,
endLine: 2,
endColumn: 1
}]
},
{
code: "class C { foo\n;[bar] }",
output: "class C { foo\n[bar] }",
options: ["never", { beforeStatementContinuationChars: "never" }],
parserOptions: { ecmaVersion: 2022 },
errors: [{
messageId: "extraSemi",
line: 2,
column: 1,
endLine: 2,
endColumn: 2
}]
},
{
code: "class C { foo = () => {}\n[bar] }",
output: "class C { foo = () => {};\n[bar] }",
options: ["never", { beforeStatementContinuationChars: "always" }],
parserOptions: { ecmaVersion: 2022 },
errors: [{
messageId: "missingSemi",
line: 1,
column: 25,
endLine: 2,
endColumn: 1
}]
},
{
code: "class C { foo = () => {}\n;[bar] }",
output: "class C { foo = () => {}\n[bar] }",
options: ["never", { beforeStatementContinuationChars: "never" }],
parserOptions: { ecmaVersion: 2022 },
errors: [{
messageId: "extraSemi",
line: 2,
column: 1,
endLine: 2,
endColumn: 2
}]
},

// class fields
{
Expand Down

0 comments on commit d0d46a8

Please sign in to comment.