-
-
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 9 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,93 @@ | ||
--- | ||
pageClass: rule-details | ||
sidebarDepth: 0 | ||
title: vue/force-types-on-object-props | ||
description: enforce user to add type declaration to object props | ||
--- | ||
# vue/force-types-on-object-props | ||
|
||
> enforce user to add type declaration 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 declaration of not primitive objects in a TypeScript projects. | ||
przemyslawjanpietrzak marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
Bad: | ||
|
||
<eslint-code-block :rules="{'vue/force-types-on-object-props': ['error']}"> | ||
|
||
```js | ||
export default { | ||
props: { | ||
prop: { | ||
type: Object, | ||
}, | ||
}, | ||
} | ||
``` | ||
|
||
</eslint-code-block> | ||
|
||
<eslint-code-block :rules="{'vue/force-types-on-object-props': ['error']}"> | ||
|
||
```ts | ||
export default { | ||
props: { | ||
prop: { | ||
type: Array | ||
} | ||
} | ||
} | ||
``` | ||
|
||
</eslint-code-block> | ||
|
||
Good: | ||
|
||
<eslint-code-block :rules="{'vue/force-types-on-object-props': ['error']}"> | ||
|
||
```ts | ||
export default { | ||
props: { | ||
prop: { | ||
type: Object as Props<Anything>, | ||
FloEdelmann marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
} | ||
} | ||
``` | ||
|
||
</eslint-code-block> | ||
|
||
<eslint-code-block :rules="{'vue/force-types-on-object-props': ['error']}"> | ||
|
||
```ts | ||
export default { | ||
props: { | ||
prop: { | ||
type: String, // or any other primitive type | ||
} | ||
} | ||
} | ||
``` | ||
|
||
</eslint-code-block> | ||
przemyslawjanpietrzak marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
### Options | ||
|
||
Nothing. | ||
|
||
## :mute: When Not To Use It | ||
|
||
When you're not using TypeScript in the project****. | ||
przemyslawjanpietrzak marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
## :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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,119 @@ | ||
/** | ||
* @author Przemysław Jan Beigert | ||
* See LICENSE file in root directory for full license. | ||
*/ | ||
'use strict' | ||
|
||
// ------------------------------------------------------------------------------ | ||
// 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
|
||
|
||
//------------------------------------------------------------------------------ | ||
// Rule Definition | ||
//------------------------------------------------------------------------------ | ||
|
||
module.exports = { | ||
meta: { | ||
type: 'suggestion', | ||
docs: { | ||
description: 'enforce user to add type declaration to object props', | ||
categories: ['base'], | ||
przemyslawjanpietrzak marked this conversation as resolved.
Show resolved
Hide resolved
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 { | ||
/** @param {ExportDefaultDeclaration} node */ | ||
ExportDefaultDeclaration(node) { | ||
FloEdelmann marked this conversation as resolved.
Show resolved
Hide resolved
|
||
if (node.declaration.type !== 'ObjectExpression') { | ||
return | ||
} | ||
if (!Array.isArray(node.declaration.properties)) { | ||
return | ||
} | ||
|
||
const property = node.declaration.properties.find( | ||
(property) => | ||
property.type === 'Property' && | ||
isLooksLike(property.key, { type: 'Identifier', name: 'props' }) && | ||
property.value.type === 'ObjectExpression' | ||
) | ||
|
||
if ( | ||
!property || | ||
property.type === 'SpreadElement' || | ||
!('properties' in property.value) | ||
) { | ||
return | ||
} | ||
const properties = property.value.properties | ||
.filter( | ||
(prop) => | ||
prop.type === 'Property' && prop.value.type === 'ObjectExpression' | ||
) | ||
.map((prop) => | ||
prop.value.properties.find((propValueProperty) => | ||
isLooksLike(propValueProperty.key, { | ||
type: 'Identifier', | ||
name: 'type' | ||
}) | ||
) | ||
) | ||
for (const prop of properties) { | ||
if (!prop) { | ||
continue | ||
} | ||
if (isLooksLike(prop.value, { type: 'Identifier', name: 'Object' })) { | ||
context.report({ | ||
node: prop, | ||
message: 'Object props has to contains type.' | ||
}) | ||
} | ||
if (prop.value.type === 'TSAsExpression') { | ||
const { typeAnnotation } = prop.value | ||
if ( | ||
[ | ||
'TSAnyKeyword', | ||
'TSTypeLiteral', | ||
'TSUnknownKeyword', | ||
'TSObjectKeyword' | ||
].includes(typeAnnotation.type) || | ||
!typeAnnotation.typeName || | ||
typeAnnotation.typeName.name !== 'Prop' | ||
) { | ||
context.report({ | ||
node: prop, | ||
message: 'Object props has to contains type.' | ||
przemyslawjanpietrzak marked this conversation as resolved.
Show resolved
Hide resolved
|
||
}) | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} |
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 @@ | ||
/** | ||
* @author *****your name***** | ||
FloEdelmann marked this conversation as resolved.
Show resolved
Hide resolved
|
||
* See LICENSE file in root directory for full license. | ||
*/ | ||
'use strict' | ||
|
||
const RuleTester = require('eslint').RuleTester | ||
const rule = require('../../../lib/rules/force-types-on-object-props') | ||
|
||
const template = (prop) => ` | ||
<script lang="ts"> | ||
import { Prop } from 'vue/types/options'; | ||
export default { | ||
props: { | ||
prop: { | ||
${prop} | ||
} | ||
} | ||
} | ||
</script> | ||
` | ||
|
||
const ruleTester = new RuleTester({ | ||
parser: require.resolve('vue-eslint-parser'), | ||
parserOptions: { | ||
ecmaVersion: 2015, | ||
sourceType: 'module', | ||
parser: '@typescript-eslint/parser' | ||
} | ||
}) | ||
|
||
ruleTester.run('force-types-on-object-props', rule, { | ||
valid: [ | ||
` | ||
<script lang="ts"> | ||
export default { | ||
} | ||
</script> | ||
`, | ||
` | ||
<script lang="ts"> | ||
export default { | ||
props: {} | ||
} | ||
</script> | ||
`, | ||
template('type: Object as Prop<{}>'), | ||
template('type: String'), | ||
template('type: Number'), | ||
template('type: Boolean'), | ||
template('type: [String, Number, Boolean]') | ||
FloEdelmann marked this conversation as resolved.
Show resolved
Hide resolved
|
||
], | ||
invalid: [ | ||
{ | ||
code: template('type: Object'), | ||
errors: [ | ||
{ | ||
message: 'Object props has to contains type.' | ||
} | ||
] | ||
}, | ||
{ | ||
code: template('type: Object as any'), | ||
errors: [ | ||
{ | ||
message: 'Object props has to contains type.' | ||
} | ||
] | ||
}, | ||
{ | ||
code: template('type: Object as {}'), | ||
errors: [ | ||
{ | ||
message: 'Object props has to contains type.' | ||
} | ||
] | ||
}, | ||
{ | ||
code: template('type: Object as unknown'), | ||
errors: [ | ||
{ | ||
message: 'Object props has to contains type.' | ||
} | ||
] | ||
}, | ||
{ | ||
code: template('type: Object as string'), | ||
errors: [ | ||
{ | ||
message: 'Object props has to contains type.' | ||
} | ||
] | ||
} | ||
] | ||
}) |
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.