From 73ebb8ef34a50fe6b1f313fb4390742ee7f1756c Mon Sep 17 00:00:00 2001 From: Flo Edelmann Date: Tue, 1 Nov 2022 16:34:48 +0100 Subject: [PATCH] Extract JSDoc comment recognition into util functions and add tests for them --- lib/rules/no-unused-properties.js | 7 +--- lib/rules/require-prop-comment.js | 9 ++--- lib/utils/comments.js | 21 ++++++++++ tests/lib/utils/comments.js | 67 +++++++++++++++++++++++++++++++ 4 files changed, 93 insertions(+), 11 deletions(-) create mode 100644 lib/utils/comments.js create mode 100644 tests/lib/utils/comments.js diff --git a/lib/rules/no-unused-properties.js b/lib/rules/no-unused-properties.js index 0a345542e..040ee414c 100644 --- a/lib/rules/no-unused-properties.js +++ b/lib/rules/no-unused-properties.js @@ -6,6 +6,7 @@ const utils = require('../utils') const eslintUtils = require('eslint-utils') +const { isJSDocComment } = require('../utils/comments.js') const { getStyleVariablesContext } = require('../utils/style-variables') const { definePropertyReferenceExtractor, @@ -169,11 +170,7 @@ function findJSDocComment(node, sourceCode) { break } - if ( - tokenBefore && - tokenBefore.type === 'Block' && - tokenBefore.value.charAt(0) === '*' - ) { + if (tokenBefore && isJSDocComment(tokenBefore)) { return tokenBefore } diff --git a/lib/rules/require-prop-comment.js b/lib/rules/require-prop-comment.js index 9c8ef994d..8dd852cbc 100644 --- a/lib/rules/require-prop-comment.js +++ b/lib/rules/require-prop-comment.js @@ -5,6 +5,7 @@ 'use strict' const utils = require('../utils') +const { isBlockComment, isJSDocComment } = require('../utils/comments.js') module.exports = { meta: { @@ -43,9 +44,7 @@ module.exports = { /** @param {Comment | undefined} comment */ const verifyBlock = (comment) => - comment && comment.type === 'Block' && comment.value.charAt(0) !== '*' - ? undefined - : 'requireBlockComment' + comment && isBlockComment(comment) ? undefined : 'requireBlockComment' /** @param {Comment | undefined} comment */ const verifyLine = (comment) => @@ -56,9 +55,7 @@ module.exports = { /** @param {Comment | undefined} comment */ const verifyJSDoc = (comment) => - comment && comment.type === 'Block' && comment.value.charAt(0) === '*' - ? undefined - : 'requireJSDocComment' + comment && isJSDocComment(comment) ? undefined : 'requireJSDocComment' /** * @param {import('../utils').ComponentProp[]} props diff --git a/lib/utils/comments.js b/lib/utils/comments.js new file mode 100644 index 000000000..d285e7cac --- /dev/null +++ b/lib/utils/comments.js @@ -0,0 +1,21 @@ +/** + * @param {Comment} node + * @returns {boolean} + */ +const isJSDocComment = (node) => + node.type === 'Block' && + node.value.charAt(0) === '*' && + node.value.charAt(1) !== '*' + +/** + * @param {Comment} node + * @returns {boolean} + */ +const isBlockComment = (node) => + node.type === 'Block' && + (node.value.charAt(0) !== '*' || node.value.charAt(1) === '*') + +module.exports = { + isJSDocComment, + isBlockComment +} diff --git a/tests/lib/utils/comments.js b/tests/lib/utils/comments.js new file mode 100644 index 000000000..9e567f6b1 --- /dev/null +++ b/tests/lib/utils/comments.js @@ -0,0 +1,67 @@ +'use strict' + +const assert = require('assert') +const { + isBlockComment, + isJSDocComment +} = require('../../../lib/utils/comments.js') + +// //foo +const lineCommentNode = { + type: 'Line', + value: 'foo' +} + +// /*foo*/ +const blockCommentNodeWithoutAsterisks = { + type: 'Block', + value: 'foo' +} + +// //** foo */ +const blockCommentNodeWithOneAsterisk = { + type: 'Block', + value: '* foo' +} + +// /*** foo */ +const blockCommentNodeWithTwoAsterisks = { + type: 'Block', + value: '** foo' +} + +describe('isJSDocComment()', () => { + it('returns true for JSDoc comments', () => { + assert.equal(isJSDocComment(blockCommentNodeWithOneAsterisk), true) + }) + + it('returns false for block comments', () => { + assert.equal(isJSDocComment(blockCommentNodeWithoutAsterisks), false) + }) + + it('returns false for line comments', () => { + assert.equal(isJSDocComment(lineCommentNode), false) + }) + + it('returns false for block comments with two asterisks', () => { + assert.equal(isJSDocComment(blockCommentNodeWithTwoAsterisks), false) + }) +}) + +describe('isBlockComment()', () => { + it('returns false for JSDoc comments', () => { + assert.equal(isBlockComment(blockCommentNodeWithOneAsterisk), false) + }) + + it('returns true for block comments', () => { + assert.equal(isBlockComment(blockCommentNodeWithoutAsterisks), true) + }) + + it('returns false for line comments', () => { + assert.equal(isBlockComment(lineCommentNode), false) + }) + + it('returns true for block comments with two asterisks', () => { + assert.equal(isBlockComment(blockCommentNodeWithTwoAsterisks), true) + }) +})