Skip to content

Commit 75d44c7

Browse files
authored
fix(compiler-sfc): resolve numeric literals and template literals without expressions as static property key (#13998)
1 parent dcc6f36 commit 75d44c7

File tree

3 files changed

+27
-9
lines changed

3 files changed

+27
-9
lines changed

packages/compiler-sfc/__tests__/compileScript/resolveType.spec.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,17 @@ describe('resolveType', () => {
2020
foo: number // property
2121
bar(): void // method
2222
'baz': string // string literal key
23+
[\`qux\`]: boolean // template literal key
24+
123: symbol // numeric literal key
2325
(e: 'foo'): void // call signature
2426
(e: 'bar'): void
2527
}>()`)
2628
expect(props).toStrictEqual({
2729
foo: ['Number'],
2830
bar: ['Function'],
2931
baz: ['String'],
32+
qux: ['Boolean'],
33+
123: ['Symbol'],
3034
})
3135
expect(calls?.length).toBe(2)
3236
})
@@ -195,7 +199,7 @@ describe('resolveType', () => {
195199
type T = 'foo' | 'bar'
196200
type S = 'x' | 'y'
197201
defineProps<{
198-
[\`_\${T}_\${S}_\`]: string
202+
[K in \`_\${T}_\${S}_\`]: string
199203
}>()
200204
`).props,
201205
).toStrictEqual({

packages/compiler-sfc/src/script/resolveType.ts

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ import {
2929
createGetCanonicalFileName,
3030
getId,
3131
getImportedName,
32+
getStringLiteralKey,
3233
joinPaths,
3334
normalizePath,
3435
} from './utils'
@@ -336,13 +337,9 @@ function typeElementsToMap(
336337
Object.assign(scope.types, typeParameters)
337338
}
338339
;(e as MaybeWithScope)._ownerScope = scope
339-
const name = getId(e.key)
340-
if (name && !e.computed) {
340+
const name = getStringLiteralKey(e)
341+
if (name !== null) {
341342
res.props[name] = e as ResolvedElements['props'][string]
342-
} else if (e.key.type === 'TemplateLiteral') {
343-
for (const key of resolveTemplateKeys(ctx, e.key, scope)) {
344-
res.props[key] = e as ResolvedElements['props'][string]
345-
}
346343
} else {
347344
ctx.error(
348345
`Unsupported computed key in type referenced by a macro`,
@@ -2029,8 +2026,7 @@ function findStaticPropertyType(node: TSTypeLiteral, key: string) {
20292026
const prop = node.members.find(
20302027
m =>
20312028
m.type === 'TSPropertySignature' &&
2032-
!m.computed &&
2033-
getId(m.key) === key &&
2029+
getStringLiteralKey(m) === key &&
20342030
m.typeAnnotation,
20352031
)
20362032
return prop && prop.typeAnnotation!.typeAnnotation

packages/compiler-sfc/src/script/utils.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ import type {
77
ImportSpecifier,
88
Node,
99
StringLiteral,
10+
TSMethodSignature,
11+
TSPropertySignature,
1012
} from '@babel/types'
1113
import path from 'path'
1214

@@ -79,6 +81,22 @@ export function getId(node: Expression) {
7981
: null
8082
}
8183

84+
export function getStringLiteralKey(
85+
node: TSPropertySignature | TSMethodSignature,
86+
): string | null {
87+
return node.computed
88+
? node.key.type === 'TemplateLiteral' && !node.key.expressions.length
89+
? node.key.quasis.map(q => q.value.cooked).join('')
90+
: null
91+
: node.key.type === 'Identifier'
92+
? node.key.name
93+
: node.key.type === 'StringLiteral'
94+
? node.key.value
95+
: node.key.type === 'NumericLiteral'
96+
? String(node.key.value)
97+
: null
98+
}
99+
82100
const identity = (str: string) => str
83101
const fileNameLowerCaseRegExp = /[^\u0130\u0131\u00DFa-z0-9\\/:\-_\. ]+/g
84102
const toLowerCase = (str: string) => str.toLowerCase()

0 commit comments

Comments
 (0)