Skip to content

Commit

Permalink
Map ts.Identifier
Browse files Browse the repository at this point in the history
  • Loading branch information
knutwannheden committed Sep 25, 2024
1 parent bcd7851 commit 75fc5e7
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 6 deletions.
8 changes: 4 additions & 4 deletions openrewrite/src/javascript/extensions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,17 @@ export function visitRightPadded<P, T>(v: JavaScriptVisitor<P>, right: JRightPad
}

export function visitJsSpace<P>(v: JavaScriptVisitor<P>, space: Space | null, loc: JsSpace.Location, p: P): Space {
throw new Error("Not yet implemented!");
return visitSpace(v, space, Space.Location.LANGUAGE_EXTENSION, p);
}

export function visitJsLeftPadded<P, T>(v: JavaScriptVisitor<P>, left: JLeftPadded<T> | null, loc: JsLeftPadded.Location, p: P): JLeftPadded<T> {
throw new Error("Not yet implemented!");
return visitLeftPadded(v, left, JLeftPadded.Location.LANGUAGE_EXTENSION, p);
}

export function visitJsRightPadded<P, T>(v: JavaScriptVisitor<P>, right: JRightPadded<T> | null, loc: JsRightPadded.Location, p: P): JRightPadded<T> {
throw new Error("Not yet implemented!");
return visitRightPadded(v, right, JRightPadded.Location.LANGUAGE_EXTENSION, p);
}

export function visitJsContainer<P, T>(v: JavaScriptVisitor<P>, container: JContainer<T> | null, loc: JsContainer.Location, p: P): JContainer<T> {
throw new Error("Not yet implemented!");
return visitContainer(v, container, JContainer.Location.LANGUAGE_EXTENSION, p);
}
10 changes: 10 additions & 0 deletions openrewrite/src/javascript/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,16 @@ export class JavaScriptParserVisitor {
}

visitIdentifier(node: ts.Identifier) {
let type = this.mapType(node);
return new J.Identifier(
randomId(),
this.prefix(node),
Markers.EMPTY,
[], // FIXME decorators
node.text,
type instanceof JavaType.Variable ? type.type : type,
type instanceof JavaType.Variable ? type : null
)
return this.visitUnknown(node);
}

Expand Down
1 change: 1 addition & 0 deletions openrewrite/src/javascript/tree/support_types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ export function isJavaScript(tree: any): tree is JS {
export function JSMixin<TBase extends Constructor<Object>>(Base: TBase) {
abstract class JSMixed extends Base implements JS {
static isTree = true;
static isJava = true;
static isJavaScript = true;

abstract get prefix(): Space;
Expand Down
13 changes: 13 additions & 0 deletions openrewrite/test/javascript/parser/identifier.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import {connect, disconnect, javaScript, rewriteRunWithOptions} from '../testHarness';

describe('literal mapping', () => {
beforeAll(() => connect());
afterAll(() => disconnect());

test('simple', () => {
rewriteRunWithOptions(
{normalizeIndent: false},
javaScript('foo')
);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ describe('variable declaration mapping', () => {

test('const', () => {
rewriteRunWithOptions(
{ validatePrintIdempotence: false},
// FIXME not quite there yet
{validatePrintIdempotence: false, allowUnknowns: true},
javaScript(
//language=javascript
`
Expand Down
16 changes: 15 additions & 1 deletion openrewrite/test/javascript/testHarness.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
import {Cursor, PrinterFactory, PrintOutputCapture, SourceFile} from '../../dist/core';
import * as J from "../../dist/java/tree";
import * as JS from "../../dist/javascript/tree";
import dedent from "dedent";
import {ReceiverContext, RemotePrinterFactory, RemotingContext, SenderContext} from "@openrewrite/rewrite-remote";
import * as deser from "@openrewrite/rewrite-remote/java/serializers";
import {JavaScriptReceiver, JavaScriptSender} from "@openrewrite/rewrite-remote/javascript";
import net from "net";
import {JavaScriptParser} from "../../dist/javascript";
import {JavaScriptParser, JavaScriptVisitor} from "../../dist/javascript";

export interface RewriteTestOptions {
normalizeIndent?: boolean
validatePrintIdempotence?: boolean
allowUnknowns?: boolean
}

export type SourceSpec = (options: RewriteTestOptions) => void;
Expand Down Expand Up @@ -70,6 +72,18 @@ const parser = JavaScriptParser.builder().build();
export function javaScript(before: string, spec?: (sourceFile: JS.CompilationUnit) => void): SourceSpec {
return (options: RewriteTestOptions) => {
const [sourceFile] = parser.parseStrings(options.normalizeIndent ?? true ? dedent(before) : before) as Iterable<JS.CompilationUnit>;
if (!(options.allowUnknowns ?? false)) {
let unknowns: J.Unknown[] = [];
new class extends JavaScriptVisitor<number> {
visitUnknown(unknown: J.Unknown, p: number): J.J | null {
unknowns.push(unknown);
return unknown;
}
}().visit(sourceFile, 0);
if (unknowns.length != 0) {
throw new Error("No J.Unknown instances were expected: " + unknowns.map(u => u.source.text));
}
}
if (options.validatePrintIdempotence ?? true) {
let printed = print(sourceFile);
expect(printed).toBe(before);
Expand Down

0 comments on commit 75fc5e7

Please sign in to comment.