From 63f96a2aceddfad6469d0ba38ec665995102c614 Mon Sep 17 00:00:00 2001 From: skirtle <65301168+skirtles-code@users.noreply.github.com> Date: Sun, 31 Dec 2023 01:36:38 +0000 Subject: [PATCH] Port changes to the new template parser --- .../__snapshots__/parse.spec.ts.snap | 21 +++++++++--------- .../compiler-core/__tests__/parse.spec.ts | 3 +++ packages/compiler-core/src/parser.ts | 22 ++++++++++++++++++- 3 files changed, 35 insertions(+), 11 deletions(-) diff --git a/packages/compiler-core/__tests__/__snapshots__/parse.spec.ts.snap b/packages/compiler-core/__tests__/__snapshots__/parse.spec.ts.snap index 5823d99ab..ae8eed7a3 100644 --- a/packages/compiler-core/__tests__/__snapshots__/parse.spec.ts.snap +++ b/packages/compiler-core/__tests__/__snapshots__/parse.spec.ts.snap @@ -3905,10 +3905,9 @@ exports[`compiler: parse > Errors > X_DIRECTIVE_SHORTHAND_NO_ARGUMENT >
Errors > X_DIRECTIVE_SHORTHAND_NO_ARGUMENT >
", "temps": 0, "type": 0, } @@ -3999,10 +3999,10 @@ exports[`compiler: parse > Errors > X_DIRECTIVE_SHORTHAND_NO_ARGUMENT >
Errors > X_DIRECTIVE_SHORTHAND_NO_ARGUMENT >
", "temps": 0, "type": 0, } @@ -4092,10 +4093,9 @@ exports[`compiler: parse > Errors > X_DIRECTIVE_SHORTHAND_NO_ARGUMENT >
Errors > X_DIRECTIVE_SHORTHAND_NO_ARGUMENT >
", "temps": 0, "type": 0, } @@ -4185,10 +4186,9 @@ exports[`compiler: parse > Errors > X_DIRECTIVE_SHORTHAND_NO_ARGUMENT >
Errors > X_DIRECTIVE_SHORTHAND_NO_ARGUMENT >
", "temps": 0, "type": 0, } diff --git a/packages/compiler-core/__tests__/parse.spec.ts b/packages/compiler-core/__tests__/parse.spec.ts index 6fc30821b..986326185 100644 --- a/packages/compiler-core/__tests__/parse.spec.ts +++ b/packages/compiler-core/__tests__/parse.spec.ts @@ -2063,6 +2063,9 @@ describe('compiler: parse', () => { exp: undefined, arg: undefined, }) + expect( + `the directive shorthand '#' cannot be used without an argument`, + ).toHaveBeenWarned() }) // edge case found in vue-macros where the input is TS or JSX diff --git a/packages/compiler-core/src/parser.ts b/packages/compiler-core/src/parser.ts index e73bffdeb..468e39742 100644 --- a/packages/compiler-core/src/parser.ts +++ b/packages/compiler-core/src/parser.ts @@ -243,7 +243,21 @@ const tokenizer = new Tokenizer(stack, { }, ondirarg(start, end) { - if (start === end) return + if (start === end) { + if (__DEV__) { + const currentDir = currentProp as DirectiveNode + + if (currentDir.rawName?.length === 1) { + emitWarning( + ErrorCodes.X_DIRECTIVE_SHORTHAND_NO_ARGUMENT, + start - 1, + `the directive shorthand '${currentDir.rawName}' cannot be used without an argument. ` + + `Use v-${currentDir.name} instead or provide an argument.`, + ) + } + } + return + } const arg = getSlice(start, end) if (inVPre) { ;(currentProp as AttributeNode).name += arg @@ -1007,6 +1021,12 @@ function emitError(code: ErrorCodes, index: number, message?: string) { ) } +function emitWarning(code: ErrorCodes, index: number, message?: string) { + currentOptions.onWarn( + createCompilerError(code, getLoc(index, index), undefined, message), + ) +} + function reset() { tokenizer.reset() currentOpenTag = null