diff --git a/javascript/extractor/src/com/semmle/jcorn/CustomParser.java b/javascript/extractor/src/com/semmle/jcorn/CustomParser.java index b0f347d53c9..ab7f7faef6b 100644 --- a/javascript/extractor/src/com/semmle/jcorn/CustomParser.java +++ b/javascript/extractor/src/com/semmle/jcorn/CustomParser.java @@ -217,7 +217,7 @@ public class CustomParser extends FlowParser { protected INode parseFunction(Position startLoc, boolean isStatement, boolean allowExpressionBody, boolean isAsync) { if (isFunctionSent(isStatement)) return super.parseFunction(startLoc, isStatement, allowExpressionBody, isAsync); - allowExpressionBody = allowExpressionBody || options.mozExtensions() && !isStatement; + allowExpressionBody = allowExpressionBody || options.mozExtensions(); boolean oldInGen = this.inGenerator, oldInAsync = this.inAsync; int oldYieldPos = this.yieldPos, oldAwaitPos = this.awaitPos; Pair p = parseFunctionName(isStatement, isAsync); diff --git a/javascript/extractor/src/com/semmle/jcorn/Parser.java b/javascript/extractor/src/com/semmle/jcorn/Parser.java index b28b02fc7ed..b8207d19e83 100644 --- a/javascript/extractor/src/com/semmle/jcorn/Parser.java +++ b/javascript/extractor/src/com/semmle/jcorn/Parser.java @@ -2991,7 +2991,7 @@ public class Parser { IFunction node; SourceLocation loc = new SourceLocation(startLoc); if (isStatement && id != null) - node = new FunctionDeclaration(loc, id, params, (BlockStatement) body, generator, async); + node = new FunctionDeclaration(loc, id, params, body, generator, async); else node = new FunctionExpression(loc, id, params, body, generator, async); return this.finishNode(node); diff --git a/javascript/extractor/src/com/semmle/js/ast/FunctionDeclaration.java b/javascript/extractor/src/com/semmle/js/ast/FunctionDeclaration.java index 4971aa671dc..1fbc7e0702d 100644 --- a/javascript/extractor/src/com/semmle/js/ast/FunctionDeclaration.java +++ b/javascript/extractor/src/com/semmle/js/ast/FunctionDeclaration.java @@ -17,26 +17,26 @@ import com.semmle.ts.ast.TypeParameter; * */ public class FunctionDeclaration extends Statement implements IFunction { - private final AFunction fn; + private final AFunction fn; private final boolean hasDeclareKeyword; private int symbol = -1; - public FunctionDeclaration(SourceLocation loc, Identifier id, List params, BlockStatement body, boolean generator, + public FunctionDeclaration(SourceLocation loc, Identifier id, List params, Node body, boolean generator, boolean async) { - this(loc, new AFunction(id, params, body, generator, async, Collections.emptyList(), Collections.emptyList(), + this(loc, new AFunction<>(id, params, body, generator, async, Collections.emptyList(), Collections.emptyList(), Collections.emptyList(), null, null), false); } public FunctionDeclaration(SourceLocation loc, Identifier id, - List params, BlockStatement body, boolean generator, boolean async, boolean hasDeclareKeyword, + List params, Node body, boolean generator, boolean async, boolean hasDeclareKeyword, List typeParameters, List parameterTypes, ITypeExpression returnType, ITypeExpression thisParameterType) { - this(loc, new AFunction(id, params, body, generator, async, typeParameters, parameterTypes, Collections.emptyList(), + this(loc, new AFunction<>(id, params, body, generator, async, typeParameters, parameterTypes, Collections.emptyList(), returnType, thisParameterType), hasDeclareKeyword); } - private FunctionDeclaration(SourceLocation loc, AFunction fn, boolean hasDeclareKeyword) { + private FunctionDeclaration(SourceLocation loc, AFunction fn, boolean hasDeclareKeyword) { super("FunctionDeclaration", loc); this.fn = fn; this.hasDeclareKeyword = hasDeclareKeyword; @@ -56,7 +56,7 @@ public class FunctionDeclaration extends Statement implements IFunction { @Override public boolean hasDefault(int i) { return fn.hasDefault(i); } @Override public Expression getDefault(int i) { return fn.getDefault(i); } @Override public IPattern getRest() { return fn.getRest(); } - @Override public BlockStatement getBody() { return fn.getBody(); } + @Override public Node getBody() { return fn.getBody(); } @Override public boolean hasRest() { return fn.hasRest(); } public boolean hasId() { return fn.hasId(); } public boolean isGenerator() { return fn.isGenerator(); } diff --git a/javascript/extractor/tests/mozilla/input/exprfns.js b/javascript/extractor/tests/mozilla/input/exprfns.js new file mode 100644 index 00000000000..434f0c84e50 --- /dev/null +++ b/javascript/extractor/tests/mozilla/input/exprfns.js @@ -0,0 +1,7 @@ +function f(x)("result") + +function(b) { + if (b) { + function g(y)("other result") + } +} diff --git a/javascript/extractor/tests/mozilla/output/trap/exprfns.js.trap b/javascript/extractor/tests/mozilla/output/trap/exprfns.js.trap new file mode 100644 index 00000000000..6092fdc9e59 --- /dev/null +++ b/javascript/extractor/tests/mozilla/output/trap/exprfns.js.trap @@ -0,0 +1,437 @@ +#10000=@"/exprfns.js;sourcefile" +files(#10000,"/exprfns.js","exprfns","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,"function f(x)(""result"")"," +") +#20003=@"loc,{#10000},1,1,1,23" +locations_default(#20003,#10000,1,1,1,23) +hasLocation(#20002,#20003) +#20004=* +lines(#20004,#20001,""," +") +#20005=@"loc,{#10000},2,1,2,0" +locations_default(#20005,#10000,2,1,2,0) +hasLocation(#20004,#20005) +#20006=* +lines(#20006,#20001,"function(b) {"," +") +#20007=@"loc,{#10000},3,1,3,13" +locations_default(#20007,#10000,3,1,3,13) +hasLocation(#20006,#20007) +#20008=* +lines(#20008,#20001," if (b) {"," +") +#20009=@"loc,{#10000},4,1,4,10" +locations_default(#20009,#10000,4,1,4,10) +hasLocation(#20008,#20009) +indentation(#10000,4," ",2) +#20010=* +lines(#20010,#20001," function g(y)(""other result"")"," +") +#20011=@"loc,{#10000},5,1,5,33" +locations_default(#20011,#10000,5,1,5,33) +hasLocation(#20010,#20011) +indentation(#10000,5," ",4) +#20012=* +lines(#20012,#20001," }"," +") +#20013=@"loc,{#10000},6,1,6,3" +locations_default(#20013,#10000,6,1,6,3) +hasLocation(#20012,#20013) +indentation(#10000,6," ",2) +#20014=* +lines(#20014,#20001,"}"," +") +#20015=@"loc,{#10000},7,1,7,1" +locations_default(#20015,#10000,7,1,7,1) +hasLocation(#20014,#20015) +numlines(#20001,7,6,0) +#20016=* +tokeninfo(#20016,7,#20001,0,"function") +#20017=@"loc,{#10000},1,1,1,8" +locations_default(#20017,#10000,1,1,1,8) +hasLocation(#20016,#20017) +#20018=* +tokeninfo(#20018,6,#20001,1,"f") +#20019=@"loc,{#10000},1,10,1,10" +locations_default(#20019,#10000,1,10,1,10) +hasLocation(#20018,#20019) +#20020=* +tokeninfo(#20020,8,#20001,2,"(") +#20021=@"loc,{#10000},1,11,1,11" +locations_default(#20021,#10000,1,11,1,11) +hasLocation(#20020,#20021) +#20022=* +tokeninfo(#20022,6,#20001,3,"x") +#20023=@"loc,{#10000},1,12,1,12" +locations_default(#20023,#10000,1,12,1,12) +hasLocation(#20022,#20023) +#20024=* +tokeninfo(#20024,8,#20001,4,")") +#20025=@"loc,{#10000},1,13,1,13" +locations_default(#20025,#10000,1,13,1,13) +hasLocation(#20024,#20025) +#20026=* +tokeninfo(#20026,8,#20001,5,"(") +#20027=@"loc,{#10000},1,14,1,14" +locations_default(#20027,#10000,1,14,1,14) +hasLocation(#20026,#20027) +#20028=* +tokeninfo(#20028,4,#20001,6,"""result""") +#20029=@"loc,{#10000},1,15,1,22" +locations_default(#20029,#10000,1,15,1,22) +hasLocation(#20028,#20029) +#20030=* +tokeninfo(#20030,8,#20001,7,")") +#20031=@"loc,{#10000},1,23,1,23" +locations_default(#20031,#10000,1,23,1,23) +hasLocation(#20030,#20031) +#20032=* +tokeninfo(#20032,7,#20001,8,"function") +#20033=@"loc,{#10000},3,1,3,8" +locations_default(#20033,#10000,3,1,3,8) +hasLocation(#20032,#20033) +#20034=* +tokeninfo(#20034,8,#20001,9,"(") +#20035=@"loc,{#10000},3,9,3,9" +locations_default(#20035,#10000,3,9,3,9) +hasLocation(#20034,#20035) +#20036=* +tokeninfo(#20036,6,#20001,10,"b") +#20037=@"loc,{#10000},3,10,3,10" +locations_default(#20037,#10000,3,10,3,10) +hasLocation(#20036,#20037) +#20038=* +tokeninfo(#20038,8,#20001,11,")") +#20039=@"loc,{#10000},3,11,3,11" +locations_default(#20039,#10000,3,11,3,11) +hasLocation(#20038,#20039) +#20040=* +tokeninfo(#20040,8,#20001,12,"{") +#20041=@"loc,{#10000},3,13,3,13" +locations_default(#20041,#10000,3,13,3,13) +hasLocation(#20040,#20041) +#20042=* +tokeninfo(#20042,7,#20001,13,"if") +#20043=@"loc,{#10000},4,3,4,4" +locations_default(#20043,#10000,4,3,4,4) +hasLocation(#20042,#20043) +#20044=* +tokeninfo(#20044,8,#20001,14,"(") +#20045=@"loc,{#10000},4,6,4,6" +locations_default(#20045,#10000,4,6,4,6) +hasLocation(#20044,#20045) +#20046=* +tokeninfo(#20046,6,#20001,15,"b") +#20047=@"loc,{#10000},4,7,4,7" +locations_default(#20047,#10000,4,7,4,7) +hasLocation(#20046,#20047) +#20048=* +tokeninfo(#20048,8,#20001,16,")") +#20049=@"loc,{#10000},4,8,4,8" +locations_default(#20049,#10000,4,8,4,8) +hasLocation(#20048,#20049) +#20050=* +tokeninfo(#20050,8,#20001,17,"{") +#20051=@"loc,{#10000},4,10,4,10" +locations_default(#20051,#10000,4,10,4,10) +hasLocation(#20050,#20051) +#20052=* +tokeninfo(#20052,7,#20001,18,"function") +#20053=@"loc,{#10000},5,5,5,12" +locations_default(#20053,#10000,5,5,5,12) +hasLocation(#20052,#20053) +#20054=* +tokeninfo(#20054,6,#20001,19,"g") +#20055=@"loc,{#10000},5,14,5,14" +locations_default(#20055,#10000,5,14,5,14) +hasLocation(#20054,#20055) +#20056=* +tokeninfo(#20056,8,#20001,20,"(") +#20057=@"loc,{#10000},5,15,5,15" +locations_default(#20057,#10000,5,15,5,15) +hasLocation(#20056,#20057) +#20058=* +tokeninfo(#20058,6,#20001,21,"y") +#20059=@"loc,{#10000},5,16,5,16" +locations_default(#20059,#10000,5,16,5,16) +hasLocation(#20058,#20059) +#20060=* +tokeninfo(#20060,8,#20001,22,")") +#20061=@"loc,{#10000},5,17,5,17" +locations_default(#20061,#10000,5,17,5,17) +hasLocation(#20060,#20061) +#20062=* +tokeninfo(#20062,8,#20001,23,"(") +#20063=@"loc,{#10000},5,18,5,18" +locations_default(#20063,#10000,5,18,5,18) +hasLocation(#20062,#20063) +#20064=* +tokeninfo(#20064,4,#20001,24,"""other result""") +#20065=@"loc,{#10000},5,19,5,32" +locations_default(#20065,#10000,5,19,5,32) +hasLocation(#20064,#20065) +#20066=* +tokeninfo(#20066,8,#20001,25,")") +#20067=@"loc,{#10000},5,33,5,33" +locations_default(#20067,#10000,5,33,5,33) +hasLocation(#20066,#20067) +#20068=* +tokeninfo(#20068,8,#20001,26,"}") +#20069=@"loc,{#10000},6,3,6,3" +locations_default(#20069,#10000,6,3,6,3) +hasLocation(#20068,#20069) +#20070=* +tokeninfo(#20070,8,#20001,27,"}") +hasLocation(#20070,#20015) +#20071=* +tokeninfo(#20071,0,#20001,28,"") +#20072=@"loc,{#10000},8,1,8,0" +locations_default(#20072,#10000,8,1,8,0) +hasLocation(#20071,#20072) +toplevels(#20001,0) +#20073=@"loc,{#10000},1,1,8,0" +locations_default(#20073,#10000,1,1,8,0) +hasLocation(#20001,#20073) +#20074=@"var;{f};{#20000}" +variables(#20074,"f",#20000) +#20075=* +stmts(#20075,17,#20001,0,"functio ... esult"")") +hasLocation(#20075,#20003) +stmtContainers(#20075,#20001) +#20076=* +exprs(#20076,78,#20075,-1,"f") +hasLocation(#20076,#20019) +exprContainers(#20076,#20075) +literals("f","f",#20076) +decl(#20076,#20074) +#20077=* +scopes(#20077,1) +scopenodes(#20075,#20077) +scopenesting(#20077,#20000) +#20078=@"var;{x};{#20077}" +variables(#20078,"x",#20077) +#20079=* +exprs(#20079,78,#20075,0,"x") +hasLocation(#20079,#20023) +exprContainers(#20079,#20075) +literals("x","x",#20079) +decl(#20079,#20078) +#20080=@"var;{arguments};{#20077}" +variables(#20080,"arguments",#20077) +isArgumentsObject(#20080) +#20081=* +exprs(#20081,63,#20075,-2,"(""result"")") +#20082=@"loc,{#10000},1,14,1,23" +locations_default(#20082,#10000,1,14,1,23) +hasLocation(#20081,#20082) +exprContainers(#20081,#20075) +#20083=* +exprs(#20083,4,#20081,0,"""result""") +hasLocation(#20083,#20029) +exprContainers(#20083,#20075) +literals("result","""result""",#20083) +#20084=* +stmts(#20084,2,#20001,1,"functio ... )\n }\n}") +#20085=@"loc,{#10000},3,1,7,1" +locations_default(#20085,#10000,3,1,7,1) +hasLocation(#20084,#20085) +stmtContainers(#20084,#20001) +#20086=* +exprs(#20086,9,#20084,0,"functio ... )\n }\n}") +hasLocation(#20086,#20085) +enclosingStmt(#20086,#20084) +exprContainers(#20086,#20001) +#20087=* +scopes(#20087,1) +scopenodes(#20086,#20087) +scopenesting(#20087,#20000) +#20088=@"var;{g};{#20087}" +variables(#20088,"g",#20087) +#20089=@"var;{b};{#20087}" +variables(#20089,"b",#20087) +#20090=* +exprs(#20090,78,#20086,0,"b") +hasLocation(#20090,#20037) +exprContainers(#20090,#20086) +literals("b","b",#20090) +decl(#20090,#20089) +#20091=@"var;{arguments};{#20087}" +variables(#20091,"arguments",#20087) +isArgumentsObject(#20091) +#20092=* +stmts(#20092,1,#20086,-2,"{\n if ... )\n }\n}") +#20093=@"loc,{#10000},3,13,7,1" +locations_default(#20093,#10000,3,13,7,1) +hasLocation(#20092,#20093) +stmtContainers(#20092,#20086) +#20094=* +stmts(#20094,3,#20092,0,"if (b) ... t"")\n }") +#20095=@"loc,{#10000},4,3,6,3" +locations_default(#20095,#10000,4,3,6,3) +hasLocation(#20094,#20095) +stmtContainers(#20094,#20086) +#20096=* +exprs(#20096,79,#20094,0,"b") +hasLocation(#20096,#20047) +enclosingStmt(#20096,#20094) +exprContainers(#20096,#20086) +literals("b","b",#20096) +bind(#20096,#20089) +#20097=* +stmts(#20097,1,#20094,1,"{\n f ... t"")\n }") +#20098=@"loc,{#10000},4,10,6,3" +locations_default(#20098,#10000,4,10,6,3) +hasLocation(#20097,#20098) +stmtContainers(#20097,#20086) +#20099=* +stmts(#20099,17,#20097,0,"functio ... esult"")") +#20100=@"loc,{#10000},5,5,5,33" +locations_default(#20100,#10000,5,5,5,33) +hasLocation(#20099,#20100) +stmtContainers(#20099,#20086) +#20101=* +exprs(#20101,78,#20099,-1,"g") +hasLocation(#20101,#20055) +exprContainers(#20101,#20099) +literals("g","g",#20101) +decl(#20101,#20088) +#20102=* +scopes(#20102,1) +scopenodes(#20099,#20102) +scopenesting(#20102,#20087) +#20103=@"var;{y};{#20102}" +variables(#20103,"y",#20102) +#20104=* +exprs(#20104,78,#20099,0,"y") +hasLocation(#20104,#20059) +exprContainers(#20104,#20099) +literals("y","y",#20104) +decl(#20104,#20103) +#20105=@"var;{arguments};{#20102}" +variables(#20105,"arguments",#20102) +isArgumentsObject(#20105) +#20106=* +exprs(#20106,63,#20099,-2,"(""other result"")") +#20107=@"loc,{#10000},5,18,5,33" +locations_default(#20107,#10000,5,18,5,33) +hasLocation(#20106,#20107) +exprContainers(#20106,#20099) +#20108=* +exprs(#20108,4,#20106,0,"""other result""") +hasLocation(#20108,#20065) +exprContainers(#20108,#20099) +literals("other result","""other result""",#20108) +#20109=* +entry_cfg_node(#20109,#20001) +#20110=@"loc,{#10000},1,1,1,0" +locations_default(#20110,#10000,1,1,1,0) +hasLocation(#20109,#20110) +#20111=* +exit_cfg_node(#20111,#20001) +hasLocation(#20111,#20072) +successor(#20084,#20086) +successor(#20086,#20111) +#20112=* +entry_cfg_node(#20112,#20086) +#20113=@"loc,{#10000},3,1,3,0" +locations_default(#20113,#10000,3,1,3,0) +hasLocation(#20112,#20113) +#20114=* +exit_cfg_node(#20114,#20086) +#20115=@"loc,{#10000},7,2,7,1" +locations_default(#20115,#10000,7,2,7,1) +hasLocation(#20114,#20115) +successor(#20092,#20094) +successor(#20094,#20096) +#20116=* +guard_node(#20116,1,#20096) +hasLocation(#20116,#20047) +successor(#20116,#20097) +#20117=* +guard_node(#20117,0,#20096) +hasLocation(#20117,#20047) +successor(#20117,#20114) +successor(#20096,#20116) +successor(#20096,#20117) +successor(#20097,#20099) +successor(#20099,#20101) +successor(#20101,#20114) +#20118=* +entry_cfg_node(#20118,#20099) +#20119=@"loc,{#10000},5,5,5,4" +locations_default(#20119,#10000,5,5,5,4) +hasLocation(#20118,#20119) +#20120=* +exit_cfg_node(#20120,#20099) +#20121=@"loc,{#10000},5,34,5,33" +locations_default(#20121,#10000,5,34,5,33) +hasLocation(#20120,#20121) +successor(#20106,#20108) +successor(#20108,#20120) +successor(#20104,#20106) +successor(#20118,#20104) +successor(#20090,#20092) +successor(#20112,#20090) +successor(#20075,#20084) +#20122=* +entry_cfg_node(#20122,#20075) +hasLocation(#20122,#20110) +#20123=* +exit_cfg_node(#20123,#20075) +#20124=@"loc,{#10000},1,24,1,23" +locations_default(#20124,#10000,1,24,1,23) +hasLocation(#20123,#20124) +successor(#20081,#20083) +successor(#20083,#20123) +successor(#20079,#20081) +successor(#20122,#20079) +successor(#20076,#20075) +successor(#20109,#20076) +#20125=* +jsParseErrors(#20125,#20001,"Error: Missing function name","function(b) { +") +hasLocation(#20125,#20035) +#20126=* +lines(#20126,#20001,"function f(x)(""result"")"," +") +hasLocation(#20126,#20003) +#20127=* +lines(#20127,#20001,""," +") +hasLocation(#20127,#20005) +#20128=* +lines(#20128,#20001,"function(b) {"," +") +hasLocation(#20128,#20007) +#20129=* +lines(#20129,#20001," if (b) {"," +") +hasLocation(#20129,#20009) +indentation(#10000,4," ",2) +#20130=* +lines(#20130,#20001," function g(y)(""other result"")"," +") +hasLocation(#20130,#20011) +indentation(#10000,5," ",4) +#20131=* +lines(#20131,#20001," }"," +") +hasLocation(#20131,#20013) +indentation(#10000,6," ",2) +#20132=* +lines(#20132,#20001,"}"," +") +hasLocation(#20132,#20015) +numlines(#20001,7,0,0) +numlines(#10000,7,6,0) +filetype(#10000,"javascript")