mirror of
https://github.com/microsoft/TypeScript.git
synced 2026-05-17 11:24:29 -05:00
Merge pull request #16120 from Microsoft/fix15857
Add wrapper to emit statics/decorators inside es5 class
This commit is contained in:
@@ -752,6 +752,14 @@ namespace ts {
|
||||
return to;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the actual offset into an array for a relative offset. Negative offsets indicate a
|
||||
* position offset from the end of the array.
|
||||
*/
|
||||
function toOffset(array: any[], offset: number) {
|
||||
return offset < 0 ? array.length + offset : offset;
|
||||
}
|
||||
|
||||
/**
|
||||
* Appends a range of value to an array, returning the array.
|
||||
*
|
||||
@@ -759,11 +767,19 @@ namespace ts {
|
||||
* 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.
|
||||
* @param start The offset in `from` at which to start copying values.
|
||||
* @param end The offset in `from` at which to stop copying values (non-inclusive).
|
||||
*/
|
||||
export function addRange<T>(to: T[] | undefined, from: T[] | undefined): T[] | undefined {
|
||||
export function addRange<T>(to: T[] | undefined, from: T[] | undefined, start?: number, end?: number): T[] | undefined {
|
||||
if (from === undefined) return to;
|
||||
for (const v of from) {
|
||||
to = append(to, v);
|
||||
if (to === undefined) return from.slice(start, end);
|
||||
start = start === undefined ? 0 : toOffset(from, start);
|
||||
end = end === undefined ? from.length : toOffset(from, end);
|
||||
for (let i = start; i < end && i < from.length; i++) {
|
||||
const v = from[i];
|
||||
if (v !== undefined) {
|
||||
to.push(from[i]);
|
||||
}
|
||||
}
|
||||
return to;
|
||||
}
|
||||
@@ -788,28 +804,38 @@ namespace ts {
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the element at a specific offset in an array if non-empty, `undefined` otherwise.
|
||||
* A negative offset indicates the element should be retrieved from the end of the array.
|
||||
*/
|
||||
export function elementAt<T>(array: T[] | undefined, offset: number): T | undefined {
|
||||
if (array) {
|
||||
offset = toOffset(array, offset);
|
||||
if (offset < array.length) {
|
||||
return array[offset];
|
||||
}
|
||||
}
|
||||
return undefined;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the first element of an array if non-empty, `undefined` otherwise.
|
||||
*/
|
||||
export function firstOrUndefined<T>(array: T[]): T {
|
||||
return array && array.length > 0
|
||||
? array[0]
|
||||
: undefined;
|
||||
export function firstOrUndefined<T>(array: T[]): T | undefined {
|
||||
return elementAt(array, 0);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the last element of an array if non-empty, `undefined` otherwise.
|
||||
*/
|
||||
export function lastOrUndefined<T>(array: T[]): T {
|
||||
return array && array.length > 0
|
||||
? array[array.length - 1]
|
||||
: undefined;
|
||||
export function lastOrUndefined<T>(array: T[]): T | undefined {
|
||||
return elementAt(array, -1);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the only element of an array if it contains only one element, `undefined` otherwise.
|
||||
*/
|
||||
export function singleOrUndefined<T>(array: T[]): T {
|
||||
export function singleOrUndefined<T>(array: T[]): T | undefined {
|
||||
return array && array.length === 1
|
||||
? array[0]
|
||||
: undefined;
|
||||
@@ -1137,6 +1163,15 @@ namespace ts {
|
||||
return Array.isArray ? Array.isArray(value) : value instanceof Array;
|
||||
}
|
||||
|
||||
export function tryCast<TOut extends TIn, TIn = any>(value: TIn | undefined, test: (value: TIn) => value is TOut): TOut | undefined {
|
||||
return value !== undefined && test(value) ? value : undefined;
|
||||
}
|
||||
|
||||
export function cast<TOut extends TIn, TIn = any>(value: TIn | undefined, test: (value: TIn) => value is TOut): TOut {
|
||||
if (value !== undefined && test(value)) return value;
|
||||
Debug.fail(`Invalid cast. The supplied value did not pass the test '${Debug.getFunctionName(test)}'.`);
|
||||
}
|
||||
|
||||
/** Does nothing. */
|
||||
export function noop(): void {}
|
||||
|
||||
@@ -2222,8 +2257,11 @@ namespace ts {
|
||||
this.declarations = undefined;
|
||||
}
|
||||
|
||||
function Type(this: Type, _checker: TypeChecker, flags: TypeFlags) {
|
||||
function Type(this: Type, checker: TypeChecker, flags: TypeFlags) {
|
||||
this.flags = flags;
|
||||
if (Debug.isDebugging) {
|
||||
this.checker = checker;
|
||||
}
|
||||
}
|
||||
|
||||
function Signature() {
|
||||
@@ -2260,24 +2298,42 @@ namespace ts {
|
||||
|
||||
export namespace Debug {
|
||||
export let currentAssertionLevel = AssertionLevel.None;
|
||||
export let isDebugging = false;
|
||||
|
||||
export function shouldAssert(level: AssertionLevel): boolean {
|
||||
return currentAssertionLevel >= level;
|
||||
}
|
||||
|
||||
export function assert(expression: boolean, message?: string, verboseDebugInfo?: () => string): void {
|
||||
export function assert(expression: boolean, message?: string, verboseDebugInfo?: () => string, stackCrawlMark?: Function): void {
|
||||
if (!expression) {
|
||||
let verboseDebugString = "";
|
||||
if (verboseDebugInfo) {
|
||||
verboseDebugString = "\r\nVerbose Debug Information: " + verboseDebugInfo();
|
||||
message += "\r\nVerbose Debug Information: " + verboseDebugInfo();
|
||||
}
|
||||
debugger;
|
||||
throw new Error("Debug Failure. False expression: " + (message || "") + verboseDebugString);
|
||||
fail(message ? "False expression: " + message : "False expression.", stackCrawlMark || assert);
|
||||
}
|
||||
}
|
||||
|
||||
export function fail(message?: string): void {
|
||||
Debug.assert(/*expression*/ false, message);
|
||||
export function fail(message?: string, stackCrawlMark?: Function): void {
|
||||
debugger;
|
||||
const e = new Error(message ? `Debug Failure. ` : "Debug Failure.");
|
||||
if ((<any>Error).captureStackTrace) {
|
||||
(<any>Error).captureStackTrace(e, stackCrawlMark || fail);
|
||||
}
|
||||
throw e;
|
||||
}
|
||||
|
||||
export function getFunctionName(func: Function) {
|
||||
if (typeof func !== "function") {
|
||||
return "";
|
||||
}
|
||||
else if (func.hasOwnProperty("name")) {
|
||||
return (<any>func).name;
|
||||
}
|
||||
else {
|
||||
const text = Function.prototype.toString.call(func);
|
||||
const match = /^function\s+([\w\$]+)\s*\(/.exec(text);
|
||||
return match ? match[1] : "";
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2443,4 +2499,4 @@ namespace ts {
|
||||
export function isCheckJsEnabledForFile(sourceFile: SourceFile, compilerOptions: CompilerOptions) {
|
||||
return sourceFile.checkJsDirective ? sourceFile.checkJsDirective.enabled : compilerOptions.checkJs;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -2134,6 +2134,24 @@ namespace ts {
|
||||
|
||||
// Compound nodes
|
||||
|
||||
export function createImmediatelyInvokedFunctionExpression(statements: Statement[]): CallExpression;
|
||||
export function createImmediatelyInvokedFunctionExpression(statements: Statement[], param: ParameterDeclaration, paramValue: Expression): CallExpression;
|
||||
export function createImmediatelyInvokedFunctionExpression(statements: Statement[], param?: ParameterDeclaration, paramValue?: Expression) {
|
||||
return createCall(
|
||||
createFunctionExpression(
|
||||
/*modifiers*/ undefined,
|
||||
/*asteriskToken*/ undefined,
|
||||
/*name*/ undefined,
|
||||
/*typeParameters*/ undefined,
|
||||
/*parameters*/ param ? [param] : [],
|
||||
/*type*/ undefined,
|
||||
createBlock(statements, /*multiLine*/ true)
|
||||
),
|
||||
/*typeArguments*/ undefined,
|
||||
/*argumentsArray*/ paramValue ? [paramValue] : []
|
||||
);
|
||||
}
|
||||
|
||||
export function createComma(left: Expression, right: Expression) {
|
||||
return <Expression>createBinary(left, SyntaxKind.CommaToken, right);
|
||||
}
|
||||
@@ -3216,6 +3234,26 @@ namespace ts {
|
||||
return isBlock(node) ? node : setTextRange(createBlock([setTextRange(createReturn(node), node)], multiLine), node);
|
||||
}
|
||||
|
||||
export function convertFunctionDeclarationToExpression(node: FunctionDeclaration) {
|
||||
Debug.assert(!!node.body);
|
||||
const updated = createFunctionExpression(
|
||||
node.modifiers,
|
||||
node.asteriskToken,
|
||||
node.name,
|
||||
node.typeParameters,
|
||||
node.parameters,
|
||||
node.type,
|
||||
node.body
|
||||
);
|
||||
setOriginalNode(updated, node);
|
||||
setTextRange(updated, node);
|
||||
if (node.startsOnNewLine) {
|
||||
updated.startsOnNewLine = true;
|
||||
}
|
||||
aggregateTransformFlags(updated);
|
||||
return updated;
|
||||
}
|
||||
|
||||
function isUseStrictPrologue(node: ExpressionStatement): boolean {
|
||||
return (node.expression as StringLiteral).text === "use strict";
|
||||
}
|
||||
@@ -3614,7 +3652,7 @@ namespace ts {
|
||||
if (kind === SyntaxKind.FunctionExpression || kind === SyntaxKind.ArrowFunction) {
|
||||
const mutableCall = getMutableClone(emittedExpression);
|
||||
mutableCall.expression = setTextRange(createParen(callee), callee);
|
||||
return recreatePartiallyEmittedExpressions(expression, mutableCall);
|
||||
return recreateOuterExpressions(expression, mutableCall, OuterExpressionKinds.PartiallyEmittedExpressions);
|
||||
}
|
||||
}
|
||||
else {
|
||||
@@ -3656,22 +3694,6 @@ namespace ts {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Clones a series of not-emitted expressions with a new inner expression.
|
||||
*
|
||||
* @param originalOuterExpression The original outer expression.
|
||||
* @param newInnerExpression The new inner expression.
|
||||
*/
|
||||
function recreatePartiallyEmittedExpressions(originalOuterExpression: Expression, newInnerExpression: Expression) {
|
||||
if (isPartiallyEmittedExpression(originalOuterExpression)) {
|
||||
const clone = getMutableClone(originalOuterExpression);
|
||||
clone.expression = recreatePartiallyEmittedExpressions(clone.expression, newInnerExpression);
|
||||
return clone;
|
||||
}
|
||||
|
||||
return newInnerExpression;
|
||||
}
|
||||
|
||||
function getLeftmostExpression(node: Expression): Expression {
|
||||
while (true) {
|
||||
switch (node.kind) {
|
||||
@@ -3718,6 +3740,22 @@ namespace ts {
|
||||
All = Parentheses | Assertions | PartiallyEmittedExpressions
|
||||
}
|
||||
|
||||
export type OuterExpression = ParenthesizedExpression | TypeAssertion | AsExpression | NonNullExpression | PartiallyEmittedExpression;
|
||||
|
||||
export function isOuterExpression(node: Node, kinds = OuterExpressionKinds.All): node is OuterExpression {
|
||||
switch (node.kind) {
|
||||
case SyntaxKind.ParenthesizedExpression:
|
||||
return (kinds & OuterExpressionKinds.Parentheses) !== 0;
|
||||
case SyntaxKind.TypeAssertionExpression:
|
||||
case SyntaxKind.AsExpression:
|
||||
case SyntaxKind.NonNullExpression:
|
||||
return (kinds & OuterExpressionKinds.Assertions) !== 0;
|
||||
case SyntaxKind.PartiallyEmittedExpression:
|
||||
return (kinds & OuterExpressionKinds.PartiallyEmittedExpressions) !== 0;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
export function skipOuterExpressions(node: Expression, kinds?: OuterExpressionKinds): Expression;
|
||||
export function skipOuterExpressions(node: Node, kinds?: OuterExpressionKinds): Node;
|
||||
export function skipOuterExpressions(node: Node, kinds = OuterExpressionKinds.All) {
|
||||
@@ -3754,8 +3792,8 @@ namespace ts {
|
||||
export function skipAssertions(node: Expression): Expression;
|
||||
export function skipAssertions(node: Node): Node;
|
||||
export function skipAssertions(node: Node): Node {
|
||||
while (isAssertionExpression(node)) {
|
||||
node = (<AssertionExpression>node).expression;
|
||||
while (isAssertionExpression(node) || node.kind === SyntaxKind.NonNullExpression) {
|
||||
node = (<AssertionExpression | NonNullExpression>node).expression;
|
||||
}
|
||||
|
||||
return node;
|
||||
@@ -3771,6 +3809,26 @@ namespace ts {
|
||||
return node;
|
||||
}
|
||||
|
||||
function updateOuterExpression(outerExpression: OuterExpression, expression: Expression) {
|
||||
switch (outerExpression.kind) {
|
||||
case SyntaxKind.ParenthesizedExpression: return updateParen(outerExpression, expression);
|
||||
case SyntaxKind.TypeAssertionExpression: return updateTypeAssertion(outerExpression, outerExpression.type, expression);
|
||||
case SyntaxKind.AsExpression: return updateAsExpression(outerExpression, expression, outerExpression.type);
|
||||
case SyntaxKind.NonNullExpression: return updateNonNullExpression(outerExpression, expression);
|
||||
case SyntaxKind.PartiallyEmittedExpression: return updatePartiallyEmittedExpression(outerExpression, expression);
|
||||
}
|
||||
}
|
||||
|
||||
export function recreateOuterExpressions(outerExpression: Expression | undefined, innerExpression: Expression, kinds = OuterExpressionKinds.All): Expression {
|
||||
if (outerExpression && isOuterExpression(outerExpression, kinds)) {
|
||||
return updateOuterExpression(
|
||||
outerExpression,
|
||||
recreateOuterExpressions(outerExpression.expression, innerExpression)
|
||||
);
|
||||
}
|
||||
return innerExpression;
|
||||
}
|
||||
|
||||
export function startOnNewLine<T extends Node>(node: T): T {
|
||||
node.startsOnNewLine = true;
|
||||
return node;
|
||||
|
||||
@@ -41,6 +41,7 @@ namespace ts {
|
||||
realpath?(path: string): string;
|
||||
/*@internal*/ getEnvironmentVariable(name: string): string;
|
||||
/*@internal*/ tryEnableSourceMapsForHost?(): void;
|
||||
/*@internal*/ debugMode?: boolean;
|
||||
setTimeout?(callback: (...args: any[]) => void, ms: number, ...args: any[]): any;
|
||||
clearTimeout?(timeoutId: any): void;
|
||||
}
|
||||
@@ -428,6 +429,7 @@ namespace ts {
|
||||
realpath(path: string): string {
|
||||
return _fs.realpathSync(path);
|
||||
},
|
||||
debugMode: some(<string[]>process.execArgv, arg => /^--(inspect|debug)(-brk)?(=\d+)?$/i.test(arg)),
|
||||
tryEnableSourceMapsForHost() {
|
||||
try {
|
||||
require("source-map-support").install();
|
||||
@@ -517,4 +519,7 @@ namespace ts {
|
||||
? AssertionLevel.Normal
|
||||
: AssertionLevel.None;
|
||||
}
|
||||
if (sys && sys.debugMode) {
|
||||
Debug.isDebugging = true;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -339,11 +339,64 @@ namespace ts {
|
||||
&& !(<ReturnStatement>node).expression;
|
||||
}
|
||||
|
||||
function isClassLikeVariableStatement(node: Node) {
|
||||
if (!isVariableStatement(node)) return false;
|
||||
const variable = singleOrUndefined((<VariableStatement>node).declarationList.declarations);
|
||||
return variable
|
||||
&& variable.initializer
|
||||
&& isIdentifier(variable.name)
|
||||
&& (isClassLike(variable.initializer)
|
||||
|| (isAssignmentExpression(variable.initializer)
|
||||
&& isIdentifier(variable.initializer.left)
|
||||
&& isClassLike(variable.initializer.right)));
|
||||
}
|
||||
|
||||
function isTypeScriptClassWrapper(node: Node) {
|
||||
const call = tryCast(node, isCallExpression);
|
||||
if (!call || isParseTreeNode(call) ||
|
||||
some(call.typeArguments) ||
|
||||
some(call.arguments)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
const func = tryCast(skipOuterExpressions(call.expression), isFunctionExpression);
|
||||
if (!func || isParseTreeNode(func) ||
|
||||
some(func.typeParameters) ||
|
||||
some(func.parameters) ||
|
||||
func.type ||
|
||||
!func.body) {
|
||||
return false;
|
||||
}
|
||||
|
||||
const statements = func.body.statements;
|
||||
if (statements.length < 2) {
|
||||
return false;
|
||||
}
|
||||
|
||||
const firstStatement = statements[0];
|
||||
if (isParseTreeNode(firstStatement) ||
|
||||
!isClassLike(firstStatement) &&
|
||||
!isClassLikeVariableStatement(firstStatement)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
const lastStatement = elementAt(statements, -1);
|
||||
const returnStatement = tryCast(isVariableStatement(lastStatement) ? elementAt(statements, -2) : lastStatement, isReturnStatement);
|
||||
if (!returnStatement ||
|
||||
!returnStatement.expression ||
|
||||
!isIdentifier(skipOuterExpressions(returnStatement.expression))) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
function shouldVisitNode(node: Node): boolean {
|
||||
return (node.transformFlags & TransformFlags.ContainsES2015) !== 0
|
||||
|| convertedLoopState !== undefined
|
||||
|| (hierarchyFacts & HierarchyFacts.ConstructorWithCapturedSuper && isStatement(node))
|
||||
|| (isIterationStatement(node, /*lookInLabeledStatements*/ false) && shouldConvertIterationStatementBody(node));
|
||||
|| (isIterationStatement(node, /*lookInLabeledStatements*/ false) && shouldConvertIterationStatementBody(node))
|
||||
|| isTypeScriptClassWrapper(node);
|
||||
}
|
||||
|
||||
function visitor(node: Node): VisitResult<Node> {
|
||||
@@ -3251,6 +3304,10 @@ namespace ts {
|
||||
* @param node a CallExpression.
|
||||
*/
|
||||
function visitCallExpression(node: CallExpression) {
|
||||
if (isTypeScriptClassWrapper(node)) {
|
||||
return visitTypeScriptClassWrapper(node);
|
||||
}
|
||||
|
||||
if (node.transformFlags & TransformFlags.ES2015) {
|
||||
return visitCallExpressionWithPotentialCapturedThisAssignment(node, /*assignToCapturedThis*/ true);
|
||||
}
|
||||
@@ -3262,6 +3319,163 @@ namespace ts {
|
||||
);
|
||||
}
|
||||
|
||||
function visitTypeScriptClassWrapper(node: CallExpression) {
|
||||
// This is a call to a class wrapper function (an IIFE) created by the 'ts' transformer.
|
||||
// The wrapper has a form similar to:
|
||||
//
|
||||
// (function() {
|
||||
// class C { // 1
|
||||
// }
|
||||
// C.x = 1; // 2
|
||||
// return C;
|
||||
// }())
|
||||
//
|
||||
// When we transform the class, we end up with something like this:
|
||||
//
|
||||
// (function () {
|
||||
// var C = (function () { // 3
|
||||
// function C() {
|
||||
// }
|
||||
// return C; // 4
|
||||
// }());
|
||||
// C.x = 1;
|
||||
// return C;
|
||||
// }())
|
||||
//
|
||||
// We want to simplify the two nested IIFEs to end up with something like this:
|
||||
//
|
||||
// (function () {
|
||||
// function C() {
|
||||
// }
|
||||
// C.x = 1;
|
||||
// return C;
|
||||
// }())
|
||||
|
||||
// We skip any outer expressions in a number of places to get to the innermost
|
||||
// expression, but we will restore them later to preserve comments and source maps.
|
||||
const body = cast(skipOuterExpressions(node.expression), isFunctionExpression).body;
|
||||
|
||||
// The class statements are the statements generated by visiting the first statement of the
|
||||
// body (1), while all other statements are added to remainingStatements (2)
|
||||
const classStatements = visitNodes(body.statements, visitor, isStatement, 0, 1);
|
||||
const remainingStatements = visitNodes(body.statements, visitor, isStatement, 1, body.statements.length - 1);
|
||||
const varStatement = cast(firstOrUndefined(classStatements), isVariableStatement);
|
||||
|
||||
// We know there is only one variable declaration here as we verified this in an
|
||||
// earlier call to isTypeScriptClassWrapper
|
||||
const variable = varStatement.declarationList.declarations[0];
|
||||
const initializer = skipOuterExpressions(variable.initializer);
|
||||
|
||||
// Under certain conditions, the 'ts' transformer may introduce a class alias, which
|
||||
// we see as an assignment, for example:
|
||||
//
|
||||
// (function () {
|
||||
// var C = C_1 = (function () {
|
||||
// function C() {
|
||||
// }
|
||||
// C.x = function () { return C_1; }
|
||||
// return C;
|
||||
// }());
|
||||
// C = C_1 = __decorate([dec], C);
|
||||
// return C;
|
||||
// var C_1;
|
||||
// }())
|
||||
//
|
||||
const aliasAssignment = tryCast(initializer, isAssignmentExpression);
|
||||
|
||||
// The underlying call (3) is another IIFE that may contain a '_super' argument.
|
||||
const call = cast(aliasAssignment ? skipOuterExpressions(aliasAssignment.right) : initializer, isCallExpression);
|
||||
const func = cast(skipOuterExpressions(call.expression), isFunctionExpression);
|
||||
|
||||
const funcStatements = func.body.statements;
|
||||
let classBodyStart = 0;
|
||||
let classBodyEnd = -1;
|
||||
|
||||
const statements: Statement[] = [];
|
||||
if (aliasAssignment) {
|
||||
// If we have a class alias assignment, we need to move it to the down-level constructor
|
||||
// function we generated for the class.
|
||||
const extendsCall = tryCast(funcStatements[classBodyStart], isExpressionStatement);
|
||||
if (extendsCall) {
|
||||
statements.push(extendsCall);
|
||||
classBodyStart++;
|
||||
}
|
||||
|
||||
// We reuse the comment and source-map positions from the original variable statement
|
||||
// and class alias, while converting the function declaration for the class constructor
|
||||
// into an expression.
|
||||
statements.push(
|
||||
updateVariableStatement(
|
||||
varStatement,
|
||||
/*modifiers*/ undefined,
|
||||
updateVariableDeclarationList(varStatement.declarationList, [
|
||||
updateVariableDeclaration(variable,
|
||||
variable.name,
|
||||
/*type*/ undefined,
|
||||
updateBinary(aliasAssignment,
|
||||
aliasAssignment.left,
|
||||
convertFunctionDeclarationToExpression(
|
||||
cast(funcStatements[classBodyStart], isFunctionDeclaration)
|
||||
)
|
||||
)
|
||||
)
|
||||
])
|
||||
)
|
||||
);
|
||||
classBodyStart++;
|
||||
}
|
||||
|
||||
// Find the trailing 'return' statement (4)
|
||||
while (!isReturnStatement(elementAt(funcStatements, classBodyEnd))) {
|
||||
classBodyEnd--;
|
||||
}
|
||||
|
||||
// When we extract the statements of the inner IIFE, we exclude the 'return' statement (4)
|
||||
// as we already have one that has been introduced by the 'ts' transformer.
|
||||
addRange(statements, funcStatements, classBodyStart, classBodyEnd);
|
||||
|
||||
if (classBodyEnd < -1) {
|
||||
// If there were any hoisted declarations following the return statement, we should
|
||||
// append them.
|
||||
addRange(statements, funcStatements, classBodyEnd + 1);
|
||||
}
|
||||
|
||||
// Add the remaining statements of the outer wrapper.
|
||||
addRange(statements, remainingStatements);
|
||||
|
||||
// The 'es2015' class transform may add an end-of-declaration marker. If so we will add it
|
||||
// after the remaining statements from the 'ts' transformer.
|
||||
addRange(statements, classStatements, /*start*/ 1);
|
||||
|
||||
// Recreate any outer parentheses or partially-emitted expressions to preserve source map
|
||||
// and comment locations.
|
||||
return recreateOuterExpressions(node.expression,
|
||||
recreateOuterExpressions(variable.initializer,
|
||||
recreateOuterExpressions(aliasAssignment && aliasAssignment.right,
|
||||
updateCall(call,
|
||||
recreateOuterExpressions(call.expression,
|
||||
updateFunctionExpression(
|
||||
func,
|
||||
/*modifiers*/ undefined,
|
||||
/*asteriskToken*/ undefined,
|
||||
/*name*/ undefined,
|
||||
/*typeParameters*/ undefined,
|
||||
func.parameters,
|
||||
/*type*/ undefined,
|
||||
updateBlock(
|
||||
func.body,
|
||||
statements
|
||||
)
|
||||
)
|
||||
),
|
||||
/*typeArguments*/ undefined,
|
||||
call.arguments
|
||||
)
|
||||
)
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
function visitImmediateSuperCallInBody(node: CallExpression) {
|
||||
return visitCallExpressionWithPotentialCapturedThisAssignment(node, /*assignToCapturedThis*/ false);
|
||||
}
|
||||
@@ -3806,9 +4020,7 @@ namespace ts {
|
||||
return false;
|
||||
}
|
||||
if (isClassElement(currentNode) && currentNode.parent === declaration) {
|
||||
// we are in the class body, but we treat static fields as outside of the class body
|
||||
return currentNode.kind !== SyntaxKind.PropertyDeclaration
|
||||
|| (getModifierFlags(currentNode) & ModifierFlags.Static) === 0;
|
||||
return true;
|
||||
}
|
||||
currentNode = currentNode.parent;
|
||||
}
|
||||
|
||||
@@ -18,6 +18,23 @@ namespace ts {
|
||||
NonQualifiedEnumMembers = 1 << 3
|
||||
}
|
||||
|
||||
const enum ClassFacts {
|
||||
None = 0,
|
||||
HasStaticInitializedProperties = 1 << 0,
|
||||
HasConstructorDecorators = 1 << 1,
|
||||
HasMemberDecorators = 1 << 2,
|
||||
IsExportOfNamespace = 1 << 3,
|
||||
IsNamedExternalExport = 1 << 4,
|
||||
IsDefaultExternalExport = 1 << 5,
|
||||
HasExtendsClause = 1 << 6,
|
||||
UseImmediatelyInvokedFunctionExpression = 1 << 7,
|
||||
|
||||
HasAnyDecorators = HasConstructorDecorators | HasMemberDecorators,
|
||||
NeedsName = HasStaticInitializedProperties | HasMemberDecorators,
|
||||
MayNeedImmediatelyInvokedFunctionExpression = HasAnyDecorators | HasStaticInitializedProperties,
|
||||
IsExported = IsExportOfNamespace | IsDefaultExternalExport | IsNamedExternalExport,
|
||||
}
|
||||
|
||||
export function transformTypeScript(context: TransformationContext) {
|
||||
const {
|
||||
startLexicalEnvironment,
|
||||
@@ -505,6 +522,19 @@ namespace ts {
|
||||
return parameter.decorators !== undefined && parameter.decorators.length > 0;
|
||||
}
|
||||
|
||||
function getClassFacts(node: ClassDeclaration, staticProperties: PropertyDeclaration[]) {
|
||||
let facts = ClassFacts.None;
|
||||
if (some(staticProperties)) facts |= ClassFacts.HasStaticInitializedProperties;
|
||||
if (getClassExtendsHeritageClauseElement(node)) facts |= ClassFacts.HasExtendsClause;
|
||||
if (shouldEmitDecorateCallForClass(node)) facts |= ClassFacts.HasConstructorDecorators;
|
||||
if (childIsDecorated(node)) facts |= ClassFacts.HasMemberDecorators;
|
||||
if (isExportOfNamespace(node)) facts |= ClassFacts.IsExportOfNamespace;
|
||||
else if (isDefaultExternalModuleExport(node)) facts |= ClassFacts.IsDefaultExternalExport;
|
||||
else if (isNamedExternalModuleExport(node)) facts |= ClassFacts.IsNamedExternalExport;
|
||||
if (languageVersion <= ScriptTarget.ES5 && (facts & ClassFacts.MayNeedImmediatelyInvokedFunctionExpression)) facts |= ClassFacts.UseImmediatelyInvokedFunctionExpression;
|
||||
return facts;
|
||||
}
|
||||
|
||||
/**
|
||||
* Transforms a class declaration with TypeScript syntax into compatible ES6.
|
||||
*
|
||||
@@ -518,32 +548,26 @@ namespace ts {
|
||||
*/
|
||||
function visitClassDeclaration(node: ClassDeclaration): VisitResult<Statement> {
|
||||
const staticProperties = getInitializedProperties(node, /*isStatic*/ true);
|
||||
const hasExtendsClause = getClassExtendsHeritageClauseElement(node) !== undefined;
|
||||
const isDecoratedClass = shouldEmitDecorateCallForClass(node);
|
||||
const facts = getClassFacts(node, staticProperties);
|
||||
|
||||
// emit name if
|
||||
// - node has a name
|
||||
// - node has static initializers
|
||||
// - node has a member that is decorated
|
||||
//
|
||||
let name = node.name;
|
||||
if (!name && (staticProperties.length > 0 || childIsDecorated(node))) {
|
||||
name = getGeneratedNameForNode(node);
|
||||
if (facts & ClassFacts.UseImmediatelyInvokedFunctionExpression) {
|
||||
context.startLexicalEnvironment();
|
||||
}
|
||||
|
||||
const classStatement = isDecoratedClass
|
||||
? createClassDeclarationHeadWithDecorators(node, name, hasExtendsClause)
|
||||
: createClassDeclarationHeadWithoutDecorators(node, name, hasExtendsClause, staticProperties.length > 0);
|
||||
const name = node.name || (facts & ClassFacts.NeedsName ? getGeneratedNameForNode(node) : undefined);
|
||||
const classStatement = facts & ClassFacts.HasConstructorDecorators
|
||||
? createClassDeclarationHeadWithDecorators(node, name, facts)
|
||||
: createClassDeclarationHeadWithoutDecorators(node, name, facts);
|
||||
|
||||
const statements: Statement[] = [classStatement];
|
||||
let statements: Statement[] = [classStatement];
|
||||
|
||||
// Emit static property assignment. Because classDeclaration is lexically evaluated,
|
||||
// it is safe to emit static property assignment after classDeclaration
|
||||
// From ES6 specification:
|
||||
// HasLexicalDeclaration (N) : Determines if the argument identifier has a binding in this environment record that was created using
|
||||
// a lexical declaration such as a LexicalDeclaration or a ClassDeclaration.
|
||||
if (staticProperties.length) {
|
||||
addInitializedPropertyStatements(statements, staticProperties, getLocalName(node));
|
||||
if (facts & ClassFacts.HasStaticInitializedProperties) {
|
||||
addInitializedPropertyStatements(statements, staticProperties, facts & ClassFacts.UseImmediatelyInvokedFunctionExpression ? getInternalName(node) : getLocalName(node));
|
||||
}
|
||||
|
||||
// Write any decorators of the node.
|
||||
@@ -551,17 +575,63 @@ namespace ts {
|
||||
addClassElementDecorationStatements(statements, node, /*isStatic*/ true);
|
||||
addConstructorDecorationStatement(statements, node);
|
||||
|
||||
if (facts & ClassFacts.UseImmediatelyInvokedFunctionExpression) {
|
||||
// When we emit a TypeScript class down to ES5, we must wrap it in an IIFE so that the
|
||||
// 'es2015' transformer can properly nest static initializers and decorators. The result
|
||||
// looks something like:
|
||||
//
|
||||
// var C = function () {
|
||||
// class C {
|
||||
// }
|
||||
// C.static_prop = 1;
|
||||
// return C;
|
||||
// }();
|
||||
//
|
||||
const closingBraceLocation = createTokenRange(skipTrivia(currentSourceFile.text, node.members.end), SyntaxKind.CloseBraceToken);
|
||||
const localName = getInternalName(node);
|
||||
|
||||
// The following partially-emitted expression exists purely to align our sourcemap
|
||||
// emit with the original emitter.
|
||||
const outer = createPartiallyEmittedExpression(localName);
|
||||
outer.end = closingBraceLocation.end;
|
||||
setEmitFlags(outer, EmitFlags.NoComments);
|
||||
|
||||
const statement = createReturn(outer);
|
||||
statement.pos = closingBraceLocation.pos;
|
||||
setEmitFlags(statement, EmitFlags.NoComments | EmitFlags.NoTokenSourceMaps);
|
||||
statements.push(statement);
|
||||
|
||||
addRange(statements, context.endLexicalEnvironment());
|
||||
|
||||
const varStatement = createVariableStatement(
|
||||
/*modifiers*/ undefined,
|
||||
createVariableDeclarationList([
|
||||
createVariableDeclaration(
|
||||
getLocalName(node, /*allowComments*/ false, /*allowSourceMaps*/ false),
|
||||
/*type*/ undefined,
|
||||
createImmediatelyInvokedFunctionExpression(statements)
|
||||
)
|
||||
])
|
||||
);
|
||||
|
||||
setOriginalNode(varStatement, node);
|
||||
setCommentRange(varStatement, node);
|
||||
setSourceMapRange(varStatement, moveRangePastDecorators(node));
|
||||
startOnNewLine(varStatement);
|
||||
statements = [varStatement];
|
||||
}
|
||||
|
||||
// If the class is exported as part of a TypeScript namespace, emit the namespace export.
|
||||
// Otherwise, if the class was exported at the top level and was decorated, emit an export
|
||||
// declaration or export default for the class.
|
||||
if (isNamespaceExport(node)) {
|
||||
if (facts & ClassFacts.IsExportOfNamespace) {
|
||||
addExportMemberAssignment(statements, node);
|
||||
}
|
||||
else if (isDecoratedClass) {
|
||||
if (isDefaultExternalModuleExport(node)) {
|
||||
else if (facts & ClassFacts.UseImmediatelyInvokedFunctionExpression || facts & ClassFacts.HasConstructorDecorators) {
|
||||
if (facts & ClassFacts.IsDefaultExternalExport) {
|
||||
statements.push(createExportDefault(getLocalName(node, /*allowComments*/ false, /*allowSourceMaps*/ true)));
|
||||
}
|
||||
else if (isNamedExternalModuleExport(node)) {
|
||||
else if (facts & ClassFacts.IsNamedExternalExport) {
|
||||
statements.push(createExternalModuleExport(getLocalName(node, /*allowComments*/ false, /*allowSourceMaps*/ true)));
|
||||
}
|
||||
}
|
||||
@@ -580,26 +650,31 @@ namespace ts {
|
||||
*
|
||||
* @param node A ClassDeclaration node.
|
||||
* @param name The name of the class.
|
||||
* @param hasExtendsClause A value indicating whether the class has an extends clause.
|
||||
* @param hasStaticProperties A value indicating whether the class has static properties.
|
||||
* @param facts Precomputed facts about the class.
|
||||
*/
|
||||
function createClassDeclarationHeadWithoutDecorators(node: ClassDeclaration, name: Identifier, hasExtendsClause: boolean, hasStaticProperties: boolean) {
|
||||
function createClassDeclarationHeadWithoutDecorators(node: ClassDeclaration, name: Identifier, facts: ClassFacts) {
|
||||
// ${modifiers} class ${name} ${heritageClauses} {
|
||||
// ${members}
|
||||
// }
|
||||
|
||||
// we do not emit modifiers on the declaration if we are emitting an IIFE
|
||||
const modifiers = !(facts & ClassFacts.UseImmediatelyInvokedFunctionExpression)
|
||||
? visitNodes(node.modifiers, modifierVisitor, isModifier)
|
||||
: undefined;
|
||||
|
||||
const classDeclaration = createClassDeclaration(
|
||||
/*decorators*/ undefined,
|
||||
visitNodes(node.modifiers, modifierVisitor, isModifier),
|
||||
modifiers,
|
||||
name,
|
||||
/*typeParameters*/ undefined,
|
||||
visitNodes(node.heritageClauses, visitor, isHeritageClause),
|
||||
transformClassMembers(node, hasExtendsClause)
|
||||
transformClassMembers(node, (facts & ClassFacts.HasExtendsClause) !== 0)
|
||||
);
|
||||
|
||||
// To better align with the old emitter, we should not emit a trailing source map
|
||||
// entry if the class has static properties.
|
||||
let emitFlags = getEmitFlags(node);
|
||||
if (hasStaticProperties) {
|
||||
if (facts & ClassFacts.HasStaticInitializedProperties) {
|
||||
emitFlags |= EmitFlags.NoTrailingSourceMap;
|
||||
}
|
||||
|
||||
@@ -612,13 +687,8 @@ namespace ts {
|
||||
/**
|
||||
* Transforms a decorated class declaration and appends the resulting statements. If
|
||||
* the class requires an alias to avoid issues with double-binding, the alias is returned.
|
||||
*
|
||||
* @param statements A statement list to which to add the declaration.
|
||||
* @param node A ClassDeclaration node.
|
||||
* @param name The name of the class.
|
||||
* @param hasExtendsClause A value indicating whether the class has an extends clause.
|
||||
*/
|
||||
function createClassDeclarationHeadWithDecorators(node: ClassDeclaration, name: Identifier, hasExtendsClause: boolean) {
|
||||
function createClassDeclarationHeadWithDecorators(node: ClassDeclaration, name: Identifier, facts: ClassFacts) {
|
||||
// When we emit an ES6 class that has a class decorator, we must tailor the
|
||||
// emit to certain specific cases.
|
||||
//
|
||||
@@ -713,7 +783,7 @@ namespace ts {
|
||||
// ${members}
|
||||
// }
|
||||
const heritageClauses = visitNodes(node.heritageClauses, visitor, isHeritageClause);
|
||||
const members = transformClassMembers(node, hasExtendsClause);
|
||||
const members = transformClassMembers(node, (facts & ClassFacts.HasExtendsClause) !== 0);
|
||||
const classExpression = createClassExpression(/*modifiers*/ undefined, name, /*typeParameters*/ undefined, heritageClauses, members);
|
||||
setOriginalNode(classExpression, node);
|
||||
setTextRange(classExpression, location);
|
||||
@@ -2163,7 +2233,7 @@ namespace ts {
|
||||
/*type*/ undefined,
|
||||
visitFunctionBody(node.body, visitor, context) || createBlock([])
|
||||
);
|
||||
if (isNamespaceExport(node)) {
|
||||
if (isExportOfNamespace(node)) {
|
||||
const statements: Statement[] = [updated];
|
||||
addExportMemberAssignment(statements, node);
|
||||
return statements;
|
||||
@@ -2256,7 +2326,7 @@ namespace ts {
|
||||
* - The node is exported from a TypeScript namespace.
|
||||
*/
|
||||
function visitVariableStatement(node: VariableStatement): Statement {
|
||||
if (isNamespaceExport(node)) {
|
||||
if (isExportOfNamespace(node)) {
|
||||
const variables = getInitializedVariables(node.declarationList);
|
||||
if (variables.length === 0) {
|
||||
// elide statement if there are no initialized variables.
|
||||
@@ -2560,7 +2630,7 @@ namespace ts {
|
||||
* or `exports.x`).
|
||||
*/
|
||||
function hasNamespaceQualifiedExportName(node: Node) {
|
||||
return isNamespaceExport(node)
|
||||
return isExportOfNamespace(node)
|
||||
|| (isExternalModuleExport(node)
|
||||
&& moduleKind !== ModuleKind.ES2015
|
||||
&& moduleKind !== ModuleKind.System);
|
||||
@@ -3002,7 +3072,7 @@ namespace ts {
|
||||
const moduleReference = createExpressionFromEntityName(<EntityName>node.moduleReference);
|
||||
setEmitFlags(moduleReference, EmitFlags.NoComments | EmitFlags.NoNestedComments);
|
||||
|
||||
if (isNamedExternalModuleExport(node) || !isNamespaceExport(node)) {
|
||||
if (isNamedExternalModuleExport(node) || !isExportOfNamespace(node)) {
|
||||
// export var ${name} = ${moduleReference};
|
||||
// var ${name} = ${moduleReference};
|
||||
return setOriginalNode(
|
||||
@@ -3043,7 +3113,7 @@ namespace ts {
|
||||
*
|
||||
* @param node The node to test.
|
||||
*/
|
||||
function isNamespaceExport(node: Node) {
|
||||
function isExportOfNamespace(node: Node) {
|
||||
return currentNamespace !== undefined && hasModifier(node, ModifierFlags.Export);
|
||||
}
|
||||
|
||||
|
||||
@@ -3087,6 +3087,7 @@ namespace ts {
|
||||
export interface Type {
|
||||
flags: TypeFlags; // Flags
|
||||
/* @internal */ id: number; // Unique ID
|
||||
/* @internal */ checker: TypeChecker;
|
||||
symbol?: Symbol; // Symbol associated with type (if any)
|
||||
pattern?: DestructuringPattern; // Destructuring pattern represented by type (if any)
|
||||
aliasSymbol?: Symbol; // Alias associated with type
|
||||
@@ -3996,34 +3997,34 @@ namespace ts {
|
||||
}
|
||||
|
||||
export const enum EmitFlags {
|
||||
SingleLine = 1 << 0, // The contents of this node should be emitted on a single line.
|
||||
AdviseOnEmitNode = 1 << 1, // The printer should invoke the onEmitNode callback when printing this node.
|
||||
NoSubstitution = 1 << 2, // Disables further substitution of an expression.
|
||||
CapturesThis = 1 << 3, // The function captures a lexical `this`
|
||||
NoLeadingSourceMap = 1 << 4, // Do not emit a leading source map location for this node.
|
||||
NoTrailingSourceMap = 1 << 5, // Do not emit a trailing source map location for this node.
|
||||
SingleLine = 1 << 0, // The contents of this node should be emitted on a single line.
|
||||
AdviseOnEmitNode = 1 << 1, // The printer should invoke the onEmitNode callback when printing this node.
|
||||
NoSubstitution = 1 << 2, // Disables further substitution of an expression.
|
||||
CapturesThis = 1 << 3, // The function captures a lexical `this`
|
||||
NoLeadingSourceMap = 1 << 4, // Do not emit a leading source map location for this node.
|
||||
NoTrailingSourceMap = 1 << 5, // Do not emit a trailing source map location for this node.
|
||||
NoSourceMap = NoLeadingSourceMap | NoTrailingSourceMap, // Do not emit a source map location for this node.
|
||||
NoNestedSourceMaps = 1 << 6, // Do not emit source map locations for children of this node.
|
||||
NoTokenLeadingSourceMaps = 1 << 7, // Do not emit leading source map location for token nodes.
|
||||
NoTokenTrailingSourceMaps = 1 << 8, // Do not emit trailing source map location for token nodes.
|
||||
NoNestedSourceMaps = 1 << 6, // Do not emit source map locations for children of this node.
|
||||
NoTokenLeadingSourceMaps = 1 << 7, // Do not emit leading source map location for token nodes.
|
||||
NoTokenTrailingSourceMaps = 1 << 8, // Do not emit trailing source map location for token nodes.
|
||||
NoTokenSourceMaps = NoTokenLeadingSourceMaps | NoTokenTrailingSourceMaps, // Do not emit source map locations for tokens of this node.
|
||||
NoLeadingComments = 1 << 9, // Do not emit leading comments for this node.
|
||||
NoTrailingComments = 1 << 10, // Do not emit trailing comments for this node.
|
||||
NoLeadingComments = 1 << 9, // Do not emit leading comments for this node.
|
||||
NoTrailingComments = 1 << 10, // Do not emit trailing comments for this node.
|
||||
NoComments = NoLeadingComments | NoTrailingComments, // Do not emit comments for this node.
|
||||
NoNestedComments = 1 << 11,
|
||||
HelperName = 1 << 12,
|
||||
ExportName = 1 << 13, // Ensure an export prefix is added for an identifier that points to an exported declaration with a local name (see SymbolFlags.ExportHasLocal).
|
||||
LocalName = 1 << 14, // Ensure an export prefix is not added for an identifier that points to an exported declaration.
|
||||
InternalName = 1 << 15, // The name is internal to an ES5 class body function.
|
||||
Indented = 1 << 16, // Adds an explicit extra indentation level for class and function bodies when printing (used to match old emitter).
|
||||
NoIndentation = 1 << 17, // Do not indent the node.
|
||||
ExportName = 1 << 13, // Ensure an export prefix is added for an identifier that points to an exported declaration with a local name (see SymbolFlags.ExportHasLocal).
|
||||
LocalName = 1 << 14, // Ensure an export prefix is not added for an identifier that points to an exported declaration.
|
||||
InternalName = 1 << 15, // The name is internal to an ES5 class body function.
|
||||
Indented = 1 << 16, // Adds an explicit extra indentation level for class and function bodies when printing (used to match old emitter).
|
||||
NoIndentation = 1 << 17, // Do not indent the node.
|
||||
AsyncFunctionBody = 1 << 18,
|
||||
ReuseTempVariableScope = 1 << 19, // Reuse the existing temp variable scope during emit.
|
||||
CustomPrologue = 1 << 20, // Treat the statement as if it were a prologue directive (NOTE: Prologue directives are *not* transformed).
|
||||
NoHoisting = 1 << 21, // Do not hoist this declaration in --module system
|
||||
HasEndOfDeclarationMarker = 1 << 22, // Declaration has an associated NotEmittedStatement to mark the end of the declaration
|
||||
Iterator = 1 << 23, // The expression to a `yield*` should be treated as an Iterator when down-leveling, not an Iterable.
|
||||
NoAsciiEscaping = 1 << 24, // When synthesizing nodes that lack an original node or textSourceNode, we want to write the text on the node with ASCII escaping substitutions.
|
||||
ReuseTempVariableScope = 1 << 19, // Reuse the existing temp variable scope during emit.
|
||||
CustomPrologue = 1 << 20, // Treat the statement as if it were a prologue directive (NOTE: Prologue directives are *not* transformed).
|
||||
NoHoisting = 1 << 21, // Do not hoist this declaration in --module system
|
||||
HasEndOfDeclarationMarker = 1 << 22, // Declaration has an associated NotEmittedStatement to mark the end of the declaration
|
||||
Iterator = 1 << 23, // The expression to a `yield*` should be treated as an Iterator when down-leveling, not an Iterable.
|
||||
NoAsciiEscaping = 1 << 24, // When synthesizing nodes that lack an original node or textSourceNode, we want to write the text on the node with ASCII escaping substitutions.
|
||||
}
|
||||
|
||||
export interface EmitHelper {
|
||||
|
||||
@@ -2935,6 +2935,14 @@ namespace ts {
|
||||
return node.modifierFlagsCache & ~ModifierFlags.HasComputedFlags;
|
||||
}
|
||||
|
||||
const flags = getModifierFlagsNoCache(node);
|
||||
node.modifierFlagsCache = flags | ModifierFlags.HasComputedFlags;
|
||||
return flags;
|
||||
}
|
||||
|
||||
/* @internal */
|
||||
export function getModifierFlagsNoCache(node: Node): ModifierFlags {
|
||||
|
||||
let flags = ModifierFlags.None;
|
||||
if (node.modifiers) {
|
||||
for (const modifier of node.modifiers) {
|
||||
@@ -2946,7 +2954,6 @@ namespace ts {
|
||||
flags |= ModifierFlags.Export;
|
||||
}
|
||||
|
||||
node.modifierFlagsCache = flags | ModifierFlags.HasComputedFlags;
|
||||
return flags;
|
||||
}
|
||||
|
||||
@@ -3235,27 +3242,76 @@ namespace ts {
|
||||
return false;
|
||||
}
|
||||
|
||||
const syntaxKindCache: string[] = [];
|
||||
|
||||
export function formatSyntaxKind(kind: SyntaxKind): string {
|
||||
const syntaxKindEnum = (<any>ts).SyntaxKind;
|
||||
if (syntaxKindEnum) {
|
||||
const cached = syntaxKindCache[kind];
|
||||
if (cached !== undefined) {
|
||||
return cached;
|
||||
}
|
||||
|
||||
for (const name in syntaxKindEnum) {
|
||||
if (syntaxKindEnum[name] === kind) {
|
||||
const result = `${kind} (${name})`;
|
||||
syntaxKindCache[kind] = result;
|
||||
return result;
|
||||
/**
|
||||
* Formats an enum value as a string for debugging and debug assertions.
|
||||
*/
|
||||
function formatEnum(value = 0, enumObject: any, isFlags?: boolean) {
|
||||
const members = getEnumMembers(enumObject);
|
||||
if (value === 0) {
|
||||
return members.length > 0 && members[0][0] === 0 ? members[0][1] : "0";
|
||||
}
|
||||
if (isFlags) {
|
||||
let result = "";
|
||||
let remainingFlags = value;
|
||||
for (let i = members.length - 1; i >= 0 && remainingFlags !== 0; i--) {
|
||||
const [enumValue, enumName] = members[i];
|
||||
if (enumValue !== 0 && (remainingFlags & enumValue) === enumValue) {
|
||||
remainingFlags &= ~enumValue;
|
||||
result = `${enumName}${result ? ", " : ""}${result}`;
|
||||
}
|
||||
}
|
||||
if (remainingFlags === 0) {
|
||||
return result;
|
||||
}
|
||||
}
|
||||
else {
|
||||
return kind.toString();
|
||||
for (const [enumValue, enumName] of members) {
|
||||
if (enumValue === value) {
|
||||
return enumName;
|
||||
}
|
||||
}
|
||||
}
|
||||
return value.toString();
|
||||
}
|
||||
|
||||
function getEnumMembers(enumObject: any) {
|
||||
const result: [number, string][] = [];
|
||||
for (const name in enumObject) {
|
||||
const value = enumObject[name];
|
||||
if (typeof value === "number") {
|
||||
result.push([value, name]);
|
||||
}
|
||||
}
|
||||
|
||||
return stableSort(result, (x, y) => compareValues(x[0], y[0]));
|
||||
}
|
||||
|
||||
export function formatSyntaxKind(kind: SyntaxKind): string {
|
||||
return formatEnum(kind, (<any>ts).SyntaxKind, /*isFlags*/ false);
|
||||
}
|
||||
|
||||
export function formatModifierFlags(flags: ModifierFlags): string {
|
||||
return formatEnum(flags, (<any>ts).ModifierFlags, /*isFlags*/ true);
|
||||
}
|
||||
|
||||
export function formatTransformFlags(flags: TransformFlags): string {
|
||||
return formatEnum(flags, (<any>ts).TransformFlags, /*isFlags*/ true);
|
||||
}
|
||||
|
||||
export function formatEmitFlags(flags: EmitFlags): string {
|
||||
return formatEnum(flags, (<any>ts).EmitFlags, /*isFlags*/ true);
|
||||
}
|
||||
|
||||
export function formatSymbolFlags(flags: SymbolFlags): string {
|
||||
return formatEnum(flags, (<any>ts).SymbolFlags, /*isFlags*/ true);
|
||||
}
|
||||
|
||||
export function formatTypeFlags(flags: TypeFlags): string {
|
||||
return formatEnum(flags, (<any>ts).TypeFlags, /*isFlags*/ true);
|
||||
}
|
||||
|
||||
export function formatObjectFlags(flags: ObjectFlags): string {
|
||||
return formatEnum(flags, (<any>ts).ObjectFlags, /*isFlags*/ true);
|
||||
}
|
||||
|
||||
export function getRangePos(range: TextRange | undefined) {
|
||||
|
||||
@@ -1517,57 +1517,80 @@ namespace ts {
|
||||
}
|
||||
|
||||
export namespace Debug {
|
||||
if (isDebugging) {
|
||||
// Add additional properties in debug mode to assist with debugging.
|
||||
Object.defineProperties(objectAllocator.getSymbolConstructor().prototype, {
|
||||
"__debugFlags": { get(this: Symbol) { return formatSymbolFlags(this.flags); } }
|
||||
});
|
||||
|
||||
Object.defineProperties(objectAllocator.getTypeConstructor().prototype, {
|
||||
"__debugFlags": { get(this: Type) { return formatTypeFlags(this.flags); } },
|
||||
"__debugObjectFlags": { get(this: Type) { return this.flags & TypeFlags.Object ? formatObjectFlags((<ObjectType>this).objectFlags) : ""; } },
|
||||
"__debugTypeToString": { value(this: Type) { return this.checker.typeToString(this); } },
|
||||
});
|
||||
|
||||
for (const ctor of [objectAllocator.getNodeConstructor(), objectAllocator.getIdentifierConstructor(), objectAllocator.getTokenConstructor(), objectAllocator.getSourceFileConstructor()]) {
|
||||
if (!ctor.prototype.hasOwnProperty("__debugKind")) {
|
||||
Object.defineProperties(ctor.prototype, {
|
||||
"__debugKind": { get(this: Node) { return formatSyntaxKind(this.kind); } },
|
||||
"__debugModifierFlags": { get(this: Node) { return formatModifierFlags(getModifierFlagsNoCache(this)); } },
|
||||
"__debugTransformFlags": { get(this: Node) { return formatTransformFlags(this.transformFlags); } },
|
||||
"__debugEmitFlags": { get(this: Node) { return formatEmitFlags(getEmitFlags(this)); } },
|
||||
"__debugGetText": { value(this: Node, includeTrivia?: boolean) {
|
||||
if (nodeIsSynthesized(this)) return "";
|
||||
const parseNode = getParseTreeNode(this);
|
||||
const sourceFile = parseNode && getSourceFileOfNode(parseNode);
|
||||
return sourceFile ? getSourceTextOfNodeFromSourceFile(sourceFile, parseNode, includeTrivia) : "";
|
||||
} }
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export const failBadSyntaxKind = shouldAssert(AssertionLevel.Normal)
|
||||
? (node: Node, message?: string) => assert(false, message || "Unexpected node.", () => `Node ${formatSyntaxKind(node.kind)} was unexpected.`)
|
||||
? (node: Node, message?: string): void => fail(
|
||||
`${message || "Unexpected node."}\r\nNode ${formatSyntaxKind(node.kind)} was unexpected.`,
|
||||
failBadSyntaxKind)
|
||||
: noop;
|
||||
|
||||
export const assertEachNode = shouldAssert(AssertionLevel.Normal)
|
||||
? (nodes: Node[], test: (node: Node) => boolean, message?: string) => assert(
|
||||
test === undefined || every(nodes, test),
|
||||
message || "Unexpected node.",
|
||||
() => `Node array did not pass test '${getFunctionName(test)}'.`)
|
||||
? (nodes: Node[], test: (node: Node) => boolean, message?: string): void => assert(
|
||||
test === undefined || every(nodes, test),
|
||||
message || "Unexpected node.",
|
||||
() => `Node array did not pass test '${getFunctionName(test)}'.`,
|
||||
assertEachNode)
|
||||
: noop;
|
||||
|
||||
export const assertNode = shouldAssert(AssertionLevel.Normal)
|
||||
? (node: Node, test: (node: Node) => boolean, message?: string) => assert(
|
||||
test === undefined || test(node),
|
||||
message || "Unexpected node.",
|
||||
() => `Node ${formatSyntaxKind(node.kind)} did not pass test '${getFunctionName(test)}'.`)
|
||||
? (node: Node, test: (node: Node) => boolean, message?: string): void => assert(
|
||||
test === undefined || test(node),
|
||||
message || "Unexpected node.",
|
||||
() => `Node ${formatSyntaxKind(node.kind)} did not pass test '${getFunctionName(test)}'.`,
|
||||
assertNode)
|
||||
: noop;
|
||||
|
||||
export const assertOptionalNode = shouldAssert(AssertionLevel.Normal)
|
||||
? (node: Node, test: (node: Node) => boolean, message?: string) => assert(
|
||||
test === undefined || node === undefined || test(node),
|
||||
message || "Unexpected node.",
|
||||
() => `Node ${formatSyntaxKind(node.kind)} did not pass test '${getFunctionName(test)}'.`)
|
||||
? (node: Node, test: (node: Node) => boolean, message?: string): void => assert(
|
||||
test === undefined || node === undefined || test(node),
|
||||
message || "Unexpected node.",
|
||||
() => `Node ${formatSyntaxKind(node.kind)} did not pass test '${getFunctionName(test)}'.`,
|
||||
assertOptionalNode)
|
||||
: noop;
|
||||
|
||||
export const assertOptionalToken = shouldAssert(AssertionLevel.Normal)
|
||||
? (node: Node, kind: SyntaxKind, message?: string) => assert(
|
||||
kind === undefined || node === undefined || node.kind === kind,
|
||||
message || "Unexpected node.",
|
||||
() => `Node ${formatSyntaxKind(node.kind)} was not a '${formatSyntaxKind(kind)}' token.`)
|
||||
? (node: Node, kind: SyntaxKind, message?: string): void => assert(
|
||||
kind === undefined || node === undefined || node.kind === kind,
|
||||
message || "Unexpected node.",
|
||||
() => `Node ${formatSyntaxKind(node.kind)} was not a '${formatSyntaxKind(kind)}' token.`,
|
||||
assertOptionalToken)
|
||||
: noop;
|
||||
|
||||
export const assertMissingNode = shouldAssert(AssertionLevel.Normal)
|
||||
? (node: Node, message?: string) => assert(
|
||||
node === undefined,
|
||||
message || "Unexpected node.",
|
||||
() => `Node ${formatSyntaxKind(node.kind)} was unexpected'.`)
|
||||
? (node: Node, message?: string): void => assert(
|
||||
node === undefined,
|
||||
message || "Unexpected node.",
|
||||
() => `Node ${formatSyntaxKind(node.kind)} was unexpected'.`,
|
||||
assertMissingNode)
|
||||
: noop;
|
||||
|
||||
function getFunctionName(func: Function) {
|
||||
if (typeof func !== "function") {
|
||||
return "";
|
||||
}
|
||||
else if (func.hasOwnProperty("name")) {
|
||||
return (<any>func).name;
|
||||
}
|
||||
else {
|
||||
const text = Function.prototype.toString.call(func);
|
||||
const match = /^function\s+([\w\$]+)\s*\(/.exec(text);
|
||||
return match ? match[1] : "";
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user