|
| 1 | +const { findVariable } = require('eslint-utils') |
| 2 | +/** |
| 3 | + * @typedef {import('@typescript-eslint/types').TSESTree.TypeNode} TypeNode |
| 4 | + * @typedef {import('@typescript-eslint/types').TSESTree.TSInterfaceBody} TSInterfaceBody |
| 5 | + * @typedef {import('@typescript-eslint/types').TSESTree.TSTypeLiteral} TSTypeLiteral |
| 6 | + */ |
| 7 | +/** |
| 8 | + * @typedef {import('../../typings/eslint-plugin-vue/util-types/utils').ComponentTypeProp} ComponentTypeProp |
| 9 | + */ |
| 10 | + |
| 11 | +module.exports = { |
| 12 | + getComponentPropsFromTypeDefine |
| 13 | +} |
| 14 | + |
| 15 | +/** |
| 16 | + * @param {TypeNode} node |
| 17 | + * @returns {node is TSTypeLiteral} |
| 18 | + */ |
| 19 | +function isTSTypeLiteral(node) { |
| 20 | + return node.type === 'TSTypeLiteral' |
| 21 | +} |
| 22 | + |
| 23 | +/** |
| 24 | + * Get all props by looking at all component's properties |
| 25 | + * @param {RuleContext} context The ESLint rule context object. |
| 26 | + * @param {TypeNode} propsNode Type with props definition |
| 27 | + * @return {ComponentTypeProp[]} Array of component props |
| 28 | + */ |
| 29 | +function getComponentPropsFromTypeDefine(context, propsNode) { |
| 30 | + /** @type {TSInterfaceBody | TSTypeLiteral|null} */ |
| 31 | + const defNode = resolveQualifiedType(context, propsNode, isTSTypeLiteral) |
| 32 | + if (!defNode) { |
| 33 | + return [] |
| 34 | + } |
| 35 | + return [...extractRuntimeProps(context, defNode)] |
| 36 | +} |
| 37 | + |
| 38 | +/** |
| 39 | + * @see https://github.com/vuejs/vue-next/blob/253ca2729d808fc051215876aa4af986e4caa43c/packages/compiler-sfc/src/compileScript.ts#L1512 |
| 40 | + * @param {RuleContext} context The ESLint rule context object. |
| 41 | + * @param {TSTypeLiteral | TSInterfaceBody} node |
| 42 | + * @returns {IterableIterator<ComponentTypeProp>} |
| 43 | + */ |
| 44 | +function* extractRuntimeProps(context, node) { |
| 45 | + const members = node.type === 'TSTypeLiteral' ? node.members : node.body |
| 46 | + for (const m of members) { |
| 47 | + if ( |
| 48 | + (m.type === 'TSPropertySignature' || m.type === 'TSMethodSignature') && |
| 49 | + m.key.type === 'Identifier' |
| 50 | + ) { |
| 51 | + let type |
| 52 | + if (m.type === 'TSMethodSignature') { |
| 53 | + type = ['Function'] |
| 54 | + } else if (m.typeAnnotation) { |
| 55 | + type = inferRuntimeType(context, m.typeAnnotation.typeAnnotation) |
| 56 | + } |
| 57 | + yield { |
| 58 | + type: 'type', |
| 59 | + key: /** @type {Identifier} */ (m.key), |
| 60 | + propName: m.key.name, |
| 61 | + value: null, |
| 62 | + node: /** @type {TSPropertySignature | TSMethodSignature} */ (m), |
| 63 | + |
| 64 | + required: !m.optional, |
| 65 | + types: type || [`null`] |
| 66 | + } |
| 67 | + } |
| 68 | + } |
| 69 | +} |
| 70 | + |
| 71 | +/** |
| 72 | + * @see https://github.com/vuejs/vue-next/blob/253ca2729d808fc051215876aa4af986e4caa43c/packages/compiler-sfc/src/compileScript.ts#L425 |
| 73 | + * |
| 74 | + * @param {RuleContext} context The ESLint rule context object. |
| 75 | + * @param {TypeNode} node |
| 76 | + * @param {(n: TypeNode)=> boolean } qualifier |
| 77 | + */ |
| 78 | +function resolveQualifiedType(context, node, qualifier) { |
| 79 | + if (qualifier(node)) { |
| 80 | + return node |
| 81 | + } |
| 82 | + if (node.type === 'TSTypeReference' && node.typeName.type === 'Identifier') { |
| 83 | + const refName = node.typeName.name |
| 84 | + const variable = findVariable(context.getScope(), refName) |
| 85 | + if (variable && variable.defs.length === 1) { |
| 86 | + const def = variable.defs[0] |
| 87 | + if (def.node.type === 'TSInterfaceDeclaration') { |
| 88 | + return /** @type {any} */ (def.node).body |
| 89 | + } |
| 90 | + if (def.node.type === 'TSTypeAliasDeclaration') { |
| 91 | + const typeAnnotation = /** @type {any} */ (def.node).typeAnnotation |
| 92 | + return qualifier(typeAnnotation) ? typeAnnotation : null |
| 93 | + } |
| 94 | + } |
| 95 | + } |
| 96 | +} |
| 97 | + |
| 98 | +/** |
| 99 | + * @param {RuleContext} context The ESLint rule context object. |
| 100 | + * @param {TypeNode} node |
| 101 | + * @param {Set<TypeNode>} [checked] |
| 102 | + * @returns {string[]} |
| 103 | + */ |
| 104 | +function inferRuntimeType(context, node, checked = new Set()) { |
| 105 | + switch (node.type) { |
| 106 | + case 'TSStringKeyword': |
| 107 | + return ['String'] |
| 108 | + case 'TSNumberKeyword': |
| 109 | + return ['Number'] |
| 110 | + case 'TSBooleanKeyword': |
| 111 | + return ['Boolean'] |
| 112 | + case 'TSObjectKeyword': |
| 113 | + return ['Object'] |
| 114 | + case 'TSTypeLiteral': |
| 115 | + return ['Object'] |
| 116 | + case 'TSFunctionType': |
| 117 | + return ['Function'] |
| 118 | + case 'TSArrayType': |
| 119 | + case 'TSTupleType': |
| 120 | + return ['Array'] |
| 121 | + |
| 122 | + case 'TSLiteralType': |
| 123 | + switch (node.literal.type) { |
| 124 | + //@ts-ignore ? |
| 125 | + case 'StringLiteral': |
| 126 | + return ['String'] |
| 127 | + //@ts-ignore ? |
| 128 | + case 'BooleanLiteral': |
| 129 | + return ['Boolean'] |
| 130 | + //@ts-ignore ? |
| 131 | + case 'NumericLiteral': |
| 132 | + //@ts-ignore ? |
| 133 | + // eslint-disable-next-line no-fallthrough |
| 134 | + case 'BigIntLiteral': |
| 135 | + return ['Number'] |
| 136 | + default: |
| 137 | + return [`null`] |
| 138 | + } |
| 139 | + |
| 140 | + case 'TSTypeReference': |
| 141 | + if (node.typeName.type === 'Identifier') { |
| 142 | + const variable = findVariable(context.getScope(), node.typeName.name) |
| 143 | + if (variable && variable.defs.length === 1) { |
| 144 | + const def = variable.defs[0] |
| 145 | + if (def.node.type === 'TSInterfaceDeclaration') { |
| 146 | + return [`Object`] |
| 147 | + } |
| 148 | + if (def.node.type === 'TSTypeAliasDeclaration') { |
| 149 | + const typeAnnotation = /** @type {any} */ (def.node).typeAnnotation |
| 150 | + if (!checked.has(typeAnnotation)) { |
| 151 | + checked.add(typeAnnotation) |
| 152 | + return inferRuntimeType(context, typeAnnotation, checked) |
| 153 | + } |
| 154 | + } |
| 155 | + } |
| 156 | + switch (node.typeName.name) { |
| 157 | + case 'Array': |
| 158 | + case 'Function': |
| 159 | + case 'Object': |
| 160 | + case 'Set': |
| 161 | + case 'Map': |
| 162 | + case 'WeakSet': |
| 163 | + case 'WeakMap': |
| 164 | + return [node.typeName.name] |
| 165 | + case 'Record': |
| 166 | + case 'Partial': |
| 167 | + case 'Readonly': |
| 168 | + case 'Pick': |
| 169 | + case 'Omit': |
| 170 | + case 'Exclude': |
| 171 | + case 'Extract': |
| 172 | + case 'Required': |
| 173 | + case 'InstanceType': |
| 174 | + return ['Object'] |
| 175 | + } |
| 176 | + } |
| 177 | + return [`null`] |
| 178 | + |
| 179 | + case 'TSUnionType': |
| 180 | + const set = new Set() |
| 181 | + for (const t of node.types) { |
| 182 | + for (const tt of inferRuntimeType(context, t, checked)) { |
| 183 | + set.add(tt) |
| 184 | + } |
| 185 | + } |
| 186 | + return [...set] |
| 187 | + |
| 188 | + case 'TSIntersectionType': |
| 189 | + return ['Object'] |
| 190 | + |
| 191 | + default: |
| 192 | + return [`null`] // no runtime check |
| 193 | + } |
| 194 | +} |
0 commit comments