Skip to content

Commit 000641b

Browse files
authored
Update ecma keyword detection regex to not misfire on strings containing periods (#30)
* update ecma keyword detection regex to not misfire on strings containing periods * added unit test
1 parent 7ee0af0 commit 000641b

File tree

2 files changed

+8
-2
lines changed

2 files changed

+8
-2
lines changed

src/util.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
const stringRE = /'(?:[^'\\]|\\.)*'|"(?:[^"\\]|\\.)*"|`(?:[^`\\]|\\.)*\$\{|\}(?:[^`\\]|\\.)*`|`(?:[^`\\]|\\.)*`/g
2-
const ecmaKeywordsRE = new RegExp('\\b' + (
2+
const ecmaKeywordsRE = new RegExp('(?<!\\.)\\b' + (
33
'delete,typeof,instanceof,void,do,if,for,let,new,try,var,case,else,with,await,break,catch,class,const,' +
44
'alert,eval,super,throw,while,yield,delete,export,import,return,switch,default,' +
55
'extends,finally,continue,debugger,function,arguments'
6-
).split(',').join('\\b|\\b') + '\\b')
6+
).split(',').join('\\b(?!\\.)|(?<!\\.)\\b') + '\\b(?!\\.)')
77

88
function warn (msg, err) {
99
if (typeof console !== 'undefined') {

test/utils.test.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,4 +22,10 @@ it('ecmascript keyword should not be evaluate', () => {
2222
const { status, value } = evaluateValue(`'while(true){alert('!');}'`)
2323
expect(status).toEqual('ng')
2424
expect(value).toEqual(undefined)
25+
})
26+
27+
it('string literal containing period delimited ecmascript keywords should evaluate', () => {
28+
const { status, value } = evaluateValue(`'new.alert.import'`)
29+
expect(status).toEqual('ok')
30+
expect(value).toEqual('new.alert.import')
2531
})

0 commit comments

Comments
 (0)