Skip to content
Permalink
Browse files

Fix: ignore marker-only comments in spaced-comment (fixes #12036) (#1…

  • Loading branch information
mdjermanovic authored and kaicataldo committed Nov 15, 2019
1 parent 6503cb8 commit 1110045e0d28a461e75d2f57d5f01533d59ef239
Showing with 102 additions and 4 deletions.
  1. +5 −4 lib/rules/spaced-comment.js
  2. +97 −0 tests/lib/rules/spaced-comment.js
@@ -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;
@@ -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;
}

@@ -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) {
@@ -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"]
}
],

@@ -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"
}
]
}
]

0 comments on commit 1110045

Please sign in to comment.
You can’t perform that action at this time.