Skip to content

Commit

Permalink
extract function to print directives
Browse files Browse the repository at this point in the history
  • Loading branch information
thomasheyenbrock committed Feb 18, 2023
1 parent b8a3c3c commit f704946
Showing 1 changed file with 29 additions and 23 deletions.
52 changes: 29 additions & 23 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -299,6 +299,12 @@ function printAST(
];
}

function printDirectives(
directives: Maybe<readonly TransformedNode[]>
): PrintToken[] {
return withSpace(join(directives || [], [SPACE]));
}

const list = visit<TransformedNode>(ast, {
Argument(node) {
return {
Expand Down Expand Up @@ -404,7 +410,7 @@ function printAST(
...comments,
text("enum "),
...node.name.p,
...withSpace(join(node.directives || [], [SPACE])),
...printDirectives(node.directives),
...withSpace(printEnumValueDefinitionSet(node.values)),
],
l,
Expand All @@ -421,7 +427,7 @@ function printAST(
),
text("extend enum "),
...node.name.p,
...withSpace(join(node.directives || [], [SPACE])),
...printDirectives(node.directives),
...withSpace(printEnumValueDefinitionSet(node.values)),
],
l,
Expand All @@ -441,7 +447,7 @@ function printAST(
...printDescription(node.description, comments),
...comments,
...node.name.p,
...withSpace(join(node.directives || [], [SPACE])),
...printDirectives(node.directives),
],
l: node.loc,
};
Expand All @@ -458,7 +464,7 @@ function printAST(
...(node.alias ? [...node.alias.p, text(":"), SPACE] : []),
...node.name.p,
...printArgumentSet(node.arguments),
...withSpace(join(node.directives || [], [SPACE])),
...printDirectives(node.directives),
...withSpace(node.selectionSet?.p || []),
],
l,
Expand All @@ -477,7 +483,7 @@ function printAST(
text(":"),
SPACE,
...node.type.p,
...withSpace(join(node.directives || [], [SPACE])),
...printDirectives(node.directives),
],
l: node.loc,
};
Expand All @@ -502,7 +508,7 @@ function printAST(
...node.name.p,
text(" on "),
...node.typeCondition.p,
...withSpace(join(node.directives || [], [SPACE])),
...printDirectives(node.directives),
...withSpace(node.selectionSet.p),
],
l,
Expand All @@ -515,7 +521,7 @@ function printAST(
...getComments(l?.startToken, node.name.l?.endToken),
text("..."),
...node.name.p,
...withSpace(join(node.directives || [], [SPACE])),
...printDirectives(node.directives),
],
l,
};
Expand All @@ -532,7 +538,7 @@ function printAST(
),
text("..."),
...(node.typeCondition ? [text("on "), ...node.typeCondition.p] : []),
...withSpace(join(node.directives || [], [SPACE])),
...printDirectives(node.directives),
...withSpace(node.selectionSet.p),
],
l,
Expand All @@ -552,7 +558,7 @@ function printAST(
...comments,
text("input "),
...node.name.p,
...withSpace(join(node.directives || [], [SPACE])),
...printDirectives(node.directives),
...withSpace(printInputValueDefinitionSet(node.fields, node.kind)),
],
l,
Expand All @@ -569,7 +575,7 @@ function printAST(
),
text("extend input "),
...node.name.p,
...withSpace(join(node.directives || [], [SPACE])),
...printDirectives(node.directives),
...withSpace(printInputValueDefinitionSet(node.fields, node.kind)),
],
l,
Expand All @@ -590,7 +596,7 @@ function printAST(
SPACE,
...node.type.p,
...printDefaultValue(node.defaultValue),
...withSpace(join(node.directives || [], [SPACE])),
...printDirectives(node.directives),
],
l: node.loc,
};
Expand All @@ -610,7 +616,7 @@ function printAST(
text("interface "),
...node.name.p,
...printDelimitedList(node.interfaces, node.kind),
...withSpace(join(node.directives || [], [SPACE])),
...printDirectives(node.directives),
...withSpace(printFieldDefinitionSet(node.fields)),
],
l,
Expand All @@ -628,7 +634,7 @@ function printAST(
text("extend interface "),
...node.name.p,
...printDelimitedList(node.interfaces, node.kind),
...withSpace(join(node.directives || [], [SPACE])),
...printDirectives(node.directives),
...withSpace(printFieldDefinitionSet(node.fields)),
],
l,
Expand Down Expand Up @@ -733,7 +739,7 @@ function printAST(
text("type "),
...node.name.p,
...printDelimitedList(node.interfaces, node.kind),
...withSpace(join(node.directives || [], [SPACE])),
...printDirectives(node.directives),
...withSpace(printFieldDefinitionSet(node.fields)),
],
l,
Expand All @@ -751,7 +757,7 @@ function printAST(
text("extend type "),
...node.name.p,
...printDelimitedList(node.interfaces, node.kind),
...withSpace(join(node.directives || [], [SPACE])),
...printDirectives(node.directives),
...withSpace(printFieldDefinitionSet(node.fields)),
],
l,
Expand Down Expand Up @@ -797,7 +803,7 @@ function printAST(
"",
"," + SPACE.v
)),
...withSpace(join(node.directives || [], [SPACE])),
...printDirectives(node.directives),
...withSpace(node.selectionSet.p),
],
l,
Expand Down Expand Up @@ -833,7 +839,7 @@ function printAST(
...comments,
text("scalar "),
...node.name.p,
...withSpace(join(node.directives || [], [SPACE])),
...printDirectives(node.directives),
],
l,
};
Expand All @@ -849,7 +855,7 @@ function printAST(
),
text("extend scalar "),
...node.name.p,
...withSpace(join(node.directives || [], [SPACE])),
...printDirectives(node.directives),
],
l,
};
Expand All @@ -864,7 +870,7 @@ function printAST(
...printDescription(node.description, comments),
...comments,
text("schema"),
...withSpace(join(node.directives || [], [SPACE])),
...printDirectives(node.directives),
...withSpace(printOperationTypeDefinitionSet(node.operationTypes)),
],
l,
Expand All @@ -879,7 +885,7 @@ function printAST(
next(l?.startToken.next, TokenKind.NAME)
),
text("extend schema"),
...withSpace(join(node.directives || [], [SPACE])),
...printDirectives(node.directives),
...withSpace(printOperationTypeDefinitionSet(node.operationTypes)),
],
l,
Expand Down Expand Up @@ -952,7 +958,7 @@ function printAST(
...comments,
text("union "),
...node.name.p,
...withSpace(join(node.directives || [], [SPACE])),
...printDirectives(node.directives),
...printDelimitedList(node.types, node.kind),
],
l,
Expand All @@ -969,7 +975,7 @@ function printAST(
),
text("extend union "),
...node.name.p,
...withSpace(join(node.directives || [], [SPACE])),
...printDirectives(node.directives),
...printDelimitedList(node.types, node.kind),
],
l,
Expand Down Expand Up @@ -1000,7 +1006,7 @@ function printAST(
SPACE,
...node.type.p,
...printDefaultValue(node.defaultValue),
...withSpace(join(node.directives || [], [SPACE])),
...printDirectives(node.directives),
],
l,
};
Expand Down

0 comments on commit f704946

Please sign in to comment.