Skip to content

Commit

Permalink
Fix computed property names in object literals
Browse files Browse the repository at this point in the history
  • Loading branch information
knutwannheden committed Sep 29, 2024
1 parent c40eab9 commit 12ff035
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 3 deletions.
2 changes: 1 addition & 1 deletion openrewrite/src/java/tree/support_types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -451,7 +451,7 @@ export class JContainer<T> {
private readonly _elements: JRightPadded<T>[];

get elements(): T[] {
return [];
return JRightPadded.getElements(this._elements);
}

private readonly _markers: Markers;
Expand Down
11 changes: 10 additions & 1 deletion openrewrite/src/javascript/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
25 changes: 25 additions & 0 deletions openrewrite/test/javascript/parser/object.test.ts
Original file line number Diff line number Diff line change
@@ -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', () => {
Expand Down Expand Up @@ -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 = (<J.NewClass>(<J.VariableDeclarations>cu.statements[0]).variables[0].initializer);
expect(literal.body).toBeDefined();
const computedName = (<J.NewArray>(<JS.PropertyAssignment>literal.body?.statements[0]).name);
expect(computedName).toBeDefined();
const expression = <J.Binary>computedName.initializer![0];
expect(expression).toBeDefined();
expect((<J.Literal>expression.left).valueSource).toBe("1");
}
)
);
});
});
2 changes: 1 addition & 1 deletion openrewrite/test/javascript/testHarness.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ function sourceFile(before: string, defaultPath: string, spec?: (sourceFile: JS.
null,
ctx) as Iterable<SourceFile>;
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 {
Expand Down

0 comments on commit 12ff035

Please sign in to comment.