Browse Source

imporve:check the delete oparator in event expression (#5072)

* improve:check the unary operators

* add:test
dev
laoxiong 8 years ago
committed by Evan You
parent
commit
6a5b8043f2
  1. 17
      src/compiler/error-detector.js
  2. 11
      test/unit/features/options/template.spec.js

17
src/compiler/error-detector.js

@ -8,6 +8,10 @@ const prohibitedKeywordRE = new RegExp('\\b' + (
'super,throw,while,yield,delete,export,import,return,switch,default,' +
'extends,finally,continue,debugger,function,arguments'
).split(',').join('\\b|\\b') + '\\b')
const unaryOperatorsRE = new RegExp('\\b' + (
'delete,typeof,void'
).split(',').join('\\s*\\([^\\)]*\\)|\\b') + '\\s*\\([^\\)]*\\)')
const eventAttrRE = /^@|^v-on:/
// check valid identifier for v-for
const identRE = /[A-Za-z_$][\w$]*/
// strip strings in expressions
@ -30,6 +34,8 @@ function checkNode (node: ASTNode, errors: Array<string>) {
if (value) {
if (name === 'v-for') {
checkFor(node, `v-for="${value}"`, errors)
} else if (eventAttrRE.test(name)) {
checkEvent(value, `${name}="${value}"`, errors)
} else {
checkExpression(value, `${name}="${value}"`, errors)
}
@ -46,6 +52,17 @@ function checkNode (node: ASTNode, errors: Array<string>) {
}
}
function checkEvent (exp: string, text: string, errors: Array<string>) {
const keywordMatch = exp.replace(stripStringRE, '').match(unaryOperatorsRE)
if (keywordMatch) {
errors.push(
`avoid using JavaScript unary operator as property name: ` +
`"${keywordMatch[0]}" in expression ${text.trim()}`
)
}
checkExpression(exp, text, errors)
}
function checkFor (node: ASTElement, text: string, errors: Array<string>) {
checkExpression(node.for || '', text, errors)
checkIdentifier(node.alias, 'v-for alias', text, errors)

11
test/unit/features/options/template.spec.js

@ -70,4 +70,15 @@ describe('Options template', () => {
expect('invalid v-for iterator "2"').toHaveBeenWarned()
expect('invalid expression: v-for="(1, 2) in a----"').toHaveBeenWarned()
})
it('warn error in generated function (v-on)', () => {
new Vue({
template: `<div @click="delete('Delete')"></div>`,
methods: { delete: function () {} }
}).$mount()
expect('Error compiling template').toHaveBeenWarned()
expect(
`avoid using JavaScript unary operator as property name: "delete()" in expression @click="delete('Delete')"`
).toHaveBeenWarned()
})
})

Loading…
Cancel
Save