JavaScript: Accept expression-bodied function declarations in experimental mode.

This commit is contained in:
Max Schaefer
2019-01-29 15:56:03 +00:00
parent a42bec7f44
commit fbf2774bb3
5 changed files with 453 additions and 9 deletions

View File

@@ -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<Boolean, Identifier> p = parseFunctionName(isStatement, isAsync);

View File

@@ -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);

View File

@@ -17,26 +17,26 @@ import com.semmle.ts.ast.TypeParameter;
* </pre>
*/
public class FunctionDeclaration extends Statement implements IFunction {
private final AFunction<BlockStatement> fn;
private final AFunction<? extends Node> fn;
private final boolean hasDeclareKeyword;
private int symbol = -1;
public FunctionDeclaration(SourceLocation loc, Identifier id, List<Expression> params, BlockStatement body, boolean generator,
public FunctionDeclaration(SourceLocation loc, Identifier id, List<Expression> params, Node body, boolean generator,
boolean async) {
this(loc, new AFunction<BlockStatement>(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<Expression> params, BlockStatement body, boolean generator, boolean async, boolean hasDeclareKeyword,
List<Expression> params, Node body, boolean generator, boolean async, boolean hasDeclareKeyword,
List<TypeParameter> typeParameters, List<ITypeExpression> parameterTypes, ITypeExpression returnType,
ITypeExpression thisParameterType) {
this(loc, new AFunction<BlockStatement>(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<BlockStatement> fn, boolean hasDeclareKeyword) {
private FunctionDeclaration(SourceLocation loc, AFunction<Node> 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(); }