Skip to content

Commit

Permalink
Map ts.ArrayLiteralExpression
Browse files Browse the repository at this point in the history
  • Loading branch information
knutwannheden committed Sep 26, 2024
1 parent af9442a commit e2ccc42
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 2 deletions.
12 changes: 10 additions & 2 deletions openrewrite/src/javascript/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -432,7 +432,15 @@ export class JavaScriptParserVisitor {
}

visitArrayLiteralExpression(node: ts.ArrayLiteralExpression) {
return this.visitUnknown(node);
return new J.NewArray(
randomId(),
this.prefix(node),
Markers.EMPTY,
null,
[],
this.mapArguments(node.getChildren()),
this.mapType(node)
);
}

visitObjectLiteralExpression(node: ts.ObjectLiteralExpression) {
Expand Down Expand Up @@ -1090,7 +1098,7 @@ export class JavaScriptParserVisitor {
return null;
}

private mapArguments(nodes: ts.Node[]): JContainer<Expression> {
private mapArguments(nodes: readonly ts.Node[]): JContainer<Expression> {
if (nodes.length === 0) {
return JContainer.empty();
}
Expand Down
34 changes: 34 additions & 0 deletions openrewrite/test/javascript/parser/arrayLiteral.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import {connect, disconnect, javaScript, rewriteRun} from '../testHarness';

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

test('empty', () => {
rewriteRun(
//language=typescript
javaScript('[]')
);
});

test('single', () => {
rewriteRun(
//language=typescript
javaScript('[ 1 ]')
);
});

test('two', () => {
rewriteRun(
//language=typescript
javaScript('[ 1 , 2 ]')
);
});

test('trailing comma', () => {
rewriteRun(
//language=typescript
javaScript('[ 1 , 2 , ]')
);
});
});

0 comments on commit e2ccc42

Please sign in to comment.