diff --git a/src/language-js/comments.js b/src/language-js/comments.js index 4ad90155..ff4ef1da 100644 --- a/src/language-js/comments.js +++ b/src/language-js/comments.js @@ -135,6 +135,13 @@ function handleRemainingComment(comment, text, options, ast, isLastComment) { comment, options ) || + handleTSMappedTypeComments( + text, + enclosingNode, + precedingNode, + followingNode, + comment + ) || handleBreakAndContinueStatementComments(enclosingNode, comment) ) { return true; @@ -700,6 +707,38 @@ function handleVariableDeclaratorComments( return false; } +function handleTSMappedTypeComments( + text, + enclosingNode, + precedingNode, + followingNode, + comment +) { + if (!enclosingNode || enclosingNode.type !== "TSMappedType") { + return false; + } + + if ( + followingNode && + followingNode.type === "TSTypeParameter" && + followingNode.name + ) { + addLeadingComment(followingNode.name, comment); + return true; + } + + if ( + precedingNode && + precedingNode.type === "TSTypeParameter" && + precedingNode.constraint + ) { + addTrailingComment(precedingNode.constraint, comment); + return true; + } + + return false; +} + function isBlockComment(comment) { return comment.type === "Block" || comment.type === "CommentBlock"; } diff --git a/src/language-js/printer-estree.js b/src/language-js/printer-estree.js index e301bd9f..762ce04a 100644 --- a/src/language-js/printer-estree.js +++ b/src/language-js/printer-estree.js @@ -2559,10 +2559,11 @@ function printPathNoParens(path, options, print, args) { case "TypeParameter": { const parent = path.getParentNode(); if (parent.type === "TSMappedType") { - parts.push(path.call(print, "name")); + parts.push("[", path.call(print, "name")); if (n.constraint) { parts.push(" in ", path.call(print, "constraint")); } + parts.push("]"); return concat(parts); } @@ -2788,9 +2789,7 @@ function printPathNoParens(path, options, print, args) { ]) : "", printTypeScriptModifiers(path, options, print), - "[", path.call(print, "typeParameter"), - "]", n.questionToken ? getTypeScriptMappedTypeModifier(n.questionToken, "?") : "", diff --git a/tests/typescript_comments/__snapshots__/jsfmt.spec.js.snap b/tests/typescript_comments/__snapshots__/jsfmt.spec.js.snap index f68bb8c5..4358de8b 100644 --- a/tests/typescript_comments/__snapshots__/jsfmt.spec.js.snap +++ b/tests/typescript_comments/__snapshots__/jsfmt.spec.js.snap @@ -116,6 +116,55 @@ export type WrappedFormUtils = { `; +exports[`mapped_types.ts 1`] = ` +type A = { + // commentA + [a in A]: string; +} + +type B = { + /* commentB */ [b in B]: string +} + +type C = { + [/* commentC */ c in C]: string +} + +type D = { + [d /* commentD */ in D]: string +} + +type E = { + [e in /* commentE */ E]: string +} + +type F = { + [f in F /* commentF */]: string +} + +type G = { + [g in G] /* commentG */: string +} +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +type A = { + // commentA + [a in A]: string +}; + +type B = { /* commentB */ [b in B]: string }; + +type C = { [/* commentC */ c in C]: string }; + +type D = { [d /* commentD */ in D]: string }; + +type E = { [e in /* commentE */ E]: string }; + +type F = { [f in F /* commentF */]: string }; + +type G = { [g in G /* commentG */]: string }; + +`; + exports[`methods.ts 1`] = ` export class Point { /** diff --git a/tests/typescript_comments/mapped_types.ts b/tests/typescript_comments/mapped_types.ts new file mode 100644 index 00000000..67dcc092 --- /dev/null +++ b/tests/typescript_comments/mapped_types.ts @@ -0,0 +1,28 @@ +type A = { + // commentA + [a in A]: string; +} + +type B = { + /* commentB */ [b in B]: string +} + +type C = { + [/* commentC */ c in C]: string +} + +type D = { + [d /* commentD */ in D]: string +} + +type E = { + [e in /* commentE */ E]: string +} + +type F = { + [f in F /* commentF */]: string +} + +type G = { + [g in G] /* commentG */: string +}