Skip to content

Commit 24d8f79

Browse files
authored
Fix crash in emitTokenWithComment (#36542)
1 parent 80ad0de commit 24d8f79

File tree

4 files changed

+39
-3
lines changed

4 files changed

+39
-3
lines changed

src/compiler/emitter.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2744,11 +2744,11 @@ namespace ts {
27442744
const node = getParseTreeNode(contextNode);
27452745
const isSimilarNode = node && node.kind === contextNode.kind;
27462746
const startPos = pos;
2747-
if (isSimilarNode) {
2748-
pos = skipTrivia(currentSourceFile!.text, pos);
2747+
if (isSimilarNode && currentSourceFile) {
2748+
pos = skipTrivia(currentSourceFile.text, pos);
27492749
}
27502750
if (emitLeadingCommentsOfPosition && isSimilarNode && contextNode.pos !== startPos) {
2751-
const needsIndent = indentLeading && !positionsAreOnSameLine(startPos, pos, currentSourceFile!);
2751+
const needsIndent = indentLeading && currentSourceFile && !positionsAreOnSameLine(startPos, pos, currentSourceFile);
27522752
if (needsIndent) {
27532753
increaseIndent();
27542754
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
tests/cases/compiler/crashInEmitTokenWithComment.ts(5,4): error TS2345: Argument of type '({ [foo.bar]: c }: {}) => any' is not assignable to parameter of type 'string'.
2+
tests/cases/compiler/crashInEmitTokenWithComment.ts(5,7): error TS2537: Type '{}' has no matching index signature for type 'string'.
3+
4+
5+
==== tests/cases/compiler/crashInEmitTokenWithComment.ts (2 errors) ====
6+
// GH#32358
7+
const fn = (param: string) => undefined;
8+
9+
const foo = {bar: 'a'};
10+
fn(({[foo.bar]: c}) => undefined);
11+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
12+
!!! error TS2345: Argument of type '({ [foo.bar]: c }: {}) => any' is not assignable to parameter of type 'string'.
13+
~~~~~~~
14+
!!! error TS2537: Type '{}' has no matching index signature for type 'string'.
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
//// [crashInEmitTokenWithComment.ts]
2+
// GH#32358
3+
const fn = (param: string) => undefined;
4+
5+
const foo = {bar: 'a'};
6+
fn(({[foo.bar]: c}) => undefined);
7+
8+
//// [crashInEmitTokenWithComment.js]
9+
// GH#32358
10+
var fn = function (param) { return undefined; };
11+
var foo = { bar: 'a' };
12+
fn(function (_a) {
13+
var _b = foo.bar, c = _a[_b];
14+
return undefined;
15+
});
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
// @noTypesAndSymbols: true
2+
3+
// GH#32358
4+
const fn = (param: string) => undefined;
5+
6+
const foo = {bar: 'a'};
7+
fn(({[foo.bar]: c}) => undefined);

0 commit comments

Comments
 (0)