Skip to content

Commit

Permalink
Also re-export tree package
Browse files Browse the repository at this point in the history
Client code can still decide to add individual imports for some types (e.g. `Space`).
  • Loading branch information
knutwannheden committed Sep 28, 2024
1 parent 2a74c10 commit 43bf325
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 18 deletions.
1 change: 1 addition & 0 deletions openrewrite/src/java/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
export * from './markers';
export * from './tree';
export * from './visitor';
1 change: 1 addition & 0 deletions openrewrite/src/javascript/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export * from './markers';
export * from './parser';
export * from './tree';
export * from './visitor';
32 changes: 14 additions & 18 deletions openrewrite/src/javascript/parser.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,13 @@
import * as ts from 'typescript';
import * as J from '../java/tree';
import * as J from '../java';
import {
Comment,
Expression,
JavaType,
JContainer,
JLeftPadded,
JRightPadded,
Space,
Statement,
TextComment, TypeTree
} from '../java/tree';
import * as JS from './tree';
} from '../java';
import * as JS from '.';
import {
ExecutionContext,
Markers,
Expand Down Expand Up @@ -298,7 +294,7 @@ export class JavaScriptParserVisitor {
);
}

private mapExtends(node: ts.ClassDeclaration): JLeftPadded<TypeTree> | null {
private mapExtends(node: ts.ClassDeclaration): JLeftPadded<J.TypeTree> | null {
if (node.heritageClauses == undefined || node.heritageClauses.length == 0) {
return null;
}
Expand All @@ -310,13 +306,13 @@ export class JavaScriptParserVisitor {
return null;
}

private mapImplements(node: ts.ClassDeclaration): JContainer<TypeTree> | null {
private mapImplements(node: ts.ClassDeclaration): JContainer<J.TypeTree> | null {
if (node.heritageClauses == undefined || node.heritageClauses.length == 0) {
return null;
}
for (let heritageClause of node.heritageClauses) {
if (heritageClause.token == ts.SyntaxKind.ImplementsKeyword) {
const _implements: JRightPadded<TypeTree>[] = [];
const _implements: JRightPadded<J.TypeTree>[] = [];
for (let type of heritageClause.types) {
_implements.push(this.rightPadded(this.visit(type), this.suffix(type)));
}
Expand Down Expand Up @@ -645,7 +641,7 @@ export class JavaScriptParserVisitor {
const prefix = this.prefix(nodes[0]);
let statementList = nodes[1] as ts.SyntaxList;

const statements: JRightPadded<Statement>[] = this.rightPaddedSeparatedList(
const statements: JRightPadded<J.Statement>[] = this.rightPaddedSeparatedList(
[...statementList.getChildren()],
ts.SyntaxKind.CommaToken,
(nodes, i) => i == nodes.length -2 && nodes[i + 1].kind == ts.SyntaxKind.CommaToken ? Markers.build([new TrailingComma(randomId(), this.prefix(nodes[i + 1]))]) : Markers.EMPTY
Expand Down Expand Up @@ -678,10 +674,10 @@ export class JavaScriptParserVisitor {

visitCallExpression(node: ts.CallExpression) {
const prefix = this.prefix(node);
let select: JRightPadded<Expression> | null;
let select: JRightPadded<J.Expression> | null;
let name: J.Identifier;
if (ts.isPropertyAccessExpression(node.expression)) {
select = this.rightPadded(this.convert<Expression>(node.expression.expression), this.prefix(node.expression.getChildAt(1)));
select = this.rightPadded(this.convert<J.Expression>(node.expression.expression), this.prefix(node.expression.getChildAt(1)));
name = this.convert(node.expression.name);
} else {
select = null;
Expand Down Expand Up @@ -1612,7 +1608,7 @@ export class JavaScriptParserVisitor {
return null;
}

private mapArguments(nodes: readonly ts.Node[]): JContainer<Expression> {
private mapArguments(nodes: readonly ts.Node[]): JContainer<J.Expression> {
if (nodes.length === 0) {
return JContainer.empty();
}
Expand All @@ -1621,7 +1617,7 @@ export class JavaScriptParserVisitor {
let argList = nodes[1] as ts.SyntaxList;
let childCount = argList.getChildCount();

const args: JRightPadded<Expression>[] = [];
const args: JRightPadded<J.Expression>[] = [];
if (childCount === 0) {
args.push(this.rightPadded(
new J.Empty(randomId(), Space.EMPTY, Markers.EMPTY),
Expand Down Expand Up @@ -1662,7 +1658,7 @@ export class JavaScriptParserVisitor {
const prefix = this.prefix(nodes[0]);
let statementList = nodes[1] as ts.SyntaxList;

const statements: JRightPadded<Statement>[] = this.rightPaddedSeparatedList(
const statements: JRightPadded<J.Statement>[] = this.rightPaddedSeparatedList(
[...statementList.getChildren()],
ts.SyntaxKind.SemicolonToken,
(nodes, i) => nodes[i].getLastToken()?.kind == ts.SyntaxKind.SemicolonToken ? Markers.build([new Semicolon(randomId())]) : Markers.EMPTY
Expand All @@ -1680,7 +1676,7 @@ export class JavaScriptParserVisitor {
}

function prefixFromNode(node: ts.Node, sourceFile: ts.SourceFile): Space {
const comments: Comment[] = [];
const comments: J.Comment[] = [];
const text = sourceFile.getFullText();
const nodeStart = node.getFullStart();

Expand All @@ -1705,7 +1701,7 @@ function prefixFromNode(node: ts.Node, sourceFile: ts.SourceFile): Space {
const commentBody = text.slice(commentStart, commentEnd); // Extract comment body
const suffix = text.slice(end, suffixEnd); // Extract suffix (whitespace after comment)

comments.push(new TextComment(isMultiline, commentBody, suffix, Markers.EMPTY));
comments.push(new J.TextComment(isMultiline, commentBody, suffix, Markers.EMPTY));
});

// Step 3: Extract leading whitespace (before the first comment)
Expand Down

0 comments on commit 43bf325

Please sign in to comment.