Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 0 additions & 15 deletions src/compiler/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1006,21 +1006,6 @@ namespace ts {
return to;
}

/**
* Appends a range of value to begin of an array, returning the array.
*
* @param to The array to which `value` is to be appended. If `to` is `undefined`, a new array
* is created if `value` was appended.
* @param from The values to append to the array. If `from` is `undefined`, nothing is
* appended. If an element of `from` is `undefined`, that element is not appended.
*/
export function prependRange<T>(to: T[], from: ReadonlyArray<T> | undefined): T[] | undefined {
if (from === undefined || from.length === 0) return to;
if (to === undefined) return from.slice();
to.unshift(...from);
return to;
}

/**
* @return Whether the value was added.
*/
Expand Down
10 changes: 5 additions & 5 deletions src/compiler/transformers/es2015.ts
Original file line number Diff line number Diff line change
Expand Up @@ -529,7 +529,7 @@ namespace ts {
createVariableStatement(/*modifiers*/ undefined,
createVariableDeclarationList(taggedTemplateStringDeclarations)));
}
prependRange(statements, endLexicalEnvironment());
prependStatements(statements, endLexicalEnvironment());
exitSubtree(ancestorFacts, HierarchyFacts.None, HierarchyFacts.None);
return updateSourceFileNode(
node,
Expand Down Expand Up @@ -837,7 +837,7 @@ namespace ts {
setEmitFlags(statement, EmitFlags.NoComments | EmitFlags.NoTokenSourceMaps);
statements.push(statement);

prependRange(statements, endLexicalEnvironment());
prependStatements(statements, endLexicalEnvironment());

const block = createBlock(setTextRange(createNodeArray(statements), /*location*/ node.members), /*multiLine*/ true);
setEmitFlags(block, EmitFlags.NoComments);
Expand Down Expand Up @@ -980,7 +980,7 @@ namespace ts {
);
}

prependRange(statements, endLexicalEnvironment());
prependStatements(statements, endLexicalEnvironment());

if (constructor) {
prependCaptureNewTargetIfNeeded(statements, constructor, /*copyOnWrite*/ false);
Expand Down Expand Up @@ -1896,7 +1896,7 @@ namespace ts {
}

const lexicalEnvironment = context.endLexicalEnvironment();
prependRange(statements, lexicalEnvironment);
prependStatements(statements, lexicalEnvironment);

prependCaptureNewTargetIfNeeded(statements, node, /*copyOnWrite*/ false);

Expand Down Expand Up @@ -2712,7 +2712,7 @@ namespace ts {
if (loopOutParameters.length) {
copyOutParameters(loopOutParameters, CopyDirection.ToOutParameter, statements);
}
prependRange(statements, lexicalEnvironment);
prependStatements(statements, lexicalEnvironment);
loopBody = createBlock(statements, /*multiline*/ true);
}

Expand Down
2 changes: 1 addition & 1 deletion src/compiler/transformers/es2017.ts
Original file line number Diff line number Diff line change
Expand Up @@ -413,7 +413,7 @@ namespace ts {
)
);

prependRange(statements, endLexicalEnvironment());
prependStatements(statements, endLexicalEnvironment());

const block = createBlock(statements, /*multiLine*/ true);
setTextRange(block, node.body);
Expand Down
4 changes: 2 additions & 2 deletions src/compiler/transformers/esnext.ts
Original file line number Diff line number Diff line change
Expand Up @@ -663,7 +663,7 @@ namespace ts {
)
);

prependRange(statements, endLexicalEnvironment());
prependStatements(statements, endLexicalEnvironment());
const block = updateBlock(node.body!, statements);

// Minor optimization, emit `_super` helper to capture `super` access in an arrow.
Expand Down Expand Up @@ -695,7 +695,7 @@ namespace ts {
const leadingStatements = endLexicalEnvironment();
if (statementOffset > 0 || some(statements) || some(leadingStatements)) {
const block = convertToFunctionBody(body, /*multiLine*/ true);
prependRange(statements, leadingStatements);
prependStatements(statements, leadingStatements);
addRange(statements, block.statements.slice(statementOffset));
return updateBlock(block, setTextRange(createNodeArray(statements), block.statements));
}
Expand Down
2 changes: 1 addition & 1 deletion src/compiler/transformers/generators.ts
Original file line number Diff line number Diff line change
Expand Up @@ -587,7 +587,7 @@ namespace ts {
transformAndEmitStatements(body.statements, statementOffset);

const buildResult = build();
prependRange(statements, endLexicalEnvironment());
prependStatements(statements, endLexicalEnvironment());
statements.push(createReturn(buildResult));

// Restore previous generator state
Expand Down
4 changes: 2 additions & 2 deletions src/compiler/transformers/module/module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ namespace ts {
append(statements, visitNode(currentModuleInfo.externalHelpersImportDeclaration, sourceElementVisitor, isStatement));
addRange(statements, visitNodes(node.statements, sourceElementVisitor, isStatement, statementOffset));
addExportEqualsIfNeeded(statements, /*emitAsReturn*/ false);
prependRange(statements, endLexicalEnvironment());
prependStatements(statements, endLexicalEnvironment());

const updated = updateSourceFileNode(node, setTextRange(createNodeArray(statements), node.statements));
if (currentModuleInfo.hasExportStarsToExportValues && !compilerOptions.importHelpers) {
Expand Down Expand Up @@ -426,7 +426,7 @@ namespace ts {

// End the lexical environment for the module body
// and merge any new lexical declarations.
prependRange(statements, endLexicalEnvironment());
prependStatements(statements, endLexicalEnvironment());

const body = createBlock(statements, /*multiLine*/ true);
if (currentModuleInfo.hasExportStarsToExportValues && !compilerOptions.importHelpers) {
Expand Down
2 changes: 1 addition & 1 deletion src/compiler/transformers/module/system.ts
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,7 @@ namespace ts {
// We emit hoisted variables early to align roughly with our previous emit output.
// Two key differences in this approach are:
// - Temporary variables will appear at the top rather than at the bottom of the file
prependRange(statements, endLexicalEnvironment());
prependStatements(statements, endLexicalEnvironment());

const exportStarFunction = addExportStarIfNeeded(statements)!; // TODO: GH#18217
const moduleObject = createObjectLiteral([
Expand Down
6 changes: 3 additions & 3 deletions src/compiler/transformers/ts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -669,7 +669,7 @@ namespace ts {
setEmitFlags(statement, EmitFlags.NoComments | EmitFlags.NoTokenSourceMaps);
statements.push(statement);

prependRange(statements, context.endLexicalEnvironment());
prependStatements(statements, context.endLexicalEnvironment());

const iife = createImmediatelyInvokedArrowFunction(statements);
setEmitFlags(iife, EmitFlags.TypeScriptClassWrapper);
Expand Down Expand Up @@ -2693,7 +2693,7 @@ namespace ts {
const statements: Statement[] = [];
startLexicalEnvironment();
const members = map(node.members, transformEnumMember);
prependRange(statements, endLexicalEnvironment());
prependStatements(statements, endLexicalEnvironment());
addRange(statements, members);

currentNamespaceContainerName = savedCurrentNamespaceLocalName;
Expand Down Expand Up @@ -3008,7 +3008,7 @@ namespace ts {
statementsLocation = moveRangePos(moduleBlock.statements, -1);
}

prependRange(statements, endLexicalEnvironment());
prependStatements(statements, endLexicalEnvironment());
currentNamespaceContainerName = savedCurrentNamespaceContainerName;
currentNamespace = savedCurrentNamespace;
currentScopeFirstDeclarationsOfName = savedCurrentScopeFirstDeclarationsOfName;
Expand Down
19 changes: 19 additions & 0 deletions src/compiler/utilities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,25 @@ namespace ts {
return !nodeIsMissing(node);
}

/**
* Appends a range of value to begin of an array, returning the array.
*
* @param to The array to which `value` is to be appended. If `to` is `undefined`, a new array
* is created if `value` was appended.
* @param from The values to append to the array. If `from` is `undefined`, nothing is
* appended. If an element of `from` is `undefined`, that element is not appended.
*/
export function prependStatements<T extends Statement>(to: T[], from: ReadonlyArray<T> | undefined): T[] | undefined {
if (from === undefined || from.length === 0) return to;
if (to === undefined) return from.slice();
const prologue = to.length && isPrologueDirective(to[0]) && to.shift();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's possible that there is more than one directive. Though I don't know if that occurs in real world code. Should this case be handled here?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We don't handle it in any other locations we check for prologues, I looked. I figured I shouldn't differ here.

to.unshift(...from);
if (prologue) {
to.unshift(prologue);
}
return to;
}

/**
* Determine if the given comment is a triple-slash
*
Expand Down
2 changes: 1 addition & 1 deletion src/compiler/visitor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1469,7 +1469,7 @@ namespace ts {

return isNodeArray(statements)
? setTextRange(createNodeArray(concatenate(declarations, statements)), statements)
: prependRange(statements, declarations);
: prependStatements(statements, declarations);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ for (; ;) {

//// [SystemModuleForStatementNoInitializer.js]
System.register([], function (exports_1, context_1) {
var i, limit;
"use strict";
var i, limit;
var __moduleName = context_1 && context_1.id;
return {
setters: [],
Expand Down
2 changes: 1 addition & 1 deletion tests/baselines/reference/aliasesInSystemModule1.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ module M {

//// [aliasesInSystemModule1.js]
System.register(["foo"], function (exports_1, context_1) {
var alias, cls, cls2, x, y, z, M;
"use strict";
var alias, cls, cls2, x, y, z, M;
var __moduleName = context_1 && context_1.id;
return {
setters: [
Expand Down
2 changes: 1 addition & 1 deletion tests/baselines/reference/aliasesInSystemModule2.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ module M {

//// [aliasesInSystemModule2.js]
System.register(["foo"], function (exports_1, context_1) {
var foo_1, cls, cls2, x, y, z, M;
"use strict";
var foo_1, cls, cls2, x, y, z, M;
var __moduleName = context_1 && context_1.id;
return {
setters: [
Expand Down
2 changes: 1 addition & 1 deletion tests/baselines/reference/allowSyntheticDefaultImports2.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ export class Foo {

//// [a.js]
System.register(["./b"], function (exports_1, context_1) {
var b_1, x;
"use strict";
var b_1, x;
var __moduleName = context_1 && context_1.id;
return {
setters: [
Expand Down
4 changes: 2 additions & 2 deletions tests/baselines/reference/allowSyntheticDefaultImports3.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ export class Foo {

//// [b.js]
System.register([], function (exports_1, context_1) {
var Foo;
"use strict";
var Foo;
var __moduleName = context_1 && context_1.id;
return {
setters: [],
Expand All @@ -29,8 +29,8 @@ System.register([], function (exports_1, context_1) {
});
//// [a.js]
System.register(["./b"], function (exports_1, context_1) {
var b_1, x;
"use strict";
var b_1, x;
var __moduleName = context_1 && context_1.id;
return {
setters: [
Expand Down
2 changes: 1 addition & 1 deletion tests/baselines/reference/allowSyntheticDefaultImports5.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ export var x = new Foo();

//// [a.js]
System.register(["./b"], function (exports_1, context_1) {
var b_1, x;
"use strict";
var b_1, x;
var __moduleName = context_1 && context_1.id;
return {
setters: [
Expand Down
2 changes: 1 addition & 1 deletion tests/baselines/reference/allowSyntheticDefaultImports6.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ export var x = new Foo();

//// [a.js]
System.register(["./b"], function (exports_1, context_1) {
var b_1, x;
"use strict";
var b_1, x;
var __moduleName = context_1 && context_1.id;
return {
setters: [
Expand Down
2 changes: 1 addition & 1 deletion tests/baselines/reference/allowSyntheticDefaultImports7.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ Foo.foo();

//// [a.js]
System.register(["./b"], function (exports_1, context_1) {
var b_1;
"use strict";
var b_1;
var __moduleName = context_1 && context_1.id;
return {
setters: [
Expand Down
2 changes: 1 addition & 1 deletion tests/baselines/reference/allowSyntheticDefaultImports8.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ Foo.foo();

//// [a.js]
System.register(["./b"], function (exports_1, context_1) {
var b_1;
"use strict";
var b_1;
var __moduleName = context_1 && context_1.id;
return {
setters: [
Expand Down
2 changes: 1 addition & 1 deletion tests/baselines/reference/anonymousDefaultExportsSystem.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ export default function() {}

//// [a.js]
System.register([], function (exports_1, context_1) {
var default_1;
"use strict";
var default_1;
var __moduleName = context_1 && context_1.id;
return {
setters: [],
Expand Down
2 changes: 1 addition & 1 deletion tests/baselines/reference/capturedLetConstInLoop4.js
Original file line number Diff line number Diff line change
Expand Up @@ -144,8 +144,8 @@ for (const y = 0; y < 1;) {

//// [capturedLetConstInLoop4.js]
System.register([], function (exports_1, context_1) {
var v0, v00, v1, v2, v3, v4, v5, v6, v7, v8, v0_c, v00_c, v1_c, v2_c, v3_c, v4_c, v5_c, v6_c, v7_c, v8_c;
"use strict";
var v0, v00, v1, v2, v3, v4, v5, v6, v7, v8, v0_c, v00_c, v1_c, v2_c, v3_c, v4_c, v5_c, v6_c, v7_c, v8_c;
var __moduleName = context_1 && context_1.id;
//======let
function exportedFoo() {
Expand Down
2 changes: 1 addition & 1 deletion tests/baselines/reference/decoratedClassExportsSystem1.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,14 @@ export class Testing123 {

//// [a.js]
System.register([], function (exports_1, context_1) {
"use strict";
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
return c > 3 && r && Object.defineProperty(target, key, r), r;
};
var Testing123_1, Testing123;
"use strict";
var __moduleName = context_1 && context_1.id;
return {
setters: [],
Expand Down
2 changes: 1 addition & 1 deletion tests/baselines/reference/decoratedClassExportsSystem2.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@ export class Testing123 { }

//// [a.js]
System.register([], function (exports_1, context_1) {
"use strict";
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
return c > 3 && r && Object.defineProperty(target, key, r), r;
};
var Testing123_1, Testing123;
"use strict";
var __moduleName = context_1 && context_1.id;
return {
setters: [],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,14 @@ export default class {}

//// [a.js]
System.register([], function (exports_1, context_1) {
"use strict";
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
return c > 3 && r && Object.defineProperty(target, key, r), r;
};
var decorator, Foo;
"use strict";
var __moduleName = context_1 && context_1.id;
return {
setters: [],
Expand All @@ -37,14 +37,14 @@ System.register([], function (exports_1, context_1) {
});
//// [b.js]
System.register([], function (exports_1, context_1) {
"use strict";
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
return c > 3 && r && Object.defineProperty(target, key, r), r;
};
var decorator, default_1;
"use strict";
var __moduleName = context_1 && context_1.id;
return {
setters: [],
Expand Down
2 changes: 1 addition & 1 deletion tests/baselines/reference/deduplicateImportsInSystem.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ console.log(A + B + C + D + E + F)

//// [deduplicateImportsInSystem.js]
System.register(["f1", "f2", "f3"], function (exports_1, context_1) {
var f1_1, f2_1, f3_1, f2_2, f2_3, f1_2;
"use strict";
var f1_1, f2_1, f3_1, f2_2, f2_3, f1_2;
var __moduleName = context_1 && context_1.id;
return {
setters: [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ export default function foo() {}

//// [a.js]
System.register([], function (exports_1, context_1) {
var Foo;
"use strict";
var Foo;
var __moduleName = context_1 && context_1.id;
return {
setters: [],
Expand Down
Loading