diff --git a/lib/rules/spaced-comment.js b/lib/rules/spaced-comment.js index 958bb2c6444..daf56cd6bb4 100644 --- a/lib/rules/spaced-comment.js +++ b/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) { diff --git a/tests/lib/rules/spaced-comment.js b/tests/lib/rules/spaced-comment.js index fd6b2e51098..ee28203d10e 100644 --- a/tests/lib/rules/spaced-comment.js +++ b/tests/lib/rules/spaced-comment.js @@ -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" + } + ] } ]