refactor(compiler-cli): function declarations must have names (#38866)

The `AstFactory.createFunctionDeclaration()` was allowing `null` to be
passed as the function `name` value. This is not actually possible, since
function declarations must always have a name.

PR Close #38866
This commit is contained in:
Pete Bacon Darwin 2020-09-23 09:16:30 +01:00 committed by Joey Perrott
parent 8c16330895
commit 1f5d7dc394
2 changed files with 3 additions and 3 deletions

View File

@ -97,7 +97,7 @@ export interface AstFactory<TStatement, TExpression> {
* @param parameters the names of the function's parameters. * @param parameters the names of the function's parameters.
* @param body a statement (or a block of statements) that are the body of the function. * @param body a statement (or a block of statements) that are the body of the function.
*/ */
createFunctionDeclaration(functionName: string|null, parameters: string[], body: TStatement): createFunctionDeclaration(functionName: string, parameters: string[], body: TStatement):
TStatement; TStatement;
/** /**

View File

@ -79,13 +79,13 @@ export class TypeScriptAstFactory implements AstFactory<ts.Statement, ts.Express
createExpressionStatement = ts.createExpressionStatement; createExpressionStatement = ts.createExpressionStatement;
createFunctionDeclaration(functionName: string|null, parameters: string[], body: ts.Statement): createFunctionDeclaration(functionName: string, parameters: string[], body: ts.Statement):
ts.Statement { ts.Statement {
if (!ts.isBlock(body)) { if (!ts.isBlock(body)) {
throw new Error(`Invalid syntax, expected a block, but got ${ts.SyntaxKind[body.kind]}.`); throw new Error(`Invalid syntax, expected a block, but got ${ts.SyntaxKind[body.kind]}.`);
} }
return ts.createFunctionDeclaration( return ts.createFunctionDeclaration(
undefined, undefined, undefined, functionName ?? undefined, undefined, undefined, undefined, undefined, functionName, undefined,
parameters.map(param => ts.createParameter(undefined, undefined, undefined, param)), parameters.map(param => ts.createParameter(undefined, undefined, undefined, param)),
undefined, body); undefined, body);
} }