Skip to content

Commit 0002567

Browse files
authored
feat(compiler-sfc): support module string names syntax (#7428)
tc39/ecma262#2154
1 parent d60e58c commit 0002567

File tree

4 files changed

+49
-14
lines changed

4 files changed

+49
-14
lines changed

packages/compiler-core/src/babelUtils.ts

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,10 @@ import type {
66
Function,
77
ObjectProperty,
88
BlockStatement,
9-
Program
9+
Program,
10+
ImportDefaultSpecifier,
11+
ImportNamespaceSpecifier,
12+
ImportSpecifier
1013
} from '@babel/types'
1114
import { walk } from 'estree-walker'
1215

@@ -243,6 +246,17 @@ export const isStaticProperty = (node: Node): node is ObjectProperty =>
243246
export const isStaticPropertyKey = (node: Node, parent: Node) =>
244247
isStaticProperty(parent) && parent.key === node
245248

249+
export function getImportedName(
250+
specifier: ImportSpecifier | ImportDefaultSpecifier | ImportNamespaceSpecifier
251+
) {
252+
if (specifier.type === 'ImportSpecifier')
253+
return specifier.imported.type === 'Identifier'
254+
? specifier.imported.name
255+
: specifier.imported.value
256+
else if (specifier.type === 'ImportNamespaceSpecifier') return '*'
257+
return 'default'
258+
}
259+
246260
/**
247261
* Copied from https://github.com/babel/babel/blob/main/packages/babel-types/src/validators/isReferenced.ts
248262
* To avoid runtime dependency on @babel/types (which includes process references)

packages/compiler-sfc/__tests__/__snapshots__/compileScript.spec.ts.snap

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1027,6 +1027,20 @@ return { ref }
10271027
}"
10281028
`;
10291029

1030+
exports[`SFC compile <script setup> > imports > should support module string names syntax 1`] = `
1031+
"import { \\"😏\\" as foo } from './foo'
1032+
1033+
export default {
1034+
setup(__props, { expose }) {
1035+
expose();
1036+
1037+
1038+
return { get foo() { return foo } }
1039+
}
1040+
1041+
}"
1042+
`;
1043+
10301044
exports[`SFC compile <script setup> > inlineTemplate mode > avoid unref() when necessary 1`] = `
10311045
"import { unref as _unref, toDisplayString as _toDisplayString, createTextVNode as _createTextVNode, withCtx as _withCtx, createVNode as _createVNode, createElementVNode as _createElementVNode, Fragment as _Fragment, openBlock as _openBlock, createElementBlock as _createElementBlock } from \\"vue\\"
10321046

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

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -515,6 +515,21 @@ defineExpose({ foo: 123 })
515515
})
516516
})
517517
})
518+
519+
test('should support module string names syntax', () => {
520+
const { content, bindings } = compile(`
521+
<script>
522+
import { "😏" as foo } from './foo'
523+
</script>
524+
<script setup>
525+
import { "😏" as foo } from './foo'
526+
</script>
527+
`)
528+
assertCode(content)
529+
expect(bindings).toStrictEqual({
530+
foo: BindingTypes.SETUP_MAYBE_REF
531+
})
532+
})
518533
})
519534

520535
// in dev mode, declared bindings are returned as an object from setup()

packages/compiler-sfc/src/compileScript.ts

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import {
1010
SimpleExpressionNode,
1111
isFunctionType,
1212
walkIdentifiers,
13+
getImportedName,
1314
unwrapTSNode
1415
} from '@vue/compiler-dom'
1516
import { DEFAULT_FILENAME, SFCDescriptor, SFCScriptBlock } from './parse'
@@ -380,7 +381,7 @@ export function compileScript(
380381
function registerUserImport(
381382
source: string,
382383
local: string,
383-
imported: string | false,
384+
imported: string,
384385
isType: boolean,
385386
isFromSetup: boolean,
386387
needTemplateUsageCheck: boolean
@@ -400,7 +401,7 @@ export function compileScript(
400401

401402
userImports[local] = {
402403
isType,
403-
imported: imported || 'default',
404+
imported,
404405
local,
405406
source,
406407
isFromSetup,
@@ -1002,10 +1003,7 @@ export function compileScript(
10021003
if (node.type === 'ImportDeclaration') {
10031004
// record imports for dedupe
10041005
for (const specifier of node.specifiers) {
1005-
const imported =
1006-
specifier.type === 'ImportSpecifier' &&
1007-
specifier.imported.type === 'Identifier' &&
1008-
specifier.imported.name
1006+
const imported = getImportedName(specifier)
10091007
registerUserImport(
10101008
node.source.value,
10111009
specifier.local.name,
@@ -1047,13 +1045,7 @@ export function compileScript(
10471045
for (let i = 0; i < node.specifiers.length; i++) {
10481046
const specifier = node.specifiers[i]
10491047
const local = specifier.local.name
1050-
let imported =
1051-
specifier.type === 'ImportSpecifier' &&
1052-
specifier.imported.type === 'Identifier' &&
1053-
specifier.imported.name
1054-
if (specifier.type === 'ImportNamespaceSpecifier') {
1055-
imported = '*'
1056-
}
1048+
const imported = getImportedName(specifier)
10571049
const source = node.source.value
10581050
const existing = userImports[local]
10591051
if (

0 commit comments

Comments
 (0)