Browse Source

fix(compiler): Remove the warning for valid v-slot value (#9917)

dev
X.L 6 years ago
committed by Evan You
parent
commit
085d188379
  1. 15
      src/compiler/error-detector.js
  2. 4
      src/compiler/parser/index.js
  3. 16
      test/unit/features/component/component-scoped-slot.spec.js

15
src/compiler/error-detector.js

@ -36,6 +36,8 @@ function checkNode (node: ASTNode, warn: Function) {
const range = node.rawAttrsMap[name] const range = node.rawAttrsMap[name]
if (name === 'v-for') { if (name === 'v-for') {
checkFor(node, `v-for="${value}"`, warn, range) 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)) { } else if (onRE.test(name)) {
checkEvent(value, `${name}="${value}"`, warn, range) checkEvent(value, `${name}="${value}"`, warn, range)
} else { } else {
@ -111,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
src/compiler/parser/index.js

@ -23,8 +23,8 @@ import {
export const onRE = /^@|^v-on:/ export const onRE = /^@|^v-on:/
export const dirRE = process.env.VBIND_PROP_SHORTHAND 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 forAliasRE = /([\s\S]*?)\s+(?:in|of)\s+([\s\S]*)/
export const forIteratorRE = /,([^,\}\]]*)(?:,([^,\}\]]*))?$/ export const forIteratorRE = /,([^,\}\]]*)(?:,([^,\}\]]*))?$/
const stripParensRE = /^\(|\)$/g const stripParensRE = /^\(|\)$/g

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

@ -759,6 +759,22 @@ describe('Component scoped slot', () => {
}).$mount() }).$mount()
expect(`Unexpected mixed usage of different slot syntaxes`).toHaveBeenWarned() 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 }
}).$mount();
expect('invalid function parameter expression').not.toHaveBeenWarned()
})
} }
// run tests for both full syntax and shorthand // run tests for both full syntax and shorthand

Loading…
Cancel
Save