Skip to content

Extract JSDoc comment recognition into util functions #2026

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

Merged
merged 1 commit into from
Nov 2, 2022
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 2 additions & 5 deletions lib/rules/no-unused-properties.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -169,11 +170,7 @@ function findJSDocComment(node, sourceCode) {
break
}

if (
tokenBefore &&
tokenBefore.type === 'Block' &&
tokenBefore.value.charAt(0) === '*'
) {
if (tokenBefore && isJSDocComment(tokenBefore)) {
return tokenBefore
}

Expand Down
9 changes: 3 additions & 6 deletions lib/rules/require-prop-comment.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
'use strict'

const utils = require('../utils')
const { isBlockComment, isJSDocComment } = require('../utils/comments.js')

module.exports = {
meta: {
Expand Down Expand Up @@ -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) =>
Expand All @@ -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
Expand Down
21 changes: 21 additions & 0 deletions lib/utils/comments.js
Original file line number Diff line number Diff line change
@@ -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
}
67 changes: 67 additions & 0 deletions tests/lib/utils/comments.js
Original file line number Diff line number Diff line change
@@ -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)
})
})