foo: number // property
bar(): void // method
'baz': string // string literal key
+ [\`qux\`]: boolean // template literal key
+ 123: symbol // numeric literal key
(e: 'foo'): void // call signature
(e: 'bar'): void
}>()`)
foo: ['Number'],
bar: ['Function'],
baz: ['String'],
+ qux: ['Boolean'],
+ 123: ['Symbol'],
})
expect(calls?.length).toBe(2)
})
type T = 'foo' | 'bar'
type S = 'x' | 'y'
defineProps<{
- [\`_\${T}_\${S}_\`]: string
+ [K in \`_\${T}_\${S}_\`]: string
}>()
`).props,
).toStrictEqual({
createGetCanonicalFileName,
getId,
getImportedName,
+ getStringLiteralKey,
joinPaths,
normalizePath,
} from './utils'
Object.assign(scope.types, typeParameters)
}
;(e as MaybeWithScope)._ownerScope = scope
- const name = getId(e.key)
- if (name && !e.computed) {
+ const name = getStringLiteralKey(e)
+ if (name !== null) {
res.props[name] = e as ResolvedElements['props'][string]
- } else if (e.key.type === 'TemplateLiteral') {
- for (const key of resolveTemplateKeys(ctx, e.key, scope)) {
- res.props[key] = e as ResolvedElements['props'][string]
- }
} else {
ctx.error(
`Unsupported computed key in type referenced by a macro`,
const prop = node.members.find(
m =>
m.type === 'TSPropertySignature' &&
- !m.computed &&
- getId(m.key) === key &&
+ getStringLiteralKey(m) === key &&
m.typeAnnotation,
)
return prop && prop.typeAnnotation!.typeAnnotation
ImportSpecifier,
Node,
StringLiteral,
+ TSMethodSignature,
+ TSPropertySignature,
} from '@babel/types'
import path from 'path'
: null
}
+export function getStringLiteralKey(
+ node: TSPropertySignature | TSMethodSignature,
+): string | null {
+ return node.computed
+ ? node.key.type === 'TemplateLiteral' && !node.key.expressions.length
+ ? node.key.quasis.map(q => q.value.cooked).join('')
+ : null
+ : node.key.type === 'Identifier'
+ ? node.key.name
+ : node.key.type === 'StringLiteral'
+ ? node.key.value
+ : node.key.type === 'NumericLiteral'
+ ? String(node.key.value)
+ : null
+}
+
const identity = (str: string) => str
const fileNameLowerCaseRegExp = /[^\u0130\u0131\u00DFa-z0-9\\/:\-_\. ]+/g
const toLowerCase = (str: string) => str.toLowerCase()