-
-
Notifications
You must be signed in to change notification settings - Fork 668
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add
no-required-prop-with-default
rule (#1943)
* feat: add optional-props-using-with-defaults * feat: improve rule name * feat: change to problem * feat: fix comments * feat: add suggest to rule
- Loading branch information
Showing
5 changed files
with
1,175 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,96 @@ | ||
--- | ||
pageClass: rule-details | ||
sidebarDepth: 0 | ||
title: vue/no-required-prop-with-default | ||
description: enforce props with default values to be optional | ||
--- | ||
# vue/no-required-prop-with-default | ||
|
||
> 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. | ||
- :bulb: Some problems reported by this rule are manually fixable by editor [suggestions](https://eslint.org/docs/developer-guide/working-with-rules#providing-suggestions). | ||
|
||
## :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/no-required-prop-with-default': ['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/no-required-prop-with-default': ['error', { autoFix: true }]}"> | ||
|
||
```vue | ||
<script> | ||
export default { | ||
/* ✓ GOOD */ | ||
props: { | ||
name: { | ||
required: true, | ||
default: 'Hello' | ||
} | ||
} | ||
/* ✗ BAD */ | ||
props: { | ||
name: { | ||
required: true, | ||
default: 'Hello' | ||
} | ||
} | ||
} | ||
</script> | ||
``` | ||
|
||
</eslint-code-block> | ||
|
||
## :wrench: Options | ||
|
||
```json | ||
{ | ||
"vue/no-required-prop-with-default": ["error", { | ||
"autofix": false, | ||
}] | ||
} | ||
``` | ||
|
||
- `"autofix"` ... If `true`, enable autofix. (Default: `false`) | ||
|
||
## :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/no-required-prop-with-default.js) | ||
- [Test source](https://github.com/vuejs/eslint-plugin-vue/blob/master/tests/lib/rules/no-required-prop-with-default.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,155 @@ | ||
/** | ||
* @author @neferqiqi | ||
* See LICENSE file in root directory for full license. | ||
*/ | ||
'use strict' | ||
// ------------------------------------------------------------------------------ | ||
// Requirements | ||
// ------------------------------------------------------------------------------ | ||
|
||
const utils = require('../utils') | ||
/** | ||
* @typedef {import('../utils').ComponentTypeProp} ComponentTypeProp | ||
* @typedef {import('../utils').ComponentArrayProp} ComponentArrayProp | ||
* @typedef {import('../utils').ComponentObjectProp} ComponentObjectProp | ||
* @typedef {import('../utils').ComponentUnknownProp} ComponentUnknownProp | ||
* @typedef {import('../utils').ComponentProp} ComponentProp | ||
*/ | ||
|
||
// ------------------------------------------------------------------------------ | ||
// Rule Definition | ||
// ------------------------------------------------------------------------------ | ||
|
||
module.exports = { | ||
meta: { | ||
hasSuggestions: true, | ||
type: 'problem', | ||
docs: { | ||
description: 'enforce props with default values to be optional', | ||
categories: undefined, | ||
url: 'https://eslint.vuejs.org/rules/no-required-prop-with-default.html' | ||
}, | ||
fixable: 'code', | ||
schema: [ | ||
{ | ||
type: 'object', | ||
properties: { | ||
autofix: { | ||
type: 'boolean' | ||
} | ||
}, | ||
additionalProperties: false | ||
} | ||
], | ||
messages: { | ||
requireOptional: `Prop "{{ key }}" should be optional.`, | ||
fixRequiredProp: `Change this prop to be optional.` | ||
} | ||
}, | ||
/** @param {RuleContext} context */ | ||
create(context) { | ||
let canAutoFix = false | ||
const option = context.options[0] | ||
if (option) { | ||
canAutoFix = option.autofix | ||
} | ||
|
||
/** | ||
* @param {ComponentArrayProp | ComponentObjectProp | ComponentUnknownProp | ComponentProp} prop | ||
* */ | ||
const handleObjectProp = (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 | ||
}, | ||
messageId: 'requireOptional', | ||
fix: canAutoFix | ||
? (fixer) => fixer.replaceText(requiredNode, 'false') | ||
: null, | ||
suggest: canAutoFix | ||
? null | ||
: [ | ||
{ | ||
messageId: 'fixRequiredProp', | ||
fix: (fixer) => fixer.replaceText(requiredNode, 'false') | ||
} | ||
] | ||
}) | ||
} | ||
} | ||
} | ||
|
||
return utils.compositingVisitors( | ||
utils.defineVueVisitor(context, { | ||
onVueObjectEnter(node) { | ||
utils.getComponentPropsFromOptions(node).map(handleObjectProp) | ||
} | ||
}), | ||
utils.defineScriptSetupVisitor(context, { | ||
onDefinePropsEnter(node, props) { | ||
if (!utils.hasWithDefaults(node)) { | ||
props.map(handleObjectProp) | ||
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)) { | ||
// skip setter & getter case | ||
if ( | ||
prop.node.type === 'TSMethodSignature' && | ||
(prop.node.kind === 'get' || prop.node.kind === 'set') | ||
) { | ||
return | ||
} | ||
// skip computed | ||
if (prop.node.computed) { | ||
return | ||
} | ||
context.report({ | ||
node: prop.node, | ||
loc: prop.node.loc, | ||
data: { | ||
key: prop.propName | ||
}, | ||
messageId: 'requireOptional', | ||
fix: canAutoFix | ||
? (fixer) => fixer.insertTextAfter(prop.key, '?') | ||
: null, | ||
suggest: canAutoFix | ||
? null | ||
: [ | ||
{ | ||
messageId: 'fixRequiredProp', | ||
fix: (fixer) => fixer.insertTextAfter(prop.key, '?') | ||
} | ||
] | ||
}) | ||
} | ||
} | ||
} | ||
}) | ||
) | ||
} | ||
} |
Oops, something went wrong.