Fix TSMappedType comments (#4418)

master
Lucas Duailibe 2018-05-05 15:43:53 -03:00 committed by GitHub
parent 1a6cf3d071
commit 5776c8405a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 118 additions and 3 deletions

View File

@ -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";
}

View File

@ -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, "?")
: "",

View File

@ -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 {
/**

View File

@ -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
}