Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 24 additions & 12 deletions packages/compiler-sfc/src/compileScript.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ import {
genRuntimeEmits,
processDefineEmits,
} from './script/defineEmits'
import { DEFINE_EXPOSE, processDefineExpose } from './script/defineExpose'
import { processDefineExpose } from './script/defineExpose'
import { DEFINE_OPTIONS, processDefineOptions } from './script/defineOptions'
import { processDefineSlots } from './script/defineSlots'
import { DEFINE_MODEL, processDefineModel } from './script/defineModel'
Expand Down Expand Up @@ -236,6 +236,10 @@ export function compileScript(
isUsedInTemplate = isImportUsed(local, sfc)
}

if (Object.keys(ctx.macrosAliases).includes(imported)) {
ctx.macrosAliases[imported] = undefined
}

ctx.userImports[local] = {
isType,
imported,
Expand Down Expand Up @@ -311,21 +315,22 @@ export function compileScript(
)
}

for (let i = 0; i < node.specifiers.length; i++) {
const specifier = node.specifiers[i]
const source = node.source.value
for (const [i, specifier] of node.specifiers.entries()) {
const local = specifier.local.name
const imported = getImportedName(specifier)
const source = node.source.value
const existing = ctx.userImports[local]
if (
source === 'vue' &&
(imported === DEFINE_PROPS ||
imported === DEFINE_EMITS ||
imported === DEFINE_EXPOSE)
Object.keys(ctx.macrosAliases).includes(imported)
) {
warnOnce(
`\`${imported}\` is a compiler macro and no longer needs to be imported.`,
)
if (local === imported)
warnOnce(
`\`${imported}\` is a compiler macro and no longer needs to be imported.`,
)
else {
ctx.macrosAliases[imported] = local
}
removeSpecifier(i)
} else if (existing) {
if (existing.source === source && existing.imported === imported) {
Expand Down Expand Up @@ -450,6 +455,7 @@ export function compileScript(
node.declaration,
scriptBindings,
vueImportAliases,
ctx.macrosAliases,
hoistStatic,
)
}
Expand All @@ -465,6 +471,7 @@ export function compileScript(
node,
scriptBindings,
vueImportAliases,
ctx.macrosAliases,
hoistStatic,
)
}
Expand Down Expand Up @@ -586,6 +593,7 @@ export function compileScript(
node,
setupBindings,
vueImportAliases,
ctx.macrosAliases,
hoistStatic,
)
}
Expand Down Expand Up @@ -1039,6 +1047,7 @@ function walkDeclaration(
node: Declaration,
bindings: Record<string, BindingTypes>,
userImportAliases: Record<string, string>,
macrosAliases: Record<string, string | undefined>,
hoistStatic: boolean,
): boolean {
let isAllLiteral = false
Expand All @@ -1058,7 +1067,10 @@ function walkDeclaration(
isConst &&
isCallOf(
init,
c => c === DEFINE_PROPS || c === DEFINE_EMITS || c === WITH_DEFAULTS,
c =>
c === macrosAliases[DEFINE_PROPS] ||
c === macrosAliases[DEFINE_EMITS] ||
c === macrosAliases[WITH_DEFAULTS],
)
)
if (id.type === 'Identifier') {
Expand Down Expand Up @@ -1093,7 +1105,7 @@ function walkDeclaration(
m === userImportAliases['shallowRef'] ||
m === userImportAliases['customRef'] ||
m === userImportAliases['toRef'] ||
m === DEFINE_MODEL,
m === macrosAliases[DEFINE_MODEL],
)
) {
bindingType = BindingTypes.SETUP_REF
Expand Down
21 changes: 19 additions & 2 deletions packages/compiler-sfc/src/script/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,19 @@ import type { SFCDescriptor } from '../parse'
import { generateCodeFrame, isArray } from '@vue/shared'
import { type ParserPlugin, parse as babelParse } from '@babel/parser'
import type { ImportBinding, SFCScriptCompileOptions } from '../compileScript'
import type { PropsDestructureBindings } from './defineProps'
import type { ModelDecl } from './defineModel'
import {
DEFINE_PROPS,
type PropsDestructureBindings,
WITH_DEFAULTS,
} from './defineProps'
import { DEFINE_MODEL, type ModelDecl } from './defineModel'
import type { BindingMetadata } from '../../../compiler-core/src'
import MagicString from 'magic-string'
import type { TypeScope } from './resolveType'
import { DEFINE_EMITS } from './defineEmits'
import { DEFINE_EXPOSE } from './defineExpose'
import { DEFINE_OPTIONS } from './defineOptions'
import { DEFINE_SLOTS } from './defineSlots'

export class ScriptCompileContext {
isJS: boolean
Expand All @@ -27,6 +35,15 @@ export class ScriptCompileContext {
scope?: TypeScope
globalScopes?: TypeScope[]
userImports: Record<string, ImportBinding> = Object.create(null)
macrosAliases: Record<string, string | undefined> = {
[DEFINE_PROPS]: DEFINE_PROPS,
[WITH_DEFAULTS]: WITH_DEFAULTS,
[DEFINE_EMITS]: DEFINE_EMITS,
[DEFINE_EXPOSE]: DEFINE_EXPOSE,
[DEFINE_OPTIONS]: DEFINE_OPTIONS,
[DEFINE_SLOTS]: DEFINE_SLOTS,
[DEFINE_MODEL]: DEFINE_MODEL,
}

// macros presence check
hasDefinePropsCall = false
Expand Down
2 changes: 1 addition & 1 deletion packages/compiler-sfc/src/script/defineEmits.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ export function processDefineEmits(
node: Node,
declId?: LVal,
): boolean {
if (!isCallOf(node, DEFINE_EMITS)) {
if (!isCallOf(node, ctx.macrosAliases[DEFINE_EMITS])) {
return false
}
if (ctx.hasDefineEmitCall) {
Expand Down
14 changes: 7 additions & 7 deletions packages/compiler-sfc/src/script/defineExpose.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@ export function processDefineExpose(
ctx: ScriptCompileContext,
node: Node,
): boolean {
if (isCallOf(node, DEFINE_EXPOSE)) {
if (ctx.hasDefineExposeCall) {
ctx.error(`duplicate ${DEFINE_EXPOSE}() call`, node)
}
ctx.hasDefineExposeCall = true
return true
if (!isCallOf(node, ctx.macrosAliases[DEFINE_EXPOSE])) {
return false
}
return false
if (ctx.hasDefineExposeCall) {
ctx.error(`duplicate ${DEFINE_EXPOSE}() call`, node)
}
ctx.hasDefineExposeCall = true
return true
}
2 changes: 1 addition & 1 deletion packages/compiler-sfc/src/script/defineModel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export function processDefineModel(
node: Node,
declId?: LVal,
): boolean {
if (!isCallOf(node, DEFINE_MODEL)) {
if (!isCallOf(node, ctx.macrosAliases[DEFINE_MODEL])) {
return false
}

Expand Down
2 changes: 1 addition & 1 deletion packages/compiler-sfc/src/script/defineOptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export function processDefineOptions(
ctx: ScriptCompileContext,
node: Node,
): boolean {
if (!isCallOf(node, DEFINE_OPTIONS)) {
if (!isCallOf(node, ctx.macrosAliases[DEFINE_OPTIONS])) {
return false
}
if (ctx.hasDefineOptionsCall) {
Expand Down
4 changes: 2 additions & 2 deletions packages/compiler-sfc/src/script/defineProps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ export function processDefineProps(
node: Node,
declId?: LVal,
) {
if (!isCallOf(node, DEFINE_PROPS)) {
if (!isCallOf(node, ctx.macrosAliases[DEFINE_PROPS])) {
return processWithDefaults(ctx, node, declId)
}

Expand Down Expand Up @@ -96,7 +96,7 @@ function processWithDefaults(
node: Node,
declId?: LVal,
): boolean {
if (!isCallOf(node, WITH_DEFAULTS)) {
if (!isCallOf(node, ctx.macrosAliases[WITH_DEFAULTS])) {
return false
}
if (!processDefineProps(ctx, node.arguments[0], declId)) {
Expand Down
2 changes: 1 addition & 1 deletion packages/compiler-sfc/src/script/defineSlots.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ export function processDefineSlots(
node: Node,
declId?: LVal,
): boolean {
if (!isCallOf(node, DEFINE_SLOTS)) {
if (!isCallOf(node, ctx.macrosAliases[DEFINE_SLOTS])) {
return false
}
if (ctx.hasDefineSlotsCall) {
Expand Down