Skip to content
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

implements proposed max-props rule #2430

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/rules/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,7 @@ For example:
| [vue/match-component-file-name](./match-component-file-name.md) | require component name property to match its file name | :bulb: | :hammer: |
| [vue/match-component-import-name](./match-component-import-name.md) | require the registered component name to match the imported component name | | :warning: |
| [vue/max-lines-per-block](./max-lines-per-block.md) | enforce maximum number of lines in Vue SFC blocks | | :warning: |
| [vue/max-props](./max-props.md) | enforce maximum number of props in Vue component | | :warning: |
| [vue/new-line-between-multi-line-property](./new-line-between-multi-line-property.md) | enforce new lines between multi-line properties in Vue components | :wrench: | :lipstick: |
| [vue/next-tick-style](./next-tick-style.md) | enforce Promise or callback style in `nextTick` | :wrench: | :hammer: |
| [vue/no-bare-strings-in-template](./no-bare-strings-in-template.md) | disallow the use of bare strings in `<template>` | | :hammer: |
Expand Down
66 changes: 66 additions & 0 deletions docs/rules/max-props.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
---
pageClass: rule-details
sidebarDepth: 0
title: vue/max-props
description: enforce maximum number of props in Vue component
---

# vue/max-props

> enforce maximum number of props in Vue component

- :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 a maximum number of props in a Vue SFC, in order to aid in maintainability and reduce complexity.


## :wrench: Options
This rule takes an object, where you can specify the maximum number of props allowed in a Vue SFC.
There is one property that can be specified for the object.

- `maxProps` ... Specify the maximum number of props in the `script` block.

### `{ maxProps: 1 }`

<eslint-code-block :rules="{'vue/max-props': ['error', { maxProps: 1 }]}">

```vue
<!-- ✗ BAD -->
<template>
</template>

<script setup>
defineProps({
prop1: String,
prop2: String,
})
</script>
```

</eslint-code-block>


### `{ maxProps: 5 }`

<eslint-code-block :rules="{'vue/max-props': ['error', { maxProps: 5 }]}">

```vue
<!-- ✓ GOOD -->
<script>
defineProps({
prop1: String
})
</script>
```

</eslint-code-block>


Nothing.

## :mag: Implementation

- [Rule source](https://github.com/vuejs/eslint-plugin-vue/blob/master/lib/rules/max-props.js)
- [Test source](https://github.com/vuejs/eslint-plugin-vue/blob/master/tests/lib/rules/max-props.js)
1 change: 1 addition & 0 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@
'max-attributes-per-line': require('./rules/max-attributes-per-line'),
'max-len': require('./rules/max-len'),
'max-lines-per-block': require('./rules/max-lines-per-block'),
'max-props': require('./rules/max-props'),
'multi-word-component-names': require('./rules/multi-word-component-names'),
'multiline-html-element-content-newline': require('./rules/multiline-html-element-content-newline'),
'multiline-ternary': require('./rules/multiline-ternary'),
Expand Down Expand Up @@ -264,7 +265,7 @@
'.vue': require('./processor')
},
environments: {
// TODO Remove in the next major version

Check warning on line 268 in lib/index.js

View workflow job for this annotation

GitHub Actions / Lint

Unexpected 'todo' comment: 'TODO Remove in the next major version'
/** @deprecated */
'setup-compiler-macros': {
globals: {
Expand Down
69 changes: 69 additions & 0 deletions lib/rules/max-props.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
/**
* @author kevsommer Kevin Sommer
* See LICENSE file in root directory for full license.
*/
'use strict'
const utils = require('../utils')

module.exports = {
meta: {
type: 'problem',
docs: {
description: 'enforce maximum number of props in Vue component',
categories: undefined,
url: 'https://eslint.vuejs.org/rules/max-props.html'
},
fixable: null,
schema: [
{
type: 'object',
properties: {
maxProps: {
type: 'integer',
minimum: 1
}
},
additionalProperties: false
}
],
messages: {
tooManyProps:
'Component has too many props ({{propCount}}). Maximum allowed is {{limit}}.'
}
},
/** @param {RuleContext} context */
create(context) {
/**
* @type {Record<string, number>}
*/
const option = context.options[0] || {}

/**
*
* @param {import('../utils').ComponentProp[]} props
*/
function checkMaxNumberOfProps(props) {
if (props.length > option.maxProps && props[0].node) {
context.report({
node: props[0].node,
messageId: 'tooManyProps',
data: {
propCount: props.length,
limit: option.maxProps
}
})
}
}

return utils.compositingVisitors(
utils.executeOnVueComponent(context, (obj) => {
checkMaxNumberOfProps(utils.getComponentPropsFromOptions(obj))
}),
utils.defineScriptSetupVisitor(context, {
onDefinePropsEnter(_, props) {
checkMaxNumberOfProps(props)
}
})
)
}
}
148 changes: 148 additions & 0 deletions tests/lib/rules/max-props.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,148 @@
/**
* @author kevsommer Kevin Sommer
* See LICENSE file in root directory for full license.
*/
'use strict'

const RuleTester = require('../../eslint-compat').RuleTester
const rule = require('../../../lib/rules/max-props')

const tester = new RuleTester({
languageOptions: {
parser: require('vue-eslint-parser'),
ecmaVersion: 2020,
sourceType: 'module'
}
})

tester.run('max-props', rule, {
valid: [
{
filename: 'test.vue',
code: `
<template>

</template>
<script setup>
defineProps({ prop1: '', prop2: '' })
</script>
`,
options: [{ maxProps: 5 }]
},
{
filename: 'test.vue',
code: `
<template>

</template>
<script>
export default {
props: {
prop1: String,
prop2: String
}
}
</script>
`,
options: [{ maxProps: 5 }]
},
{
filename: 'test.vue',
code: `
<template>

</template>
<script>
export default {
props: {
prop1: String,
prop2: String,
prop3: String,
prop4: String,
prop5: String
}
}
</script>
`,
options: [{ maxProps: 5 }]
},
{
filename: 'test.vue',
code: `
<template>

</template>
<script>
export default {
props: {}
}
</script>
`,
options: [{ maxProps: 5 }]
},
{
filename: 'test.vue',
code: `
<template>

</template>
<script setup>
defineProps({})
</script>
`,
options: [{ maxProps: 5 }]
},
{
filename: 'test.vue',
code: `
<template>

</template>
<script>
</script>
`,
options: [{ maxProps: 5 }]
}
],
invalid: [
{
filename: 'test.vue',
code: `
<template>
</template>
<script setup>
defineProps({ prop1: '', prop2: '' })
</script>
`,
options: [{ maxProps: 1 }],
errors: [
{
message: 'Component has too many props (2). Maximum allowed is 1.',
line: 5
}
]
},
{
filename: 'test.vue',
code: `
<template>
</template>
<script>
export default {
props: {
prop1: String,
prop2: String
}
}
</script>
`,
options: [{ maxProps: 1 }],
errors: [
{
message: 'Component has too many props (2). Maximum allowed is 1.',
line: 7
}
]
}
]
})