Skip to content

Commit 73ebb8e

Browse files
committed
Extract JSDoc comment recognition into util functions
and add tests for them
1 parent 1c0bd96 commit 73ebb8e

File tree

4 files changed

+93
-11
lines changed

4 files changed

+93
-11
lines changed

lib/rules/no-unused-properties.js

+2-5
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
const utils = require('../utils')
88
const eslintUtils = require('eslint-utils')
9+
const { isJSDocComment } = require('../utils/comments.js')
910
const { getStyleVariablesContext } = require('../utils/style-variables')
1011
const {
1112
definePropertyReferenceExtractor,
@@ -169,11 +170,7 @@ function findJSDocComment(node, sourceCode) {
169170
break
170171
}
171172

172-
if (
173-
tokenBefore &&
174-
tokenBefore.type === 'Block' &&
175-
tokenBefore.value.charAt(0) === '*'
176-
) {
173+
if (tokenBefore && isJSDocComment(tokenBefore)) {
177174
return tokenBefore
178175
}
179176

lib/rules/require-prop-comment.js

+3-6
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
'use strict'
66

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

910
module.exports = {
1011
meta: {
@@ -43,9 +44,7 @@ module.exports = {
4344

4445
/** @param {Comment | undefined} comment */
4546
const verifyBlock = (comment) =>
46-
comment && comment.type === 'Block' && comment.value.charAt(0) !== '*'
47-
? undefined
48-
: 'requireBlockComment'
47+
comment && isBlockComment(comment) ? undefined : 'requireBlockComment'
4948

5049
/** @param {Comment | undefined} comment */
5150
const verifyLine = (comment) =>
@@ -56,9 +55,7 @@ module.exports = {
5655

5756
/** @param {Comment | undefined} comment */
5857
const verifyJSDoc = (comment) =>
59-
comment && comment.type === 'Block' && comment.value.charAt(0) === '*'
60-
? undefined
61-
: 'requireJSDocComment'
58+
comment && isJSDocComment(comment) ? undefined : 'requireJSDocComment'
6259

6360
/**
6461
* @param {import('../utils').ComponentProp[]} props

lib/utils/comments.js

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/**
2+
* @param {Comment} node
3+
* @returns {boolean}
4+
*/
5+
const isJSDocComment = (node) =>
6+
node.type === 'Block' &&
7+
node.value.charAt(0) === '*' &&
8+
node.value.charAt(1) !== '*'
9+
10+
/**
11+
* @param {Comment} node
12+
* @returns {boolean}
13+
*/
14+
const isBlockComment = (node) =>
15+
node.type === 'Block' &&
16+
(node.value.charAt(0) !== '*' || node.value.charAt(1) === '*')
17+
18+
module.exports = {
19+
isJSDocComment,
20+
isBlockComment
21+
}

tests/lib/utils/comments.js

+67
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
'use strict'
2+
3+
const assert = require('assert')
4+
const {
5+
isBlockComment,
6+
isJSDocComment
7+
} = require('../../../lib/utils/comments.js')
8+
9+
// //foo
10+
const lineCommentNode = {
11+
type: 'Line',
12+
value: 'foo'
13+
}
14+
15+
// /*foo*/
16+
const blockCommentNodeWithoutAsterisks = {
17+
type: 'Block',
18+
value: 'foo'
19+
}
20+
21+
// //** foo */
22+
const blockCommentNodeWithOneAsterisk = {
23+
type: 'Block',
24+
value: '* foo'
25+
}
26+
27+
// /*** foo */
28+
const blockCommentNodeWithTwoAsterisks = {
29+
type: 'Block',
30+
value: '** foo'
31+
}
32+
33+
describe('isJSDocComment()', () => {
34+
it('returns true for JSDoc comments', () => {
35+
assert.equal(isJSDocComment(blockCommentNodeWithOneAsterisk), true)
36+
})
37+
38+
it('returns false for block comments', () => {
39+
assert.equal(isJSDocComment(blockCommentNodeWithoutAsterisks), false)
40+
})
41+
42+
it('returns false for line comments', () => {
43+
assert.equal(isJSDocComment(lineCommentNode), false)
44+
})
45+
46+
it('returns false for block comments with two asterisks', () => {
47+
assert.equal(isJSDocComment(blockCommentNodeWithTwoAsterisks), false)
48+
})
49+
})
50+
51+
describe('isBlockComment()', () => {
52+
it('returns false for JSDoc comments', () => {
53+
assert.equal(isBlockComment(blockCommentNodeWithOneAsterisk), false)
54+
})
55+
56+
it('returns true for block comments', () => {
57+
assert.equal(isBlockComment(blockCommentNodeWithoutAsterisks), true)
58+
})
59+
60+
it('returns false for line comments', () => {
61+
assert.equal(isBlockComment(lineCommentNode), false)
62+
})
63+
64+
it('returns true for block comments with two asterisks', () => {
65+
assert.equal(isBlockComment(blockCommentNodeWithTwoAsterisks), true)
66+
})
67+
})

0 commit comments

Comments
 (0)