提交 6289e0e0 编写于 作者: fxy060608's avatar fxy060608

fix(mp): ignore javascript keywords when generate identifier (#3397)

上级 a0551504
......@@ -9,10 +9,11 @@ describe('identifier', () => {
}
expect(ids.next()).toBe('ab')
expect(ids.next()).toBe('ac')
// do if in 已被忽略
for (let i = 0; i < 52 * 52 - 2; i++) {
ids.next()
}
expect(ids.next()).toBe('acc')
expect(ids.next()).toBe('acd')
expect(ids.next()).toBe('acf')
expect(ids.next()).toBe('acg')
})
})
......@@ -3,13 +3,17 @@ export default class IdentifierGenerator {
private _chars: string = 'abcdefghijklmnopqrstvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'
private _nextIds: number[] = [0]
next() {
next(): string {
const r = []
for (const char of this._nextIds) {
r.unshift(this._chars[char])
}
this._increment()
return r.join('')
const id = r.join('')
if (keywords.includes(id)) {
return this.next()
}
return id
}
_increment() {
......@@ -30,3 +34,69 @@ export default class IdentifierGenerator {
}
}
}
const keywords = [
'abstract',
'arguments',
'await',
'boolean',
'break',
'byte',
'case',
'catch',
'char',
'class',
'const',
'continue',
'debugger',
'default',
'delete',
'do',
'double',
'else',
'enum',
'eval',
'export',
'extends',
'false',
'final',
'finally',
'float',
'for',
'function',
'goto',
'if',
'implements',
'import',
'in',
'instanceof',
'int',
'interface',
'let',
'long',
'native',
'new',
'null',
'package',
'private',
'protected',
'public',
'return',
'short',
'static',
'super',
'switch',
'synchronized',
'this',
'throw',
'throws',
'transient',
'true',
'try',
'typeof',
'var',
'void',
'volatile',
'while',
'with',
'yield',
]
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册