Skip to content

Improve OXC Support #76

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 9 commits into
base: main
Choose a base branch
from
Open
5 changes: 5 additions & 0 deletions .changeset/fifty-dolls-divide.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'esrap': patch
---

Properly print `ParenthesizedExpression`s
5 changes: 5 additions & 0 deletions .changeset/twelve-parents-battle.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'esrap': minor
---

Add automatic handling of indexed locations.
31 changes: 29 additions & 2 deletions src/context.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,27 @@ export const indent = 2;
export const dedent = 3;
export const space = 4;

/**
* Get the line and column number from a character index in the source text.
*
* @param {number} charIndex
* @param {string} sourceText
* @returns {{ line: number, column: number }}
*/
function getLineAndColumn(charIndex, sourceText) {
const lineZeroBased = sourceText.slice(0, charIndex).split('\n');
const columnZeroBased = lineZeroBased[lineZeroBased.length - 1].length;
return {
line: lineZeroBased.length + 1,
column: columnZeroBased
};
}

export class Context {
#visitors;
#commands;
#has_newline = false;
#sourceText = undefined;

multiline = false;

Expand All @@ -19,9 +36,10 @@ export class Context {
* @param {Visitors} visitors
* @param {Command[]} commands
*/
constructor(visitors, commands = []) {
constructor(visitors, commands = [], sourceText = undefined) {
this.#visitors = visitors;
this.#commands = commands;
this.#sourceText = sourceText;
}

indent() {
Expand Down Expand Up @@ -85,7 +103,7 @@ export class Context {
}

/**
* @param {{ type: string }} node
* @param {{ type: string, start?: number, end?: number }} node
*/
visit(node) {
const visitor = this.#visitors[node.type];
Expand All @@ -104,13 +122,22 @@ export class Context {
throw new Error(message);
}

if (node.start != null && this.#sourceText) {
const { line, column } = getLineAndColumn(node.start, this.#sourceText);
this.location(line, column);
}

if (this.#visitors._) {
// @ts-ignore
this.#visitors._(node, this, (node) => visitor(node, this));
} else {
// @ts-ignore
visitor(node, this);
}
if (node.end != null && this.#sourceText) {
const { line, column } = getLineAndColumn(node.end, this.#sourceText);
this.location(line, column);
}
}

empty() {
Expand Down
3 changes: 2 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
/** @import { BaseNode, Command, Visitors, PrintOptions } from './types' */
/** @import { TSESTree } from '@typescript-eslint/types' */
import { encode } from '@jridgewell/sourcemap-codec';
import { Context, dedent, indent, margin, newline, space } from './context.js';

Expand Down Expand Up @@ -56,7 +57,7 @@ export function print(node, visitors, opts = {}) {
const commands = [];

// @ts-expect-error some nonsense I don't understand
const context = new Context(visitors, commands);
const context = new Context(visitors, commands, opts.sourceMapContent);

context.visit(node);

Expand Down
4 changes: 3 additions & 1 deletion src/languages/ts/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -1067,7 +1067,9 @@ export default (options = {}) => {

// @ts-expect-error this isn't a real node type, but Acorn produces it
ParenthesizedExpression(node, context) {
return context.visit(node.expression);
context.write('(');
context.visit(node.expression);
context.write(')');
},

PrivateIdentifier(node, context) {
Expand Down
1 change: 1 addition & 0 deletions test/esrap.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { walk } from 'zimmerframe';
import { print } from '../src/index.js';
import { acornTs, acornTsx, load } from './common.js';
import tsx from '../src/languages/tsx/index.js';
import { rm } from 'node:fs/promises';

/** @param {TSESTree.Node} ast */
function clean(ast) {
Expand Down
4 changes: 2 additions & 2 deletions test/samples/array-expressions/expected.js.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion test/samples/arrow-function-as-statement/expected.js.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion test/samples/arrow-function-needs-braces/expected.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
let a = () => ({ x } = { x: 42 });
let b = () => ({} || bar);
let c = () => ({} ? foo : bar);
let c = () => ({} ? foo : bar);
2 changes: 1 addition & 1 deletion test/samples/arrow-function-needs-braces/expected.js.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions test/samples/arrow-function-parenthesized/expected.js.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion test/samples/await-precedence/expected.js
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
await (a || b);
await c;
await c;
2 changes: 1 addition & 1 deletion test/samples/await-precedence/expected.js.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions test/samples/basic/expected.js.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion test/samples/bigint/expected.js.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion test/samples/break-continue/expected.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,4 @@ x: for (let i = 0; i < 10; i += 1) {
if (should_continue_with_label) {
continue x;
}
}
}
2 changes: 1 addition & 1 deletion test/samples/break-continue/expected.js.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion test/samples/call-expressions/expected.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,4 @@ x(
() => {
console.log('c');
}
);
);
2 changes: 1 addition & 1 deletion test/samples/call-expressions/expected.js.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion test/samples/chain-expressions/expected.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
foo?.bar.baz;
x?.(a, b, c);
x()?.();
x()?.();
2 changes: 1 addition & 1 deletion test/samples/chain-expressions/expected.js.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion test/samples/class-property/expected.js.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion test/samples/class-static-block/expected.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@ class Foo {
static {
this.abc = 1;
}
}
}
2 changes: 1 addition & 1 deletion test/samples/class-static-block/expected.js.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion test/samples/comment-after-function-argument/expected.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@ foo(
// logic goes here
},
true
); // trailing comment
); // trailing comment

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion test/samples/comment-before-import/expected.js.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion test/samples/comment-between-blocks/expected.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@ if (condition) {
} else // otherwise nope
{
console.log('nope');
}
}
2 changes: 1 addition & 1 deletion test/samples/comment-between-blocks/expected.js.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion test/samples/comment-block/expected.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@ console.log(1);

console.log(2); /* comment on same line as node */

const obj = { foo: 1, /* comment in middle of object */ bar: 2 };
const obj = { foo: 1, /* comment in middle of object */ bar: 2 };
2 changes: 1 addition & 1 deletion test/samples/comment-block/expected.js.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion test/samples/comment-inline-inserted/expected.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@
"use strict";

// comment before an inserted node
import { foo } from "wherever";
import { foo } from "wherever";
2 changes: 1 addition & 1 deletion test/samples/comment-inline-inserted/expected.js.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion test/samples/comment-inline/expected.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,4 @@ const obj = {

function bar() {
return (/*result*/ foo);
}
}
2 changes: 1 addition & 1 deletion test/samples/comment-inline/expected.js.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion test/samples/comment-interpolated/expected.js.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion test/samples/comment-mixed-trailing/expected.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@ function foo() {
/*
hey2
*/
}
}
2 changes: 1 addition & 1 deletion test/samples/comment-mixed-trailing/expected.js.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion test/samples/comment-within-parentheses/expected.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@ function foo() {
function bar() {
return (/* hey */
abc);
}
}
2 changes: 1 addition & 1 deletion test/samples/comment-within-parentheses/expected.js.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion test/samples/computed-getter/expected.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@ obj = {
get [foo]() {
return bar;
}
};
};
2 changes: 1 addition & 1 deletion test/samples/computed-getter/expected.js.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading
Loading