Skip to content

Commit

Permalink
Fix visitor bug and implement ts.NumericLiteral mapping
Browse files Browse the repository at this point in the history
  • Loading branch information
knutwannheden committed Sep 24, 2024
1 parent bfab21a commit a232956
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 14 deletions.
5 changes: 5 additions & 0 deletions openrewrite/src/java/tree/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,9 @@ export namespace JavaType {
export class Method implements JavaType {
returnType: JavaType = null!;
}

@LstType("org.openrewrite.java.tree.JavaType$Unknown")
export class Unknown implements JavaType {
static INSTANCE = new Unknown();
}
}
47 changes: 35 additions & 12 deletions openrewrite/src/javascript/parser.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import * as ts from 'typescript';
import * as J from '../java/tree';
import {Comment, JRightPadded, Space, TextComment} from '../java/tree';
import {Comment, JavaType, JRightPadded, Space, TextComment} from '../java/tree';
import * as JS from './tree';
import {ExecutionContext, Markers, ParseError, Parser, ParserInput, randomId, SourceFile} from "../core";

Expand Down Expand Up @@ -84,27 +84,29 @@ export namespace JavaScriptParser {
}
}

// we use this instead of `ts.SyntaxKind[node.kind]` because the numeric values are not unique and we want
// the first one rather than the last one, as the last ones are things like `FirstToken`, `LastToken`, etc.
const visitMethodMap = new Map<number, string>();
for (const [key, value] of Object.entries(ts.SyntaxKind)) {
if (typeof value === 'number' && !visitMethodMap.has(value)) {
visitMethodMap.set(value, 'visit' + key);
}
}

// noinspection JSUnusedGlobalSymbols
export class JavaScriptParserVisitor {
constructor(private readonly sourceFile: ts.SourceFile, private readonly typeChecker: ts.TypeChecker) {
}

visit(node: ts.Node): any {
const member = this[(`visit${ts.SyntaxKind[node.kind]}` as keyof JavaScriptParserVisitor)];
const member = this[(visitMethodMap.get(node.kind) as keyof JavaScriptParserVisitor)];
if (typeof member === 'function') {
return member.bind(this)(node as any);
} else {
return this.visitUnknown(node);
}
}

private prefix(node: ts.Node) {
if (node.getLeadingTriviaWidth(this.sourceFile) == 0) {
return Space.EMPTY;
}
return prefixFromNode(node, this.sourceFile);
// return Space.format(this.sourceFile.text, node.getFullStart(), node.getFullStart() + node.getLeadingTriviaWidth());
}

visitSourceFile(node: ts.SourceFile): JS.CompilationUnit {
return new JS.CompilationUnit(
randomId(),
Expand Down Expand Up @@ -151,7 +153,7 @@ export class JavaScriptParserVisitor {

private semicolonPrefix = (n: ts.Node) => {
const last = n.getLastToken();
return last?.kind == ts.SyntaxKind.SemicolonToken ? prefixFromNode(last, this.sourceFile) : Space.EMPTY;
return last?.kind == ts.SyntaxKind.SemicolonToken ? this.prefix(last) : Space.EMPTY;
}


Expand All @@ -161,7 +163,15 @@ export class JavaScriptParserVisitor {
}

visitNumericLiteral(node: ts.NumericLiteral) {
return this.visitUnknown(node)
return new J.Literal(
randomId(),
this.prefix(node),
Markers.EMPTY,
node.text, // FIXME value not in AST
node.text,
null,
this.mapType(node) as JavaType.Primitive
);
}

visitBigIntLiteral(node: ts.BigIntLiteral) {
Expand Down Expand Up @@ -973,6 +983,19 @@ export class JavaScriptParserVisitor {
visitSyntheticReferenceExpression(node: ts.Node) {
return this.visitUnknown(node);
}

private prefix(node: ts.Node) {
if (node.getLeadingTriviaWidth(this.sourceFile) == 0) {
return Space.EMPTY;
}
// FIXME either mark ranges as consumed or implement cursor tracking
return prefixFromNode(node, this.sourceFile);
// return Space.format(this.sourceFile.text, node.getFullStart(), node.getFullStart() + node.getLeadingTriviaWidth());
}

private mapType(node: ts.Expression): JavaType | null {
return JavaType.Unknown.INSTANCE;
}
}

function prefixFromNode(node: ts.Node, sourceFile: ts.SourceFile): Space {
Expand Down
7 changes: 5 additions & 2 deletions openrewrite/test/javascript/parser.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import {JavaScriptParser} from "../../src/javascript";
import * as J from "../../src/java/tree";
import * as JS from "../../src/javascript/tree";
import dedent from "dedent";
import {JRightPadded} from "../../src/java/tree";

describe('Parser API', () => {
const parser = JavaScriptParser.builder().build();
Expand Down Expand Up @@ -35,7 +34,11 @@ describe('LST mapping', () => {
javaScript('1', sourceFile => {
expect(sourceFile).toBeDefined();
expect(sourceFile.statements).toHaveLength(1);
expect(sourceFile.statements[0]).toBeInstanceOf(JS.ExpressionStatement);
let statement = sourceFile.statements[0];
expect(statement).toBeInstanceOf(JS.ExpressionStatement);
let expression = (statement as JS.ExpressionStatement).expression;
expect(expression).toBeInstanceOf(J.Literal);
expect((expression as J.Literal).valueSource).toBe('1');
}));
});

Expand Down

0 comments on commit a232956

Please sign in to comment.