Skip to content

Commit 3fd1a11

Browse files
committed
feat(reactivity-transform): support
1 parent 3313d36 commit 3fd1a11

File tree

5 files changed

+46
-23
lines changed

5 files changed

+46
-23
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

@@ -245,6 +248,17 @@ export const isStaticProperty = (node: Node): node is ObjectProperty =>
245248
export const isStaticPropertyKey = (node: Node, parent: Node) =>
246249
isStaticProperty(parent) && parent.key === node
247250

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

packages/compiler-sfc/src/compileScript.ts

Lines changed: 4 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@ import {
99
UNREF,
1010
SimpleExpressionNode,
1111
isFunctionType,
12-
walkIdentifiers
12+
walkIdentifiers,
13+
getImportedName
1314
} from '@vue/compiler-dom'
1415
import { DEFAULT_FILENAME, SFCDescriptor, SFCScriptBlock } from './parse'
1516
import {
@@ -380,20 +381,6 @@ export function compileScript(
380381
s.move(start, end, 0)
381382
}
382383

383-
function getImported(
384-
specifier:
385-
| ImportSpecifier
386-
| ImportDefaultSpecifier
387-
| ImportNamespaceSpecifier
388-
) {
389-
if (specifier.type === 'ImportSpecifier')
390-
return specifier.imported.type === 'Identifier'
391-
? specifier.imported.name
392-
: specifier.imported.value
393-
else if (specifier.type === 'ImportNamespaceSpecifier') return '*'
394-
return 'default'
395-
}
396-
397384
function registerUserImport(
398385
source: string,
399386
local: string,
@@ -974,7 +961,7 @@ export function compileScript(
974961
if (node.type === 'ImportDeclaration') {
975962
// record imports for dedupe
976963
for (const specifier of node.specifiers) {
977-
const imported = getImported(specifier)
964+
const imported = getImportedName(specifier)
978965
registerUserImport(
979966
node.source.value,
980967
specifier.local.name,
@@ -1016,7 +1003,7 @@ export function compileScript(
10161003
for (let i = 0; i < node.specifiers.length; i++) {
10171004
const specifier = node.specifiers[i]
10181005
const local = specifier.local.name
1019-
const imported = getImported(specifier)
1006+
const imported = getImportedName(specifier)
10201007
const source = node.source.value
10211008
const existing = userImports[local]
10221009
if (

packages/reactivity-transform/__tests__/__snapshots__/reactivityTransform.spec.ts.snap

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -280,6 +280,15 @@ const props = defineProps<{msg: string; ids?: string[]}>()
280280
let ids = _ref([])"
281281
`;
282282
283+
exports[`should support module string names syntax 1`] = `
284+
"
285+
286+
287+
let a = (ref(0));
288+
console.log((a))
289+
"
290+
`;
291+
283292
exports[`using ref binding in property shorthand 1`] = `
284293
"import { ref as _ref } from 'vue'
285294

packages/reactivity-transform/__tests__/reactivityTransform.spec.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -460,6 +460,22 @@ test('macro import alias and removal', () => {
460460
assertCode(code)
461461
})
462462

463+
test('should support module string names syntax', () => {
464+
const { code } = transform(
465+
`
466+
import { "$" as fromRefs, "$$" as escapeRefs } from 'vue/macros'
467+
468+
let a = fromRefs(ref(0));
469+
console.log(escapeRefs(a))
470+
`
471+
)
472+
// should remove imports
473+
expect(code).not.toMatch(`from 'vue/macros'`)
474+
expect(code).toMatch(`let a = (ref(0))`)
475+
expect(code).toMatch(`console.log((a))`)
476+
assertCode(code)
477+
})
478+
463479
// #6838
464480
test('should not overwrite importing', () => {
465481
const { code } = transform(

packages/reactivity-transform/src/reactivityTransform.ts

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import MagicString, { SourceMap } from 'magic-string'
1818
import { walk } from 'estree-walker'
1919
import {
2020
extractIdentifiers,
21+
getImportedName,
2122
isFunctionType,
2223
isInDestructureAssignment,
2324
isReferencedIdentifier,
@@ -199,11 +200,7 @@ export function transformAST(
199200

200201
for (const specifier of node.specifiers) {
201202
const local = specifier.local.name
202-
const imported =
203-
(specifier.type === 'ImportSpecifier' &&
204-
specifier.imported.type === 'Identifier' &&
205-
specifier.imported.name) ||
206-
'default'
203+
const imported = getImportedName(specifier)
207204
userImports[local] = {
208205
source,
209206
local,

0 commit comments

Comments
 (0)