diff --git a/openrewrite/src/javascript/extensions.ts b/openrewrite/src/javascript/extensions.ts index 9670aa53..58da0c16 100644 --- a/openrewrite/src/javascript/extensions.ts +++ b/openrewrite/src/javascript/extensions.ts @@ -20,17 +20,17 @@ export function visitRightPadded(v: JavaScriptVisitor

, right: JRightPad } export function visitJsSpace

(v: JavaScriptVisitor

, 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(v: JavaScriptVisitor

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

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

, container: JContainer | null, loc: JsContainer.Location, p: P): JContainer { - throw new Error("Not yet implemented!"); + return visitContainer(v, container, JContainer.Location.LANGUAGE_EXTENSION, p); } diff --git a/openrewrite/src/javascript/parser.ts b/openrewrite/src/javascript/parser.ts index c68e0375..9baaee52 100644 --- a/openrewrite/src/javascript/parser.ts +++ b/openrewrite/src/javascript/parser.ts @@ -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); } diff --git a/openrewrite/src/javascript/tree/support_types.ts b/openrewrite/src/javascript/tree/support_types.ts index 37f7ce33..f086f6e1 100644 --- a/openrewrite/src/javascript/tree/support_types.ts +++ b/openrewrite/src/javascript/tree/support_types.ts @@ -29,6 +29,7 @@ export function isJavaScript(tree: any): tree is JS { export function JSMixin>(Base: TBase) { abstract class JSMixed extends Base implements JS { static isTree = true; + static isJava = true; static isJavaScript = true; abstract get prefix(): Space; diff --git a/openrewrite/test/javascript/parser/identifier.test.ts b/openrewrite/test/javascript/parser/identifier.test.ts new file mode 100644 index 00000000..76d852f7 --- /dev/null +++ b/openrewrite/test/javascript/parser/identifier.test.ts @@ -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') + ); + }); +}); diff --git a/openrewrite/test/javascript/parser/variableDeclarations.test.ts b/openrewrite/test/javascript/parser/variableDeclarations.test.ts index 928acb54..d21066cd 100644 --- a/openrewrite/test/javascript/parser/variableDeclarations.test.ts +++ b/openrewrite/test/javascript/parser/variableDeclarations.test.ts @@ -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 ` diff --git a/openrewrite/test/javascript/testHarness.ts b/openrewrite/test/javascript/testHarness.ts index 1a4a67b0..b0d263be 100644 --- a/openrewrite/test/javascript/testHarness.ts +++ b/openrewrite/test/javascript/testHarness.ts @@ -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; @@ -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; + if (!(options.allowUnknowns ?? false)) { + let unknowns: J.Unknown[] = []; + new class extends JavaScriptVisitor { + 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);