feat: emit compiler error for invalid JavaScript expressions

This commit is contained in:
Evan You 2019-10-16 16:33:23 -04:00
parent 0ca4896a38
commit e97951dd2e
3 changed files with 9 additions and 2 deletions

View File

@ -379,6 +379,8 @@ describe('compiler: expression transform', () => {
test('should handle parse error', () => { test('should handle parse error', () => {
const onError = jest.fn() const onError = jest.fn()
parseWithExpressionTransform(`{{ a( }}`, { onError }) 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)`
)
}) })
}) })

View File

@ -82,6 +82,7 @@ export const enum ErrorCodes {
X_V_MODEL_NO_EXPRESSION, X_V_MODEL_NO_EXPRESSION,
X_V_MODEL_MALFORMED_EXPRESSION, X_V_MODEL_MALFORMED_EXPRESSION,
X_V_MODEL_ON_SCOPE_VARIABLE, X_V_MODEL_ON_SCOPE_VARIABLE,
X_INVALID_EXPRESSION,
// generic errors // generic errors
X_PREFIX_ID_NOT_SUPPORTED, 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_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_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_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 // generic errors
[ErrorCodes.X_PREFIX_ID_NOT_SUPPORTED]: `"prefixIdentifiers" option is not supported in this build of compiler.`, [ErrorCodes.X_PREFIX_ID_NOT_SUPPORTED]: `"prefixIdentifiers" option is not supported in this build of compiler.`,

View File

@ -24,6 +24,7 @@ import {
walkJS walkJS
} from '../utils' } from '../utils'
import { isGloballyWhitelisted, makeMap } from '@vue/shared' import { isGloballyWhitelisted, makeMap } from '@vue/shared'
import { createCompilerError, ErrorCodes } from '../errors'
const isLiteralWhitelisted = /*#__PURE__*/ makeMap('true,false,null,this') const isLiteralWhitelisted = /*#__PURE__*/ makeMap('true,false,null,this')
@ -105,7 +106,9 @@ export function processExpression(
try { try {
ast = parseJS(source, { ranges: true }) ast = parseJS(source, { ranges: true })
} catch (e) { } catch (e) {
context.onError(e) context.onError(
createCompilerError(ErrorCodes.X_INVALID_EXPRESSION, node.loc)
)
return node return node
} }