From 24d8f795b29ed5b71a7635167e5ba177258d7093 Mon Sep 17 00:00:00 2001 From: Ron Buckton Date: Fri, 31 Jan 2020 10:41:09 -0800 Subject: [PATCH] Fix crash in emitTokenWithComment (#36542) --- src/compiler/emitter.ts | 6 +++--- .../crashInEmitTokenWithComment.errors.txt | 14 ++++++++++++++ .../reference/crashInEmitTokenWithComment.js | 15 +++++++++++++++ .../cases/compiler/crashInEmitTokenWithComment.ts | 7 +++++++ 4 files changed, 39 insertions(+), 3 deletions(-) create mode 100644 tests/baselines/reference/crashInEmitTokenWithComment.errors.txt create mode 100644 tests/baselines/reference/crashInEmitTokenWithComment.js create mode 100644 tests/cases/compiler/crashInEmitTokenWithComment.ts diff --git a/src/compiler/emitter.ts b/src/compiler/emitter.ts index 5ebc32643b1..a5eb13647e9 100644 --- a/src/compiler/emitter.ts +++ b/src/compiler/emitter.ts @@ -2744,11 +2744,11 @@ namespace ts { const node = getParseTreeNode(contextNode); const isSimilarNode = node && node.kind === contextNode.kind; const startPos = pos; - if (isSimilarNode) { - pos = skipTrivia(currentSourceFile!.text, pos); + if (isSimilarNode && currentSourceFile) { + pos = skipTrivia(currentSourceFile.text, pos); } if (emitLeadingCommentsOfPosition && isSimilarNode && contextNode.pos !== startPos) { - const needsIndent = indentLeading && !positionsAreOnSameLine(startPos, pos, currentSourceFile!); + const needsIndent = indentLeading && currentSourceFile && !positionsAreOnSameLine(startPos, pos, currentSourceFile); if (needsIndent) { increaseIndent(); } diff --git a/tests/baselines/reference/crashInEmitTokenWithComment.errors.txt b/tests/baselines/reference/crashInEmitTokenWithComment.errors.txt new file mode 100644 index 00000000000..b11ca0d32bc --- /dev/null +++ b/tests/baselines/reference/crashInEmitTokenWithComment.errors.txt @@ -0,0 +1,14 @@ +tests/cases/compiler/crashInEmitTokenWithComment.ts(5,4): error TS2345: Argument of type '({ [foo.bar]: c }: {}) => any' is not assignable to parameter of type 'string'. +tests/cases/compiler/crashInEmitTokenWithComment.ts(5,7): error TS2537: Type '{}' has no matching index signature for type 'string'. + + +==== tests/cases/compiler/crashInEmitTokenWithComment.ts (2 errors) ==== + // GH#32358 + const fn = (param: string) => undefined; + + const foo = {bar: 'a'}; + fn(({[foo.bar]: c}) => undefined); + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +!!! error TS2345: Argument of type '({ [foo.bar]: c }: {}) => any' is not assignable to parameter of type 'string'. + ~~~~~~~ +!!! error TS2537: Type '{}' has no matching index signature for type 'string'. \ No newline at end of file diff --git a/tests/baselines/reference/crashInEmitTokenWithComment.js b/tests/baselines/reference/crashInEmitTokenWithComment.js new file mode 100644 index 00000000000..e799cc3acd3 --- /dev/null +++ b/tests/baselines/reference/crashInEmitTokenWithComment.js @@ -0,0 +1,15 @@ +//// [crashInEmitTokenWithComment.ts] +// GH#32358 +const fn = (param: string) => undefined; + +const foo = {bar: 'a'}; +fn(({[foo.bar]: c}) => undefined); + +//// [crashInEmitTokenWithComment.js] +// GH#32358 +var fn = function (param) { return undefined; }; +var foo = { bar: 'a' }; +fn(function (_a) { + var _b = foo.bar, c = _a[_b]; + return undefined; +}); diff --git a/tests/cases/compiler/crashInEmitTokenWithComment.ts b/tests/cases/compiler/crashInEmitTokenWithComment.ts new file mode 100644 index 00000000000..41d2ca18929 --- /dev/null +++ b/tests/cases/compiler/crashInEmitTokenWithComment.ts @@ -0,0 +1,7 @@ +// @noTypesAndSymbols: true + +// GH#32358 +const fn = (param: string) => undefined; + +const foo = {bar: 'a'}; +fn(({[foo.bar]: c}) => undefined); \ No newline at end of file