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
@ -69,7 +69,7 @@ B.decorators = [
|
||||
{ type: OtherB },
|
||||
{ type: Directive, args: [{ selector: '[b]' }] }
|
||||
];
|
||||
var C_1;
|
||||
let C_1;
|
||||
let C = C_1 = class C {};
|
||||
C.decorators = [
|
||||
{ type: Directive, args: [{ selector: '[c]' }] },
|
||||
@ -111,7 +111,7 @@ B.decorators = [
|
||||
];
|
||||
return B;
|
||||
})();
|
||||
var C_1;
|
||||
let C_1;
|
||||
let C = C_1 = /** @class */ (() => {
|
||||
class C {}
|
||||
C.decorators = [
|
||||
@ -432,7 +432,7 @@ A.decorators = [
|
||||
name: _('/node_modules/test-package/some/file.js'),
|
||||
contents: `
|
||||
import * as tslib_1 from "tslib";
|
||||
var D_1;
|
||||
let D_1;
|
||||
/* A copyright notice */
|
||||
import { Directive } from '@angular/core';
|
||||
const OtherA = () => (node) => { };
|
||||
@ -681,9 +681,9 @@ export { D };
|
||||
const stmt3 = new DeclareVarStmt('baz', new LiteralExpr('qux'), undefined, []);
|
||||
|
||||
expect(renderer.printStatement(stmt1, sourceFile, importManager)).toBe('const foo = 42;');
|
||||
expect(renderer.printStatement(stmt2, sourceFile, importManager)).toBe('var bar = true;');
|
||||
expect(renderer.printStatement(stmt2, sourceFile, importManager)).toBe('let bar = true;');
|
||||
expect(renderer.printStatement(stmt3, sourceFile, importManager))
|
||||
.toBe('var baz = "qux";');
|
||||
.toBe('let baz = "qux";');
|
||||
});
|
||||
});
|
||||
});
|
||||
|
@ -32,6 +32,8 @@ import {getRootFiles, makeTestEntryPointBundle} from '../helpers/utils';
|
||||
class TestRenderingFormatter implements RenderingFormatter {
|
||||
private printer = ts.createPrinter({newLine: ts.NewLineKind.LineFeed});
|
||||
|
||||
constructor(private isEs5: boolean) {}
|
||||
|
||||
addImports(output: MagicString, imports: Import[], sf: ts.SourceFile) {
|
||||
output.prepend('\n// ADD IMPORTS\n');
|
||||
}
|
||||
@ -63,7 +65,8 @@ class TestRenderingFormatter implements RenderingFormatter {
|
||||
}
|
||||
printStatement(stmt: Statement, sourceFile: ts.SourceFile, importManager: ImportManager): string {
|
||||
const node = translateStatement(
|
||||
stmt, importManager, NOOP_DEFAULT_IMPORT_RECORDER, ts.ScriptTarget.ES2015);
|
||||
stmt, importManager, NOOP_DEFAULT_IMPORT_RECORDER,
|
||||
this.isEs5 ? ts.ScriptTarget.ES5 : ts.ScriptTarget.ES2015);
|
||||
const code = this.printer.printNode(ts.EmitHint.Unspecified, node, sourceFile);
|
||||
|
||||
return `// TRANSPILED\n${code}`;
|
||||
@ -94,7 +97,7 @@ function createTestRenderer(
|
||||
.analyzeProgram(bundle.src.program);
|
||||
const privateDeclarationsAnalyses =
|
||||
new PrivateDeclarationsAnalyzer(host, referencesRegistry).analyzeProgram(bundle.src.program);
|
||||
const testFormatter = new TestRenderingFormatter();
|
||||
const testFormatter = new TestRenderingFormatter(isEs5);
|
||||
spyOn(testFormatter, 'addExports').and.callThrough();
|
||||
spyOn(testFormatter, 'addImports').and.callThrough();
|
||||
spyOn(testFormatter, 'addDefinitions').and.callThrough();
|
||||
@ -569,7 +572,7 @@ UndecoratedBase.ɵfac = function UndecoratedBase_Factory(t) { return new (t || U
|
||||
UndecoratedBase.ɵdir = ɵngcc0.ɵɵdefineDirective({ type: UndecoratedBase, viewQuery: function UndecoratedBase_Query(rf, ctx) { if (rf & 1) {
|
||||
ɵngcc0.ɵɵstaticViewQuery(_c0, true);
|
||||
} if (rf & 2) {
|
||||
var _t;
|
||||
let _t;
|
||||
ɵngcc0.ɵɵqueryRefresh(_t = ɵngcc0.ɵɵloadQuery()) && (ctx.test = _t.first);
|
||||
} } });`);
|
||||
});
|
||||
|
Reference in New Issue
Block a user