Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 1 commit into from Nov 15, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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