From 641a48344aafe30921e06e054d81412ab612c57f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=94=A1=E6=9C=9D=E5=8B=83?= <3279338858@qq.com> Date: Wed, 28 Sep 2022 14:18:27 +0800 Subject: [PATCH 01/22] require-prop-comment --- docs/rules/README.md | 1 + docs/rules/require-prop-comment.md | 68 ++++++++ lib/index.js | 1 + lib/rules/require-prop-comment.js | 92 +++++++++++ tests/lib/rules/require-prop-comment.js | 200 ++++++++++++++++++++++++ 5 files changed, 362 insertions(+) create mode 100644 docs/rules/require-prop-comment.md create mode 100644 lib/rules/require-prop-comment.js create mode 100644 tests/lib/rules/require-prop-comment.js diff --git a/docs/rules/README.md b/docs/rules/README.md index 780b10cbe..b35c2b8ff 100644 --- a/docs/rules/README.md +++ b/docs/rules/README.md @@ -260,6 +260,7 @@ For example: | [vue/require-emit-validator](./require-emit-validator.md) | require type definitions in emits | :bulb: | :hammer: | | [vue/require-expose](./require-expose.md) | require declare public properties using `expose` | :bulb: | :hammer: | | [vue/require-name-property](./require-name-property.md) | require a name property in Vue components | | :hammer: | +| [vue/require-prop-comment](./require-prop-comment.md) | require prop should have a comment | | :warning: | | [vue/script-indent](./script-indent.md) | enforce consistent indentation in ` +``` + + + + + +```vue + + + +``` + + + +## :wrench: Options + +Nothing. + +## :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) diff --git a/lib/index.js b/lib/index.js index e7dcd5bec..7578f7740 100644 --- a/lib/index.js +++ b/lib/index.js @@ -175,6 +175,7 @@ module.exports = { 'require-explicit-emits': require('./rules/require-explicit-emits'), 'require-expose': require('./rules/require-expose'), 'require-name-property': require('./rules/require-name-property'), + 'require-prop-comment': require('./rules/require-prop-comment'), 'require-prop-type-constructor': require('./rules/require-prop-type-constructor'), 'require-prop-types': require('./rules/require-prop-types'), 'require-render-return': require('./rules/require-render-return'), diff --git a/lib/rules/require-prop-comment.js b/lib/rules/require-prop-comment.js new file mode 100644 index 000000000..cfb5b3cdb --- /dev/null +++ b/lib/rules/require-prop-comment.js @@ -0,0 +1,92 @@ +/** + * @author *****your name***** + * See LICENSE file in root directory for full license. + */ +'use strict' + +// ------------------------------------------------------------------------------ +// Requirements +// ------------------------------------------------------------------------------ + +const utils = require('../utils') + +// ------------------------------------------------------------------------------ +// Helpers +// ------------------------------------------------------------------------------ + +// ... + +// ------------------------------------------------------------------------------ +// Rule Definition +// ------------------------------------------------------------------------------ + +module.exports = { + meta: { + type: 'problem', + docs: { + description: 'require prop should have a comment', + categories: undefined, + url: 'https://eslint.vuejs.org/rules/require-prop-comment.html' + }, + fixable: null, + schema: [], + messages: { + // ... + } + }, + /** @param {RuleContext} context */ + create(context) { + const sourceCode = context.getSourceCode() + + /** + * @param {import('../utils').ComponentProp[]} props + */ + function verifyProps(props) { + for (const prop of props) { + if (!prop.propName) { + continue + } + const beforeComments = sourceCode.getCommentsBefore(prop.node) + + if (beforeComments.length > 0) continue + context.report({ + node: prop.node, + message: 'The "{{name}}" property should have a comment.', + data: { + name: prop.propName + } + }) + } + } + // 合成选择器 + return utils.compositingVisitors( + // 遍历 + ` + }, + { + code: ` + + + ` + }, + { + code: ` + import { defineComponent } from '@vue/composition-api' + export const ComponentName = defineComponent({ + props: { + /** + * a comment + */ + a: Number + }, + render() { + return
1
+ } + }) + ` + } + ], + invalid: [ + { + code: ` + + + `, + errors: [ + { + line: 10 + } + ] + }, + { + code: ` + + + `, + errors: [ + { + line: 14 + } + ] + }, + { + code: ` + + + `, + errors: [ + { + line: 7 + } + ] + }, + { + code: ` + + + `, + errors: [ + { + line: 7 + } + ] + }, + { + code: ` + import { defineComponent } from '@vue/composition-api' + export const ComponentName = defineComponent({ + props: { + a: Number + }, + render() { + return
1
+ } + }) + `, + errors: [ + { + line: 5 + } + ] + }, + { + code: ` + import Vue from 'vue' + new Vue({ + props: { + a: Number + } + }) + `, + errors: [ + { + line: 5 + } + ] + } + ] +}) From 3e8d0e1e16c5d14d72af31ce4b27e90459647e33 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=94=A1=E6=9C=9D=E5=8B=83?= <3279338858@qq.com> Date: Wed, 19 Oct 2022 16:26:10 +0800 Subject: [PATCH 02/22] add type arg --- docs/rules/README.md | 2 +- docs/rules/require-prop-comment.md | 121 +++++++++++++++++++++++- lib/rules/require-prop-comment.js | 77 ++++++++++----- tests/lib/rules/require-prop-comment.js | 95 +++++++++++-------- 4 files changed, 225 insertions(+), 70 deletions(-) diff --git a/docs/rules/README.md b/docs/rules/README.md index b35c2b8ff..269e9c1fa 100644 --- a/docs/rules/README.md +++ b/docs/rules/README.md @@ -260,7 +260,7 @@ For example: | [vue/require-emit-validator](./require-emit-validator.md) | require type definitions in emits | :bulb: | :hammer: | | [vue/require-expose](./require-expose.md) | require declare public properties using `expose` | :bulb: | :hammer: | | [vue/require-name-property](./require-name-property.md) | require a name property in Vue components | | :hammer: | -| [vue/require-prop-comment](./require-prop-comment.md) | require prop should have a comment | | :warning: | +| [vue/require-prop-comment](./require-prop-comment.md) | require that props have a comment | | :warning: | | [vue/script-indent](./script-indent.md) | enforce consistent indentation in ` ``` @@ -60,7 +60,118 @@ const props = defineProps({ ## :wrench: Options -Nothing. +```json +{ + "vue/require-prop-comment": ["error", { + "type": "block" + }] +} +``` +- `type` ... Type of comment.Default is `"block"` + - `"line"` ... Only line comment are allowed,one or more. + - `"block"` ... Only block comment are allowed,one is allowed. + - `"unlimited"` ... There is no limit to the number and type. + +### `"type":"block"` + + + + + + + + + + + + + +### `"type":"line"` + + + + + + + + + + + + + +### `"type":"unlimited"` + + + + + + + + + + + + ## :mag: Implementation diff --git a/lib/rules/require-prop-comment.js b/lib/rules/require-prop-comment.js index cfb5b3cdb..8c5c32568 100644 --- a/lib/rules/require-prop-comment.js +++ b/lib/rules/require-prop-comment.js @@ -24,69 +24,96 @@ module.exports = { meta: { type: 'problem', docs: { - description: 'require prop should have a comment', + description: 'require that props have a comment', categories: undefined, url: 'https://eslint.vuejs.org/rules/require-prop-comment.html' }, fixable: null, - schema: [], + schema: [ + { + type: 'object', + properties: { + type: { enum: ['line', 'block', 'unlimited'] } + }, + additionalProperties: false + } + ], messages: { // ... } }, /** @param {RuleContext} context */ create(context) { + /** @type {{type: "line" | "block" | "unlimited"}|undefined} */ + const schema = context.options[0] + + /** @type {"line" | "block" | "unlimited"} */ + const type = schema ? schema.type : 'block' + const sourceCode = context.getSourceCode() + /** @type {{(comments:Comment[]):string}} */ + const verifyBlock = (comments) => { + if (comments.length === 1 && comments[0].type === 'Block') return '' + return 'The "{{name}}" property should have one block comment.' + } + /** @type {{(comments:Comment[]):string}} */ + const verifyLine = (comments) => { + if (comments.length > 0 && comments.every((c) => c.type === 'Line')) { + return '' + } + return 'The "{{name}}" property should have a line comment.' + } + /** @type {{(comments:Comment[]):string}} */ + const verifyUnlimited = (comments) => { + if (comments.length > 0) return '' + return 'The "{{name}}" property should have a comment.' + } + /** * @param {import('../utils').ComponentProp[]} props */ function verifyProps(props) { for (const prop of props) { - if (!prop.propName) { - continue - } + if (!prop.propName) continue + const beforeComments = sourceCode.getCommentsBefore(prop.node) + let message = '' + switch (type) { + case 'block': + message = verifyBlock(beforeComments) + break + case 'line': + message = verifyLine(beforeComments) + break + case 'unlimited': + message = verifyUnlimited(beforeComments) + break + default: + break + } - if (beforeComments.length > 0) continue + if (!message) continue context.report({ node: prop.node, - message: 'The "{{name}}" property should have a comment.', + message, data: { name: prop.propName } }) } } - // 合成选择器 return utils.compositingVisitors( - // 遍历 @@ -54,7 +49,24 @@ tester.run('require-prop-comment', rule, { a: Number }) - ` + `, + options: [{ type: 'block' }] + }, + { + code: ` + import { defineComponent } from '@vue/composition-api' + export const ComponentName = defineComponent({ + props: { + // a comment + // a other comment + a: Number + }, + render() { + return
1
+ } + }) + `, + options: [{ type: 'line' }] }, { code: ` @@ -64,13 +76,16 @@ tester.run('require-prop-comment', rule, { /** * a comment */ - a: Number + a: Number, + // a comment + b: Number }, render() { return
1
} }) - ` + `, + options: [{ type: 'unlimited' }] } ], invalid: [ @@ -85,18 +100,25 @@ tester.run('require-prop-comment', rule, { export default defineComponent({ props: { a: Number, - // b comment + /** + * b comment + */ + /** + * b other comment + */ b: Number, - // c - // comment - c: Number } }) `, errors: [ { - line: 10 + line: 10, + message: 'The "a" property should have one block comment.' + }, + { + line: 17, + message: 'The "b" property should have one block comment.' } ] }, @@ -115,17 +137,20 @@ tester.run('require-prop-comment', rule, { */ a: Number, b: Number, - // c - // comment - c: Number }, setup() {} }) `, + options: [{ type: 'line' }], errors: [ { - line: 14 + line: 12, + message: 'The "a" property should have a line comment.' + }, + { + line: 13, + message: 'The "b" property should have a line comment.' } ] }, @@ -136,13 +161,21 @@ tester.run('require-prop-comment', rule, { `, + options: [{ type: 'unlimited' }], errors: [ { - line: 7 + line: 7, + message: `The "a" property should have a comment.` } ] }, @@ -159,25 +192,8 @@ tester.run('require-prop-comment', rule, { `, errors: [ { - line: 7 - } - ] - }, - { - code: ` - import { defineComponent } from '@vue/composition-api' - export const ComponentName = defineComponent({ - props: { - a: Number - }, - render() { - return
1
- } - }) - `, - errors: [ - { - line: 5 + line: 7, + message: 'The "a" property should have one block comment.' } ] }, @@ -192,7 +208,8 @@ tester.run('require-prop-comment', rule, { `, errors: [ { - line: 5 + line: 5, + message: 'The "a" property should have one block comment.' } ] } From f271f4816e95056a8fed6b8a27906bee191355c6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=94=A1=E6=9C=9D=E5=8B=83?= <3279338858@qq.com> Date: Thu, 20 Oct 2022 10:11:21 +0800 Subject: [PATCH 03/22] add jsdoc type --- docs/rules/require-prop-comment.md | 9 ++-- lib/rules/require-prop-comment.js | 19 +++++-- tests/lib/rules/require-prop-comment.js | 68 +++++++++++++++++++++---- 3 files changed, 77 insertions(+), 19 deletions(-) diff --git a/docs/rules/require-prop-comment.md b/docs/rules/require-prop-comment.md index 6d481bf13..4dd30ddc4 100644 --- a/docs/rules/require-prop-comment.md +++ b/docs/rules/require-prop-comment.md @@ -67,7 +67,8 @@ const props = defineProps({ }] } ``` -- `type` ... Type of comment.Default is `"block"` +- `type` ... Type of comment.Default is `"JSDoc"` + - `"JSDoc"` ... Only JSDoc comment are allowed,one is allowed. - `"line"` ... Only line comment are allowed,one or more. - `"block"` ... Only block comment are allowed,one is allowed. - `"unlimited"` ... There is no limit to the number and type. @@ -78,7 +79,7 @@ const props = defineProps({ `, @@ -60,9 +64,6 @@ tester.run('require-prop-comment', rule, { // a comment // a other comment a: Number - }, - render() { - return
1
} }) `, @@ -79,9 +80,6 @@ tester.run('require-prop-comment', rule, { a: Number, // a comment b: Number - }, - render() { - return
1
} }) `, @@ -107,10 +105,54 @@ tester.run('require-prop-comment', rule, { * b other comment */ b: Number, + /* + * c comment + */ + c: Number } }) `, + errors: [ + { + line: 10, + message: 'The "a" property should have one JSDoc comment.' + }, + { + line: 17, + message: 'The "b" property should have one JSDoc comment.' + }, + { + line: 21, + message: 'The "c" property should have one JSDoc comment.' + } + ] + }, + { + code: ` + + + `, + options: [{ type: 'block' }], errors: [ { line: 10, @@ -119,6 +161,10 @@ tester.run('require-prop-comment', rule, { { line: 17, message: 'The "b" property should have one block comment.' + }, + { + line: 19, + message: 'The "c" property should have one block comment.' } ] }, @@ -193,7 +239,7 @@ tester.run('require-prop-comment', rule, { errors: [ { line: 7, - message: 'The "a" property should have one block comment.' + message: 'The "a" property should have one JSDoc comment.' } ] }, @@ -209,7 +255,7 @@ tester.run('require-prop-comment', rule, { errors: [ { line: 5, - message: 'The "a" property should have one block comment.' + message: 'The "a" property should have one JSDoc comment.' } ] } From 07ea38a04e06a2456a12195ef1e5609bab3ad02a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=94=A1=E6=9C=9D=E5=8B=83?= <3279338858@qq.com> Date: Thu, 20 Oct 2022 14:37:33 +0800 Subject: [PATCH 04/22] edit rule --- tests/lib/rules/require-prop-comment.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/lib/rules/require-prop-comment.js b/tests/lib/rules/require-prop-comment.js index a729669dc..ce8f036db 100644 --- a/tests/lib/rules/require-prop-comment.js +++ b/tests/lib/rules/require-prop-comment.js @@ -191,11 +191,11 @@ tester.run('require-prop-comment', rule, { options: [{ type: 'line' }], errors: [ { - line: 12, + line: 13, message: 'The "a" property should have a line comment.' }, { - line: 13, + line: 14, message: 'The "b" property should have a line comment.' } ] From 3ec9ba0ec42ef2a2d8302f60325da8682e6d7d36 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=94=A1=E6=9C=9D=E5=8B=83?= <3279338858@qq.com> Date: Thu, 20 Oct 2022 14:46:12 +0800 Subject: [PATCH 05/22] =?UTF-8?q?npm=20run=20update=E8=BF=90=E8=A1=8C?= =?UTF-8?q?=E7=BB=93=E6=9E=9C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- docs/rules/README.md | 68 +++++++++++++-------------- docs/rules/array-bracket-newline.md | 4 +- docs/rules/array-bracket-spacing.md | 4 +- docs/rules/arrow-spacing.md | 4 +- docs/rules/block-spacing.md | 4 +- docs/rules/brace-style.md | 4 +- docs/rules/camelcase.md | 4 +- docs/rules/comma-dangle.md | 4 +- docs/rules/comma-spacing.md | 4 +- docs/rules/comma-style.md | 4 +- docs/rules/dot-location.md | 4 +- docs/rules/dot-notation.md | 4 +- docs/rules/eqeqeq.md | 4 +- docs/rules/func-call-spacing.md | 4 +- docs/rules/key-spacing.md | 4 +- docs/rules/keyword-spacing.md | 4 +- docs/rules/multiline-ternary.md | 4 +- docs/rules/no-constant-condition.md | 4 +- docs/rules/no-empty-pattern.md | 4 +- docs/rules/no-extra-parens.md | 4 +- docs/rules/no-loss-of-precision.md | 4 +- docs/rules/no-restricted-syntax.md | 4 +- docs/rules/no-sparse-arrays.md | 4 +- docs/rules/no-useless-concat.md | 4 +- docs/rules/object-curly-newline.md | 4 +- docs/rules/object-curly-spacing.md | 4 +- docs/rules/object-property-newline.md | 4 +- docs/rules/object-shorthand.md | 4 +- docs/rules/operator-linebreak.md | 4 +- docs/rules/prefer-template.md | 4 +- docs/rules/quote-props.md | 4 +- docs/rules/space-in-parens.md | 4 +- docs/rules/space-infix-ops.md | 4 +- docs/rules/space-unary-ops.md | 4 +- docs/rules/template-curly-spacing.md | 4 +- 35 files changed, 102 insertions(+), 102 deletions(-) diff --git a/docs/rules/README.md b/docs/rules/README.md index 6ebf47c9e..5f11c9370 100644 --- a/docs/rules/README.md +++ b/docs/rules/README.md @@ -277,42 +277,42 @@ The following rules extend the rules provided by ESLint itself and apply them to | Rule ID | Description | | | |:--------|:------------|:--:|:--:| -| [vue/array-bracket-newline](./array-bracket-newline.md) | enforce linebreaks after opening and before closing array brackets in `