Skip to content

Commit

Permalink
Map ts.PropertyAccessExpression
Browse files Browse the repository at this point in the history
  • Loading branch information
knutwannheden committed Sep 26, 2024
1 parent acc05ef commit b2fea46
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 12 deletions.
31 changes: 21 additions & 10 deletions openrewrite/src/javascript/parser.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import * as ts from 'typescript';
import * as J from '../java/tree';
import {Comment, Expression, JavaType, JContainer, JRightPadded, Space, TextComment} from '../java/tree';
import {Comment, Expression, JavaType, JContainer, JLeftPadded, JRightPadded, Space, TextComment} from '../java/tree';
import * as JS from './tree';
import {
ExecutionContext,
Expand Down Expand Up @@ -173,16 +173,20 @@ export class JavaScriptParserVisitor {
return [];
}

private rightPadded<T>(t: T, trailing?: Space, markers?: Markers) {
return new JRightPadded<T>(
t,
trailing ?? Space.EMPTY,
markers ?? Markers.EMPTY
);
private rightPadded<T>(t: T, trailing: Space, markers?: Markers) {
return new JRightPadded<T>(t, trailing, markers ?? Markers.EMPTY);
}

private rightPaddedList<N extends ts.Node, T extends J.J>(nodes: ts.NodeArray<N>, trailing: (node: N) => Space, markers?: (node: N) => Markers): JRightPadded<T>[] {
return nodes.map(n => this.rightPadded(this.convert(n), trailing(n), markers?.(n)));
}

private rightPaddedList<N extends ts.Node, T extends J.J>(nodes: ts.NodeArray<N>, trailing?: (node: N) => Space, markers?: (node: N) => Markers): JRightPadded<T>[] {
return nodes.map(n => this.rightPadded(this.convert(n), trailing?.(n), markers?.(n)));
private leftPadded<T>(before: Space, t: T, markers?: Markers) {
return new JLeftPadded<T>(before, t, markers ?? Markers.EMPTY);
}

private leftPaddedList<N extends ts.Node, T extends J.J>(before: (node: N) => Space, nodes: ts.NodeArray<N>, markers?: (node: N) => Markers): JLeftPadded<T>[] {
return nodes.map(n => this.leftPadded(before(n), this.convert(n), markers?.(n)));
}

private semicolonPrefix = (n: ts.Node) => {
Expand Down Expand Up @@ -469,7 +473,14 @@ export class JavaScriptParserVisitor {
}

visitPropertyAccessExpression(node: ts.PropertyAccessExpression) {
return this.visitUnknown(node);
return new J.FieldAccess(
randomId(),
this.prefix(node),
Markers.EMPTY,
this.convert(node.expression),
this.leftPadded(this.prefix(node.getChildAt(1)), this.convert(node.name)),
this.mapType(node)
);
}

visitElementAccessExpression(node: ts.ElementAccessExpression) {
Expand Down
2 changes: 1 addition & 1 deletion openrewrite/test/javascript/parser/arrayLiteral.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {connect, disconnect, javaScript, rewriteRun} from '../testHarness';

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

Expand Down
2 changes: 1 addition & 1 deletion openrewrite/test/javascript/parser/call.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {connect, disconnect, javaScript, rewriteRun} from '../testHarness';

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

Expand Down
20 changes: 20 additions & 0 deletions openrewrite/test/javascript/parser/propertyAccess.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import {connect, disconnect, javaScript, rewriteRun} from '../testHarness';

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

test('with array literal receiver', () => {
rewriteRun(
//language=typescript
javaScript('[1] . length')
);
});

test('with array literal receiver', () => {
rewriteRun(
//language=typescript
javaScript('foo . bar . baz')
);
});
});

0 comments on commit b2fea46

Please sign in to comment.