Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add optional-props-using-with-defaults
- Loading branch information
Showing
5 changed files
with
1,130 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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 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/prefer-optional-props-using-with-defaults | ||
description: enforce props with default values to be optional | ||
--- | ||
# vue/prefer-optional-props-using-with-defaults | ||
|
||
> enforce props with default values to be optional | ||
- :exclamation: <badge text="This rule has not been released yet." vertical="middle" type="error"> ***This rule has not been released yet.*** </badge> | ||
- :wrench: The `--fix` option on the [command line](https://eslint.org/docs/user-guide/command-line-interface#fixing-problems) can automatically fix some of the problems reported by this rule. | ||
|
||
## :book: Rule Details | ||
|
||
If a prop is declared with a default value, whether it is required or not, we can always skip it in actual use. In that situation, the default value would be applied. | ||
So, a required prop with a default value is essentially the same as an optional prop. | ||
This rule enforces all props with default values to be optional. | ||
|
||
<eslint-code-block fix :rules="{'vue/prefer-optional-props-using-with-defaults': ['error', { autoFix: true }]}"> | ||
|
||
```vue | ||
<script setup lang="ts"> | ||
/* ✓ GOOD */ | ||
const props = withDefaults( | ||
defineProps<{ | ||
name?: string | number | ||
age?: number | ||
}>(), | ||
{ | ||
name: "Foo", | ||
} | ||
); | ||
/* ✗ BAD */ | ||
const props = withDefaults( | ||
defineProps<{ | ||
name: string | number | ||
age?: number | ||
}>(), | ||
{ | ||
name: "Foo", | ||
} | ||
); | ||
</script> | ||
``` | ||
|
||
</eslint-code-block> | ||
|
||
<eslint-code-block fix :rules="{'vue/prefer-optional-props-using-with-defaults': ['error', { autoFix: true }]}"> | ||
|
||
```vue | ||
<script setup lang="ts"> | ||
export default { | ||
/* ✓ GOOD */ | ||
props: { | ||
name: { | ||
required: true, | ||
default: 'Hello' | ||
} | ||
} | ||
/* ✗ BAD */ | ||
props: { | ||
name: { | ||
required: true, | ||
default: 'Hello' | ||
} | ||
} | ||
} | ||
</script> | ||
``` | ||
|
||
</eslint-code-block> | ||
|
||
## :wrench: Options | ||
|
||
```json | ||
{ | ||
"vue/prefer-optional-props-using-with-defaults": ["error", { | ||
"autofix": false, | ||
}] | ||
} | ||
``` | ||
|
||
- `"autofix"` ... If `true`, enable autofix. | ||
|
||
## :couple: Related Rules | ||
|
||
- [vue/require-default-prop](./require-default-prop.md) | ||
|
||
## :mag: Implementation | ||
|
||
- [Rule source](https://github.com/vuejs/eslint-plugin-vue/blob/master/lib/rules/prefer-optional-props-using-with-defaults.js) | ||
- [Test source](https://github.com/vuejs/eslint-plugin-vue/blob/master/tests/lib/rules/prefer-optional-props-using-with-defaults.js) |
This file contains 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 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,154 @@ | ||
/** | ||
* @author @neferqiqi | ||
* See LICENSE file in root directory for full license. | ||
*/ | ||
'use strict' | ||
// ------------------------------------------------------------------------------ | ||
// Requirements | ||
// ------------------------------------------------------------------------------ | ||
|
||
const utils = require('../utils') | ||
/** | ||
* @typedef {import('../utils').ComponentTypeProp} ComponentTypeProp | ||
*/ | ||
|
||
// ------------------------------------------------------------------------------ | ||
// Helpers | ||
// ------------------------------------------------------------------------------ | ||
|
||
// ... | ||
|
||
// ------------------------------------------------------------------------------ | ||
// Rule Definition | ||
// ------------------------------------------------------------------------------ | ||
|
||
module.exports = { | ||
meta: { | ||
type: 'suggestion', | ||
docs: { | ||
description: 'enforce props with default values to be optional', | ||
categories: undefined, | ||
url: 'https://eslint.vuejs.org/rules/prefer-optional-props-using-with-defaults.html' | ||
}, | ||
fixable: 'code', | ||
schema: [ | ||
{ | ||
type: 'object', | ||
properties: { | ||
autofix: { | ||
type: 'boolean' | ||
} | ||
}, | ||
additionalProperties: false | ||
} | ||
], | ||
messages: { | ||
// ... | ||
} | ||
}, | ||
/** @param {RuleContext} context */ | ||
create(context) { | ||
/** | ||
* @param {ComponentTypeProp} prop | ||
* @param {Token[]} tokens | ||
* */ | ||
const findKeyToken = (prop, tokens) => | ||
tokens.find((token) => { | ||
const isKeyIdentifierEqual = | ||
prop.key.type === 'Identifier' && token.value === prop.key.name | ||
const isKeyLiteralEqual = | ||
prop.key.type === 'Literal' && token.value === prop.key.raw | ||
return isKeyIdentifierEqual || isKeyLiteralEqual | ||
}) | ||
|
||
let canAutoFix = false | ||
const option = context.options[0] | ||
if (option) { | ||
canAutoFix = option.autofix | ||
} | ||
|
||
return utils.compositingVisitors( | ||
utils.defineVueVisitor(context, { | ||
onVueObjectEnter(node) { | ||
utils.getComponentPropsFromOptions(node).map((prop) => { | ||
if ( | ||
prop.type === 'object' && | ||
prop.propName && | ||
prop.value.type === 'ObjectExpression' && | ||
utils.findProperty(prop.value, 'default') | ||
) { | ||
const requiredProperty = utils.findProperty( | ||
prop.value, | ||
'required' | ||
) | ||
if (!requiredProperty) return | ||
const requiredNode = requiredProperty.value | ||
if ( | ||
requiredNode && | ||
requiredNode.type === 'Literal' && | ||
!!requiredNode.value | ||
) { | ||
context.report({ | ||
node: prop.node, | ||
loc: prop.node.loc, | ||
data: { | ||
key: prop.propName | ||
}, | ||
message: `Prop "{{ key }}" should be optional.`, | ||
fix: canAutoFix | ||
? (fixer) => fixer.replaceText(requiredNode, 'false') | ||
: null | ||
}) | ||
} | ||
} | ||
}) | ||
} | ||
}), | ||
utils.defineScriptSetupVisitor(context, { | ||
onDefinePropsEnter(node, props) { | ||
if (!utils.hasWithDefaults(node)) { | ||
return | ||
} | ||
const withDefaultsProps = Object.keys( | ||
utils.getWithDefaultsPropExpressions(node) | ||
) | ||
const requiredProps = props.flatMap((item) => | ||
item.type === 'type' && item.required ? [item] : [] | ||
) | ||
|
||
for (const prop of requiredProps) { | ||
if (withDefaultsProps.includes(prop.propName)) { | ||
const firstToken = context | ||
.getSourceCode() | ||
.getFirstToken(prop.node) | ||
// skip setter & getter case | ||
if (firstToken.value === 'get' || firstToken.value === 'set') { | ||
return | ||
} | ||
// skip computed | ||
if (prop.node.computed) { | ||
return | ||
} | ||
const keyToken = findKeyToken( | ||
prop, | ||
context.getSourceCode().getTokens(prop.node) | ||
) | ||
if (!keyToken) return | ||
context.report({ | ||
node: prop.node, | ||
loc: prop.node.loc, | ||
data: { | ||
key: prop.propName | ||
}, | ||
message: `Prop "{{ key }}" should be optional.`, | ||
fix: canAutoFix | ||
? (fixer) => fixer.insertTextAfter(keyToken, '?') | ||
: null | ||
}) | ||
} | ||
} | ||
} | ||
}) | ||
) | ||
} | ||
} |
Oops, something went wrong.