Skip to content

Commit

Permalink
fix(compiler-sfc): check source for user imported bindings
Browse files Browse the repository at this point in the history
closes #6825
  • Loading branch information
sxzz committed Oct 5, 2022
1 parent c454aa5 commit 5ad8f0c
Showing 1 changed file with 20 additions and 13 deletions.
33 changes: 20 additions & 13 deletions packages/compiler-sfc/src/compileScript.ts
Expand Up @@ -128,6 +128,7 @@ export interface SFCScriptCompileOptions {
export interface ImportBinding {
isType: boolean
imported: string
local: string
source: string
isFromSetup: boolean
isUsedInTemplate: boolean
Expand Down Expand Up @@ -271,7 +272,6 @@ export function compileScript(
const bindingMetadata: BindingMetadata = {}
const helperImports: Set<string> = new Set()
const userImports: Record<string, ImportBinding> = Object.create(null)
const userImportAlias: Record<string, string> = Object.create(null)
const scriptBindings: Record<string, BindingTypes> = Object.create(null)
const setupBindings: Record<string, BindingTypes> = Object.create(null)

Expand Down Expand Up @@ -361,10 +361,6 @@ export function compileScript(
isFromSetup: boolean,
needTemplateUsageCheck: boolean
) {
if (source === 'vue' && imported) {
userImportAlias[imported] = local
}

// template usage check is only needed in non-inline mode, so we can skip
// the work if inlineTemplate is true.
let isUsedInTemplate = needTemplateUsageCheck
Expand All @@ -381,6 +377,7 @@ export function compileScript(
userImports[local] = {
isType,
imported: imported || 'default',
local,
source,
isFromSetup,
isUsedInTemplate
Expand Down Expand Up @@ -918,7 +915,7 @@ export function compileScript(
}
}
if (node.declaration) {
walkDeclaration(node.declaration, scriptBindings, userImportAlias)
walkDeclaration(node.declaration, scriptBindings, userImports)
}
} else if (
(node.type === 'VariableDeclaration' ||
Expand All @@ -927,7 +924,7 @@ export function compileScript(
node.type === 'TSEnumDeclaration') &&
!node.declare
) {
walkDeclaration(node, scriptBindings, userImportAlias)
walkDeclaration(node, scriptBindings, userImports)
}
}

Expand Down Expand Up @@ -1127,7 +1124,7 @@ export function compileScript(
node.type === 'ClassDeclaration') &&
!node.declare
) {
walkDeclaration(node, setupBindings, userImportAlias)
walkDeclaration(node, setupBindings, userImports)
}

// walk statements & named exports / variable declarations for top level
Expand Down Expand Up @@ -1582,8 +1579,17 @@ function registerBinding(
function walkDeclaration(
node: Declaration,
bindings: Record<string, BindingTypes>,
userImportAlias: Record<string, string>
userImports: Record<string, ImportBinding>
) {
function getUserBinding(name: string) {
const binding = Object.values(userImports).find(
binding => binding.source === 'vue' && binding.imported === name
)
if (binding) return binding.local
else if (!userImports[name]) return name
return undefined
}

if (node.type === 'VariableDeclaration') {
const isConst = node.kind === 'const'
// export const foo = ...
Expand All @@ -1597,7 +1603,7 @@ function walkDeclaration(
)
if (id.type === 'Identifier') {
let bindingType
const userReactiveBinding = userImportAlias['reactive'] || 'reactive'
const userReactiveBinding = getUserBinding('reactive')
if (isCallOf(init, userReactiveBinding)) {
// treat reactive() calls as let since it's meant to be mutable
bindingType = isConst
Expand All @@ -1613,7 +1619,7 @@ function walkDeclaration(
? BindingTypes.SETUP_REACTIVE_CONST
: BindingTypes.SETUP_CONST
} else if (isConst) {
if (isCallOf(init, userImportAlias['ref'] || 'ref')) {
if (isCallOf(init, getUserBinding('ref'))) {
bindingType = BindingTypes.SETUP_REF
} else {
bindingType = BindingTypes.SETUP_MAYBE_REF
Expand Down Expand Up @@ -1910,10 +1916,11 @@ function genRuntimeEmits(emits: Set<string>) {

function isCallOf(
node: Node | null | undefined,
test: string | ((id: string) => boolean)
test: string | ((id: string) => boolean) | null | undefined
): node is CallExpression {
return !!(
node &&
test &&
node.type === 'CallExpression' &&
node.callee.type === 'Identifier' &&
(typeof test === 'string'
Expand All @@ -1922,7 +1929,7 @@ function isCallOf(
)
}

function canNeverBeRef(node: Node, userReactiveImport: string): boolean {
function canNeverBeRef(node: Node, userReactiveImport?: string): boolean {
if (isCallOf(node, userReactiveImport)) {
return true
}
Expand Down

0 comments on commit 5ad8f0c

Please sign in to comment.