Skip to content

Commit

Permalink
Map ts.VoidExpression and dynamic imports
Browse files Browse the repository at this point in the history
Also fixes bug in `binarySearch()` function.
  • Loading branch information
knutwannheden committed Sep 27, 2024
1 parent c239c0b commit 179b69c
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 50 deletions.
57 changes: 12 additions & 45 deletions openrewrite/src/javascript/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import {
SourceFile
} from "../core";
import {Semicolon, TrailingComma} from "../java";
import {getNextSibling} from "./parserUtils";
import {binarySearch, compareTextSpans, getNextSibling, TextSpan} from "./parserUtils";

export class JavaScriptParser extends Parser {

Expand Down Expand Up @@ -104,8 +104,6 @@ for (const [key, value] of Object.entries(ts.SyntaxKind)) {
}
}

type TextSpan = [number, number];

// noinspection JSUnusedGlobalSymbols
export class JavaScriptParserVisitor {
constructor(
Expand Down Expand Up @@ -606,7 +604,12 @@ export class JavaScriptParserVisitor {
}

visitVoidExpression(node: ts.VoidExpression) {
return this.visitUnknown(node);
return new JS.Void(
randomId(),
this.prefix(node),
Markers.EMPTY,
this.convert(node.expression)
);
}

visitAwaitExpression(node: ts.AwaitExpression) {
Expand Down Expand Up @@ -1003,6 +1006,11 @@ export class JavaScriptParserVisitor {
return this.visitUnknown(node);
}

visitImportKeyword(node: ts.ImportExpression) {
// this is used for dynamic imports as in `await import('foo')`
return this.mapIdentifier(node, 'import');
}

visitImportDeclaration(node: ts.ImportDeclaration) {
const children = node.getChildren();
const _default = !!node.importClause?.name;
Expand Down Expand Up @@ -1489,44 +1497,3 @@ function prefixFromNode(node: ts.Node, sourceFile: ts.SourceFile): Space {
// Step 4: Return the Space object with comments and leading whitespace
return new Space(comments, whitespace.length > 0 ? whitespace : null);
}

function compareTextSpans(span1: TextSpan, span2: TextSpan) {
// First, compare the first elements
if (span1[0] < span2[0]) {
return -1;
}
if (span1[0] > span2[0]) {
return 1;
}

// If the first elements are equal, compare the second elements
if (span1[1] < span2[1]) {
return -1;
}
if (span1[1] > span2[1]) {
return 1;
}

// If both elements are equal, the tuples are considered equal
return 0;
}

function binarySearch<T>(arr: T[], target: T, compare: (a: T, b: T) => number) {
let low = 0;
let high = arr.length - 1;

while (low <= high) {
const mid = Math.floor((low + high) / 2);

const comparison = compare(arr[mid], target);

if (comparison === 0) {
return mid; // Element found, return index
} else if (comparison < 0) {
low = mid + 1; // Search the right half
} else {
high = mid - 1; // Search the left half
}
}
return -1; // Element not found
}
51 changes: 46 additions & 5 deletions openrewrite/src/javascript/parserUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,8 @@ export function getNextSibling(node: ts.Node): ts.Node | null {
return null;
}

// Return the last child of the parent's previous sibling
const siblings = parentNextSibling.getChildren();
return siblings[0] || null;
// Return the first child of the parent's next sibling
return parentNextSibling.getChildCount() > 0 ? parentNextSibling.getChildAt(0) : parentNextSibling;
}

// Otherwise, return the next sibling
Expand Down Expand Up @@ -112,8 +111,7 @@ export function getPreviousSibling(node: ts.Node): ts.Node | null {
}

// Return the last child of the parent's previous sibling
const siblings = parentPreviousSibling.getChildren();
return siblings[siblings.length - 1] || null;
return parentPreviousSibling.getChildCount() > 0 ? parentPreviousSibling.getLastToken()! : parentPreviousSibling;
}

// Otherwise, return the previous sibling
Expand All @@ -135,3 +133,46 @@ function findContainingSyntaxList(node: ts.Node): ts.SyntaxList | null {

return null;
}

export type TextSpan = [number, number];

export function compareTextSpans(span1: TextSpan, span2: TextSpan) {
// First, compare the first elements
if (span1[0] < span2[0]) {
return -1;
}
if (span1[0] > span2[0]) {
return 1;
}

// If the first elements are equal, compare the second elements
if (span1[1] < span2[1]) {
return -1;
}
if (span1[1] > span2[1]) {
return 1;
}

// If both elements are equal, the tuples are considered equal
return 0;
}

export function binarySearch<T>(arr: T[], target: T, compare: (a: T, b: T) => number) {
let low = 0;
let high = arr.length - 1;

while (low <= high) {
const mid = Math.floor((low + high) / 2);

const comparison = compare(arr[mid], target);

if (comparison === 0) {
return mid; // Element found, return index
} else if (comparison < 0) {
low = mid + 1; // Search the right half
} else {
high = mid - 1; // Search the left half
}
}
return ~low; // Element not found, return bitwise complement of the insertion point
}
13 changes: 13 additions & 0 deletions openrewrite/test/javascript/parser/await.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import {connect, disconnect, rewriteRun, typeScript} from '../testHarness';

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

test('simple', () => {
rewriteRun(
//language=typescript
typeScript('await 1')
);
});
});

0 comments on commit 179b69c

Please sign in to comment.