]> git.ipfire.org Git - thirdparty/vuejs/core.git/commitdiff
feat: emit compiler error for invalid JavaScript expressions
authorEvan You <yyx990803@gmail.com>
Wed, 16 Oct 2019 20:33:23 +0000 (16:33 -0400)
committerEvan You <yyx990803@gmail.com>
Wed, 16 Oct 2019 21:43:41 +0000 (17:43 -0400)
packages/compiler-core/__tests__/transforms/transformExpressions.spec.ts
packages/compiler-core/src/errors.ts
packages/compiler-core/src/transforms/transformExpression.ts

index 3b2c70de7117b10e672c68876af1781128c8efb4..d6c142bd9943bc4354f4677290398e79114fe7b7 100644 (file)
@@ -379,6 +379,8 @@ describe('compiler: expression transform', () => {
   test('should handle parse error', () => {
     const onError = jest.fn()
     parseWithExpressionTransform(`{{ a( }}`, { onError })
-    expect(onError.mock.calls[0][0].message).toMatch(`Unexpected token (1:4)`)
+    expect(onError.mock.calls[0][0].message).toMatch(
+      `Invalid JavaScript expression. (1:4)`
+    )
   })
 })
index 30b839058e0ae04a81814af10c92a3fe85fe0098..ebd113c5831ea50323106f8e6f96ff9ba66c92c4 100644 (file)
@@ -82,6 +82,7 @@ export const enum ErrorCodes {
   X_V_MODEL_NO_EXPRESSION,
   X_V_MODEL_MALFORMED_EXPRESSION,
   X_V_MODEL_ON_SCOPE_VARIABLE,
+  X_INVALID_EXPRESSION,
 
   // generic errors
   X_PREFIX_ID_NOT_SUPPORTED,
@@ -173,6 +174,7 @@ export const errorMessages: { [code: number]: string } = {
   [ErrorCodes.X_V_MODEL_NO_EXPRESSION]: `v-model is missing expression.`,
   [ErrorCodes.X_V_MODEL_MALFORMED_EXPRESSION]: `v-model value must be a valid JavaScript member expression.`,
   [ErrorCodes.X_V_MODEL_ON_SCOPE_VARIABLE]: `v-model cannot be used on v-for or v-slot scope variables because they are not writable.`,
+  [ErrorCodes.X_INVALID_EXPRESSION]: `Invalid JavaScript expression.`,
 
   // generic errors
   [ErrorCodes.X_PREFIX_ID_NOT_SUPPORTED]: `"prefixIdentifiers" option is not supported in this build of compiler.`,
index 2894e33e6bb6878ebef32f419b6a64628c1d1a8c..6e10782e627f4a317658cf25153f9506f27d9c61 100644 (file)
@@ -24,6 +24,7 @@ import {
   walkJS
 } from '../utils'
 import { isGloballyWhitelisted, makeMap } from '@vue/shared'
+import { createCompilerError, ErrorCodes } from '../errors'
 
 const isLiteralWhitelisted = /*#__PURE__*/ makeMap('true,false,null,this')
 
@@ -105,7 +106,9 @@ export function processExpression(
   try {
     ast = parseJS(source, { ranges: true })
   } catch (e) {
-    context.onError(e)
+    context.onError(
+      createCompilerError(ErrorCodes.X_INVALID_EXPRESSION, node.loc)
+    )
     return node
   }