Skip to content

Commit 50a5bc8

Browse files
author
Minh Quy
authored
fix(48540): Extract to typedef from (invalid) type with comments in JS file causes assertion failure (#48545)
* fix(48540): Remove comments from jsdoc union type expression * fix(48540) - Remove comment for top level import and add extract typedef tests * fix(48540) - Remove comments from jsdoc's descendant * fix(48540) - Using no nested comments instead of traversing
1 parent 42032df commit 50a5bc8

5 files changed

+92
-0
lines changed

src/services/refactors/extractType.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,8 @@ namespace ts.refactor {
229229
function doTypedefChange(changes: textChanges.ChangeTracker, file: SourceFile, name: string, info: ExtractInfo) {
230230
const { firstStatement, selection, typeParameters } = info;
231231

232+
setEmitFlags(selection, EmitFlags.NoComments | EmitFlags.NoNestedComments);
233+
232234
const node = factory.createJSDocTypedefTag(
233235
factory.createIdentifier("typedef"),
234236
factory.createJSDocTypeExpression(selection),
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/// <reference path='fourslash.ts' />
2+
3+
// @allowJs: true
4+
// @Filename: a.js
5+
////type Foo = /*a*/[|{
6+
//// oops: string;
7+
//// /**
8+
//// *
9+
//// */
10+
////}|]/*b*/;
11+
12+
goTo.file('a.js')
13+
goTo.select("a", "b");
14+
edit.applyRefactor({
15+
refactorName: "Extract type",
16+
actionName: "Extract to typedef",
17+
actionDescription: "Extract to typedef",
18+
newContent:
19+
`/**
20+
* @typedef {{
21+
oops: string;
22+
}} /*RENAME*/NewType
23+
*/
24+
25+
type Foo = NewType;`,
26+
});
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/// <reference path='fourslash.ts' />
2+
3+
// @allowJs: true
4+
// @Filename: a.js
5+
////type Foo = /*a*/[|{
6+
//// /**
7+
//// *
8+
//// */
9+
//// oops: string;
10+
////}|]/*b*/;
11+
12+
goTo.file('a.js')
13+
goTo.select("a", "b");
14+
edit.applyRefactor({
15+
refactorName: "Extract type",
16+
actionName: "Extract to typedef",
17+
actionDescription: "Extract to typedef",
18+
newContent:
19+
`/**
20+
* @typedef {{
21+
oops: string;
22+
}} /*RENAME*/NewType
23+
*/
24+
25+
type Foo = NewType;`,
26+
});
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/// <reference path='fourslash.ts' />
2+
3+
// @allowJs: true
4+
// @Filename: a.js
5+
////type Bar = /*a*/[|string | boolean /* oops */ |]/*b*/;
6+
7+
goTo.file('a.js')
8+
goTo.select("a", "b");
9+
edit.applyRefactor({
10+
refactorName: "Extract type",
11+
actionName: "Extract to typedef",
12+
actionDescription: "Extract to typedef",
13+
newContent:
14+
`/**
15+
* @typedef {string | boolean} /*RENAME*/NewType
16+
*/
17+
18+
type Bar = NewType /* oops */ ;`,
19+
});
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/// <reference path='fourslash.ts' />
2+
3+
// @allowJs: true
4+
// @Filename: a.js
5+
////type Bar = /*a*/[|string | /* oops */ boolean|]/*b*/;
6+
7+
goTo.file('a.js')
8+
goTo.select("a", "b");
9+
edit.applyRefactor({
10+
refactorName: "Extract type",
11+
actionName: "Extract to typedef",
12+
actionDescription: "Extract to typedef",
13+
newContent:
14+
`/**
15+
* @typedef {string | boolean} /*RENAME*/NewType
16+
*/
17+
18+
type Bar = NewType;`,
19+
});

0 commit comments

Comments
 (0)