-
-
Notifications
You must be signed in to change notification settings - Fork 679
feat: add define-props-destructuring
rule
#2736
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 2 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
--- | ||
pageClass: rule-details | ||
sidebarDepth: 0 | ||
title: vue/define-props-destructuring | ||
description: enforce consistent style for prop destructuring | ||
--- | ||
|
||
# vue/define-props-destructuring | ||
|
||
> enforce consistent style for prop destructuring | ||
|
||
- :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 consistent style for handling Vue 3 Composition API props, allowing you to choose between requiring destructuring or prohibiting it. | ||
|
||
By default, the rule requires you to use destructuring syntax when using `defineProps` instead of storing props in a variable and warns against combining `withDefaults` with destructuring. | ||
|
||
<eslint-code-block :rules="{'vue/define-props-destructuring': ['error']}"> | ||
|
||
```vue | ||
<script setup> | ||
// ✓ GOOD | ||
const { foo } = defineProps(['foo']) | ||
const { bar = 'default' } = defineProps(['bar']) | ||
|
||
// ✗ BAD | ||
const props = defineProps(['foo']) | ||
const propsWithDefaults = withDefaults(defineProps(['foo']), { foo: 'default' }) | ||
|
||
// ✗ BAD | ||
const { baz } = withDefaults(defineProps(['baz']), { baz: 'default' }) | ||
</script> | ||
``` | ||
|
||
</eslint-code-block> | ||
|
||
The rule applies to both JavaScript and TypeScript props: | ||
|
||
<eslint-code-block :rules="{'vue/define-props-destructuring': ['error']}"> | ||
|
||
```vue | ||
<script setup lang="ts"> | ||
// ✓ GOOD | ||
const { foo } = defineProps<{ foo?: string }>() | ||
const { bar = 'default' } = defineProps<{ bar?: string }>() | ||
|
||
// ✗ BAD | ||
const props = defineProps<{ foo?: string }>() | ||
const propsWithDefaults = withDefaults(defineProps<{ foo?: string }>(), { foo: 'default' }) | ||
</script> | ||
``` | ||
|
||
</eslint-code-block> | ||
|
||
## :wrench: Options | ||
|
||
```js | ||
{ | ||
"vue/define-props-destructuring": ["error", { | ||
"destructure": "always" | "never" | ||
}] | ||
} | ||
``` | ||
|
||
- `destructure` - Sets the destructuring preference for props | ||
- `"always"` (default) - Requires destructuring when using `defineProps` and warns against using `withDefaults` with destructuring | ||
- `"never"` - Requires using a variable to store props and prohibits destructuring | ||
|
||
### `"destructure": "never"` | ||
|
||
<eslint-code-block :rules="{'vue/define-props-destructuring': ['error', { destructure: 'never' }]}"> | ||
|
||
```vue | ||
<script setup> | ||
// ✓ GOOD | ||
const props = defineProps(['foo']) | ||
const propsWithDefaults = withDefaults(defineProps(['foo']), { foo: 'default' }) | ||
|
||
// ✗ BAD | ||
const { foo } = defineProps(['foo']) | ||
</script> | ||
``` | ||
|
||
</eslint-code-block> | ||
|
||
## :books: Further Reading | ||
|
||
- [Reactive Props Destructure](https://vuejs.org/guide/components/props.html#reactive-props-destructure) | ||
|
||
## :mag: Implementation | ||
|
||
- [Rule source](https://github.com/vuejs/eslint-plugin-vue/blob/master/lib/rules/define-props-destructuring.js) | ||
- [Test source](https://github.com/vuejs/eslint-plugin-vue/blob/master/tests/lib/rules/define-props-destructuring.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
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,78 @@ | ||
/** | ||
* @author Wayne Zhang | ||
* See LICENSE file in root directory for full license. | ||
*/ | ||
'use strict' | ||
|
||
const utils = require('../utils') | ||
|
||
module.exports = { | ||
meta: { | ||
type: 'suggestion', | ||
docs: { | ||
description: 'enforce consistent style for prop destructuring', | ||
categories: undefined, | ||
url: 'https://eslint.vuejs.org/rules/define-props-destructuring.html' | ||
}, | ||
fixable: null, | ||
schema: [ | ||
{ | ||
type: 'object', | ||
properties: { | ||
destructure: { | ||
enum: ['always', 'never'] | ||
} | ||
}, | ||
additionalProperties: false | ||
} | ||
], | ||
messages: { | ||
preferDestructuring: 'Prefer destructuring from `defineProps` directly.', | ||
avoidDestructuring: 'Avoid destructuring from `defineProps`.', | ||
avoidWithDefaults: 'Avoid using `withDefaults` with destructuring.' | ||
} | ||
}, | ||
/** @param {RuleContext} context */ | ||
create(context) { | ||
const options = context.options[0] || {} | ||
const destructurePreference = options.destructure || 'always' | ||
|
||
return utils.compositingVisitors( | ||
utils.defineScriptSetupVisitor(context, { | ||
onDefinePropsEnter(node, props) { | ||
const hasNoArgs = props.filter((prop) => prop.propName).length === 0 | ||
if (hasNoArgs) { | ||
return | ||
} | ||
|
||
const hasDestructure = utils.isUsingPropsDestructure(node) | ||
const hasWithDefaults = utils.hasWithDefaults(node) | ||
|
||
if (destructurePreference === 'always') { | ||
if (!hasDestructure) { | ||
context.report({ | ||
node, | ||
messageId: 'preferDestructuring' | ||
}) | ||
return | ||
} | ||
|
||
if (hasWithDefaults) { | ||
context.report({ | ||
node: node.parent.callee, | ||
messageId: 'avoidWithDefaults' | ||
}) | ||
} | ||
} else { | ||
if (hasDestructure) { | ||
context.report({ | ||
node, | ||
messageId: 'avoidDestructuring' | ||
}) | ||
} | ||
} | ||
waynzh marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
}) | ||
) | ||
} | ||
} |
Oops, something went wrong.
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.