From 50a5bc839ab7e724625f151c7273b275de34069b Mon Sep 17 00:00:00 2001 From: Minh Quy Date: Wed, 6 Apr 2022 01:51:32 +0200 Subject: [PATCH] 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 --- src/services/refactors/extractType.ts | 2 ++ ...ype_js_TypeLiteral_CommentAfterProperty.ts | 26 +++++++++++++++++++ ...pe_js_TypeLiteral_CommentBeforeProperty.ts | 26 +++++++++++++++++++ ...ExtractType_js_Union_CommentAfterMember.ts | 19 ++++++++++++++ ...xtractType_js_Union_CommentBeforeMember.ts | 19 ++++++++++++++ 5 files changed, 92 insertions(+) create mode 100644 tests/cases/fourslash/refactorExtractType_js_TypeLiteral_CommentAfterProperty.ts create mode 100644 tests/cases/fourslash/refactorExtractType_js_TypeLiteral_CommentBeforeProperty.ts create mode 100644 tests/cases/fourslash/refactorExtractType_js_Union_CommentAfterMember.ts create mode 100644 tests/cases/fourslash/refactorExtractType_js_Union_CommentBeforeMember.ts diff --git a/src/services/refactors/extractType.ts b/src/services/refactors/extractType.ts index c785160ad80..ac6ab73963b 100644 --- a/src/services/refactors/extractType.ts +++ b/src/services/refactors/extractType.ts @@ -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), diff --git a/tests/cases/fourslash/refactorExtractType_js_TypeLiteral_CommentAfterProperty.ts b/tests/cases/fourslash/refactorExtractType_js_TypeLiteral_CommentAfterProperty.ts new file mode 100644 index 00000000000..3b24179882d --- /dev/null +++ b/tests/cases/fourslash/refactorExtractType_js_TypeLiteral_CommentAfterProperty.ts @@ -0,0 +1,26 @@ +/// + +// @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;`, +}); diff --git a/tests/cases/fourslash/refactorExtractType_js_TypeLiteral_CommentBeforeProperty.ts b/tests/cases/fourslash/refactorExtractType_js_TypeLiteral_CommentBeforeProperty.ts new file mode 100644 index 00000000000..1283bbdf8fc --- /dev/null +++ b/tests/cases/fourslash/refactorExtractType_js_TypeLiteral_CommentBeforeProperty.ts @@ -0,0 +1,26 @@ +/// + +// @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;`, +}); diff --git a/tests/cases/fourslash/refactorExtractType_js_Union_CommentAfterMember.ts b/tests/cases/fourslash/refactorExtractType_js_Union_CommentAfterMember.ts new file mode 100644 index 00000000000..94c805a63a0 --- /dev/null +++ b/tests/cases/fourslash/refactorExtractType_js_Union_CommentAfterMember.ts @@ -0,0 +1,19 @@ +/// + +// @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 */ ;`, +}); diff --git a/tests/cases/fourslash/refactorExtractType_js_Union_CommentBeforeMember.ts b/tests/cases/fourslash/refactorExtractType_js_Union_CommentBeforeMember.ts new file mode 100644 index 00000000000..091fedb6c74 --- /dev/null +++ b/tests/cases/fourslash/refactorExtractType_js_Union_CommentBeforeMember.ts @@ -0,0 +1,19 @@ +/// + +// @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;`, +});