Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add parser option to getSchema #26

Merged
merged 4 commits into from
Nov 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/getConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@ import {
type LilconfigResult as ConfigResultRaw,
} from 'lilconfig';

export type PrismaAstParserConfig = Pick<IParserConfig, 'nodeLocationTracking'>;
export interface PrismaAstConfig {
parser: Pick<IParserConfig, 'nodeLocationTracking'>;
parser: PrismaAstParserConfig;
}

type ConfigResult<T> = Omit<ConfigResultRaw, 'config'> & {
Expand Down
17 changes: 13 additions & 4 deletions src/getSchema.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { PrismaLexer } from './lexer';
import { PrismaVisitor } from './visitor';
import { parser } from './parser';
import { PrismaVisitor, defaultVisitor } from './visitor';
import type { CstNodeLocation } from 'chevrotain';
import { PrismaParser, defaultParser } from './parser';

/**
* Parses a string containing a prisma schema's source code and returns an
Expand All @@ -14,12 +14,21 @@ import type { CstNodeLocation } from 'chevrotain';
* // ... make changes to schema object ...
* const changedSource = printSchema(schema)
* */
export function getSchema(source: string): Schema {
export function getSchema(
source: string,
options?: {
parser: PrismaParser;
visitor: PrismaVisitor;
}
): Schema {
const lexingResult = PrismaLexer.tokenize(source);

const parser = options?.parser ?? defaultParser;
parser.input = lexingResult.tokens;
const cstNode = parser.schema();
if (parser.errors.length > 0) throw parser.errors[0];
const visitor = new PrismaVisitor();

const visitor = options?.visitor ?? defaultVisitor;
return visitor.visit(cstNode);
}

Expand Down
2 changes: 2 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,5 @@ export * from './printSchema';
export * from './PrismaSchemaBuilder';
export type { PrismaAstConfig } from './getConfig';
export type { CstNodeLocation } from 'chevrotain';
export { VisitorClassFactory } from './visitor';
export { PrismaParser } from './parser';
11 changes: 7 additions & 4 deletions src/parser.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
import { CstParser } from 'chevrotain';
import getConfig from './getConfig';
import getConfig, { PrismaAstParserConfig } from './getConfig';
import * as lexer from './lexer';

type ComponentType = 'datasource' | 'generator' | 'model' | 'view' | 'enum';
export class PrismaParser extends CstParser {
constructor() {
super(lexer.multiModeTokens, getConfig().parser);
readonly config: PrismaAstParserConfig;

constructor(config: PrismaAstParserConfig) {
super(lexer.multiModeTokens, config);
this.performSelfAnalysis();
this.config = config;
}

private break = this.RULE('break', () => {
Expand Down Expand Up @@ -229,4 +232,4 @@ export class PrismaParser extends CstParser {
});
}

export const parser = new PrismaParser();
export const defaultParser = new PrismaParser(getConfig().parser);
4 changes: 0 additions & 4 deletions src/schemaUtils.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import type { CstNode, IToken } from 'chevrotain';
import getConfig from './getConfig';
import * as schema from './getSchema';

const schemaObjects = ['model', 'view'];
Expand Down Expand Up @@ -28,9 +27,6 @@ export function appendLocationData<T extends Record<string, unknown>>(
data: T,
...tokens: IToken[]
): T {
const { parser } = getConfig();
if (parser.nodeLocationTracking === 'none') return data;

const location = tokens.reduce((memo, token) => {
if (!token) return memo;

Expand Down