-
-
Notifications
You must be signed in to change notification settings - Fork 680
implements proposed max-props rule #2430
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
ota-meshi
merged 9 commits into
vuejs:master
from
kevsommer:implements-proposed-max-props-rule
Jul 13, 2024
Merged
Changes from 3 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
297d750
implements proposed max-props rule
kevsommer 5bdfa40
Merge remote-tracking branch 'origin' into implements-proposed-max-pr…
kevsommer eca2b73
fixes max props rule tests for defineProps with type-only declarion
kevsommer ca5ce34
Update docs/rules/max-props.md
kevsommer ae1be39
Update lib/rules/max-props.js
kevsommer babf40d
Update lib/rules/max-props.js
kevsommer d5804b0
Update lib/rules/max-props.js
kevsommer 35d0d24
removes empty template blocks from max-props tests
kevsommer f46c376
fixes test cases
kevsommer 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,64 @@ | ||
--- | ||
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) |
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,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 | ||
kevsommer marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
], | ||
messages: { | ||
tooManyProps: | ||
'Component has too many props ({{propCount}}). Maximum allowed is {{limit}}.' | ||
} | ||
}, | ||
/** @param {RuleContext} context */ | ||
create(context) { | ||
/** | ||
* @type {Record<string, number>} | ||
*/ | ||
kevsommer marked this conversation as resolved.
Show resolved
Hide resolved
|
||
const option = context.options[0] || {} | ||
|
||
/** | ||
* | ||
* @param {import('../utils').ComponentProp[]} props | ||
*/ | ||
kevsommer marked this conversation as resolved.
Show resolved
Hide resolved
|
||
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.executeOnVue(context, (obj) => { | ||
checkMaxNumberOfProps(utils.getComponentPropsFromOptions(obj)) | ||
}), | ||
utils.defineScriptSetupVisitor(context, { | ||
onDefinePropsEnter(_, props) { | ||
checkMaxNumberOfProps(props) | ||
} | ||
}) | ||
) | ||
} | ||
} |
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,189 @@ | ||
/** | ||
* @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> | ||
kevsommer marked this conversation as resolved.
Show resolved
Hide resolved
|
||
<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 }] | ||
}, | ||
{ | ||
filename: 'test.vue', | ||
code: ` | ||
<template> | ||
|
||
</template> | ||
<script setup lang="ts"> | ||
defineProps<{ prop1: string, prop2: string }>(); | ||
</script> | ||
`, | ||
options: [{ maxProps: 5 }], | ||
languageOptions: { | ||
parser: require('vue-eslint-parser'), | ||
parserOptions: { | ||
parser: require.resolve('@typescript-eslint/parser') | ||
} | ||
} | ||
} | ||
], | ||
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 | ||
} | ||
] | ||
}, | ||
{ | ||
filename: 'test.vue', | ||
code: ` | ||
<template> | ||
</template> | ||
<script setup lang="ts"> | ||
defineProps<{ prop1: string, prop2: string, prop3: string }>(); | ||
</script> | ||
`, | ||
options: [{ maxProps: 2 }], | ||
languageOptions: { | ||
parser: require('vue-eslint-parser'), | ||
parserOptions: { | ||
parser: require.resolve('@typescript-eslint/parser') | ||
} | ||
}, | ||
errors: [ | ||
{ | ||
message: 'Component has too many props (3). Maximum allowed is 2.', | ||
line: 5 | ||
} | ||
] | ||
} | ||
] | ||
}) |
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.