Skip to content

Commit

Permalink
Add Parser.Builder
Browse files Browse the repository at this point in the history
  • Loading branch information
knutwannheden committed Sep 23, 2024
1 parent 63afefa commit 321b728
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 4 deletions.
4 changes: 2 additions & 2 deletions openrewrite/src/core/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ export abstract class Parser {
}

export namespace Parser {
abstract class Builder {
export abstract class Builder {
protected _sourceFileType: any;

get sourceFileType(): any {
Expand All @@ -120,7 +120,7 @@ export namespace Parser {
}
}

function requirePrintEqualsInput(
export function requirePrintEqualsInput(
parser: Parser,
sourceFile: SourceFile,
parserInput: ParserInput,
Expand Down
13 changes: 13 additions & 0 deletions openrewrite/src/javascript/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {Expression, JRightPadded, Space, Statement} from "../java/tree";
import {Node} from "typescript";

export class JavaScriptParser extends Parser {

parseInputs(inputs: Iterable<ParserInput>, relativeTo: string | null, ctx: ExecutionContext): Iterable<SourceFile> {
const inputsArray = Array.from(inputs);
const compilerOptions: ts.CompilerOptions = {
Expand Down Expand Up @@ -69,6 +70,18 @@ export class JavaScriptParser extends Parser {
sourcePathFromSourceText(prefix: string, sourceCode: string): string {
return prefix + "/source.js";
}

static builder(): JavaScriptParser.Builder {
return new JavaScriptParser.Builder();
}
}

export namespace JavaScriptParser {
export class Builder extends Parser.Builder {
build(): JavaScriptParser {
return new JavaScriptParser();
}
}
}

class ParserVisitor {
Expand Down
4 changes: 2 additions & 2 deletions openrewrite/test/core/tree.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,13 @@ describe('tree utils', () => {
});

test('parse', () => {
const parser = new JavaScriptParser();
const parser = JavaScriptParser.builder().build();
const [sourceFile] = parser.parseInputs([new ParserInput('foo.ts', null, true, () => Buffer.from('1', 'utf8'))], null, new InMemoryExecutionContext());
console.log(sourceFile);
});

test('parse strings', () => {
const parser = new JavaScriptParser();
const parser = JavaScriptParser.builder().build();
const [sourceFile] = parser.parseStrings('const c = 1;');
console.log(sourceFile);
});
Expand Down

0 comments on commit 321b728

Please sign in to comment.