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
This commit is contained in:
Minh Quy
2022-04-06 01:51:32 +02:00
committed by GitHub
parent 42032df06d
commit 50a5bc839a
5 changed files with 92 additions and 0 deletions

View File

@@ -229,6 +229,8 @@ namespace ts.refactor {
function doTypedefChange(changes: textChanges.ChangeTracker, file: SourceFile, name: string, info: ExtractInfo) {
const { firstStatement, selection, typeParameters } = info;
setEmitFlags(selection, EmitFlags.NoComments | EmitFlags.NoNestedComments);
const node = factory.createJSDocTypedefTag(
factory.createIdentifier("typedef"),
factory.createJSDocTypeExpression(selection),

View File

@@ -0,0 +1,26 @@
/// <reference path='fourslash.ts' />
// @allowJs: true
// @Filename: a.js
////type Foo = /*a*/[|{
//// oops: string;
//// /**
//// *
//// */
////}|]/*b*/;
goTo.file('a.js')
goTo.select("a", "b");
edit.applyRefactor({
refactorName: "Extract type",
actionName: "Extract to typedef",
actionDescription: "Extract to typedef",
newContent:
`/**
* @typedef {{
oops: string;
}} /*RENAME*/NewType
*/
type Foo = NewType;`,
});

View File

@@ -0,0 +1,26 @@
/// <reference path='fourslash.ts' />
// @allowJs: true
// @Filename: a.js
////type Foo = /*a*/[|{
//// /**
//// *
//// */
//// oops: string;
////}|]/*b*/;
goTo.file('a.js')
goTo.select("a", "b");
edit.applyRefactor({
refactorName: "Extract type",
actionName: "Extract to typedef",
actionDescription: "Extract to typedef",
newContent:
`/**
* @typedef {{
oops: string;
}} /*RENAME*/NewType
*/
type Foo = NewType;`,
});

View File

@@ -0,0 +1,19 @@
/// <reference path='fourslash.ts' />
// @allowJs: true
// @Filename: a.js
////type Bar = /*a*/[|string | boolean /* oops */ |]/*b*/;
goTo.file('a.js')
goTo.select("a", "b");
edit.applyRefactor({
refactorName: "Extract type",
actionName: "Extract to typedef",
actionDescription: "Extract to typedef",
newContent:
`/**
* @typedef {string | boolean} /*RENAME*/NewType
*/
type Bar = NewType /* oops */ ;`,
});

View File

@@ -0,0 +1,19 @@
/// <reference path='fourslash.ts' />
// @allowJs: true
// @Filename: a.js
////type Bar = /*a*/[|string | /* oops */ boolean|]/*b*/;
goTo.file('a.js')
goTo.select("a", "b");
edit.applyRefactor({
refactorName: "Extract type",
actionName: "Extract to typedef",
actionDescription: "Extract to typedef",
newContent:
`/**
* @typedef {string | boolean} /*RENAME*/NewType
*/
type Bar = NewType;`,
});