Skip to content

Commit b8ee169

Browse files
committed
Add wrapper to emit statics/decorators inside es5 class
1 parent 5e20c1c commit b8ee169

File tree

176 files changed

+1909
-1428
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

176 files changed

+1909
-1428
lines changed

src/compiler/core.ts

Lines changed: 54 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -777,27 +777,33 @@ namespace ts {
777777
}
778778

779779
/**
780-
* Returns the first element of an array if non-empty, `undefined` otherwise.
780+
* Returns the element at a specific offset in an array if non-empty, `undefined` otherwise.
781+
* A negative offset indicates the element should be retrieved from the end of the array.
781782
*/
782-
export function firstOrUndefined<T>(array: T[]): T {
783-
return array && array.length > 0
784-
? array[0]
783+
export function elementAt<T>(array: T[] | undefined, offset: number): T | undefined {
784+
return array && array.length > 0 && (offset < 0 ? ~offset : offset) < array.length
785+
? array[offset < 0 ? array.length + offset : offset]
785786
: undefined;
786787
}
787788

789+
/**
790+
* Returns the first element of an array if non-empty, `undefined` otherwise.
791+
*/
792+
export function firstOrUndefined<T>(array: T[]): T | undefined {
793+
return elementAt(array, 0);
794+
}
795+
788796
/**
789797
* Returns the last element of an array if non-empty, `undefined` otherwise.
790798
*/
791-
export function lastOrUndefined<T>(array: T[]): T {
792-
return array && array.length > 0
793-
? array[array.length - 1]
794-
: undefined;
799+
export function lastOrUndefined<T>(array: T[]): T | undefined {
800+
return elementAt(array, -1);
795801
}
796802

797803
/**
798804
* Returns the only element of an array if it contains only one element, `undefined` otherwise.
799805
*/
800-
export function singleOrUndefined<T>(array: T[]): T {
806+
export function singleOrUndefined<T>(array: T[]): T | undefined {
801807
return array && array.length === 1
802808
? array[0]
803809
: undefined;
@@ -1125,6 +1131,15 @@ namespace ts {
11251131
return Array.isArray ? Array.isArray(value) : value instanceof Array;
11261132
}
11271133

1134+
export function tryCast<TOut extends TIn, TIn = any>(value: TIn | undefined, test: (value: TIn) => value is TOut): TOut | undefined {
1135+
return value !== undefined && test(value) ? value : undefined;
1136+
}
1137+
1138+
export function cast<TOut extends TIn, TIn = any>(value: TIn | undefined, test: (value: TIn) => value is TOut): TOut {
1139+
if (value !== undefined && test(value)) return value;
1140+
Debug.fail(`Invalid cast. The supplied value did not pass the test '${Debug.getFunctionName(test)}'.`);
1141+
}
1142+
11281143
/** Does nothing. */
11291144
export function noop(): void {}
11301145

@@ -2204,8 +2219,11 @@ namespace ts {
22042219
this.declarations = undefined;
22052220
}
22062221

2207-
function Type(this: Type, _checker: TypeChecker, flags: TypeFlags) {
2222+
function Type(this: Type, checker: TypeChecker, flags: TypeFlags) {
22082223
this.flags = flags;
2224+
if (Debug.isDebugging) {
2225+
this.checker = checker;
2226+
}
22092227
}
22102228

22112229
function Signature() {
@@ -2242,24 +2260,42 @@ namespace ts {
22422260

22432261
export namespace Debug {
22442262
export let currentAssertionLevel = AssertionLevel.None;
2263+
export let isDebugging = false;
22452264

22462265
export function shouldAssert(level: AssertionLevel): boolean {
22472266
return currentAssertionLevel >= level;
22482267
}
22492268

2250-
export function assert(expression: boolean, message?: string, verboseDebugInfo?: () => string): void {
2269+
export function assert(expression: boolean, message?: string, verboseDebugInfo?: () => string, stackCrawlMark?: Function): void {
22512270
if (!expression) {
2252-
let verboseDebugString = "";
22532271
if (verboseDebugInfo) {
2254-
verboseDebugString = "\r\nVerbose Debug Information: " + verboseDebugInfo();
2272+
message += "\r\nVerbose Debug Information: " + verboseDebugInfo();
22552273
}
2256-
debugger;
2257-
throw new Error("Debug Failure. False expression: " + (message || "") + verboseDebugString);
2274+
fail(message ? "False expression: " + message : "False expression.", stackCrawlMark || assert);
2275+
}
2276+
}
2277+
2278+
export function fail(message?: string, stackCrawlMark?: Function): void {
2279+
debugger;
2280+
const e = new Error(message ? `Debug Failure. ` : "Debug Failure.");
2281+
if ((<any>Error).captureStackTrace) {
2282+
(<any>Error).captureStackTrace(e, stackCrawlMark || fail);
22582283
}
2284+
throw e;
22592285
}
22602286

2261-
export function fail(message?: string): void {
2262-
Debug.assert(/*expression*/ false, message);
2287+
export function getFunctionName(func: Function) {
2288+
if (typeof func !== "function") {
2289+
return "";
2290+
}
2291+
else if (func.hasOwnProperty("name")) {
2292+
return (<any>func).name;
2293+
}
2294+
else {
2295+
const text = Function.prototype.toString.call(func);
2296+
const match = /^function\s+([\w\$]+)\s*\(/.exec(text);
2297+
return match ? match[1] : "";
2298+
}
22632299
}
22642300
}
22652301

@@ -2425,4 +2461,4 @@ namespace ts {
24252461
export function isCheckJsEnabledForFile(sourceFile: SourceFile, compilerOptions: CompilerOptions) {
24262462
return sourceFile.checkJsDirective ? sourceFile.checkJsDirective.enabled : compilerOptions.checkJs;
24272463
}
2428-
}
2464+
}

src/compiler/factory.ts

Lines changed: 73 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2130,6 +2130,24 @@ namespace ts {
21302130

21312131
// Compound nodes
21322132

2133+
export function createImmediatelyInvokedFunctionExpression(statements: Statement[]): CallExpression;
2134+
export function createImmediatelyInvokedFunctionExpression(statements: Statement[], param: ParameterDeclaration, paramValue: Expression): CallExpression;
2135+
export function createImmediatelyInvokedFunctionExpression(statements: Statement[], param?: ParameterDeclaration, paramValue?: Expression) {
2136+
return createCall(
2137+
createFunctionExpression(
2138+
/*modifiers*/ undefined,
2139+
/*asteriskToken*/ undefined,
2140+
/*name*/ undefined,
2141+
/*typeParameters*/ undefined,
2142+
/*parameters*/ param ? [param] : [],
2143+
/*type*/ undefined,
2144+
createBlock(statements, /*multiLine*/ true)
2145+
),
2146+
/*typeArguments*/ undefined,
2147+
/*argumentsArray*/ paramValue ? [paramValue] : []
2148+
);
2149+
}
2150+
21332151
export function createComma(left: Expression, right: Expression) {
21342152
return <Expression>createBinary(left, SyntaxKind.CommaToken, right);
21352153
}
@@ -3212,6 +3230,26 @@ namespace ts {
32123230
return isBlock(node) ? node : setTextRange(createBlock([setTextRange(createReturn(node), node)], multiLine), node);
32133231
}
32143232

3233+
export function convertFunctionDeclarationToExpression(node: FunctionDeclaration) {
3234+
Debug.assert(!!node.body);
3235+
const updated = createFunctionExpression(
3236+
node.modifiers,
3237+
node.asteriskToken,
3238+
node.name,
3239+
node.typeParameters,
3240+
node.parameters,
3241+
node.type,
3242+
node.body
3243+
);
3244+
setOriginalNode(updated, node);
3245+
setTextRange(updated, node);
3246+
if (node.startsOnNewLine) {
3247+
updated.startsOnNewLine = true;
3248+
}
3249+
aggregateTransformFlags(updated);
3250+
return updated;
3251+
}
3252+
32153253
function isUseStrictPrologue(node: ExpressionStatement): boolean {
32163254
return (node.expression as StringLiteral).text === "use strict";
32173255
}
@@ -3610,7 +3648,7 @@ namespace ts {
36103648
if (kind === SyntaxKind.FunctionExpression || kind === SyntaxKind.ArrowFunction) {
36113649
const mutableCall = getMutableClone(emittedExpression);
36123650
mutableCall.expression = setTextRange(createParen(callee), callee);
3613-
return recreatePartiallyEmittedExpressions(expression, mutableCall);
3651+
return recreateOuterExpressions(expression, mutableCall, OuterExpressionKinds.PartiallyEmittedExpressions);
36143652
}
36153653
}
36163654
else {
@@ -3652,22 +3690,6 @@ namespace ts {
36523690
}
36533691
}
36543692

3655-
/**
3656-
* Clones a series of not-emitted expressions with a new inner expression.
3657-
*
3658-
* @param originalOuterExpression The original outer expression.
3659-
* @param newInnerExpression The new inner expression.
3660-
*/
3661-
function recreatePartiallyEmittedExpressions(originalOuterExpression: Expression, newInnerExpression: Expression) {
3662-
if (isPartiallyEmittedExpression(originalOuterExpression)) {
3663-
const clone = getMutableClone(originalOuterExpression);
3664-
clone.expression = recreatePartiallyEmittedExpressions(clone.expression, newInnerExpression);
3665-
return clone;
3666-
}
3667-
3668-
return newInnerExpression;
3669-
}
3670-
36713693
function getLeftmostExpression(node: Expression): Expression {
36723694
while (true) {
36733695
switch (node.kind) {
@@ -3714,6 +3736,21 @@ namespace ts {
37143736
All = Parentheses | Assertions | PartiallyEmittedExpressions
37153737
}
37163738

3739+
export type OuterExpression = ParenthesizedExpression | TypeAssertion | AsExpression | PartiallyEmittedExpression;
3740+
3741+
export function isOuterExpression(node: Node, kinds = OuterExpressionKinds.All): node is OuterExpression {
3742+
switch (node.kind) {
3743+
case SyntaxKind.ParenthesizedExpression:
3744+
return (kinds & OuterExpressionKinds.Parentheses) !== 0;
3745+
case SyntaxKind.TypeAssertionExpression:
3746+
case SyntaxKind.AsExpression:
3747+
return (kinds & OuterExpressionKinds.Assertions) !== 0;
3748+
case SyntaxKind.PartiallyEmittedExpression:
3749+
return (kinds & OuterExpressionKinds.PartiallyEmittedExpressions) !== 0;
3750+
}
3751+
return false;
3752+
}
3753+
37173754
export function skipOuterExpressions(node: Expression, kinds?: OuterExpressionKinds): Expression;
37183755
export function skipOuterExpressions(node: Node, kinds?: OuterExpressionKinds): Node;
37193756
export function skipOuterExpressions(node: Node, kinds = OuterExpressionKinds.All) {
@@ -3767,6 +3804,25 @@ namespace ts {
37673804
return node;
37683805
}
37693806

3807+
function updateOuterExpression(outerExpression: OuterExpression, expression: Expression) {
3808+
switch (outerExpression.kind) {
3809+
case SyntaxKind.ParenthesizedExpression: return updateParen(outerExpression, expression);
3810+
case SyntaxKind.TypeAssertionExpression: return updateTypeAssertion(outerExpression, outerExpression.type, expression);
3811+
case SyntaxKind.AsExpression: return updateAsExpression(outerExpression, expression, outerExpression.type);
3812+
case SyntaxKind.PartiallyEmittedExpression: return updatePartiallyEmittedExpression(outerExpression, expression);
3813+
}
3814+
}
3815+
3816+
export function recreateOuterExpressions(outerExpression: Expression | undefined, innerExpression: Expression, kinds = OuterExpressionKinds.All): Expression {
3817+
if (outerExpression && isOuterExpression(outerExpression, kinds)) {
3818+
return updateOuterExpression(
3819+
outerExpression,
3820+
recreateOuterExpressions(outerExpression.expression, innerExpression)
3821+
);
3822+
}
3823+
return innerExpression;
3824+
}
3825+
37703826
export function startOnNewLine<T extends Node>(node: T): T {
37713827
node.startsOnNewLine = true;
37723828
return node;

src/compiler/sys.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ namespace ts {
4141
realpath?(path: string): string;
4242
/*@internal*/ getEnvironmentVariable(name: string): string;
4343
/*@internal*/ tryEnableSourceMapsForHost?(): void;
44+
/*@internal*/ debugMode?: boolean;
4445
setTimeout?(callback: (...args: any[]) => void, ms: number, ...args: any[]): any;
4546
clearTimeout?(timeoutId: any): void;
4647
}
@@ -428,6 +429,7 @@ namespace ts {
428429
realpath(path: string): string {
429430
return _fs.realpathSync(path);
430431
},
432+
debugMode: some(<string[]>process.execArgv, arg => /^--(inspect|debug)(-brk)?(=\d+)?$/i.test(arg)),
431433
tryEnableSourceMapsForHost() {
432434
try {
433435
require("source-map-support").install();
@@ -517,4 +519,7 @@ namespace ts {
517519
? AssertionLevel.Normal
518520
: AssertionLevel.None;
519521
}
522+
if (sys && sys.debugMode) {
523+
Debug.isDebugging = true;
524+
}
520525
}

0 commit comments

Comments
 (0)