-
-
Notifications
You must be signed in to change notification settings - Fork 652
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 8acfba0
Showing
3 changed files
with
129 additions
and
52 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,92 @@ | ||
/** | ||
* @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) => { | ||
return ( | ||
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 || /^[sbn]/.test(typeof bVal) ? bVal === aVal : isLooksLike(aVal, bVal); | ||
}) | ||
) | ||
} | ||
|
||
// ------------------------------------------------------------------------------ | ||
//------------------------------------------------------------------------------ | ||
// Rule Definition | ||
// ------------------------------------------------------------------------------ | ||
//------------------------------------------------------------------------------ | ||
|
||
module.exports = { | ||
meta: { | ||
type: 'problem', | ||
docs: { | ||
description: '', | ||
categories: undefined, | ||
url: '' | ||
description: 'Force user to add type declaration to object props', | ||
category: 'type safe', | ||
recommended: false, | ||
}, | ||
fixable: null, | ||
schema: [], | ||
messages: { | ||
// ... | ||
} | ||
}, | ||
/** @param {RuleContext} context */ | ||
/** @param {RuleContext} context */ | ||
create(context) { | ||
// ... | ||
return { | ||
/** @param {ExportDefaultDeclaration} node */ | ||
ExportDefaultDeclaration(node) { | ||
if (node.declaration.type !== 'ObjectExpression') { | ||
return; | ||
} | ||
if (!Array.isArray(node.declaration.properties)) { | ||
return; | ||
} | ||
|
||
return utils.defineTemplateBodyVisitor(context, { | ||
// ... | ||
}) | ||
} | ||
} | ||
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; | ||
} | ||
|
||
property.value.properties | ||
.filter(prop => prop.type === 'Property' && prop.value.type === 'ObjectExpression') | ||
.map(prop => prop.value.properties.find(propValueProperty => { | ||
return isLooksLike(propValueProperty.key, { type: 'Identifier', name: 'type' }) | ||
})) | ||
.forEach(prop => { | ||
if (!prop) { | ||
return; | ||
} | ||
if (isLooksLike(prop.value, { type: 'Identifier', name: 'Object' })) { | ||
context.report({ node: prop, message: 'Object props has to be typed' }) | ||
} | ||
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 should be typed like this: "type: Object as Prop<T>"'}) | ||
} | ||
} | ||
}) | ||
}, | ||
}; | ||
}, | ||
}; |
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