Skip to content

Commit

Permalink
Map ts.ThisExpression
Browse files Browse the repository at this point in the history
  • Loading branch information
knutwannheden committed Sep 25, 2024
1 parent 75fc5e7 commit d1e6677
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 5 deletions.
14 changes: 10 additions & 4 deletions openrewrite/src/javascript/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import {Comment, JavaType, JRightPadded, Space, TextComment} from '../java/tree'
import * as JS from './tree';
import {ExecutionContext, Markers, ParseError, Parser, ParserInput, randomId, SourceFile} from "../core";
import {Semicolon} from "../java";
import {FalseLiteral, NullLiteral, TrueLiteral} from "typescript";

export class JavaScriptParser extends Parser {

Expand Down Expand Up @@ -189,7 +188,7 @@ export class JavaScriptParserVisitor {
return this.mapLiteral(node, null);
}

private mapLiteral(node: ts.LiteralExpression | TrueLiteral | FalseLiteral | NullLiteral, value: any): J.Literal {
private mapLiteral(node: ts.LiteralExpression | ts.TrueLiteral | ts.FalseLiteral | ts.NullLiteral, value: any): J.Literal {
return new J.Literal(
randomId(),
this.prefix(node),
Expand Down Expand Up @@ -234,17 +233,24 @@ export class JavaScriptParserVisitor {
}

visitIdentifier(node: ts.Identifier) {
return this.mapIdentifier(node, node.text);
}

private mapIdentifier(node: ts.PrimaryExpression, name: string) {
let type = this.mapType(node);
return new J.Identifier(
randomId(),
this.prefix(node),
Markers.EMPTY,
[], // FIXME decorators
node.text,
name,
type instanceof JavaType.Variable ? type.type : type,
type instanceof JavaType.Variable ? type : null
)
return this.visitUnknown(node);
}

visitThisKeyword(node: ts.ThisExpression) {
return this.mapIdentifier(node, 'this');
}

visitPrivateIdentifier(node: ts.PrivateIdentifier) {
Expand Down
2 changes: 1 addition & 1 deletion openrewrite/test/javascript/parser/literal.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import * as JS from "../../../dist/javascript/tree";
import {connect, disconnect, javaScript, rewriteRunWithOptions} from '../testHarness';
import {JavaType} from "../../../dist/java/tree";

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

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

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

test('simple', () => {
rewriteRunWithOptions(
{normalizeIndent: false},
javaScript('this')
);
});
});

0 comments on commit d1e6677

Please sign in to comment.