Skip to content

Commit

Permalink
Fix starred-block alignment/missing star fixer
Browse files Browse the repository at this point in the history
  • Loading branch information
kaicataldo committed Nov 7, 2019
1 parent cab04fd commit 3c9007f
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 43 deletions.
53 changes: 37 additions & 16 deletions lib/rules/multiline-comment-style.js
Expand Up @@ -42,6 +42,15 @@ module.exports = {
// Helpers
//----------------------------------------------------------------------

/**
* Checks if a comment line is starred.
* @param {string} line A string representing a comment line.
* @returns {boolean} Whether or not the comment line is starred.
*/
function isStarredCommentLine(line) {
return /^\s*\*/u.test(line);
}

/**
* Checks if a comment group is in starred-block form.
* @param {Token[]} commentGroup A group of comments, containing either multiple line comments or a single block comment.
Expand Down Expand Up @@ -273,30 +282,42 @@ module.exports = {

for (let lineNumber = firstComment.loc.start.line + 1; lineNumber <= firstComment.loc.end.line; lineNumber++) {
const lineText = sourceCode.lines[lineNumber - 1];
const errorType = isStarredCommentLine(lineText)
? "alignment"
: "missingStar";

if (!lineText.startsWith(expectedLinePrefix)) {
context.report({
loc: {
start: { line: lineNumber, column: 0 },
end: { line: lineNumber, column: sourceCode.lines[lineNumber - 1].length }
end: { line: lineNumber, column: lineText.length }
},
messageId: /^\s*\*/u.test(lineText)
? "alignment"
: "missingStar",
messageId: errorType,
fix(fixer) {
const lineStartIndex = sourceCode.getIndexFromLoc({ line: lineNumber, column: 0 });
const [linePrefix, whitespaceBefore, whitespaceAfter] = lineText.match(/^(\s*)\*?(\s*)/u);
const leadingWhitespace = whitespaceAfter.length && whitespaceAfter || ` ${
whitespaceBefore.startsWith(expectedLeadingWhitespace)
? whitespaceBefore.replace(expectedLeadingWhitespace, "")
: ""
}`;
const replacementText = lineNumber === firstComment.loc.end.line || lineText.length === linePrefix.length
? expectedLinePrefix
: `${expectedLinePrefix}${leadingWhitespace}`;
const commentStartIndex = lineStartIndex + linePrefix.length;

return fixer.replaceTextRange([lineStartIndex, commentStartIndex], replacementText);

if (errorType === "alignment") {
const [, commentTextPrefix = ""] = lineText.match(/^(\s*\*)/u) || [];
const commentTextStartIndex = lineStartIndex + commentTextPrefix.length;

return fixer.replaceTextRange([lineStartIndex, commentTextStartIndex], expectedLinePrefix);
}

const [, commentTextPrefix = ""] = lineText.match(/^(\s*)/u) || [];
const commentTextStartIndex = lineStartIndex + commentTextPrefix.length;
let offset;

for (const [idx, line] of commentLines.entries()) {
if (line.trim().length > 0) {
const lineTextToAlignWith = sourceCode.lines[firstComment.loc.start.line - 1 + idx];
const [, prefix = "", initialOffset = ""] = lineTextToAlignWith.match(/^(\s*(?:\/?\*)?(\s*))/u) || [];

offset = `${commentTextPrefix.slice(prefix.length)}${initialOffset}`;
break;
}
}

return fixer.replaceTextRange([lineStartIndex, commentTextStartIndex], `${expectedLinePrefix}${offset}`);
}
});
}
Expand Down
69 changes: 42 additions & 27 deletions tests/lib/rules/multiline-comment-style.js
Expand Up @@ -471,7 +471,7 @@ ruleTester.run("multiline-comment-style", rule, {
code: `
/*
* the following line
is missing a '*' at the start
is missing a '*' at the start
*/
`,
output: `
Expand All @@ -492,7 +492,22 @@ ruleTester.run("multiline-comment-style", rule, {
output: `
/*
* the following line
* is missing a '*' at the start
* is missing a '*' at the start
*/
`,
errors: [{ messageId: "missingStar", line: 4 }]
},
{
code: `
/*
* the following line
is missing a '*' at the start
*/
`,
output: `
/*
* the following line
* is missing a '*' at the start
*/
`,
errors: [{ messageId: "missingStar", line: 4 }]
Expand Down Expand Up @@ -784,10 +799,10 @@ ruleTester.run("multiline-comment-style", rule, {
`,
output: `
/*
* {
* "foo": 1,
* "bar": 2
* }
*{
* "foo": 1,
* "bar": 2
*}
*/
`,
errors: [
Expand All @@ -809,10 +824,10 @@ ruleTester.run("multiline-comment-style", rule, {
`,
output: `
/*
* {
* \t"foo": 1,
* \t"bar": 2
* }
*{
*\t"foo": 1,
*\t"bar": 2
*}
*/
`,
errors: [
Expand All @@ -834,10 +849,10 @@ ruleTester.run("multiline-comment-style", rule, {
`,
output: `
/*
* {
* \t "foo": 1,
* \t "bar": 2
* }
*{
*\t "foo": 1,
*\t "bar": 2
*}
*/
`,
errors: [
Expand All @@ -859,10 +874,10 @@ ruleTester.run("multiline-comment-style", rule, {
`,
output: `
/*
* {
* "foo": 1,
* "bar": 2
* }
*{
*"foo": 1,
*"bar": 2
*}
*/
`,
errors: [
Expand All @@ -884,10 +899,10 @@ ruleTester.run("multiline-comment-style", rule, {
`,
output: `
\t /*
\t * {
\t * "foo": 1,
\t * "bar": 2
\t * }
\t *{
\t *"foo": 1,
\t *"bar": 2
\t *}
\t */
`,
errors: [
Expand Down Expand Up @@ -1290,8 +1305,8 @@ ${" "}
output: `
/*
* foo
*
* bar
*${" "}
* bar${" "}
*/
`,
options: ["starred-block"],
Expand All @@ -1311,8 +1326,8 @@ ${" "}
output: `
/*
* foo
*
* bar
*${" "}
* bar${" "}
*/
`,
options: ["starred-block"],
Expand Down Expand Up @@ -1353,7 +1368,7 @@ ${" "}
output: `
/*
*foo
*
*${" "}
*bar${" "}
*/
`,
Expand Down

0 comments on commit 3c9007f

Please sign in to comment.