diff --git a/src/services/refactors/extractType.ts b/src/services/refactors/extractType.ts index c785160ad80d2..ac6ab73963b31 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 0000000000000..3b24179882db0 --- /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 0000000000000..1283bbdf8fc50 --- /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 0000000000000..94c805a63a032 --- /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 0000000000000..091fedb6c74aa --- /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;`, +});