Skip to content

Commit

Permalink
Merge pull request #256 from Quramy/chore/break_for_each_statements
Browse files Browse the repository at this point in the history
chore: Reactor insertLeadingBreakMarker fn
  • Loading branch information
Quramy authored Jan 15, 2024
2 parents 5801afc + b6f6c9a commit fcd0470
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 7 deletions.
2 changes: 1 addition & 1 deletion examples/example-prj/src/__generated__/fabbrica/index.d.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

19 changes: 15 additions & 4 deletions packages/prisma-fabbrica/src/templates/ast-tools/comment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,19 @@ export function wrapWithTSDoc<T extends ts.Node>(text: string, node: T): T {
);
}

export function insertLeadingBreakMarker<T extends ts.Node>(node: T): T {
const comments = ts.getSyntheticLeadingComments(node);
if (comments?.length) return node;
return ts.addSyntheticLeadingComment(node, ts.SyntaxKind.SingleLineCommentTrivia, "%BR%", true);
function isNodeArray(x: any): x is ts.NodeArray<any> {
return Array.isArray(x);
}

export function insertLeadingBreakMarker<T extends ts.Node | ts.NodeArray<ts.Node>>(nodeOrNodeList: T): T {
const processNode = <S extends ts.Node>(node: S) => {
const comments = ts.getSyntheticLeadingComments(node);
if (comments?.length) return node;
return ts.addSyntheticLeadingComment(node, ts.SyntaxKind.SingleLineCommentTrivia, "%BR%", true) as S;
};
if (!isNodeArray(nodeOrNodeList)) {
return processNode(nodeOrNodeList) as T;
} else {
return nodeOrNodeList.map(processNode) as unknown as T;
}
}
4 changes: 2 additions & 2 deletions packages/prisma-fabbrica/src/templates/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ export const importStatement = (specifier: string, prismaClientModuleSpecifier:
`();

export const symbols = () =>
template.statement`
template.sourceFile`
const factoryFor = Symbol("factoryFor");
`();

Expand Down Expand Up @@ -513,7 +513,7 @@ export function getSourceFile({
...modelEnums.map(enumName => importStatement(enumName, prismaClientModuleSpecifier)),
...header(prismaClientModuleSpecifier).statements,
insertLeadingBreakMarker(buildDataOptions()),
insertLeadingBreakMarker(symbols()),
...insertLeadingBreakMarker(symbols().statements),
insertLeadingBreakMarker(modelFieldDefinitions(document.datamodel.models)),
...document.datamodel.models
.reduce((acc, model) => {
Expand Down

0 comments on commit fcd0470

Please sign in to comment.