-
-
Notifications
You must be signed in to change notification settings - Fork 679
Add vue/require-typed-object-prop
rule
#1983
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 29 commits into
vuejs:master
from
przemyslawjanpietrzak:feat/force-types-on-object-props
Jul 2, 2023
Merged
Changes from 14 commits
Commits
Show all changes
29 commits
Select commit
Hold shift + click to select a range
c4b20ca
add rule template
przemyslawjanpietrzak 49d0f30
prepare documentation
przemyslawjanpietrzak 9a5320b
prepare force types on object implementation
przemyslawjanpietrzak 681608f
Merge branch 'master' into feat/force-types-on-object-props
przemyslawjanpietrzak bfa7a9f
fix some unit tests
przemyslawjanpietrzak 2d88680
Merge branch 'master' into feat/force-types-on-object-props
przemyslawjanpietrzak 92c09a8
remove double blank lines
przemyslawjanpietrzak 95aa2a3
set proper category
przemyslawjanpietrzak 96eb868
update docs
przemyslawjanpietrzak 9264f63
Apply suggestions from code review
przemyslawjanpietrzak 1183f8f
add new test case
przemyslawjanpietrzak 75fc825
add new test case & update docs
przemyslawjanpietrzak 9161964
rewrite force-types-on-object-props
przemyslawjanpietrzak 1aa1de6
update docs
przemyslawjanpietrzak 534a2f6
rewrite rule
przemyslawjanpietrzak 9e375c0
rename rule
przemyslawjanpietrzak cf4f722
Merge branch 'master' into feat/force-types-on-object-props
przemyslawjanpietrzak ae47555
Merge branch 'master' into feat/force-types-on-object-props
FloEdelmann fa69006
Fix TypeScript type inference
FloEdelmann 6398ced
Also report untyped array props
FloEdelmann 30145fd
Fix docs
FloEdelmann d6d87f2
Regenerate docs
FloEdelmann e5794b0
Rename to `vue/require-typed-object-prop`
FloEdelmann a0bc1c1
Fix docs
FloEdelmann bbe3a61
Better docs
FloEdelmann 31009cf
Simplify logic and treat `as any` and `as unknown` as valid
FloEdelmann 934030b
Rename variables
FloEdelmann 7143dc5
Add suggestions to rule, use message IDs
FloEdelmann dbd17e3
Simplify docs
FloEdelmann 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,53 @@ | ||
--- | ||
pageClass: rule-details | ||
sidebarDepth: 0 | ||
title: vue/force-types-on-object-props | ||
description: enforce adding type declarations to object props | ||
--- | ||
# vue/force-types-on-object-props | ||
|
||
> 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> | ||
- :gear: This rule is included in all of `"plugin:vue/base"`, `"plugin:vue/essential"`, `"plugin:vue/vue3-essential"`, `"plugin:vue/strongly-recommended"`, `"plugin:vue/vue3-strongly-recommended"`, `"plugin:vue/recommended"` and `"plugin:vue/vue3-recommended"`. | ||
FloEdelmann marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
## :book: Rule Details | ||
|
||
Prevent missing type declarations for non-primitive object props in TypeScript projects. | ||
|
||
<eslint-code-block :rules="{'vue/force-types-on-object-props': ['error']}"> | ||
|
||
```ts | ||
export default { | ||
props: { | ||
prop: { | ||
// ✗ BAD | ||
type: Object, | ||
type: Array, | ||
|
||
// ✓ GOOD | ||
type: Object as Props<Anything>, | ||
FloEdelmann marked this conversation as resolved.
Show resolved
Hide resolved
|
||
type: String, // or any other primitive type | ||
} | ||
} | ||
} | ||
``` | ||
|
||
</eslint-code-block> | ||
|
||
### Options | ||
|
||
Nothing. | ||
|
||
## :mute: When Not To Use It | ||
|
||
When you're not using TypeScript in the project. | ||
|
||
## :books: Further Reading | ||
|
||
Nothing | ||
|
||
## :mag: Implementation | ||
|
||
- [Rule source](https://github.com/vuejs/eslint-plugin-vue/blob/master/lib/rules/force-types-on-object-props.js) | ||
- [Test source](https://github.com/vuejs/eslint-plugin-vue/blob/master/tests/lib/rules/force-types-on-object-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
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,163 @@ | ||
/** | ||
* @author Przemysław Jan Beigert | ||
* See LICENSE file in root directory for full license. | ||
*/ | ||
'use strict' | ||
|
||
const utils = require('../utils') | ||
/** | ||
* @typedef {import('../utils').ComponentProp} ComponentProp | ||
*/ | ||
|
||
// ------------------------------------------------------------------------------ | ||
// Helpers | ||
// ------------------------------------------------------------------------------ | ||
|
||
/** | ||
* Check if all keys and values from second object are resent in first object | ||
* | ||
* @param {{ [key: string]: any }} a object to | ||
* @param {{ [key: string]: any }} b The string to escape. | ||
* @returns {boolean} Returns the escaped string. | ||
*/ | ||
const isLooksLike = (a, b) => | ||
a && | ||
b && | ||
Object.keys(b).every((bKey) => { | ||
const bVal = b[bKey] | ||
const aVal = a[bKey] | ||
if (typeof bVal === 'function') { | ||
return bVal(aVal) | ||
} | ||
return bVal == null || /^[bns]/.test(typeof bVal) | ||
? bVal === aVal | ||
: isLooksLike(aVal, bVal) | ||
}) | ||
FloEdelmann marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
/** | ||
* @param {ComponentProp} property | ||
* @param {RuleContext} context | ||
*/ | ||
const checkProperty = (property, context) => { | ||
if (!property.value) { | ||
return | ||
} | ||
|
||
if ( | ||
isLooksLike(property.value, { type: 'Identifier', name: 'Object' }) && | ||
property.node.value.type !== 'TSAsExpression' | ||
) { | ||
context.report({ | ||
node: property.node, | ||
message: 'Expected type annotation on object prop.' | ||
}) | ||
} | ||
|
||
if ( | ||
property.type === 'object' && | ||
property.value.type === 'ObjectExpression' && | ||
property.node.value.type === 'ObjectExpression' | ||
) { | ||
const typePropert = property.node.value.properties.find( | ||
FloEdelmann marked this conversation as resolved.
Show resolved
Hide resolved
|
||
(prop) => | ||
prop.type === 'Property' && | ||
prop.key.type === 'Identifier' && | ||
prop.key.name === 'type' | ||
) | ||
if ( | ||
typePropert && | ||
typePropert.type === 'Property' && | ||
isLooksLike(typePropert.value, { type: 'Identifier', name: 'Object' }) | ||
) { | ||
context.report({ | ||
node: property.node, | ||
message: 'Expected type annotation on object prop.' | ||
}) | ||
} | ||
} | ||
|
||
if (property.node.value.type === 'ObjectExpression') { | ||
for (const prop of property.node.value.properties) { | ||
if (prop.type !== 'Property') { | ||
continue | ||
} | ||
if (prop.key.type !== 'Identifier' || prop.key.name !== 'type') { | ||
continue | ||
} | ||
if (prop.value.type !== 'TSAsExpression') { | ||
continue | ||
} | ||
|
||
const { typeAnnotation } = prop.value | ||
if ( | ||
['TSAnyKeyword', 'TSUnknownKeyword'].includes(typeAnnotation.type) || | ||
!typeAnnotation.typeName || | ||
!['Prop', 'PropType'].includes(typeAnnotation.typeName.name) | ||
) { | ||
context.report({ | ||
node: property.node, | ||
message: 'Expected type annotation on object prop.' | ||
}) | ||
} | ||
} | ||
} | ||
|
||
if (property.node.value.type === 'TSAsExpression') { | ||
const { typeAnnotation } = property.node.value | ||
if (typeAnnotation.type === 'TSFunctionType') { | ||
return | ||
} | ||
if ( | ||
[ | ||
'TSAnyKeyword', | ||
'TSTypeLiteral', | ||
'TSUnknownKeyword', | ||
'TSObjectKeyword' | ||
].includes(typeAnnotation.type) || | ||
!typeAnnotation.typeName || | ||
!['Prop', 'PropType'].includes(typeAnnotation.typeName.name) | ||
) { | ||
context.report({ | ||
node: property.node, | ||
message: 'Expected type annotation on object prop.' | ||
}) | ||
} | ||
} | ||
} | ||
|
||
//------------------------------------------------------------------------------ | ||
// Rule Definition | ||
//------------------------------------------------------------------------------ | ||
|
||
module.exports = { | ||
meta: { | ||
type: 'suggestion', | ||
docs: { | ||
description: 'enforce adding type declarations to object props', | ||
categories: ['base'], | ||
FloEdelmann marked this conversation as resolved.
Show resolved
Hide resolved
|
||
recommended: false, | ||
url: 'https://eslint.vuejs.org/rules/force-types-on-object-props.html' | ||
}, | ||
fixable: null, | ||
schema: [] | ||
}, | ||
/** @param {RuleContext} context */ | ||
create(context) { | ||
return utils.compositingVisitors( | ||
utils.defineScriptSetupVisitor(context, { | ||
onDefinePropsEnter(_node, props) { | ||
for (const prop of props) { | ||
checkProperty(prop, context) | ||
} | ||
} | ||
}), | ||
utils.executeOnVue(context, (obj) => { | ||
const props = utils.getComponentPropsFromOptions(obj) | ||
|
||
for (const prop of props) { | ||
checkProperty(prop, context) | ||
} | ||
}) | ||
) | ||
} | ||
} |
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.