Skip to content

Commit

Permalink
feat: improve template expression error message
Browse files Browse the repository at this point in the history
  • Loading branch information
yyx990803 authored and ztlevi committed Feb 14, 2018
1 parent 8856ab5 commit e2b0bab
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 8 deletions.
8 changes: 6 additions & 2 deletions src/compiler/error-detector.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,10 +89,14 @@ function checkExpression (exp: string, text: string, errors: Array<string>) {
if (keywordMatch) {
errors.push(
`avoid using JavaScript keyword as property name: ` +
`"${keywordMatch[0]}" in expression ${text.trim()}`
`"${keywordMatch[0]}"\n Raw expression: ${text.trim()}`
)
} else {
errors.push(`invalid expression: ${text.trim()}`)
errors.push(
`invalid expression: ${e.message} in\n\n` +
` ${exp}\n\n` +
` Raw expression: ${text.trim()}\n`
)
}
}
}
8 changes: 4 additions & 4 deletions test/unit/features/options/template.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,9 @@ describe('Options template', () => {
template: '<div v-if="!@"><span>{{ a"" }}</span><span>{{ do + 1 }}</span></div>'
}).$mount()
expect('Error compiling template').toHaveBeenWarned()
expect('invalid expression: v-if="!@"').toHaveBeenWarned()
expect('invalid expression: {{ a"" }}').toHaveBeenWarned()
expect('avoid using JavaScript keyword as property name: "do" in expression {{ do + 1 }}').toHaveBeenWarned()
expect('Raw expression: v-if="!@"').toHaveBeenWarned()
expect('Raw expression: {{ a"" }}').toHaveBeenWarned()
expect('avoid using JavaScript keyword as property name: "do"').toHaveBeenWarned()
})

it('should not warn $ prefixed keywords', () => {
Expand All @@ -75,7 +75,7 @@ describe('Options template', () => {
expect('Error compiling template').toHaveBeenWarned()
expect('invalid v-for alias "1"').toHaveBeenWarned()
expect('invalid v-for iterator "2"').toHaveBeenWarned()
expect('invalid expression: v-for="(1, 2) in a----"').toHaveBeenWarned()
expect('Raw expression: v-for="(1, 2) in a----"').toHaveBeenWarned()
})

it('warn error in generated function (v-on)', () => {
Expand Down
4 changes: 2 additions & 2 deletions test/unit/modules/compiler/compiler-options.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ describe('compile options', () => {

compiled = compile('<div v-if="a----">{{ b++++ }}</div>')
expect(compiled.errors.length).toBe(2)
expect(compiled.errors[0]).toContain('invalid expression: v-if="a----"')
expect(compiled.errors[1]).toContain('invalid expression: {{ b++++ }}')
expect(compiled.errors[0]).toContain('Raw expression: v-if="a----"')
expect(compiled.errors[1]).toContain('Raw expression: {{ b++++ }}')
})
})

0 comments on commit e2b0bab

Please sign in to comment.