]> git.ipfire.org Git - thirdparty/vuejs/core.git/commitdiff
fix(compiler): report invalid directive name error (#4494) (#4495)
authorHerrington Darkholme <2883231+HerringtonDarkholme@users.noreply.github.com>
Thu, 2 Sep 2021 13:42:20 +0000 (21:42 +0800)
committerGitHub <noreply@github.com>
Thu, 2 Sep 2021 13:42:20 +0000 (09:42 -0400)
packages/compiler-core/__tests__/parse.spec.ts
packages/compiler-core/src/errors.ts
packages/compiler-core/src/parse.ts

index 0e839d26bc7d96c9813e8cdd7b717ab2d8ce8bbc..51cdb0e38c1c66afb822206494fb5038c33acd31 100644 (file)
@@ -1244,6 +1244,27 @@ describe('compiler: parse', () => {
         }
       })
     })
+    test('directive with no name', () => {
+      let errorCode = -1
+      const ast = baseParse('<div v-/>', {
+        onError: err => {
+          errorCode = err.code as number
+        }
+      })
+      const directive = (ast.children[0] as ElementNode).props[0]
+
+      expect(errorCode).toBe(ErrorCodes.X_MISSING_DIRECTIVE_NAME)
+      expect(directive).toStrictEqual({
+        type: NodeTypes.ATTRIBUTE,
+        name: 'v-',
+        value: undefined,
+        loc: {
+          start: { offset: 5, line: 1, column: 6 },
+          end: { offset: 7, line: 1, column: 8 },
+          source: 'v-'
+        }
+      })
+    })
 
     test('v-bind shorthand', () => {
       const ast = baseParse('<div :a=b />')
index 57f2f3b2ef7036c67fe0a01564bfcf666cb26929..1f2afac7e668e8020d4bd48ccd094c0f98f98999 100644 (file)
@@ -67,6 +67,7 @@ export const enum ErrorCodes {
   X_INVALID_END_TAG,
   X_MISSING_END_TAG,
   X_MISSING_INTERPOLATION_END,
+  X_MISSING_DIRECTIVE_NAME,
   X_MISSING_DYNAMIC_DIRECTIVE_ARGUMENT_END,
 
   // transform errors
@@ -143,6 +144,7 @@ export const errorMessages: Record<ErrorCodes, string> = {
   [ErrorCodes.X_MISSING_DYNAMIC_DIRECTIVE_ARGUMENT_END]:
     'End bracket for dynamic directive argument was not found. ' +
     'Note that dynamic directive argument cannot contain spaces.',
+  [ErrorCodes.X_MISSING_DIRECTIVE_NAME]: 'Legal directive name was expected.',
 
   // transform errors
   [ErrorCodes.X_V_IF_NO_EXPRESSION]: `v-if/v-else-if is missing expression.`,
index 21973878dbca53eec180db9af61349776c0ae026..907e54d742e947a63ff1f0af489105190e3a44f2 100644 (file)
@@ -775,7 +775,7 @@ function parseAttribute(
   }
   const loc = getSelection(context, start)
 
-  if (!context.inVPre && /^(v-|:|\.|@|#)/.test(name)) {
+  if (!context.inVPre && /^(v-[A-Za-z0-9-]|:|\.|@|#)/.test(name)) {
     const match =
       /(?:^v-([a-z0-9-]+))?(?:(?::|^\.|^@|^#)(\[[^\]]+\]|[^\.]+))?(.+)?$/i.exec(
         name
@@ -888,6 +888,11 @@ function parseAttribute(
     }
   }
 
+  // missing directive name or illegal directive name
+  if (!context.inVPre && startsWith(name, 'v-')) {
+    emitError(context, ErrorCodes.X_MISSING_DIRECTIVE_NAME)
+  }
+
   return {
     type: NodeTypes.ATTRIBUTE,
     name,