|
1 | 1 | /**
|
2 |
| - * @author *****your name***** |
| 2 | + * @author Przemysław Jan Beigert |
3 | 3 | * See LICENSE file in root directory for full license.
|
4 | 4 | */
|
5 | 5 | 'use strict'
|
6 | 6 |
|
7 |
| -// ------------------------------------------------------------------------------ |
8 |
| -// Requirements |
9 |
| -// ------------------------------------------------------------------------------ |
10 |
| - |
11 |
| -const utils = require('../utils') |
12 |
| - |
13 | 7 | // ------------------------------------------------------------------------------
|
14 | 8 | // Helpers
|
15 | 9 | // ------------------------------------------------------------------------------
|
16 | 10 |
|
17 |
| -// ... |
| 11 | +/** |
| 12 | + * Check if all keys and values from second object are resent in first object |
| 13 | + * |
| 14 | + * @param {{ [key: string]: any }} a object to |
| 15 | + * @param {{ [key: string]: any }} b The string to escape. |
| 16 | + * @returns {boolean} Returns the escaped string. |
| 17 | + */ |
| 18 | + const isLooksLike = (a, b) => { |
| 19 | + return ( |
| 20 | + a && |
| 21 | + b && |
| 22 | + Object.keys(b).every(bKey => { |
| 23 | + const bVal = b[bKey]; |
| 24 | + const aVal = a[bKey]; |
| 25 | + if (typeof bVal === 'function') { |
| 26 | + return bVal(aVal); |
| 27 | + } |
| 28 | + return bVal == null || /^[sbn]/.test(typeof bVal) ? bVal === aVal : isLooksLike(aVal, bVal); |
| 29 | + }) |
| 30 | + ) |
| 31 | +} |
18 | 32 |
|
19 |
| -// ------------------------------------------------------------------------------ |
| 33 | +//------------------------------------------------------------------------------ |
20 | 34 | // Rule Definition
|
21 |
| -// ------------------------------------------------------------------------------ |
| 35 | +//------------------------------------------------------------------------------ |
22 | 36 |
|
23 | 37 | module.exports = {
|
24 | 38 | meta: {
|
25 |
| - type: 'problem', |
26 | 39 | docs: {
|
27 |
| - description: '', |
28 |
| - categories: undefined, |
29 |
| - url: '' |
| 40 | + description: 'Force user to add type declaration to object props', |
| 41 | + category: 'type safe', |
| 42 | + recommended: false, |
30 | 43 | },
|
31 | 44 | fixable: null,
|
32 | 45 | schema: [],
|
33 |
| - messages: { |
34 |
| - // ... |
35 |
| - } |
36 | 46 | },
|
37 |
| - /** @param {RuleContext} context */ |
| 47 | + /** @param {RuleContext} context */ |
38 | 48 | create(context) {
|
39 |
| - // ... |
| 49 | + return { |
| 50 | + /** @param {ExportDefaultDeclaration} node */ |
| 51 | + ExportDefaultDeclaration(node) { |
| 52 | + if (node.declaration.type !== 'ObjectExpression') { |
| 53 | + return; |
| 54 | + } |
| 55 | + if (!Array.isArray(node.declaration.properties)) { |
| 56 | + return; |
| 57 | + } |
40 | 58 |
|
41 |
| - return utils.defineTemplateBodyVisitor(context, { |
42 |
| - // ... |
43 |
| - }) |
44 |
| - } |
45 |
| -} |
| 59 | + const property = node.declaration.properties.find(property => |
| 60 | + property.type === 'Property' && isLooksLike(property.key, { type: 'Identifier', name: 'props' }) && property.value.type === 'ObjectExpression') |
| 61 | + |
| 62 | + if (!property || property.type === 'SpreadElement' || !('properties' in property.value)) { |
| 63 | + return; |
| 64 | + } |
| 65 | + |
| 66 | + property.value.properties |
| 67 | + .filter(prop => prop.type === 'Property' && prop.value.type === 'ObjectExpression') |
| 68 | + .map(prop => prop.value.properties.find(propValueProperty => { |
| 69 | + return isLooksLike(propValueProperty.key, { type: 'Identifier', name: 'type' }) |
| 70 | + })) |
| 71 | + .forEach(prop => { |
| 72 | + if (!prop) { |
| 73 | + return; |
| 74 | + } |
| 75 | + if (isLooksLike(prop.value, { type: 'Identifier', name: 'Object' })) { |
| 76 | + context.report({ node: prop, message: 'Object props has to be typed' }) |
| 77 | + } |
| 78 | + if (prop.value.type === 'TSAsExpression') { |
| 79 | + const { typeAnnotation } = prop.value; |
| 80 | + if ( |
| 81 | + ['TSAnyKeyword', 'TSTypeLiteral', 'TSUnknownKeyword', 'TSObjectKeyword'].includes(typeAnnotation.type) |
| 82 | + || !typeAnnotation.typeName |
| 83 | + || typeAnnotation.typeName.name !== 'Prop' |
| 84 | + ) { |
| 85 | + context.report({ node: prop, message: 'Object props should be typed like this: "type: Object as Prop<T>"'}) |
| 86 | + } |
| 87 | + } |
| 88 | + }) |
| 89 | + }, |
| 90 | + }; |
| 91 | + }, |
| 92 | +}; |
0 commit comments