Skip to content

Commit

Permalink
fix(taroize): 事件名需传入有效的 JavaScript 变量名,close #2277
Browse files Browse the repository at this point in the history
  • Loading branch information
yuche committed Mar 1, 2019
1 parent f62bb85 commit 41d3fba
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 1 deletion.
19 changes: 19 additions & 0 deletions packages/taroize/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,25 @@ export function isAliasThis (p: NodePath<t.Node>, name: string) {
return false
}

export function isValidVarName (str: string) {
if (typeof str !== 'string') {
return false
}

if (str.trim() !== str) {
return false
}

try {
// tslint:disable-next-line:no-unused-expression
new Function(str, 'var ' + str)
} catch (e) {
return false
}

return true
}

export function parseCode (code: string) {
return (transform(code, {
parserOpts: {
Expand Down
5 changes: 4 additions & 1 deletion packages/taroize/src/wxml.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { parse } from 'himalaya-wxml'
import * as t from 'babel-types'
import { camelCase, cloneDeep } from 'lodash'
import traverse, { NodePath, Visitor } from 'babel-traverse'
import { buildTemplate, DEFAULT_Component_SET, buildImportStatement, buildBlockElement, parseCode, codeFrameError } from './utils'
import { buildTemplate, DEFAULT_Component_SET, buildImportStatement, buildBlockElement, parseCode, codeFrameError, isValidVarName } from './utils'
import { specialEvents } from './events'
import { parseTemplate, parseModule } from './template'
import { usedComponents, errors } from './global'
Expand Down Expand Up @@ -753,6 +753,9 @@ function handleAttrKey (key: string) {
} else if (key === 'class') {
return 'className'
} else if (/^(bind|catch)[a-z|:]/.test(key)) {
if (!isValidVarName(key)) {
throw new Error(`"${key}" 不是一个有效 JavaScript 变量名`)
}
if (specialEvents.has(key)) {
return specialEvents.get(key)!
} else {
Expand Down

0 comments on commit 41d3fba

Please sign in to comment.