Skip to content

Commit f271f48

Browse files
committed
add jsdoc type
1 parent 3e8d0e1 commit f271f48

File tree

3 files changed

+77
-19
lines changed

3 files changed

+77
-19
lines changed

docs/rules/require-prop-comment.md

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,8 @@ const props = defineProps({
6767
}]
6868
}
6969
```
70-
- `type` ... Type of comment.Default is `"block"`
70+
- `type` ... Type of comment.Default is `"JSDoc"`
71+
- `"JSDoc"` ... Only JSDoc comment are allowed,one is allowed.
7172
- `"line"` ... Only line comment are allowed,one or more.
7273
- `"block"` ... Only block comment are allowed,one is allowed.
7374
- `"unlimited"` ... There is no limit to the number and type.
@@ -78,7 +79,7 @@ const props = defineProps({
7879
<!-- ✓ GOOD -->
7980
<script setup>
8081
const props = defineProps({
81-
/**
82+
/*
8283
* a comment
8384
*/
8485
a: Number,
@@ -88,10 +89,10 @@ const props = defineProps({
8889
<!-- ✗ BAD -->
8990
<script setup>
9091
const props = defineProps({
91-
/**
92+
/*
9293
* a comment
9394
*/
94-
/**
95+
/*
9596
* a other comment
9697
*/
9798
a: Number,

lib/rules/require-prop-comment.js

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* @author *****your name*****
2+
* @author *****CZB*****
33
* See LICENSE file in root directory for full license.
44
*/
55
'use strict'
@@ -44,11 +44,11 @@ module.exports = {
4444
},
4545
/** @param {RuleContext} context */
4646
create(context) {
47-
/** @type {{type: "line" | "block" | "unlimited"}|undefined} */
47+
/** @type {{type: "line" | "block" | "unlimited" | "JSDoc"}|undefined} */
4848
const schema = context.options[0]
4949

50-
/** @type {"line" | "block" | "unlimited"} */
51-
const type = schema ? schema.type : 'block'
50+
/** @type {"line" | "block" | "unlimited" | "JSDoc"} */
51+
const type = schema ? schema.type : 'JSDoc'
5252

5353
const sourceCode = context.getSourceCode()
5454

@@ -69,6 +69,16 @@ module.exports = {
6969
if (comments.length > 0) return ''
7070
return 'The "{{name}}" property should have a comment.'
7171
}
72+
/** @type {{(comments:Comment[]):string}} */
73+
const verifyJSDoc = (comments) => {
74+
if (
75+
comments.length === 1 &&
76+
comments[0].type === 'Block' &&
77+
/^\*[^*]+/.test(comments[0].value)
78+
)
79+
return ''
80+
return 'The "{{name}}" property should have one JSDoc comment.'
81+
}
7282

7383
/**
7484
* @param {import('../utils').ComponentProp[]} props
@@ -90,6 +100,7 @@ module.exports = {
90100
message = verifyUnlimited(beforeComments)
91101
break
92102
default:
103+
message = verifyJSDoc(beforeComments)
93104
break
94105
}
95106

tests/lib/rules/require-prop-comment.js

Lines changed: 57 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* @author *****your name*****
2+
* @author *****CZB*****
33
* See LICENSE file in root directory for full license.
44
*/
55
'use strict'
@@ -43,10 +43,14 @@ tester.run('require-prop-comment', rule, {
4343
</template>
4444
<script setup>
4545
const props = defineProps({
46-
/**
46+
/*
4747
* a comment
4848
*/
49-
a: Number
49+
a: Number,
50+
/**
51+
* b comment
52+
*/
53+
b: Number
5054
})
5155
</script>
5256
`,
@@ -60,9 +64,6 @@ tester.run('require-prop-comment', rule, {
6064
// a comment
6165
// a other comment
6266
a: Number
63-
},
64-
render() {
65-
return <div>1</div>
6667
}
6768
})
6869
`,
@@ -79,9 +80,6 @@ tester.run('require-prop-comment', rule, {
7980
a: Number,
8081
// a comment
8182
b: Number
82-
},
83-
render() {
84-
return <div>1</div>
8583
}
8684
})
8785
`,
@@ -107,10 +105,54 @@ tester.run('require-prop-comment', rule, {
107105
* b other comment
108106
*/
109107
b: Number,
108+
/*
109+
* c comment
110+
*/
111+
c: Number
110112
}
111113
})
112114
</script>
113115
`,
116+
errors: [
117+
{
118+
line: 10,
119+
message: 'The "a" property should have one JSDoc comment.'
120+
},
121+
{
122+
line: 17,
123+
message: 'The "b" property should have one JSDoc comment.'
124+
},
125+
{
126+
line: 21,
127+
message: 'The "c" property should have one JSDoc comment.'
128+
}
129+
]
130+
},
131+
{
132+
code: `
133+
<template>
134+
<div>1</div>
135+
</template>
136+
<script>
137+
import { defineComponent } from '@vue/composition-api'
138+
139+
export default defineComponent({
140+
props: {
141+
a: Number,
142+
/*
143+
* b comment
144+
*/
145+
/*
146+
* b other comment
147+
*/
148+
b: Number,
149+
// c comment
150+
c: Number
151+
}
152+
})
153+
</script>
154+
`,
155+
options: [{ type: 'block' }],
114156
errors: [
115157
{
116158
line: 10,
@@ -119,6 +161,10 @@ tester.run('require-prop-comment', rule, {
119161
{
120162
line: 17,
121163
message: 'The "b" property should have one block comment.'
164+
},
165+
{
166+
line: 19,
167+
message: 'The "c" property should have one block comment.'
122168
}
123169
]
124170
},
@@ -193,7 +239,7 @@ tester.run('require-prop-comment', rule, {
193239
errors: [
194240
{
195241
line: 7,
196-
message: 'The "a" property should have one block comment.'
242+
message: 'The "a" property should have one JSDoc comment.'
197243
}
198244
]
199245
},
@@ -209,7 +255,7 @@ tester.run('require-prop-comment', rule, {
209255
errors: [
210256
{
211257
line: 5,
212-
message: 'The "a" property should have one block comment.'
258+
message: 'The "a" property should have one JSDoc comment.'
213259
}
214260
]
215261
}

0 commit comments

Comments
 (0)