Skip to content

Commit

Permalink
fix(compiler): Check syntax error for v-slot attribute value
Browse files Browse the repository at this point in the history
  • Loading branch information
liximomo committed Apr 25, 2019
1 parent 05051e4 commit 88e00f3
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 5 deletions.
16 changes: 15 additions & 1 deletion src/compiler/error-detector.js
Expand Up @@ -31,12 +31,13 @@ function checkNode (node: ASTNode, warn: Function) {
if (node.type === 1) {
for (const name in node.attrsMap) {
if (dirRE.test(name)) {
if (name === 'v-slot') break;
const value = node.attrsMap[name]
if (value) {
const range = node.rawAttrsMap[name]
if (name === 'v-for') {
checkFor(node, `v-for="${value}"`, warn, range)
} else if (name === 'v-slot' || name[0] === '#') {
checkFunctionParameterExpression(value, `${name}="${value}"`, warn, range)
} else if (onRE.test(name)) {
checkEvent(value, `${name}="${value}"`, warn, range)
} else {
Expand Down Expand Up @@ -112,3 +113,16 @@ function checkExpression (exp: string, text: string, warn: Function, range?: Ran
}
}
}

function checkFunctionParameterExpression (exp: string, text: string, warn: Function, range?: Range) {
try {
new Function(exp, '')
} catch (e) {
warn(
`invalid function parameter expression: ${e.message} in\n\n` +
` ${exp}\n\n` +
` Raw expression: ${text.trim()}\n`,
range
)
}
}
4 changes: 2 additions & 2 deletions src/compiler/parser/index.js
Expand Up @@ -23,8 +23,8 @@ import {

export const onRE = /^@|^v-on:/
export const dirRE = process.env.VBIND_PROP_SHORTHAND
? /^v-|^@|^:|^\./
: /^v-|^@|^:/
? /^v-|^@|^:|^\.|^#/
: /^v-|^@|^:|^#/
export const forAliasRE = /([\s\S]*?)\s+(?:in|of)\s+([\s\S]*)/
export const forIteratorRE = /,([^,\}\]]*)(?:,([^,\}\]]*))?$/
const stripParensRE = /^\(|\)$/g
Expand Down
12 changes: 10 additions & 2 deletions test/unit/features/component/component-scoped-slot.spec.js
Expand Up @@ -760,12 +760,20 @@ describe('Component scoped slot', () => {
expect(`Unexpected mixed usage of different slot syntaxes`).toHaveBeenWarned()
})

it('should warn invalid parameter expression', () => {
new Vue({
template: `<foo ${syntax}="1"></foo>`,
components: { Foo }
}).$mount();
expect('invalid function parameter expression').toHaveBeenWarned()
})

it('should allow destructuring props with default value', () => {
new Vue({
template: `<foo ${syntax}="{ foo = { bar: '1' } }"></foo>`,
components: { Foo, Bar }
components: { Foo }
}).$mount();
expect('Invalid shorthand property initializer').not.toHaveBeenWarned()
expect('invalid function parameter expression').not.toHaveBeenWarned()
})
}

Expand Down

0 comments on commit 88e00f3

Please sign in to comment.