Skip to content

Commit fbf2774

Browse files
author
Max Schaefer
committed
JavaScript: Accept expression-bodied function declarations in experimental mode.
1 parent a42bec7 commit fbf2774

File tree

5 files changed

+453
-9
lines changed

5 files changed

+453
-9
lines changed

javascript/extractor/src/com/semmle/jcorn/CustomParser.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -217,7 +217,7 @@ protected Node parseLetExpression(Position startLoc, boolean maybeStatement) {
217217
protected INode parseFunction(Position startLoc, boolean isStatement, boolean allowExpressionBody, boolean isAsync) {
218218
if (isFunctionSent(isStatement))
219219
return super.parseFunction(startLoc, isStatement, allowExpressionBody, isAsync);
220-
allowExpressionBody = allowExpressionBody || options.mozExtensions() && !isStatement;
220+
allowExpressionBody = allowExpressionBody || options.mozExtensions();
221221
boolean oldInGen = this.inGenerator, oldInAsync = this.inAsync;
222222
int oldYieldPos = this.yieldPos, oldAwaitPos = this.awaitPos;
223223
Pair<Boolean, Identifier> p = parseFunctionName(isStatement, isAsync);

javascript/extractor/src/com/semmle/jcorn/Parser.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2991,7 +2991,7 @@ protected IFunction parseFunctionRest(Position startLoc, boolean isStatement,
29912991
IFunction node;
29922992
SourceLocation loc = new SourceLocation(startLoc);
29932993
if (isStatement && id != null)
2994-
node = new FunctionDeclaration(loc, id, params, (BlockStatement) body, generator, async);
2994+
node = new FunctionDeclaration(loc, id, params, body, generator, async);
29952995
else
29962996
node = new FunctionExpression(loc, id, params, body, generator, async);
29972997
return this.finishNode(node);

javascript/extractor/src/com/semmle/js/ast/FunctionDeclaration.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,26 +17,26 @@
1717
* </pre>
1818
*/
1919
public class FunctionDeclaration extends Statement implements IFunction {
20-
private final AFunction<BlockStatement> fn;
20+
private final AFunction<? extends Node> fn;
2121
private final boolean hasDeclareKeyword;
2222
private int symbol = -1;
2323

24-
public FunctionDeclaration(SourceLocation loc, Identifier id, List<Expression> params, BlockStatement body, boolean generator,
24+
public FunctionDeclaration(SourceLocation loc, Identifier id, List<Expression> params, Node body, boolean generator,
2525
boolean async) {
26-
this(loc, new AFunction<BlockStatement>(id, params, body, generator, async, Collections.emptyList(), Collections.emptyList(),
26+
this(loc, new AFunction<>(id, params, body, generator, async, Collections.emptyList(), Collections.emptyList(),
2727
Collections.emptyList(), null, null),
2828
false);
2929
}
3030

3131
public FunctionDeclaration(SourceLocation loc, Identifier id,
32-
List<Expression> params, BlockStatement body, boolean generator, boolean async, boolean hasDeclareKeyword,
32+
List<Expression> params, Node body, boolean generator, boolean async, boolean hasDeclareKeyword,
3333
List<TypeParameter> typeParameters, List<ITypeExpression> parameterTypes, ITypeExpression returnType,
3434
ITypeExpression thisParameterType) {
35-
this(loc, new AFunction<BlockStatement>(id, params, body, generator, async, typeParameters, parameterTypes, Collections.emptyList(),
35+
this(loc, new AFunction<>(id, params, body, generator, async, typeParameters, parameterTypes, Collections.emptyList(),
3636
returnType, thisParameterType), hasDeclareKeyword);
3737
}
3838

39-
private FunctionDeclaration(SourceLocation loc, AFunction<BlockStatement> fn, boolean hasDeclareKeyword) {
39+
private FunctionDeclaration(SourceLocation loc, AFunction<Node> fn, boolean hasDeclareKeyword) {
4040
super("FunctionDeclaration", loc);
4141
this.fn = fn;
4242
this.hasDeclareKeyword = hasDeclareKeyword;
@@ -56,7 +56,7 @@ public FunctionExpression asFunctionExpression() {
5656
@Override public boolean hasDefault(int i) { return fn.hasDefault(i); }
5757
@Override public Expression getDefault(int i) { return fn.getDefault(i); }
5858
@Override public IPattern getRest() { return fn.getRest(); }
59-
@Override public BlockStatement getBody() { return fn.getBody(); }
59+
@Override public Node getBody() { return fn.getBody(); }
6060
@Override public boolean hasRest() { return fn.hasRest(); }
6161
public boolean hasId() { return fn.hasId(); }
6262
public boolean isGenerator() { return fn.isGenerator(); }
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
function f(x)("result")
2+
3+
function(b) {
4+
if (b) {
5+
function g(y)("other result")
6+
}
7+
}

0 commit comments

Comments
 (0)