From 2e574658b0ad04f1f3e5aaebd4575d99b27e9dfc Mon Sep 17 00:00:00 2001 From: Ron Buckton Date: Tue, 21 May 2019 16:31:53 -0700 Subject: [PATCH] Fix compiler crash with object rest in catch binding --- src/compiler/binder.ts | 2 +- src/compiler/checker.ts | 11 +- src/compiler/core.ts | 2 +- src/compiler/debug.ts | 180 ++++++++++++++++++ src/compiler/transformers/es2018.ts | 24 +++ src/compiler/tsconfig.json | 1 + src/compiler/utilities.ts | 97 +--------- src/compiler/visitor.ts | 96 ---------- .../codefixes/addNameToNamelessParameter.ts | 4 +- src/services/findAllReferences.ts | 2 +- src/services/importTracker.ts | 4 +- src/services/services.ts | 2 +- src/services/signatureHelp.ts | 2 +- src/testRunner/unittests/factory.ts | 2 +- .../baselines/reference/objectRestCatchES5.js | 21 ++ .../reference/objectRestCatchES5.symbols | 9 + .../reference/objectRestCatchES5.types | 11 ++ .../types/rest/objectRestCatchES5.ts | 2 + 18 files changed, 267 insertions(+), 205 deletions(-) create mode 100644 src/compiler/debug.ts create mode 100644 tests/baselines/reference/objectRestCatchES5.js create mode 100644 tests/baselines/reference/objectRestCatchES5.symbols create mode 100644 tests/baselines/reference/objectRestCatchES5.types create mode 100644 tests/cases/conformance/types/rest/objectRestCatchES5.ts diff --git a/src/compiler/binder.ts b/src/compiler/binder.ts index cb6f67d66b1df..4540a4409d65b 100644 --- a/src/compiler/binder.ts +++ b/src/compiler/binder.ts @@ -2515,7 +2515,7 @@ namespace ts { break; default: - Debug.fail(Debug.showSyntaxKind(thisContainer)); + Debug.failBadSyntaxKind(thisContainer); } } diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index e9dddff64af39..33f6f821740b1 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -5695,7 +5695,7 @@ namespace ts { type = getTypeOfEnumMember(symbol); } else { - return Debug.fail("Unhandled declaration kind! " + Debug.showSyntaxKind(declaration) + " for " + Debug.showSymbol(symbol)); + return Debug.fail("Unhandled declaration kind! " + Debug.formatSyntaxKind(declaration.kind) + " for " + Debug.formatSymbol(symbol)); } if (!popTypeResolution()) { @@ -25536,7 +25536,7 @@ namespace ts { case SyntaxKind.ImportSpecifier: // https://github.com/Microsoft/TypeScript/pull/7591 return DeclarationSpaces.ExportValue; default: - return Debug.fail(Debug.showSyntaxKind(d)); + return Debug.failBadSyntaxKind(d); } } } @@ -30162,12 +30162,17 @@ namespace ts { return undefined; } + function isSymbolOfDestructuredElementOfCatchBinding(symbol: Symbol) { + return isBindingElement(symbol.valueDeclaration) + && walkUpBindingElementsAndPatterns(symbol.valueDeclaration).parent.kind === SyntaxKind.CatchClause; + } + function isSymbolOfDeclarationWithCollidingName(symbol: Symbol): boolean { if (symbol.flags & SymbolFlags.BlockScoped && !isSourceFile(symbol.valueDeclaration)) { const links = getSymbolLinks(symbol); if (links.isDeclarationWithCollidingName === undefined) { const container = getEnclosingBlockScopeContainer(symbol.valueDeclaration); - if (isStatementWithLocals(container)) { + if (isStatementWithLocals(container) || isSymbolOfDestructuredElementOfCatchBinding(symbol)) { const nodeLinks = getNodeLinks(symbol.valueDeclaration); if (resolveName(container.parent, symbol.escapedName, SymbolFlags.Value, /*nameNotFoundMessage*/ undefined, /*nameArg*/ undefined, /*isUse*/ false)) { // redeclaration - always should be renamed diff --git a/src/compiler/core.ts b/src/compiler/core.ts index 2dfc7acf18826..b1d423cc7b229 100644 --- a/src/compiler/core.ts +++ b/src/compiler/core.ts @@ -1750,7 +1750,7 @@ namespace ts { } export function assertNever(member: never, message = "Illegal value:", stackCrawlMark?: AnyFunction): never { - const detail = typeof member === "object" && "kind" in member && "pos" in member ? "SyntaxKind: " + showSyntaxKind(member as Node) : JSON.stringify(member); + const detail = typeof member === "object" && "kind" in member && "pos" in member && formatSyntaxKind ? "SyntaxKind: " + formatSyntaxKind((member as Node).kind) : JSON.stringify(member); return fail(`${message} ${detail}`, stackCrawlMark || assertNever); } diff --git a/src/compiler/debug.ts b/src/compiler/debug.ts new file mode 100644 index 0000000000000..870f2e627b157 --- /dev/null +++ b/src/compiler/debug.ts @@ -0,0 +1,180 @@ +/* @internal */ +namespace ts { + export namespace Debug { + export function formatSymbol(symbol: Symbol): string { + return `{ name: ${unescapeLeadingUnderscores(symbol.escapedName)}; flags: ${formatSymbolFlags(symbol.flags)}; declarations: ${map(symbol.declarations, node => formatSyntaxKind(node.kind))} }`; + } + + /** + * Formats an enum value as a string for debugging and debug assertions. + */ + export 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 { + 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<[number, string]>(result, (x, y) => compareValues(x[0], y[0])); + } + + export function formatSyntaxKind(kind: SyntaxKind | undefined): string { + return formatEnum(kind, (ts).SyntaxKind, /*isFlags*/ false); + } + + export function formatNodeFlags(flags: NodeFlags | undefined): string { + return formatEnum(flags, (ts).NodeFlags, /*isFlags*/ true); + } + + export function formatModifierFlags(flags: ModifierFlags | undefined): string { + return formatEnum(flags, (ts).ModifierFlags, /*isFlags*/ true); + } + + export function formatTransformFlags(flags: TransformFlags | undefined): string { + return formatEnum(flags, (ts).TransformFlags, /*isFlags*/ true); + } + + export function formatEmitFlags(flags: EmitFlags | undefined): string { + return formatEnum(flags, (ts).EmitFlags, /*isFlags*/ true); + } + + export function formatSymbolFlags(flags: SymbolFlags | undefined): string { + return formatEnum(flags, (ts).SymbolFlags, /*isFlags*/ true); + } + + export function formatTypeFlags(flags: TypeFlags | undefined): string { + return formatEnum(flags, (ts).TypeFlags, /*isFlags*/ true); + } + + export function formatObjectFlags(flags: ObjectFlags | undefined): string { + return formatEnum(flags, (ts).ObjectFlags, /*isFlags*/ true); + } + + export function failBadSyntaxKind(node: Node, message?: string): never { + return fail( + `${message || "Unexpected node."}\r\nNode ${formatSyntaxKind(node.kind)} was unexpected.`, + failBadSyntaxKind); + } + + export const assertEachNode = shouldAssert(AssertionLevel.Normal) + ? (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 | undefined, test: ((node: Node | undefined) => boolean) | undefined, 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): 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): 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): void => assert( + node === undefined, + message || "Unexpected node.", + () => `Node ${formatSyntaxKind(node.kind)} was unexpected'.`, + assertMissingNode) + : noop; + + let isDebugInfoEnabled = false; + + /** + * Injects debug information into frequently used types. + */ + export function enableDebugInfo() { + if (isDebugInfoEnabled) return; + + // 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((this).objectFlags) : ""; } }, + __debugTypeToString: { value(this: Type) { return this.checker.typeToString(this); } }, + }); + + const nodeConstructors = [ + objectAllocator.getNodeConstructor(), + objectAllocator.getIdentifierConstructor(), + objectAllocator.getTokenConstructor(), + objectAllocator.getSourceFileConstructor() + ]; + + for (const ctor of nodeConstructors) { + if (!ctor.prototype.hasOwnProperty("__debugKind")) { + Object.defineProperties(ctor.prototype, { + __debugKind: { get(this: Node) { return formatSyntaxKind(this.kind); } }, + __debugNodeFlags: { get(this: Node) { return formatNodeFlags(this.flags); } }, + __debugModifierFlags: { get(this: Node) { return formatModifierFlags(getModifierFlagsNoCache(this)); } }, + __debugTransformFlags: { get(this: Node) { return formatTransformFlags(this.transformFlags); } }, + __debugIsParseTreeNode: { get(this: Node) { return isParseTreeNode(this); } }, + __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) : ""; + } + } + }); + } + } + + isDebugInfoEnabled = true; + } + } +} \ No newline at end of file diff --git a/src/compiler/transformers/es2018.ts b/src/compiler/transformers/es2018.ts index 504870b4ff784..89c43dcf599cf 100644 --- a/src/compiler/transformers/es2018.ts +++ b/src/compiler/transformers/es2018.ts @@ -77,6 +77,8 @@ namespace ts { return visitObjectLiteralExpression(node as ObjectLiteralExpression); case SyntaxKind.BinaryExpression: return visitBinaryExpression(node as BinaryExpression, noDestructuringValue); + case SyntaxKind.CatchClause: + return visitCatchClause(node as CatchClause); case SyntaxKind.VariableDeclaration: return visitVariableDeclaration(node as VariableDeclaration); case SyntaxKind.ForOfStatement: @@ -272,6 +274,28 @@ namespace ts { return visitEachChild(node, visitor, context); } + function visitCatchClause(node: CatchClause) { + if (node.variableDeclaration && + isBindingPattern(node.variableDeclaration.name) && + node.variableDeclaration.name.transformFlags & TransformFlags.ContainsObjectRestOrSpread) { + const name = getGeneratedNameForNode(node.variableDeclaration.name); + const updatedDecl = updateVariableDeclaration(node.variableDeclaration, node.variableDeclaration.name, /*type*/ undefined, name); + const visitedBindings = flattenDestructuringBinding(updatedDecl, visitor, context, FlattenLevel.ObjectRest); + let block = visitNode(node.block, visitor, isBlock); + if (some(visitedBindings)) { + block = updateBlock(block, [ + createVariableStatement(/*modifiers*/ undefined, visitedBindings), + ...block.statements, + ]); + } + return updateCatchClause( + node, + updateVariableDeclaration(node.variableDeclaration, name, /*type*/ undefined, /*initializer*/ undefined), + block); + } + return visitEachChild(node, visitor, context); + } + /** * Visits a VariableDeclaration node with a binding pattern. * diff --git a/src/compiler/tsconfig.json b/src/compiler/tsconfig.json index 18934aa9bb1cd..28bd5266adecc 100644 --- a/src/compiler/tsconfig.json +++ b/src/compiler/tsconfig.json @@ -9,6 +9,7 @@ "files": [ "core.ts", "performance.ts", + "debug.ts", "semver.ts", "types.ts", diff --git a/src/compiler/utilities.ts b/src/compiler/utilities.ts index 45dc980296be7..402e2d60656eb 100644 --- a/src/compiler/utilities.ts +++ b/src/compiler/utilities.ts @@ -2073,7 +2073,7 @@ namespace ts { } export function importFromModuleSpecifier(node: StringLiteralLike): AnyValidImportOrReExport { - return tryGetImportFromModuleSpecifier(node) || Debug.fail(Debug.showSyntaxKind(node.parent)); + return tryGetImportFromModuleSpecifier(node) || Debug.failBadSyntaxKind(node.parent); } export function tryGetImportFromModuleSpecifier(node: StringLiteralLike): AnyValidImportOrReExport | undefined { @@ -4186,78 +4186,6 @@ namespace ts { return getNewLine ? getNewLine() : sys ? sys.newLine : carriageReturnLineFeed; } - /** - * 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 { - 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<[number, string]>(result, (x, y) => compareValues(x[0], y[0])); - } - - export function formatSyntaxKind(kind: SyntaxKind | undefined): string { - return formatEnum(kind, (ts).SyntaxKind, /*isFlags*/ false); - } - - export function formatModifierFlags(flags: ModifierFlags | undefined): string { - return formatEnum(flags, (ts).ModifierFlags, /*isFlags*/ true); - } - - export function formatTransformFlags(flags: TransformFlags | undefined): string { - return formatEnum(flags, (ts).TransformFlags, /*isFlags*/ true); - } - - export function formatEmitFlags(flags: EmitFlags | undefined): string { - return formatEnum(flags, (ts).EmitFlags, /*isFlags*/ true); - } - - export function formatSymbolFlags(flags: SymbolFlags | undefined): string { - return formatEnum(flags, (ts).SymbolFlags, /*isFlags*/ true); - } - - export function formatTypeFlags(flags: TypeFlags | undefined): string { - return formatEnum(flags, (ts).TypeFlags, /*isFlags*/ true); - } - - export function formatObjectFlags(flags: ObjectFlags | undefined): string { - return formatEnum(flags, (ts).ObjectFlags, /*isFlags*/ true); - } - /** * Creates a new TextRange from the provided pos and end. * @@ -8420,29 +8348,6 @@ namespace ts { return pathext ? path.slice(0, path.length - pathext.length) + (startsWith(ext, ".") ? ext : "." + ext) : path; } - export namespace Debug { - export function showSymbol(symbol: Symbol): string { - const symbolFlags = (ts as any).SymbolFlags; - return `{ flags: ${symbolFlags ? showFlags(symbol.flags, symbolFlags) : symbol.flags}; declarations: ${map(symbol.declarations, showSyntaxKind)} }`; - } - - function showFlags(flags: number, flagsEnum: { [flag: number]: string }): string { - const out: string[] = []; - for (let pow = 0; pow <= 30; pow++) { - const n = 1 << pow; - if (flags & n) { - out.push(flagsEnum[n]); - } - } - return out.join("|"); - } - - export function showSyntaxKind(node: Node): string { - const syntaxKind = (ts as any).SyntaxKind; - return syntaxKind ? syntaxKind[node.kind] : node.kind.toString(); - } - } - export function tryParsePattern(pattern: string): Pattern | undefined { // This should be verified outside of here and a proper error thrown. Debug.assert(hasZeroOrOneAsteriskCharacter(pattern)); diff --git a/src/compiler/visitor.ts b/src/compiler/visitor.ts index 0cb3b2691057e..53ccd81f7a6a3 100644 --- a/src/compiler/visitor.ts +++ b/src/compiler/visitor.ts @@ -1558,100 +1558,4 @@ namespace ts { function aggregateTransformFlagsForChildNodes(transformFlags: TransformFlags, nodes: NodeArray): TransformFlags { return transformFlags | aggregateTransformFlagsForNodeArray(nodes); } - - export namespace Debug { - let isDebugInfoEnabled = false; - - export function failBadSyntaxKind(node: Node, message?: string): never { - return fail( - `${message || "Unexpected node."}\r\nNode ${formatSyntaxKind(node.kind)} was unexpected.`, - failBadSyntaxKind); - } - - export const assertEachNode = shouldAssert(AssertionLevel.Normal) - ? (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 | undefined, test: ((node: Node | undefined) => boolean) | undefined, 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): 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): 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): void => assert( - node === undefined, - message || "Unexpected node.", - () => `Node ${formatSyntaxKind(node.kind)} was unexpected'.`, - assertMissingNode) - : noop; - - /** - * Injects debug information into frequently used types. - */ - export function enableDebugInfo() { - if (isDebugInfoEnabled) return; - - // 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((this).objectFlags) : ""; } }, - __debugTypeToString: { value(this: Type) { return this.checker.typeToString(this); } }, - }); - - const nodeConstructors = [ - objectAllocator.getNodeConstructor(), - objectAllocator.getIdentifierConstructor(), - objectAllocator.getTokenConstructor(), - objectAllocator.getSourceFileConstructor() - ]; - - for (const ctor of nodeConstructors) { - 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) : ""; - } - } - }); - } - } - - isDebugInfoEnabled = true; - } - } } diff --git a/src/services/codefixes/addNameToNamelessParameter.ts b/src/services/codefixes/addNameToNamelessParameter.ts index f87816ab22864..3339f71900845 100644 --- a/src/services/codefixes/addNameToNamelessParameter.ts +++ b/src/services/codefixes/addNameToNamelessParameter.ts @@ -15,11 +15,11 @@ namespace ts.codefix { function makeChange(changeTracker: textChanges.ChangeTracker, sourceFile: SourceFile, pos: number) { const token = getTokenAtPosition(sourceFile, pos); if (!isIdentifier(token)) { - return Debug.fail("add-name-to-nameless-parameter operates on identifiers, but got a " + formatSyntaxKind(token.kind)); + return Debug.fail("add-name-to-nameless-parameter operates on identifiers, but got a " + Debug.formatSyntaxKind(token.kind)); } const param = token.parent; if (!isParameter(param)) { - return Debug.fail("Tried to add a parameter name to a non-parameter: " + formatSyntaxKind(token.kind)); + return Debug.fail("Tried to add a parameter name to a non-parameter: " + Debug.formatSyntaxKind(token.kind)); } const i = param.parent.parameters.indexOf(param); Debug.assert(!param.type, "Tried to add a parameter name to a parameter that already had one."); diff --git a/src/services/findAllReferences.ts b/src/services/findAllReferences.ts index 147fee53393c1..cf0046d2688fa 100644 --- a/src/services/findAllReferences.ts +++ b/src/services/findAllReferences.ts @@ -635,7 +635,7 @@ namespace ts.FindAllReferences.Core { // Ignore UMD module and global merge if (symbol.flags & SymbolFlags.Transient) return undefined; // Assertions for GH#21814. We should be handling SourceFile symbols in `getReferencedSymbolsForModule` instead of getting here. - Debug.fail(`Unexpected symbol at ${Debug.showSyntaxKind(node)}: ${Debug.showSymbol(symbol)}`); + Debug.fail(`Unexpected symbol at ${Debug.formatSyntaxKind(node.kind)}: ${Debug.formatSymbol(symbol)}`); } return isTypeLiteralNode(decl.parent) && isUnionTypeNode(decl.parent.parent) ? checker.getPropertyOfType(checker.getTypeFromTypeNode(decl.parent.parent), symbol.name) diff --git a/src/services/importTracker.ts b/src/services/importTracker.ts index e8512528af958..2734d05900070 100644 --- a/src/services/importTracker.ts +++ b/src/services/importTracker.ts @@ -133,7 +133,7 @@ namespace ts.FindAllReferences { break; default: - Debug.assertNever(direct, `Unexpected import kind: ${Debug.showSyntaxKind(direct)}`); + Debug.failBadSyntaxKind(direct, "Unexpected import kind."); } } } @@ -515,7 +515,7 @@ namespace ts.FindAllReferences { const sym = useLhsSymbol ? checker.getSymbolAtLocation(cast(node.left, isPropertyAccessExpression).name) : symbol; // Better detection for GH#20803 if (sym && !(checker.getMergedSymbol(sym.parent!).flags & SymbolFlags.Module)) { - Debug.fail(`Special property assignment kind does not have a module as its parent. Assignment is ${Debug.showSymbol(sym)}, parent is ${Debug.showSymbol(sym.parent!)}`); + Debug.fail(`Special property assignment kind does not have a module as its parent. Assignment is ${Debug.formatSymbol(sym)}, parent is ${Debug.formatSymbol(sym.parent!)}`); } return sym && exportInfo(sym, kind); } diff --git a/src/services/services.ts b/src/services/services.ts index 9637b3c5321f6..6c882bbb28f0e 100644 --- a/src/services/services.ts +++ b/src/services/services.ts @@ -173,7 +173,7 @@ namespace ts { const textPos = scanner.getTextPos(); if (textPos <= end) { if (token === SyntaxKind.Identifier) { - Debug.fail(`Did not expect ${Debug.showSyntaxKind(parent)} to have an Identifier in its trivia`); + Debug.fail(`Did not expect ${Debug.formatSyntaxKind(parent.kind)} to have an Identifier in its trivia`); } nodes.push(createNode(token, pos, textPos, parent)); } diff --git a/src/services/signatureHelp.ts b/src/services/signatureHelp.ts index 0c9f76acacb7f..25f958dfc6a75 100644 --- a/src/services/signatureHelp.ts +++ b/src/services/signatureHelp.ts @@ -455,7 +455,7 @@ namespace ts.SignatureHelp { for (let n = node; !isSourceFile(n) && (isManuallyInvoked || !isBlock(n)); n = n.parent) { // If the node is not a subspan of its parent, this is a big problem. // There have been crashes that might be caused by this violation. - Debug.assert(rangeContainsRange(n.parent, n), "Not a subspan", () => `Child: ${Debug.showSyntaxKind(n)}, parent: ${Debug.showSyntaxKind(n.parent)}`); + Debug.assert(rangeContainsRange(n.parent, n), "Not a subspan", () => `Child: ${Debug.formatSyntaxKind(n.kind)}, parent: ${Debug.formatSyntaxKind(n.parent.kind)}`); const argumentInfo = getImmediatelyContainingArgumentOrContextualParameterInfo(n, position, sourceFile, checker); if (argumentInfo) { return argumentInfo; diff --git a/src/testRunner/unittests/factory.ts b/src/testRunner/unittests/factory.ts index cc760059c199d..5593b39256707 100644 --- a/src/testRunner/unittests/factory.ts +++ b/src/testRunner/unittests/factory.ts @@ -1,7 +1,7 @@ namespace ts { describe("unittests:: FactoryAPI", () => { function assertSyntaxKind(node: Node, expected: SyntaxKind) { - assert.strictEqual(node.kind, expected, `Actual: ${Debug.showSyntaxKind(node)} Expected: ${(ts as any).SyntaxKind[expected]}`); + assert.strictEqual(node.kind, expected, `Actual: ${Debug.formatSyntaxKind(node.kind)} Expected: ${Debug.formatSyntaxKind(expected)}`); } describe("createExportAssignment", () => { it("parenthesizes default export if necessary", () => { diff --git a/tests/baselines/reference/objectRestCatchES5.js b/tests/baselines/reference/objectRestCatchES5.js new file mode 100644 index 0000000000000..210e8cec3bfea --- /dev/null +++ b/tests/baselines/reference/objectRestCatchES5.js @@ -0,0 +1,21 @@ +//// [objectRestCatchES5.ts] +let a = 1, b = 2; +try {} catch ({ a, ...b }) {} + +//// [objectRestCatchES5.js] +var __rest = (this && this.__rest) || function (s, e) { + var t = {}; + for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0) + t[p] = s[p]; + if (s != null && typeof Object.getOwnPropertySymbols === "function") + for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) { + if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i])) + t[p[i]] = s[p[i]]; + } + return t; +}; +var a = 1, b = 2; +try { } +catch (_a) { + var a_1 = _a.a, b_1 = __rest(_a, ["a"]); +} diff --git a/tests/baselines/reference/objectRestCatchES5.symbols b/tests/baselines/reference/objectRestCatchES5.symbols new file mode 100644 index 0000000000000..007424bd6c32c --- /dev/null +++ b/tests/baselines/reference/objectRestCatchES5.symbols @@ -0,0 +1,9 @@ +=== tests/cases/conformance/types/rest/objectRestCatchES5.ts === +let a = 1, b = 2; +>a : Symbol(a, Decl(objectRestCatchES5.ts, 0, 3)) +>b : Symbol(b, Decl(objectRestCatchES5.ts, 0, 10)) + +try {} catch ({ a, ...b }) {} +>a : Symbol(a, Decl(objectRestCatchES5.ts, 1, 15)) +>b : Symbol(b, Decl(objectRestCatchES5.ts, 1, 18)) + diff --git a/tests/baselines/reference/objectRestCatchES5.types b/tests/baselines/reference/objectRestCatchES5.types new file mode 100644 index 0000000000000..3d6b0a3e69530 --- /dev/null +++ b/tests/baselines/reference/objectRestCatchES5.types @@ -0,0 +1,11 @@ +=== tests/cases/conformance/types/rest/objectRestCatchES5.ts === +let a = 1, b = 2; +>a : number +>1 : 1 +>b : number +>2 : 2 + +try {} catch ({ a, ...b }) {} +>a : any +>b : any + diff --git a/tests/cases/conformance/types/rest/objectRestCatchES5.ts b/tests/cases/conformance/types/rest/objectRestCatchES5.ts new file mode 100644 index 0000000000000..0e568d32b5863 --- /dev/null +++ b/tests/cases/conformance/types/rest/objectRestCatchES5.ts @@ -0,0 +1,2 @@ +let a = 1, b = 2; +try {} catch ({ a, ...b }) {} \ No newline at end of file