fix(compiler-cli): generate let
statements in ES2015+ mode (#38775)
When the target of the compiler is ES2015 or newer then we should be generating `let` and `const` variable declarations rather than `var`. PR Close #38775
This commit is contained in:

committed by
Misko Hevery

parent
6158dc16b4
commit
123bff7cb6
@ -134,15 +134,16 @@ class ExpressionTranslatorVisitor implements ExpressionVisitor, StatementVisitor
|
||||
private scriptTarget: Exclude<ts.ScriptTarget, ts.ScriptTarget.JSON>) {}
|
||||
|
||||
visitDeclareVarStmt(stmt: DeclareVarStmt, context: Context): ts.VariableStatement {
|
||||
const isConst =
|
||||
this.scriptTarget >= ts.ScriptTarget.ES2015 && stmt.hasModifier(StmtModifier.Final);
|
||||
const varType = this.scriptTarget < ts.ScriptTarget.ES2015 ?
|
||||
ts.NodeFlags.None :
|
||||
stmt.hasModifier(StmtModifier.Final) ? ts.NodeFlags.Const : ts.NodeFlags.Let;
|
||||
const varDeclaration = ts.createVariableDeclaration(
|
||||
/* name */ stmt.name,
|
||||
/* type */ undefined,
|
||||
/* initializer */ stmt.value?.visitExpression(this, context.withExpressionMode));
|
||||
const declarationList = ts.createVariableDeclarationList(
|
||||
/* declarations */[varDeclaration],
|
||||
/* flags */ isConst ? ts.NodeFlags.Const : ts.NodeFlags.None);
|
||||
/* flags */ varType);
|
||||
const varStatement = ts.createVariableStatement(undefined, declarationList);
|
||||
return attachComments(varStatement, stmt.leadingComments);
|
||||
}
|
||||
|
Reference in New Issue
Block a user