Skip to content

Commit ff61887

Browse files
committed
fix(compiler-sfc): throw error when import macro as alias
1 parent f8994da commit ff61887

File tree

1 file changed

+27
-20
lines changed

1 file changed

+27
-20
lines changed

packages/compiler-sfc/src/compileScript.ts

+27-20
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ import {
4949
} from './script/defineEmits'
5050
import { DEFINE_EXPOSE, processDefineExpose } from './script/defineExpose'
5151
import { DEFINE_OPTIONS, processDefineOptions } from './script/defineOptions'
52-
import { processDefineSlots } from './script/defineSlots'
52+
import { DEFINE_SLOTS, processDefineSlots } from './script/defineSlots'
5353
import { DEFINE_MODEL, processDefineModel } from './script/defineModel'
5454
import { getImportedName, isCallOf, isLiteralNode } from './script/utils'
5555
import { analyzeScriptBindings } from './script/analyzeScriptBindings'
@@ -135,6 +135,16 @@ export interface ImportBinding {
135135
isUsedInTemplate: boolean
136136
}
137137

138+
const MACROS = [
139+
DEFINE_PROPS,
140+
DEFINE_EMITS,
141+
DEFINE_EXPOSE,
142+
DEFINE_OPTIONS,
143+
DEFINE_SLOTS,
144+
DEFINE_MODEL,
145+
WITH_DEFAULTS,
146+
]
147+
138148
/**
139149
* Compile `<script setup>`
140150
* It requires the whole SFC descriptor because we need to handle and merge
@@ -317,15 +327,18 @@ export function compileScript(
317327
const imported = getImportedName(specifier)
318328
const source = node.source.value
319329
const existing = ctx.userImports[local]
320-
if (
321-
source === 'vue' &&
322-
(imported === DEFINE_PROPS ||
323-
imported === DEFINE_EMITS ||
324-
imported === DEFINE_EXPOSE)
325-
) {
326-
warnOnce(
327-
`\`${imported}\` is a compiler macro and no longer needs to be imported.`,
328-
)
330+
if (source === 'vue' && MACROS.includes(imported)) {
331+
if (local === imported) {
332+
warnOnce(
333+
`\`${imported}\` is a compiler macro and no longer needs to be imported.`,
334+
)
335+
} else {
336+
ctx.error(
337+
`\`${imported}\` is a compiler macro and cannot be aliased to ` +
338+
`a different name.`,
339+
specifier,
340+
)
341+
}
329342
removeSpecifier(i)
330343
} else if (existing) {
331344
if (existing.source === source && existing.imported === imported) {
@@ -1054,13 +1067,7 @@ function walkDeclaration(
10541067
// export const foo = ...
10551068
for (const { id, init: _init } of node.declarations) {
10561069
const init = _init && unwrapTSNode(_init)
1057-
const isDefineCall = !!(
1058-
isConst &&
1059-
isCallOf(
1060-
init,
1061-
c => c === DEFINE_PROPS || c === DEFINE_EMITS || c === WITH_DEFAULTS,
1062-
)
1063-
)
1070+
const isMacroCall = !!(isConst && isCallOf(init, c => MACROS.includes(c)))
10641071
if (id.type === 'Identifier') {
10651072
let bindingType
10661073
const userReactiveBinding = userImportAliases['reactive']
@@ -1077,7 +1084,7 @@ function walkDeclaration(
10771084
} else if (
10781085
// if a declaration is a const literal, we can mark it so that
10791086
// the generated render fn code doesn't need to unref() it
1080-
isDefineCall ||
1087+
isMacroCall ||
10811088
(isConst && canNeverBeRef(init!, userReactiveBinding))
10821089
) {
10831090
bindingType = isCallOf(init, DEFINE_PROPS)
@@ -1109,9 +1116,9 @@ function walkDeclaration(
11091116
continue
11101117
}
11111118
if (id.type === 'ObjectPattern') {
1112-
walkObjectPattern(id, bindings, isConst, isDefineCall)
1119+
walkObjectPattern(id, bindings, isConst, isMacroCall)
11131120
} else if (id.type === 'ArrayPattern') {
1114-
walkArrayPattern(id, bindings, isConst, isDefineCall)
1121+
walkArrayPattern(id, bindings, isConst, isMacroCall)
11151122
}
11161123
}
11171124
}

0 commit comments

Comments
 (0)