From 5a89024507e9fd298ed062d98f5571dd226fe1ee Mon Sep 17 00:00:00 2001 From: Max Schaefer Date: Tue, 29 Jan 2019 15:57:37 +0000 Subject: [PATCH] JavaScript: Be more lenient about keywords used as identifiers. --- .../src/com/semmle/jcorn/Parser.java | 10 +- .../extractor/tests/errors/input/kwident.js | 1 + .../tests/errors/output/trap/kwident.js.trap | 104 ++++++++++++++++++ 3 files changed, 109 insertions(+), 6 deletions(-) create mode 100644 javascript/extractor/tests/errors/input/kwident.js create mode 100644 javascript/extractor/tests/errors/output/trap/kwident.js.trap diff --git a/javascript/extractor/src/com/semmle/jcorn/Parser.java b/javascript/extractor/src/com/semmle/jcorn/Parser.java index 0e8be97ba3b..0773d94a08e 100644 --- a/javascript/extractor/src/com/semmle/jcorn/Parser.java +++ b/javascript/extractor/src/com/semmle/jcorn/Parser.java @@ -2106,8 +2106,10 @@ public class Parser { if (this.inAsync && this.value.equals("await")) this.raiseRecoverable(this.start, "Can not use 'await' as identifier inside an async function"); name = String.valueOf(this.value); - } else if (liberal && this.type.keyword != null) { + } else if (this.type.keyword != null) { name = this.type.keyword; + if (!liberal) + raiseRecoverable(this.start, "Cannot use keyword '" + name + "' as an identifier."); } else { this.unexpected(); } @@ -2264,9 +2266,6 @@ public class Parser { if (this.options.ecmaVersion() < 6) return this.parseIdent(false); - if (this.type == TokenType.name) - return this.parseIdent(false); - if (this.type == TokenType.bracketL) { Position start = this.startLoc; this.next(); @@ -2278,8 +2277,7 @@ public class Parser { if (this.type == TokenType.braceL) return this.parseObj(true, null); - this.unexpected(); - return null; + return this.parseIdent(false); } protected List parseBindingList(TokenType close, boolean allowEmpty, boolean allowTrailingComma, boolean allowNonIdent) { diff --git a/javascript/extractor/tests/errors/input/kwident.js b/javascript/extractor/tests/errors/input/kwident.js new file mode 100644 index 00000000000..5fd35e0b27a --- /dev/null +++ b/javascript/extractor/tests/errors/input/kwident.js @@ -0,0 +1 @@ +var class = 23; diff --git a/javascript/extractor/tests/errors/output/trap/kwident.js.trap b/javascript/extractor/tests/errors/output/trap/kwident.js.trap new file mode 100644 index 00000000000..ca9a423bf0e --- /dev/null +++ b/javascript/extractor/tests/errors/output/trap/kwident.js.trap @@ -0,0 +1,104 @@ +#10000=@"/kwident.js;sourcefile" +files(#10000,"/kwident.js","kwident","js",0) +#10001=@"/;folder" +folders(#10001,"/","") +containerparent(#10001,#10000) +#10002=@"loc,{#10000},0,0,0,0" +locations_default(#10002,#10000,0,0,0,0) +hasLocation(#10000,#10002) +#20000=@"global_scope" +scopes(#20000,0) +#20001=@"script;{#10000},1,1" +#20002=* +lines(#20002,#20001,"var class = 23;"," +") +#20003=@"loc,{#10000},1,1,1,15" +locations_default(#20003,#10000,1,1,1,15) +hasLocation(#20002,#20003) +numlines(#20001,1,1,0) +#20004=* +tokeninfo(#20004,7,#20001,0,"var") +#20005=@"loc,{#10000},1,1,1,3" +locations_default(#20005,#10000,1,1,1,3) +hasLocation(#20004,#20005) +#20006=* +tokeninfo(#20006,7,#20001,1,"class") +#20007=@"loc,{#10000},1,5,1,9" +locations_default(#20007,#10000,1,5,1,9) +hasLocation(#20006,#20007) +#20008=* +tokeninfo(#20008,8,#20001,2,"=") +#20009=@"loc,{#10000},1,11,1,11" +locations_default(#20009,#10000,1,11,1,11) +hasLocation(#20008,#20009) +#20010=* +tokeninfo(#20010,3,#20001,3,"23") +#20011=@"loc,{#10000},1,13,1,14" +locations_default(#20011,#10000,1,13,1,14) +hasLocation(#20010,#20011) +#20012=* +tokeninfo(#20012,8,#20001,4,";") +#20013=@"loc,{#10000},1,15,1,15" +locations_default(#20013,#10000,1,15,1,15) +hasLocation(#20012,#20013) +#20014=* +tokeninfo(#20014,0,#20001,5,"") +#20015=@"loc,{#10000},2,1,2,0" +locations_default(#20015,#10000,2,1,2,0) +hasLocation(#20014,#20015) +toplevels(#20001,0) +#20016=@"loc,{#10000},1,1,2,0" +locations_default(#20016,#10000,1,1,2,0) +hasLocation(#20001,#20016) +#20017=@"var;{class};{#20000}" +variables(#20017,"class",#20000) +#20018=* +stmts(#20018,18,#20001,0,"var class = 23;") +hasLocation(#20018,#20003) +stmtContainers(#20018,#20001) +#20019=* +exprs(#20019,64,#20018,0,"class = 23") +#20020=@"loc,{#10000},1,5,1,14" +locations_default(#20020,#10000,1,5,1,14) +hasLocation(#20019,#20020) +enclosingStmt(#20019,#20018) +exprContainers(#20019,#20001) +#20021=* +exprs(#20021,78,#20019,0,"class") +hasLocation(#20021,#20007) +enclosingStmt(#20021,#20018) +exprContainers(#20021,#20001) +literals("class","class",#20021) +decl(#20021,#20017) +#20022=* +exprs(#20022,3,#20019,1,"23") +hasLocation(#20022,#20011) +enclosingStmt(#20022,#20018) +exprContainers(#20022,#20001) +literals("23","23",#20022) +#20023=* +entry_cfg_node(#20023,#20001) +#20024=@"loc,{#10000},1,1,1,0" +locations_default(#20024,#10000,1,1,1,0) +hasLocation(#20023,#20024) +#20025=* +exit_cfg_node(#20025,#20001) +hasLocation(#20025,#20015) +successor(#20018,#20021) +successor(#20022,#20019) +successor(#20021,#20022) +successor(#20019,#20025) +successor(#20023,#20018) +#20026=* +jsParseErrors(#20026,#20001,"Error: Cannot use keyword 'class' as an identifier.","var class = 23; +") +#20027=@"loc,{#10000},1,5,1,5" +locations_default(#20027,#10000,1,5,1,5) +hasLocation(#20026,#20027) +#20028=* +lines(#20028,#20001,"var class = 23;"," +") +hasLocation(#20028,#20003) +numlines(#20001,1,0,0) +numlines(#10000,1,1,0) +filetype(#10000,"javascript")