-
-
Notifications
You must be signed in to change notification settings - Fork 651
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add
vue/require-typed-object-prop
rule (#1983)
Co-authored-by: Flo Edelmann <git@flo-edelmann.de>
- Loading branch information
1 parent
cd32f03
commit 7f906ea
Showing
5 changed files
with
823 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,50 @@ | ||
--- | ||
pageClass: rule-details | ||
sidebarDepth: 0 | ||
title: vue/require-typed-object-prop | ||
description: enforce adding type declarations to object props | ||
--- | ||
# vue/require-typed-object-prop | ||
|
||
> enforce adding type declarations to object props | ||
- :exclamation: <badge text="This rule has not been released yet." vertical="middle" type="error"> ***This rule has not been released yet.*** </badge> | ||
- :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 | ||
|
||
Prevent missing type declarations for non-primitive object props in TypeScript projects. | ||
|
||
<eslint-code-block :rules="{'vue/require-typed-object-prop': ['error']}"> | ||
|
||
```vue | ||
<script lang="ts"> | ||
export default { | ||
props: { | ||
// ✗ BAD | ||
bad1: Object, | ||
bad2: { type: Array }, | ||
// ✓ GOOD | ||
good1: Object as PropType<Anything>, | ||
good2: { type: Array as PropType<Anything[]> }, | ||
good3: [String, Boolean], // or any other primitive type | ||
} | ||
} | ||
</script> | ||
``` | ||
|
||
</eslint-code-block> | ||
|
||
## :wrench: Options | ||
|
||
Nothing. | ||
|
||
## :mute: When Not To Use It | ||
|
||
When you're not using TypeScript in the project. | ||
|
||
## :mag: Implementation | ||
|
||
- [Rule source](https://github.com/vuejs/eslint-plugin-vue/blob/master/lib/rules/require-typed-object-prop.js) | ||
- [Test source](https://github.com/vuejs/eslint-plugin-vue/blob/master/tests/lib/rules/require-typed-object-prop.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,150 @@ | ||
/** | ||
* @author Przemysław Jan Beigert | ||
* See LICENSE file in root directory for full license. | ||
*/ | ||
'use strict' | ||
|
||
const utils = require('../utils') | ||
|
||
/** | ||
* @param {RuleContext} context | ||
* @param {Identifier} identifierNode | ||
*/ | ||
const checkPropIdentifierType = (context, identifierNode) => { | ||
if (identifierNode.name === 'Object' || identifierNode.name === 'Array') { | ||
const arrayTypeSuggestion = identifierNode.name === 'Array' ? '[]' : '' | ||
context.report({ | ||
node: identifierNode, | ||
messageId: 'expectedTypeAnnotation', | ||
suggest: [ | ||
{ | ||
messageId: 'addTypeAnnotation', | ||
data: { type: `any${arrayTypeSuggestion}` }, | ||
fix(fixer) { | ||
return fixer.insertTextAfter( | ||
identifierNode, | ||
` as PropType<any${arrayTypeSuggestion}>` | ||
) | ||
} | ||
}, | ||
{ | ||
messageId: 'addTypeAnnotation', | ||
data: { type: `unknown${arrayTypeSuggestion}` }, | ||
fix(fixer) { | ||
return fixer.insertTextAfter( | ||
identifierNode, | ||
` as PropType<unknown${arrayTypeSuggestion}>` | ||
) | ||
} | ||
} | ||
] | ||
}) | ||
} | ||
} | ||
|
||
/** | ||
* @param {RuleContext} context | ||
* @param {ArrayExpression} arrayNode | ||
*/ | ||
const checkPropArrayType = (context, arrayNode) => { | ||
for (const elementNode of arrayNode.elements) { | ||
if (elementNode?.type === 'Identifier') { | ||
checkPropIdentifierType(context, elementNode) | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* @param {RuleContext} context | ||
* @param {ObjectExpression} objectNode | ||
*/ | ||
const checkPropObjectType = (context, objectNode) => { | ||
const typeProperty = objectNode.properties.find( | ||
(prop) => | ||
prop.type === 'Property' && | ||
prop.key.type === 'Identifier' && | ||
prop.key.name === 'type' | ||
) | ||
if (!typeProperty || typeProperty.type !== 'Property') { | ||
return | ||
} | ||
|
||
if (typeProperty.value.type === 'Identifier') { | ||
// `foo: { type: String }` | ||
checkPropIdentifierType(context, typeProperty.value) | ||
} else if (typeProperty.value.type === 'ArrayExpression') { | ||
// `foo: { type: [String, Boolean] }` | ||
checkPropArrayType(context, typeProperty.value) | ||
} | ||
} | ||
|
||
/** | ||
* @param {import('../utils').ComponentProp} prop | ||
* @param {RuleContext} context | ||
*/ | ||
const checkProp = (prop, context) => { | ||
if (prop.type !== 'object') { | ||
return | ||
} | ||
|
||
switch (prop.node.value.type) { | ||
case 'Identifier': { | ||
// e.g. `foo: String` | ||
checkPropIdentifierType(context, prop.node.value) | ||
break | ||
} | ||
case 'ArrayExpression': { | ||
// e.g. `foo: [String, Boolean]` | ||
checkPropArrayType(context, prop.node.value) | ||
break | ||
} | ||
case 'ObjectExpression': { | ||
// e.g. `foo: { type: … }` | ||
checkPropObjectType(context, prop.node.value) | ||
return | ||
} | ||
} | ||
} | ||
|
||
//------------------------------------------------------------------------------ | ||
// Rule Definition | ||
//------------------------------------------------------------------------------ | ||
|
||
module.exports = { | ||
meta: { | ||
type: 'suggestion', | ||
docs: { | ||
description: 'enforce adding type declarations to object props', | ||
categories: undefined, | ||
recommended: false, | ||
url: 'https://eslint.vuejs.org/rules/require-typed-object-prop.html' | ||
}, | ||
fixable: null, | ||
schema: [], | ||
// eslint-disable-next-line eslint-plugin/require-meta-has-suggestions -- `context.report` with suggestion is not recognized in `checkPropIdentifierType` | ||
hasSuggestions: true, | ||
messages: { | ||
expectedTypeAnnotation: 'Expected type annotation on object prop.', | ||
addTypeAnnotation: 'Add `{{ type }}` type annotation.' | ||
} | ||
}, | ||
/** @param {RuleContext} context */ | ||
create(context) { | ||
return utils.compositingVisitors( | ||
utils.defineScriptSetupVisitor(context, { | ||
onDefinePropsEnter(_node, props) { | ||
for (const prop of props) { | ||
checkProp(prop, context) | ||
} | ||
} | ||
}), | ||
utils.executeOnVue(context, (obj) => { | ||
const props = utils.getComponentPropsFromOptions(obj) | ||
|
||
for (const prop of props) { | ||
checkProp(prop, context) | ||
} | ||
}) | ||
) | ||
} | ||
} |
Oops, something went wrong.