diff --git a/openrewrite/src/java/tree/support_types.ts b/openrewrite/src/java/tree/support_types.ts index 402d1215..f13acfa5 100644 --- a/openrewrite/src/java/tree/support_types.ts +++ b/openrewrite/src/java/tree/support_types.ts @@ -451,7 +451,7 @@ export class JContainer { private readonly _elements: JRightPadded[]; get elements(): T[] { - return []; + return JRightPadded.getElements(this._elements); } private readonly _markers: Markers; diff --git a/openrewrite/src/javascript/parser.ts b/openrewrite/src/javascript/parser.ts index 68092fe3..7c73822f 100644 --- a/openrewrite/src/javascript/parser.ts +++ b/openrewrite/src/javascript/parser.ts @@ -498,7 +498,16 @@ export class JavaScriptParserVisitor { } visitComputedPropertyName(node: ts.ComputedPropertyName) { - return this.visitUnknown(node); + // using a `J.NewArray` is a bit of a trick; in the TS Compiler AST there is no array for this + return new J.NewArray( + randomId(), + this.prefix(node), + Markers.EMPTY, + null, + [], + new JContainer(Space.EMPTY, [this.rightPadded(this.convert(node.expression), this.suffix(node.expression))], Markers.EMPTY), + this.mapType(node) + ); } visitTypeParameter(node: ts.TypeParameterDeclaration) { diff --git a/openrewrite/test/javascript/parser/object.test.ts b/openrewrite/test/javascript/parser/object.test.ts index 2f43774a..e552d392 100644 --- a/openrewrite/test/javascript/parser/object.test.ts +++ b/openrewrite/test/javascript/parser/object.test.ts @@ -1,3 +1,5 @@ +import * as J from "../../../dist/src/java"; +import * as JS from "../../../dist/src/javascript/tree"; import {connect, disconnect, rewriteRun, typeScript} from '../testHarness'; describe('object literal mapping', () => { @@ -36,4 +38,27 @@ describe('object literal mapping', () => { typeScript('const c = { "foo": 1 }') ); }); + test('undefined key', () => { + rewriteRun( + //language=typescript + typeScript('const c = { undefined: 1 }') + ); + }); + test('computed property', () => { + rewriteRun( + //language=typescript + typeScript( + 'const c = { [ 1 + 1 ] : 1 }', + cu => { + const literal = ((cu.statements[0]).variables[0].initializer); + expect(literal.body).toBeDefined(); + const computedName = ((literal.body?.statements[0]).name); + expect(computedName).toBeDefined(); + const expression = computedName.initializer![0]; + expect(expression).toBeDefined(); + expect((expression.left).valueSource).toBe("1"); + } + ) + ); + }); }); diff --git a/openrewrite/test/javascript/testHarness.ts b/openrewrite/test/javascript/testHarness.ts index b41c4b9d..eeb3a88f 100644 --- a/openrewrite/test/javascript/testHarness.ts +++ b/openrewrite/test/javascript/testHarness.ts @@ -90,7 +90,7 @@ function sourceFile(before: string, defaultPath: string, spec?: (sourceFile: JS. null, ctx) as Iterable; if (isParseError(sourceFile)) { - throw new Error(`Parsing failed for ${sourceFile.sourcePath}: ${sourceFile.markers.findFirst(ParseExceptionResult)!.exceptionMessage}`); + throw new Error(`Parsing failed for ${sourceFile.sourcePath}: ${sourceFile.markers.findFirst(ParseExceptionResult)!.message}`); } if (!(options.allowUnknowns ?? false)) { try {