diff --git a/src/compiler/diagnosticMessages.json b/src/compiler/diagnosticMessages.json index b3741af80e8..c6024e0974c 100644 --- a/src/compiler/diagnosticMessages.json +++ b/src/compiler/diagnosticMessages.json @@ -3704,8 +3704,12 @@ "category": "Message", "code": 95004 }, - "Convert to Typescript type": { + "Annotate with type from JSDoc": { "category": "Message", "code": 95005 + }, + "Annotate with return type from JSDoc": { + "category": "Message", + "code": 95006 } } diff --git a/src/services/refactors/convertJSDocToTypes.ts b/src/services/refactors/convertJSDocToTypes.ts index 562c26bf50f..c9915d85961 100644 --- a/src/services/refactors/convertJSDocToTypes.ts +++ b/src/services/refactors/convertJSDocToTypes.ts @@ -1,10 +1,16 @@ /* @internal */ namespace ts.refactor.convertJSDocToTypes { - const actionName = "convert"; + const actionName = "annotate"; - const convertJSDocToTypes: Refactor = { - name: "Convert to Typescript type", - description: Diagnostics.Convert_to_Typescript_type.message, + const annotateTypeFromJSDoc: Refactor = { + name: "Annotate with type from JSDoc", + description: Diagnostics.Annotate_with_type_from_JSDoc.message, + getEditsForAction, + getAvailableActions + }; + const annotateReturnTypeFromJSDoc: Refactor = { + name: "Annotate with return type from JSDoc", + description: Diagnostics.Annotate_with_return_type_from_JSDoc.message, getEditsForAction, getAvailableActions }; @@ -16,7 +22,8 @@ namespace ts.refactor.convertJSDocToTypes { | PropertySignature | PropertyDeclaration; - registerRefactor(convertJSDocToTypes); + registerRefactor(annotateTypeFromJSDoc); + registerRefactor(annotateReturnTypeFromJSDoc); function getAvailableActions(context: RefactorContext): ApplicableRefactorInfo[] | undefined { if (isInJavaScriptFile(context.file)) { @@ -25,19 +32,22 @@ namespace ts.refactor.convertJSDocToTypes { const node = getTokenAtPosition(context.file, context.startPosition, /*includeJsDocComment*/ false); const decl = findAncestor(node, isTypedNode); - if (decl && (getJSDocType(decl) || getJSDocReturnType(decl)) && !decl.type) { - return [ - { - name: convertJSDocToTypes.name, - description: convertJSDocToTypes.description, + if (decl && !decl.type) { + const annotate = getJSDocType(decl) ? annotateTypeFromJSDoc : + getJSDocReturnType(decl) ? annotateReturnTypeFromJSDoc : + undefined; + if (annotate) { + return [{ + name: annotate.name, + description: annotate.description, actions: [ { - description: convertJSDocToTypes.description, - name: actionName - } + description: annotate.description, + name: actionName + } ] - } - ]; + }]; + } } } diff --git a/tests/cases/fourslash/convertJSDocToTypes1.ts b/tests/cases/fourslash/convertJSDocToTypes1.ts index 7f90febaca1..99cf5f8db1c 100644 --- a/tests/cases/fourslash/convertJSDocToTypes1.ts +++ b/tests/cases/fourslash/convertJSDocToTypes1.ts @@ -7,4 +7,4 @@ verify.applicableRefactorAvailableAtMarker('1'); verify.fileAfterApplyingRefactorAtMarker('1', `/** @type {number} */ -var x: number;`, 'Convert to Typescript type', 'convert'); +var x: number;`, 'Annotate with type from JSDoc', 'annotate'); diff --git a/tests/cases/fourslash/convertJSDocToTypes10.ts b/tests/cases/fourslash/convertJSDocToTypes10.ts index 1e925db81e0..88b565fa932 100644 --- a/tests/cases/fourslash/convertJSDocToTypes10.ts +++ b/tests/cases/fourslash/convertJSDocToTypes10.ts @@ -12,4 +12,4 @@ verify.fileAfterApplyingRefactorAtMarker('1', * @param {?} x * @returns {number} */ -var f = (x): number => x`, 'Convert to Typescript type', 'convert'); +var f = (x): number => x`, 'Annotate with return type from JSDoc', 'annotate'); diff --git a/tests/cases/fourslash/convertJSDocToTypes11.ts b/tests/cases/fourslash/convertJSDocToTypes11.ts index 0315e506dc1..63b2d85fbfe 100644 --- a/tests/cases/fourslash/convertJSDocToTypes11.ts +++ b/tests/cases/fourslash/convertJSDocToTypes11.ts @@ -12,4 +12,4 @@ verify.fileAfterApplyingRefactorAtMarker('2', * @param {?} x * @returns {number} */ -var f = (x: any) => x`, 'Convert to Typescript type', 'convert'); +var f = (x: any) => x`, 'Annotate with type from JSDoc', 'annotate'); diff --git a/tests/cases/fourslash/convertJSDocToTypes12.ts b/tests/cases/fourslash/convertJSDocToTypes12.ts index 5015cb825d8..95fa0b55cd2 100644 --- a/tests/cases/fourslash/convertJSDocToTypes12.ts +++ b/tests/cases/fourslash/convertJSDocToTypes12.ts @@ -18,4 +18,4 @@ verify.fileAfterApplyingRefactorAtMarker('1', */ m(x): any[] { } -}`, 'Convert to Typescript type', 'convert'); +}`, 'Annotate with return type from JSDoc', 'annotate'); diff --git a/tests/cases/fourslash/convertJSDocToTypes13.ts b/tests/cases/fourslash/convertJSDocToTypes13.ts index 4ee58da1caa..caa3315b87f 100644 --- a/tests/cases/fourslash/convertJSDocToTypes13.ts +++ b/tests/cases/fourslash/convertJSDocToTypes13.ts @@ -8,4 +8,4 @@ verify.fileAfterApplyingRefactorAtMarker('1', `class C { /** @return {number} */ get c(): number { return 12; } -}`, 'Convert to Typescript type', 'convert'); +}`, 'Annotate with return type from JSDoc', 'annotate'); diff --git a/tests/cases/fourslash/convertJSDocToTypes14.ts b/tests/cases/fourslash/convertJSDocToTypes14.ts new file mode 100644 index 00000000000..43ac95a1c4a --- /dev/null +++ b/tests/cases/fourslash/convertJSDocToTypes14.ts @@ -0,0 +1,11 @@ +/// +/////** @return {number} */ +////function f() { +//// /*1*/return 12; +////} +verify.applicableRefactorAvailableAtMarker('1'); +verify.fileAfterApplyingRefactorAtMarker('1', +`/** @return {number} */ +function f(): number { + return 12; +}`, 'Annotate with return type from JSDoc', 'annotate'); diff --git a/tests/cases/fourslash/convertJSDocToTypes3.ts b/tests/cases/fourslash/convertJSDocToTypes3.ts index 3d3c1d35f44..985b89ed709 100644 --- a/tests/cases/fourslash/convertJSDocToTypes3.ts +++ b/tests/cases/fourslash/convertJSDocToTypes3.ts @@ -19,7 +19,7 @@ verify.fileAfterApplyingRefactorAtMarker('1', * @param {*} beta - I have no idea how this got here */ function f(x: number, y, z: string, alpha, beta) { -}`, 'Convert to Typescript type', 'convert'); +}`, 'Annotate with type from JSDoc', 'annotate'); verify.applicableRefactorAvailableAtMarker('2'); verify.fileAfterApplyingRefactorAtMarker('2', @@ -34,7 +34,7 @@ function f(x: number, y: { a: string; b: Date; }, z: string, alpha, beta) { -}`, 'Convert to Typescript type', 'convert'); +}`, 'Annotate with type from JSDoc', 'annotate'); verify.not.applicableRefactorAvailableAtMarker('3'); verify.not.applicableRefactorAvailableAtMarker('4'); @@ -51,4 +51,4 @@ function f(x: number, y: { a: string; b: Date; }, z: string, alpha, beta: any) { -}`, 'Convert to Typescript type', 'convert'); +}`, 'Annotate with type from JSDoc', 'annotate'); diff --git a/tests/cases/fourslash/convertJSDocToTypes4.ts b/tests/cases/fourslash/convertJSDocToTypes4.ts index ea80bf0452d..d4d5384b19c 100644 --- a/tests/cases/fourslash/convertJSDocToTypes4.ts +++ b/tests/cases/fourslash/convertJSDocToTypes4.ts @@ -24,7 +24,7 @@ verify.fileAfterApplyingRefactorAtMarker('1', * @param {number!} delta */ function f(x: any, y, z, alpha, beta, gamma, delta) { -}`, 'Convert to Typescript type', 'convert'); +}`, 'Annotate with type from JSDoc', 'annotate'); verify.applicableRefactorAvailableAtMarker('2'); verify.fileAfterApplyingRefactorAtMarker('2', @@ -38,7 +38,7 @@ verify.fileAfterApplyingRefactorAtMarker('2', * @param {number!} delta */ function f(x: any, y: any, z, alpha, beta, gamma, delta) { -}`, 'Convert to Typescript type', 'convert'); +}`, 'Annotate with type from JSDoc', 'annotate'); verify.applicableRefactorAvailableAtMarker('3'); verify.fileAfterApplyingRefactorAtMarker('3', @@ -52,7 +52,7 @@ verify.fileAfterApplyingRefactorAtMarker('3', * @param {number!} delta */ function f(x: any, y: any, z: number | undefined, alpha, beta, gamma, delta) { -}`, 'Convert to Typescript type', 'convert'); +}`, 'Annotate with type from JSDoc', 'annotate'); verify.applicableRefactorAvailableAtMarker('4'); verify.fileAfterApplyingRefactorAtMarker('4', `/** @@ -65,7 +65,7 @@ verify.fileAfterApplyingRefactorAtMarker('4', * @param {number!} delta */ function f(x: any, y: any, z: number | undefined, alpha: number[], beta, gamma, delta) { -}`, 'Convert to Typescript type', 'convert'); +}`, 'Annotate with type from JSDoc', 'annotate'); verify.applicableRefactorAvailableAtMarker('5'); verify.fileAfterApplyingRefactorAtMarker('5', @@ -81,7 +81,7 @@ verify.fileAfterApplyingRefactorAtMarker('5', function f(x: any, y: any, z: number | undefined, alpha: number[], beta: (this: { a: string; }, arg1: string, arg2: number) => boolean, gamma, delta) { -}`, 'Convert to Typescript type', 'convert'); +}`, 'Annotate with type from JSDoc', 'annotate'); verify.applicableRefactorAvailableAtMarker('6'); verify.fileAfterApplyingRefactorAtMarker('6', `/** @@ -96,7 +96,7 @@ verify.fileAfterApplyingRefactorAtMarker('6', function f(x: any, y: any, z: number | undefined, alpha: number[], beta: (this: { a: string; }, arg1: string, arg2: number) => boolean, gamma: number | null, delta) { -}`, 'Convert to Typescript type', 'convert'); +}`, 'Annotate with type from JSDoc', 'annotate'); verify.applicableRefactorAvailableAtMarker('7'); verify.fileAfterApplyingRefactorAtMarker('7', @@ -112,4 +112,4 @@ verify.fileAfterApplyingRefactorAtMarker('7', function f(x: any, y: any, z: number | undefined, alpha: number[], beta: (this: { a: string; }, arg1: string, arg2: number) => boolean, gamma: number | null, delta: number) { -}`, 'Convert to Typescript type', 'convert'); +}`, 'Annotate with type from JSDoc', 'annotate'); diff --git a/tests/cases/fourslash/convertJSDocToTypes5.ts b/tests/cases/fourslash/convertJSDocToTypes5.ts index 7647e7be2f8..1f15bf59924 100644 --- a/tests/cases/fourslash/convertJSDocToTypes5.ts +++ b/tests/cases/fourslash/convertJSDocToTypes5.ts @@ -12,4 +12,4 @@ verify.fileAfterApplyingRefactorAtMarker('1', /** @type {number | null} */ /** @type {number | null} */ p: number | null = null; -}`, 'Convert to Typescript type', 'convert'); +}`, 'Annotate with type from JSDoc', 'annotate'); diff --git a/tests/cases/fourslash/convertJSDocToTypes6.ts b/tests/cases/fourslash/convertJSDocToTypes6.ts index 991e84b76b6..91bc1523ea3 100644 --- a/tests/cases/fourslash/convertJSDocToTypes6.ts +++ b/tests/cases/fourslash/convertJSDocToTypes6.ts @@ -12,4 +12,4 @@ verify.fileAfterApplyingRefactorAtMarker('1', /** @type {number | null} */ /** @type {number | null} */ p: number | null; -}`, 'Convert to Typescript type', 'convert'); +}`, 'Annotate with type from JSDoc', 'annotate'); diff --git a/tests/cases/fourslash/convertJSDocToTypes7.ts b/tests/cases/fourslash/convertJSDocToTypes7.ts index 5046b4237ec..c78f8949b18 100644 --- a/tests/cases/fourslash/convertJSDocToTypes7.ts +++ b/tests/cases/fourslash/convertJSDocToTypes7.ts @@ -14,4 +14,4 @@ verify.fileAfterApplyingRefactorAtMarker('1', * @returns {number} */ function f(x): number { -}`, 'Convert to Typescript type', 'convert'); +}`, 'Annotate with return type from JSDoc', 'annotate'); diff --git a/tests/cases/fourslash/convertJSDocToTypes8.ts b/tests/cases/fourslash/convertJSDocToTypes8.ts index b3d0f821836..502b945819c 100644 --- a/tests/cases/fourslash/convertJSDocToTypes8.ts +++ b/tests/cases/fourslash/convertJSDocToTypes8.ts @@ -14,4 +14,4 @@ verify.fileAfterApplyingRefactorAtMarker('1', * @returns {number} */ var f = function(x): number { -}`, 'Convert to Typescript type', 'convert'); +}`, 'Annotate with return type from JSDoc', 'annotate'); diff --git a/tests/cases/fourslash/convertJSDocToTypes9.ts b/tests/cases/fourslash/convertJSDocToTypes9.ts index 7f682a2459c..cf2581f5d8f 100644 --- a/tests/cases/fourslash/convertJSDocToTypes9.ts +++ b/tests/cases/fourslash/convertJSDocToTypes9.ts @@ -12,4 +12,4 @@ verify.fileAfterApplyingRefactorAtMarker('1', * @param {?} x * @returns {number} */ -var f = (x: any) => x`, 'Convert to Typescript type', 'convert'); +var f = (x: any) => x`, 'Annotate with type from JSDoc', 'annotate');