|
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 | + a && |
| 20 | + b && |
| 21 | + Object.keys(b).every((bKey) => { |
| 22 | + const bVal = b[bKey] |
| 23 | + const aVal = a[bKey] |
| 24 | + if (typeof bVal === 'function') { |
| 25 | + return bVal(aVal) |
| 26 | + } |
| 27 | + return bVal == null || /^[bns]/.test(typeof bVal) |
| 28 | + ? bVal === aVal |
| 29 | + : isLooksLike(aVal, bVal) |
| 30 | + }) |
18 | 31 |
|
19 |
| -// ------------------------------------------------------------------------------ |
| 32 | +//------------------------------------------------------------------------------ |
20 | 33 | // Rule Definition
|
21 |
| -// ------------------------------------------------------------------------------ |
| 34 | +//------------------------------------------------------------------------------ |
22 | 35 |
|
23 | 36 | module.exports = {
|
24 | 37 | meta: {
|
25 |
| - type: 'problem', |
| 38 | + type: 'suggestion', |
26 | 39 | docs: {
|
27 |
| - description: '', |
28 |
| - categories: undefined, |
29 |
| - url: '' |
| 40 | + description: 'enforce user to add type declaration to object props', |
| 41 | + categories: ['type safe'], |
| 42 | + recommended: false, |
| 43 | + url: 'https://eslint.vuejs.org/rules/force-types-on-object-props.html' |
30 | 44 | },
|
31 | 45 | fixable: null,
|
32 |
| - schema: [], |
33 |
| - messages: { |
34 |
| - // ... |
35 |
| - } |
| 46 | + schema: [] |
36 | 47 | },
|
37 | 48 | /** @param {RuleContext} context */
|
38 | 49 | create(context) {
|
39 |
| - // ... |
| 50 | + return { |
| 51 | + /** @param {ExportDefaultDeclaration} node */ |
| 52 | + ExportDefaultDeclaration(node) { |
| 53 | + if (node.declaration.type !== 'ObjectExpression') { |
| 54 | + return |
| 55 | + } |
| 56 | + if (!Array.isArray(node.declaration.properties)) { |
| 57 | + return |
| 58 | + } |
| 59 | + |
| 60 | + const property = node.declaration.properties.find( |
| 61 | + (property) => |
| 62 | + property.type === 'Property' && |
| 63 | + isLooksLike(property.key, { type: 'Identifier', name: 'props' }) && |
| 64 | + property.value.type === 'ObjectExpression' |
| 65 | + ) |
40 | 66 |
|
41 |
| - return utils.defineTemplateBodyVisitor(context, { |
42 |
| - // ... |
43 |
| - }) |
| 67 | + if ( |
| 68 | + !property || |
| 69 | + property.type === 'SpreadElement' || |
| 70 | + !('properties' in property.value) |
| 71 | + ) { |
| 72 | + return |
| 73 | + } |
| 74 | + const properties = property.value.properties |
| 75 | + .filter( |
| 76 | + (prop) => |
| 77 | + prop.type === 'Property' && prop.value.type === 'ObjectExpression' |
| 78 | + ) |
| 79 | + .map((prop) => |
| 80 | + prop.value.properties.find((propValueProperty) => |
| 81 | + isLooksLike(propValueProperty.key, { |
| 82 | + type: 'Identifier', |
| 83 | + name: 'type' |
| 84 | + }) |
| 85 | + ) |
| 86 | + ) |
| 87 | + for (const prop of properties) { |
| 88 | + if (!prop) { |
| 89 | + break |
| 90 | + } |
| 91 | + if (isLooksLike(prop.value, { type: 'Identifier', name: 'Object' })) { |
| 92 | + context.report({ |
| 93 | + node: prop, |
| 94 | + message: 'Object props has to contains type.' |
| 95 | + }) |
| 96 | + } |
| 97 | + if (prop.value.type === 'TSAsExpression') { |
| 98 | + const { typeAnnotation } = prop.value |
| 99 | + if ( |
| 100 | + [ |
| 101 | + 'TSAnyKeyword', |
| 102 | + 'TSTypeLiteral', |
| 103 | + 'TSUnknownKeyword', |
| 104 | + 'TSObjectKeyword' |
| 105 | + ].includes(typeAnnotation.type) || |
| 106 | + !typeAnnotation.typeName || |
| 107 | + typeAnnotation.typeName.name !== 'Prop' |
| 108 | + ) { |
| 109 | + context.report({ |
| 110 | + node: prop, |
| 111 | + message: 'Object props has to contains type.' |
| 112 | + }) |
| 113 | + } |
| 114 | + } |
| 115 | + } |
| 116 | + } |
| 117 | + } |
44 | 118 | }
|
45 | 119 | }
|
0 commit comments