Skip to content

Commit

Permalink
chore: Modify source format
Browse files Browse the repository at this point in the history
  • Loading branch information
Quramy committed Jan 12, 2024
1 parent 4356c09 commit 9a14bc0
Show file tree
Hide file tree
Showing 8 changed files with 26 additions and 15 deletions.

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

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

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

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

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

Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,11 @@ import { Prisma } from "@prisma/client";
import type { PrismaClient } from "@prisma/client";
import { getClient, ModelWithFields, createScreener, getScalarFieldValueGenerator, Resolver, normalizeResolver, normalizeList, getSequenceCounter, createCallbackChain, } from "@quramy/prisma-fabbrica/lib/internal";
export { initialize, resetSequence, registerScalarFieldValueGenerator, resetScalarFieldValueGenerator } from "@quramy/prisma-fabbrica/lib/internal";
type BuildDataOptions = {
readonly seq: number;
};
type CallbackDefineOptions<TCreated, TCreateInput> = {
onAfterBuild?: (createInput: TCreateInput) => void | PromiseLike<void>;
onBeforeCreate?: (createInput: TCreateInput) => void | PromiseLike<void>;
Expand Down
11 changes: 7 additions & 4 deletions packages/prisma-fabbrica/src/templates/ast-tools/comment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,15 @@ function isNodeArray(x: any): x is ts.NodeArray<any> {
return Array.isArray(x);
}

export function insertLeadingBreakMarker<T extends ts.Node | ts.NodeArray<any>>(node: T): T {
if (!isNodeArray(node)) {
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 T;
return ts.addSyntheticLeadingComment(node, ts.SyntaxKind.SingleLineCommentTrivia, "%BR%", true) as S;
};
if (!isNodeArray(nodeOrNodeList)) {
return processNode(nodeOrNodeList) as T;
} else {
return node as T;
return nodeOrNodeList.map(processNode) as unknown as T;
}
}
18 changes: 7 additions & 11 deletions packages/prisma-fabbrica/src/templates/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,12 @@ export const header = (prismaClientModuleSpecifier: string) =>
export { initialize, resetSequence, registerScalarFieldValueGenerator, resetScalarFieldValueGenerator } from "@quramy/prisma-fabbrica/lib/internal";
`();

export const genericTypeDeclarations = () =>
export const importStatement = (specifier: string, prismaClientModuleSpecifier: string) =>
template.statement<ts.ImportDeclaration>`
import type { ${() => ast.identifier(specifier)} } from ${() => ast.stringLiteral(prismaClientModuleSpecifier)};
`();

export const genericDeclarations = () =>
template.sourceFile`
type BuildDataOptions = {
readonly seq: number;
Expand All @@ -105,15 +110,7 @@ export const genericTypeDeclarations = () =>
onBeforeCreate?: (createInput: TCreateInput) => void | PromiseLike<void>;
onAfterCreate?: (created: TCreated) => void | PromiseLike<void>;
};
`();
export const importStatement = (specifier: string, prismaClientModuleSpecifier: string) =>
template.statement<ts.ImportDeclaration>`
import type { ${() => ast.identifier(specifier)} } from ${() => ast.stringLiteral(prismaClientModuleSpecifier)};
`();

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

Expand Down Expand Up @@ -553,8 +550,7 @@ export function getSourceFile({
...modelNames.map(modelName => importStatement(modelName, prismaClientModuleSpecifier)),
...modelEnums.map(enumName => importStatement(enumName, prismaClientModuleSpecifier)),
...header(prismaClientModuleSpecifier).statements,
...insertLeadingBreakMarker(genericTypeDeclarations().statements),
insertLeadingBreakMarker(symbols()),
...insertLeadingBreakMarker(genericDeclarations().statements),
insertLeadingBreakMarker(modelFieldDefinitions(document.datamodel.models)),
...document.datamodel.models
.reduce((acc, model) => {
Expand Down

0 comments on commit 9a14bc0

Please sign in to comment.