Skip to content

Commit

Permalink
Fix: ignore marker-only comments in spaced-comment (fixes #12036) (#1…
Browse files Browse the repository at this point in the history
  • Loading branch information
mdjermanovic authored and kaicataldo committed Nov 15, 2019
1 parent 6503cb8 commit 1110045
Show file tree
Hide file tree
Showing 2 changed files with 102 additions and 4 deletions.
9 changes: 5 additions & 4 deletions lib/rules/spaced-comment.js
Expand Up @@ -249,7 +249,8 @@ module.exports = {
beginRegex: requireSpace ? createAlwaysStylePattern(markers, exceptions) : createNeverStylePattern(markers),
endRegex: balanced && requireSpace ? new RegExp(`${createExceptionsPattern(exceptions)}$`, "u") : new RegExp(endNeverPattern, "u"),
hasExceptions: exceptions.length > 0,
markers: new RegExp(`^(${markers.map(escape).join("|")})`, "u")
captureMarker: new RegExp(`^(${markers.map(escape).join("|")})`, "u"),
markers: new Set(markers)
};

return rule;
Expand Down Expand Up @@ -322,8 +323,8 @@ module.exports = {
rule = styleRules[type],
commentIdentifier = type === "block" ? "/*" : "//";

// Ignores empty comments.
if (node.value.length === 0) {
// Ignores empty comments and comments that consist only of a marker.
if (node.value.length === 0 || rule.markers.has(node.value)) {
return;
}

Expand All @@ -333,7 +334,7 @@ module.exports = {
// Checks.
if (requireSpace) {
if (!beginMatch) {
const hasMarker = rule.markers.exec(node.value);
const hasMarker = rule.captureMarker.exec(node.value);
const marker = hasMarker ? commentIdentifier + hasMarker[0] : commentIdentifier;

if (rule.hasExceptions) {
Expand Down
97 changes: 97 additions & 0 deletions tests/lib/rules/spaced-comment.js
Expand Up @@ -304,6 +304,44 @@ ruleTester.run("spaced-comment", rule, {
{
code: "/***\u2028*/",
options: ["always", { exceptions: ["*"] }]
},

// ignore marker-only comments, https://github.com/eslint/eslint/issues/12036
{
code: "//#endregion",
options: ["always", { line: { markers: ["#endregion"] } }]
},
{
code: "/*foo*/",
options: ["always", { block: { markers: ["foo"] } }]
},
{
code: "/*foo*/",
options: ["always", { block: { markers: ["foo"], balanced: true } }]
},
{
code: "/*foo*/ /*bar*/",
options: ["always", { markers: ["foo", "bar"] }]
},
{
code: "//foo\n//bar",
options: ["always", { markers: ["foo", "bar"] }]
},
{
code: "/* foo */",
options: ["never", { markers: [" foo "] }]
},
{
code: "// foo ",
options: ["never", { markers: [" foo "] }]
},
{
code: "//*", // "*" is a marker by default
options: ["always"]
},
{
code: "/***/", // "*" is a marker by default
options: ["always"]
}
],

Expand Down Expand Up @@ -586,6 +624,65 @@ ruleTester.run("spaced-comment", rule, {
output: null,
options: ["never"],
errors: 1
},

// not a marker-only comment, regression tests for https://github.com/eslint/eslint/issues/12036
{
code: "//#endregionfoo",
output: "//#endregion foo",
options: ["always", { line: { markers: ["#endregion"] } }],
errors: [{
message: "Expected space or tab after '//#endregion' in comment.",
type: "Line"
}]
},
{
code: "/*#endregion*/",
output: "/* #endregion*/", // not an allowed marker for block comments
options: ["always", { line: { markers: ["#endregion"] } }],
errors: [{
message: "Expected space or tab after '/*' in comment.",
type: "Block"
}]
},
{
code: "/****/",
output: "/** **/",
options: ["always"],
errors: [{
message: "Expected space or tab after '/**' in comment.",
type: "Block"
}]
},
{
code: "/****/",
output: "/** * */",
options: ["always", { block: { balanced: true } }],
errors: [
{
message: "Expected space or tab after '/**' in comment.",
type: "Block"
},
{
message: "Expected space or tab before '*/' in comment.",
type: "Block"
}
]
},
{
code: "/* foo */",
output: "/*foo*/",
options: ["never", { block: { markers: ["foo"], balanced: true } }], // not " foo "
errors: [
{
message: "Unexpected space or tab after '/*' in comment.",
type: "Block"
},
{
message: "Unexpected space or tab before '*/' in comment.",
type: "Block"
}
]
}
]

Expand Down

0 comments on commit 1110045

Please sign in to comment.