-
-
Notifications
You must be signed in to change notification settings - Fork 669
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
prepare force types on object implementation
- Loading branch information
1 parent
49d0f30
commit 9a5320b
Showing
3 changed files
with
165 additions
and
47 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 |
---|---|---|
@@ -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.' | ||
}) | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} |
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