Skip to content

Commit

Permalink
Add support for allowJsDoc parameter (defaults: true) for "multiline-…
Browse files Browse the repository at this point in the history
…comment-style" rule
  • Loading branch information
laurent22 committed Jan 22, 2023
1 parent 3be0748 commit 3dde96a
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 3 deletions.
29 changes: 26 additions & 3 deletions lib/rules/multiline-comment-style.js
Expand Up @@ -22,7 +22,21 @@ module.exports = {
},

fixable: "whitespace",
schema: [{ enum: ["starred-block", "separate-lines", "bare-block"] }],
schema: [
{
enum: ["starred-block", "separate-lines", "bare-block"]
},
{
type: "object",
properties: {
allowJsDoc: {
type: "boolean",
default: true
}
},
additionalProperties: false
}
],
messages: {
expectedBlock: "Expected a block comment instead of consecutive line comments.",
expectedBareBlock: "Expected a block comment without padding stars.",
Expand All @@ -37,6 +51,8 @@ module.exports = {
create(context) {
const sourceCode = context.getSourceCode();
const option = context.options[0] || "starred-block";
const params = context.options[1] || {};
const allowJsDoc = "allowJsDoc" in params ? params.allowJsDoc : true;

//----------------------------------------------------------------------
// Helpers
Expand Down Expand Up @@ -333,11 +349,18 @@ module.exports = {
"separate-lines"(commentGroup) {
const [firstComment] = commentGroup;

if (firstComment.type !== "Block" || isJSDocComment(commentGroup)) {
const isJSDoc = isJSDocComment(commentGroup);

if (firstComment.type !== "Block" || (allowJsDoc && isJSDoc)) {
return;
}

const commentLines = getCommentLines(commentGroup);
let commentLines = getCommentLines(commentGroup);

if (!allowJsDoc && isJSDoc) {
commentLines = commentLines.slice(1, commentLines.length - 1);
}

const tokenAfter = sourceCode.getTokenAfter(firstComment, { includeComments: true });

if (tokenAfter && firstComment.loc.end.line === tokenAfter.loc.start.line) {
Expand Down
14 changes: 14 additions & 0 deletions tests/lib/rules/multiline-comment-style.js
Expand Up @@ -628,6 +628,20 @@ ruleTester.run("multiline-comment-style", rule, {
options: ["separate-lines"],
errors: [{ messageId: "expectedLines", line: 2 }]
},
{
code: `
/**
* JSDoc
* Comment
*/
`,
output: `
// JSDoc
// Comment
`,
options: ["separate-lines", { allowJsDoc: false }],
errors: [{ messageId: "expectedLines", line: 2 }]
},
{
code: `
/* foo
Expand Down

0 comments on commit 3dde96a

Please sign in to comment.