Skip to content

Commit 88e00f3

Browse files
committed
fix(compiler): Check syntax error for v-slot attribute value
1 parent 05051e4 commit 88e00f3

File tree

3 files changed

+27
-5
lines changed

3 files changed

+27
-5
lines changed

src/compiler/error-detector.js

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,13 @@ function checkNode (node: ASTNode, warn: Function) {
3131
if (node.type === 1) {
3232
for (const name in node.attrsMap) {
3333
if (dirRE.test(name)) {
34-
if (name === 'v-slot') break;
3534
const value = node.attrsMap[name]
3635
if (value) {
3736
const range = node.rawAttrsMap[name]
3837
if (name === 'v-for') {
3938
checkFor(node, `v-for="${value}"`, warn, range)
39+
} else if (name === 'v-slot' || name[0] === '#') {
40+
checkFunctionParameterExpression(value, `${name}="${value}"`, warn, range)
4041
} else if (onRE.test(name)) {
4142
checkEvent(value, `${name}="${value}"`, warn, range)
4243
} else {
@@ -112,3 +113,16 @@ function checkExpression (exp: string, text: string, warn: Function, range?: Ran
112113
}
113114
}
114115
}
116+
117+
function checkFunctionParameterExpression (exp: string, text: string, warn: Function, range?: Range) {
118+
try {
119+
new Function(exp, '')
120+
} catch (e) {
121+
warn(
122+
`invalid function parameter expression: ${e.message} in\n\n` +
123+
` ${exp}\n\n` +
124+
` Raw expression: ${text.trim()}\n`,
125+
range
126+
)
127+
}
128+
}

src/compiler/parser/index.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@ import {
2323

2424
export const onRE = /^@|^v-on:/
2525
export const dirRE = process.env.VBIND_PROP_SHORTHAND
26-
? /^v-|^@|^:|^\./
27-
: /^v-|^@|^:/
26+
? /^v-|^@|^:|^\.|^#/
27+
: /^v-|^@|^:|^#/
2828
export const forAliasRE = /([\s\S]*?)\s+(?:in|of)\s+([\s\S]*)/
2929
export const forIteratorRE = /,([^,\}\]]*)(?:,([^,\}\]]*))?$/
3030
const stripParensRE = /^\(|\)$/g

test/unit/features/component/component-scoped-slot.spec.js

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -760,12 +760,20 @@ describe('Component scoped slot', () => {
760760
expect(`Unexpected mixed usage of different slot syntaxes`).toHaveBeenWarned()
761761
})
762762

763+
it('should warn invalid parameter expression', () => {
764+
new Vue({
765+
template: `<foo ${syntax}="1"></foo>`,
766+
components: { Foo }
767+
}).$mount();
768+
expect('invalid function parameter expression').toHaveBeenWarned()
769+
})
770+
763771
it('should allow destructuring props with default value', () => {
764772
new Vue({
765773
template: `<foo ${syntax}="{ foo = { bar: '1' } }"></foo>`,
766-
components: { Foo, Bar }
774+
components: { Foo }
767775
}).$mount();
768-
expect('Invalid shorthand property initializer').not.toHaveBeenWarned()
776+
expect('invalid function parameter expression').not.toHaveBeenWarned()
769777
})
770778
}
771779

0 commit comments

Comments
 (0)