-
-
Notifications
You must be signed in to change notification settings - Fork 681
Add new vue/require-prop-comment
rule
#2019
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
Changes from 22 commits
Commits
Show all changes
28 commits
Select commit
Hold shift + click to select a range
641a483
require-prop-comment
czb3279338858 a6892f7
Merge branch 'master' of https://github.com/czb3279338858/eslint-plug…
czb3279338858 3e8d0e1
add type arg
czb3279338858 f271f48
add jsdoc type
czb3279338858 0fbb5f8
Merge branch 'master' of https://github.com/czb3279338858/eslint-plug…
czb3279338858 07ea38a
edit rule
czb3279338858 3ec9ba0
npm run update运行结果
czb3279338858 763fa82
Merge branch 'master' of https://github.com/czb3279338858/eslint-plug…
czb3279338858 1abad76
npm run update edit
czb3279338858 4b58d8b
Modify according to the requirements during consolidation
czb3279338858 a695a74
edit test
czb3279338858 9545392
delete only one check
czb3279338858 c4dd9f6
delete template
czb3279338858 2549e6b
edit md
czb3279338858 9acb7ab
Merge branch 'master' into master
FloEdelmann 89b4b15
Lint
FloEdelmann 7c08d5d
Improve docs
FloEdelmann 57d9c5f
Only check last preceding comment
FloEdelmann d304e62
Use message IDs
FloEdelmann a2d8144
Rename unlimited → any
FloEdelmann e8070ac
Fix docs
FloEdelmann 4063129
edit rules
czb3279338858 5a517aa
edit schema type
czb3279338858 03f33ff
Merge remote-tracking branch 'upstream/master'
czb3279338858 310fd26
update readme.md
czb3279338858 b4b7e6a
add rules
czb3279338858 1c0bd96
add rules
czb3279338858 0d54c19
Merge branch 'master' of https://github.com/czb3279338858/eslint-plug…
czb3279338858 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,144 @@ | ||
--- | ||
pageClass: rule-details | ||
sidebarDepth: 0 | ||
title: vue/require-prop-comment | ||
description: require props to have a comment | ||
--- | ||
# vue/require-prop-comment | ||
|
||
> require props to have a comment | ||
|
||
- :exclamation: <badge text="This rule has not been released yet." vertical="middle" type="error"> ***This rule has not been released yet.*** </badge> | ||
|
||
## :book: Rule Details | ||
|
||
This rule enforces that every prop has a comment that documents it. | ||
|
||
<eslint-code-block :rules="{'vue/require-prop-comment': ['error']}"> | ||
|
||
```vue | ||
<script> | ||
export default defineComponent({ | ||
props: { | ||
// ✓ GOOD | ||
|
||
/** JSDoc comment */ | ||
a: Number, | ||
|
||
// ✗ BAD | ||
|
||
// line comment | ||
b: Number, | ||
|
||
/* block comment */ | ||
c: Number, | ||
|
||
d: Number, | ||
} | ||
}) | ||
</script> | ||
``` | ||
|
||
</eslint-code-block> | ||
|
||
## :wrench: Options | ||
|
||
```json | ||
{ | ||
"vue/require-prop-comment": ["error", { | ||
"type": "JSDoc" | ||
}] | ||
} | ||
``` | ||
|
||
- `type` ... Type of comment. Default is `"JSDoc"` | ||
- `"JSDoc"` ... Only JSDoc comment are allowed. | ||
- `"line"` ... Only line comment are allowed. | ||
- `"block"` ... Only block comment are allowed. | ||
- `"any"` ... All comment types are allowed. | ||
|
||
### `"type": "block"` | ||
|
||
<eslint-code-block :rules="{'vue/require-prop-comment': ['error', {type: 'block'}]}"> | ||
|
||
```vue | ||
<script setup> | ||
// ✓ GOOD | ||
const goodProps = defineProps({ | ||
/* block comment */ | ||
a: Number, | ||
}) | ||
|
||
// ✗ BAD | ||
const badProps = defineProps({ | ||
/** JSDoc comment */ | ||
b: Number, | ||
|
||
// line comment | ||
c: Number, | ||
|
||
d: Number, | ||
}) | ||
</script> | ||
``` | ||
|
||
</eslint-code-block> | ||
|
||
### `"type": "line"` | ||
|
||
<eslint-code-block :rules="{'vue/require-prop-comment': ['error', {type: 'line'}]}"> | ||
|
||
```vue | ||
<script setup> | ||
// ✓ GOOD | ||
const goodProps = defineProps({ | ||
// line comment | ||
a: Number, | ||
}) | ||
|
||
// ✗ BAD | ||
const badProps = defineProps({ | ||
/** JSDoc comment */ | ||
b: Number, | ||
|
||
/* block comment */ | ||
c: Number, | ||
|
||
d: Number, | ||
}) | ||
</script> | ||
``` | ||
|
||
</eslint-code-block> | ||
|
||
### `"type": "any"` | ||
|
||
<eslint-code-block :rules="{'vue/require-prop-comment': ['error', {type: 'any'}]}"> | ||
|
||
```vue | ||
<script setup> | ||
// ✓ GOOD | ||
const goodProps = defineProps({ | ||
/** JSDoc comment */ | ||
a: Number, | ||
|
||
/* block comment */ | ||
b: Number, | ||
|
||
// line comment | ||
c: Number, | ||
}) | ||
|
||
// ✗ BAD | ||
const badProps = defineProps({ | ||
d: Number, | ||
}) | ||
</script> | ||
``` | ||
|
||
</eslint-code-block> | ||
|
||
## :mag: Implementation | ||
|
||
- [Rule source](https://github.com/vuejs/eslint-plugin-vue/blob/master/lib/rules/require-prop-comment.js) | ||
- [Test source](https://github.com/vuejs/eslint-plugin-vue/blob/master/tests/lib/rules/require-prop-comment.js) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,123 @@ | ||
/** | ||
* @author CZB | ||
* See LICENSE file in root directory for full license. | ||
*/ | ||
'use strict' | ||
|
||
const utils = require('../utils') | ||
|
||
module.exports = { | ||
meta: { | ||
type: 'problem', | ||
czb3279338858 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
docs: { | ||
description: 'require props to have a comment', | ||
categories: undefined, | ||
url: 'https://eslint.vuejs.org/rules/require-prop-comment.html' | ||
}, | ||
fixable: null, | ||
schema: [ | ||
{ | ||
type: 'object', | ||
properties: { | ||
type: { enum: ['JSDoc', 'line', 'block', 'any'] } | ||
}, | ||
additionalProperties: false | ||
} | ||
], | ||
messages: { | ||
requireAnyComment: 'The "{{name}}" property should have a comment.', | ||
requireLineComment: 'The "{{name}}" property should have a line comment.', | ||
requireBlockComment: | ||
'The "{{name}}" property should have a block comment.', | ||
requireJSDocComment: | ||
'The "{{name}}" property should have a JSDoc comment.' | ||
} | ||
}, | ||
/** @param {RuleContext} context */ | ||
create(context) { | ||
/** @type {{type: "JSDoc" | "line" | "block" | "any"}|undefined} */ | ||
czb3279338858 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
const schema = context.options[0] | ||
const type = schema ? schema.type : 'JSDoc' | ||
czb3279338858 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
const sourceCode = context.getSourceCode() | ||
|
||
/** @param {Comment | undefined} comment */ | ||
const verifyBlock = (comment) => | ||
comment && comment.type === 'Block' && comment.value.charAt(0) !== '*' | ||
? undefined | ||
: 'requireBlockComment' | ||
|
||
/** @param {Comment | undefined} comment */ | ||
const verifyLine = (comment) => | ||
comment && comment.type === 'Line' ? undefined : 'requireLineComment' | ||
|
||
/** @param {Comment | undefined} comment */ | ||
const verifyAny = (comment) => (comment ? undefined : 'requireAnyComment') | ||
|
||
/** @param {Comment | undefined} comment */ | ||
const verifyJSDoc = (comment) => | ||
comment && comment.type === 'Block' && comment.value.charAt(0) === '*' | ||
? undefined | ||
: 'requireJSDocComment' | ||
|
||
/** | ||
* @param {import('../utils').ComponentProp[]} props | ||
*/ | ||
function verifyProps(props) { | ||
for (const prop of props) { | ||
if (!prop.propName) { | ||
continue | ||
} | ||
|
||
const precedingComments = sourceCode.getCommentsBefore(prop.node) | ||
const lastPrecedingComment = | ||
precedingComments.length > 0 | ||
? precedingComments[precedingComments.length - 1] | ||
: undefined | ||
|
||
/** @type {string|undefined} */ | ||
let messageId | ||
|
||
switch (type) { | ||
case 'block': | ||
messageId = verifyBlock(lastPrecedingComment) | ||
break | ||
case 'line': | ||
messageId = verifyLine(lastPrecedingComment) | ||
break | ||
case 'any': | ||
messageId = verifyAny(lastPrecedingComment) | ||
break | ||
default: | ||
messageId = verifyJSDoc(lastPrecedingComment) | ||
break | ||
} | ||
|
||
if (!messageId) { | ||
continue | ||
} | ||
|
||
context.report({ | ||
node: prop.node, | ||
messageId, | ||
data: { | ||
name: prop.propName | ||
} | ||
}) | ||
} | ||
} | ||
|
||
return utils.compositingVisitors( | ||
utils.defineScriptSetupVisitor(context, { | ||
onDefinePropsEnter(_node, props) { | ||
verifyProps(props) | ||
} | ||
}), | ||
utils.defineVueVisitor(context, { | ||
onVueObjectEnter(node) { | ||
verifyProps(utils.getComponentPropsFromOptions(node)) | ||
} | ||
}) | ||
) | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.