Skip to content

Commit

Permalink
prepare force types on object implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
przemyslawjanpietrzak committed Oct 3, 2022
1 parent 49d0f30 commit 9a5320b
Show file tree
Hide file tree
Showing 3 changed files with 165 additions and 47 deletions.
2 changes: 1 addition & 1 deletion docs/rules/force-types-on-object-props.md
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ Nothing.

## When Not To Use It

When you're not using TypeScript in the project.
When you're not using TypeScript in the project****.

## Further Reading

Expand Down
118 changes: 96 additions & 22 deletions lib/rules/force-types-on-object-props.js
Original file line number Diff line number Diff line change
@@ -1,45 +1,119 @@
/**
* @author *****your name*****
* @author Przemysław Jan Beigert
* See LICENSE file in root directory for full license.
*/
'use strict'

// ------------------------------------------------------------------------------
// Requirements
// ------------------------------------------------------------------------------

const utils = require('../utils')

// ------------------------------------------------------------------------------
// 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)
})

// ------------------------------------------------------------------------------
//------------------------------------------------------------------------------
// Rule Definition
// ------------------------------------------------------------------------------
//------------------------------------------------------------------------------

module.exports = {
meta: {
type: 'problem',
type: 'suggestion',
docs: {
description: '',
categories: undefined,
url: ''
description: 'enforce user to add type declaration to object props',
categories: ['type safe'],
recommended: false,
url: 'https://eslint.vuejs.org/rules/force-types-on-object-props.html'
},
fixable: null,
schema: [],
messages: {
// ...
}
schema: []
},
/** @param {RuleContext} context */
create(context) {
// ...
return {
/** @param {ExportDefaultDeclaration} node */
ExportDefaultDeclaration(node) {
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'
)

return utils.defineTemplateBodyVisitor(context, {
// ...
})
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) {
break
}
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.'
})
}
}
}
}
}
}
}
92 changes: 68 additions & 24 deletions tests/lib/rules/force-types-on-object-props.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,39 +7,83 @@
const RuleTester = require('eslint').RuleTester
const rule = require('../../../lib/rules/force-types-on-object-props')

const tester = new RuleTester({
const template = (prop) => `
<template></template>
<script>
export default {
}
</script>
`

const ruleTester = new RuleTester({
parser: require.resolve('vue-eslint-parser'),
parserOptions: {
ecmaVersion: 2020,
sourceType: 'module'
}
parserOptions: { ecmaVersion: 2015 }
})

tester.run('force-types-on-object-props', rule, {
ruleTester.run('force-types-on-object-props', rule, {
valid: [
{
filename: 'test.vue',
code: `
<template>
</template>
`
},
`
<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]')
],
invalid: [
{
filename: 'test.vue',
code: `
<template>
</template>
`,
code: template('type: Object'),
errors: [
{
message: 'Object props has to be typed'
}
]
},
{
code: template('type: Object as any'),
errors: [
{
message:
'Object props should be typed like this: "type: Object as Prop<T>"'
}
]
},
{
code: template('type: Object as {}'),
errors: [
{
message:
'Object props should be typed like this: "type: Object as Prop<T>"'
}
]
},
{
code: template('type: Object as unknown'),
errors: [
{
message:
'Object props should be typed like this: "type: Object as Prop<T>"'
}
]
},
{
code: template('type: Object as string'),
errors: [
{
message: '...',
line: 'line',
column: 'col'
},
message:
'Object props should be typed like this: "type: Object as Prop<T>"'
}
]
}
]
Expand Down

0 comments on commit 9a5320b

Please sign in to comment.