Skip to content

Commit

Permalink
Merge branch 'master' into feature/improve-support-of-export-namespac…
Browse files Browse the repository at this point in the history
…e-specifiers
  • Loading branch information
coderaiser authored Apr 5, 2024
2 parents ad086f3 + 51d5c89 commit aba90c6
Show file tree
Hide file tree
Showing 20 changed files with 1,321 additions and 1,920 deletions.
27 changes: 14 additions & 13 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,28 +2,29 @@ name: CI

on:
push:
branches: [ master ]
branches: [master]
pull_request:
branches: [ master ]
branches: [master]

jobs:
test:
name: Test on node ${{ matrix.node_version }} and ${{ matrix.os }}
runs-on: ${{ matrix.os }}
strategy:
matrix:
node_version: ['10.x', '12.x', '14.x', '15.x', '16.x']
node_version: ["12.x", "14.x", "16.x", "18.x", "19.x", "20.x"]
os: [ubuntu-latest]

steps:
- uses: actions/checkout@v2
- name: Use Node.js ${{ matrix.node_version }}
uses: actions/setup-node@v1
with:
node-version: ${{ matrix.node_version }}
- uses: actions/checkout@v2
- name: Use Node.js ${{ matrix.node_version }}
uses: actions/setup-node@v1
with:
node-version: ${{ matrix.node_version }}

- name: npm install, build and test
run: |
npm install
npm run build --if-present
npm test
- name: npm install, build and test
run: |
npm install
npm run build --if-present
npm run format:check
npm test
6 changes: 6 additions & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
*.d.ts
test/data/
/example

# output files
/main.js
/lib/**/*.js
/parsers/**/*.js
/test/**/*.js
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ const ast = recast.parse(code);

Now do _whatever_ you want to `ast`. Really, anything at all!

See [ast-types](https://github.com/benjamn/ast-types) (especially the [def/core.ts](https://github.com/benjamn/ast-types/blob/master/def/core.ts)) module for a thorough overview of the `ast` API.
See [ast-types](https://github.com/benjamn/ast-types) (especially the [def/core.ts](https://github.com/benjamn/ast-types/blob/master/src/def/core.ts)) module for a thorough overview of the `ast` API.

```js
// Grab a reference to the function declaration we just parsed.
Expand Down Expand Up @@ -170,7 +170,7 @@ const tsAst = recast.parse(source, {

**Note:** Some of these parsers import npm packages that Recast does not directly depend upon, so please be aware you may have to run `npm install @babel/parser` to use the `typescript`, `flow`, or `babel` parsers, or `npm install acorn` to use the `acorn` parser. Only Esprima is installed by default when Recast is installed.

After calling `recast.parse`, if you're going to transform the AST, make sure that the `.original` property is preserved. With Babel, for instance, if you call `transformFromAST`, you must pass `cloneInputAst: false` in its options. ([More detail](https://github.com/babel/babel/issues/12882).
After calling `recast.parse`, if you're going to transform the AST, make sure that the `.original` property is preserved. With Babel, for instance, if you call `transformFromAST`, you must pass `cloneInputAst: false` in its options. ([More detail](https://github.com/babel/babel/issues/12882)).

## Source maps

Expand Down
20 changes: 14 additions & 6 deletions lib/comments.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import assert from "assert";
import invariant from "tiny-invariant";
import * as types from "ast-types";
const n = types.namedTypes;
const isArray = types.builtInTypes.array;
Expand Down Expand Up @@ -147,9 +147,9 @@ export function attach(comments: any[], ast: any, lines: any) {
if (tieCount > 0) {
const lastTie = tiesToBreak[tieCount - 1];

assert.strictEqual(
lastTie.precedingNode === comment.precedingNode,
lastTie.followingNode === comment.followingNode,
invariant(
(lastTie.precedingNode === comment.precedingNode) ===
(lastTie.followingNode === comment.followingNode),
);

if (lastTie.followingNode !== comment.followingNode) {
Expand Down Expand Up @@ -206,8 +206,8 @@ function breakTies(tiesToBreak: any[], lines: any) {
let comment;
for (; indexOfFirstLeadingComment > 0; --indexOfFirstLeadingComment) {
comment = tiesToBreak[indexOfFirstLeadingComment - 1];
assert.strictEqual(comment.precedingNode, pn);
assert.strictEqual(comment.followingNode, fn);
invariant(comment.precedingNode === pn);
invariant(comment.followingNode === fn);

const gap = lines.sliceString(comment.loc.end, gapEndPos);
if (/\S/.test(gap)) {
Expand All @@ -229,6 +229,14 @@ function breakTies(tiesToBreak: any[], lines: any) {
++indexOfFirstLeadingComment;
}

if (indexOfFirstLeadingComment) {
const { enclosingNode } = tiesToBreak[indexOfFirstLeadingComment - 1];

if (enclosingNode?.type === "CallExpression") {
--indexOfFirstLeadingComment;
}
}

tiesToBreak.forEach(function (comment, i) {
if (i < indexOfFirstLeadingComment) {
addTrailingComment(pn, comment);
Expand Down
37 changes: 24 additions & 13 deletions lib/fast-path.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import assert from "assert";
import invariant from "tiny-invariant";
import * as types from "ast-types";
import * as util from "./util";

Expand Down Expand Up @@ -52,7 +52,7 @@ interface FastPathConstructor {
}

const FastPath = function FastPath(this: FastPathType, value: any) {
assert.ok(this instanceof FastPath);
invariant(this instanceof FastPath);
this.stack = [value];
} as any as FastPathConstructor;

Expand Down Expand Up @@ -355,6 +355,17 @@ FPp.needsParens = function (assumeExpressionContext) {

if (!parent) return false;

// Wrap e.g. `-1` in parentheses inside `(-1) ** 2`.
if (
node.type === "UnaryExpression" &&
parent.type === "BinaryExpression" &&
name === "left" &&
parent.left === node &&
parent.operator === "**"
) {
return true;
}

switch (node.type) {
case "UnaryExpression":
case "SpreadElement":
Expand Down Expand Up @@ -391,7 +402,7 @@ FPp.needsParens = function (assumeExpressionContext) {
}

if (pp === np && name === "right") {
assert.strictEqual(parent.right, node);
invariant(parent.right === node);
return true;
}

Expand Down Expand Up @@ -625,22 +636,22 @@ FPp.firstInStatement = function () {
parentName === "body" &&
childName === 0
) {
assert.strictEqual(parent.body[0], child);
invariant(parent.body[0] === child);
return true;
}

if (n.ExpressionStatement.check(parent) && childName === "expression") {
assert.strictEqual(parent.expression, child);
invariant(parent.expression === child);
return true;
}

if (n.AssignmentExpression.check(parent) && childName === "left") {
assert.strictEqual(parent.left, child);
invariant(parent.left === child);
return true;
}

if (n.ArrowFunctionExpression.check(parent) && childName === "body") {
assert.strictEqual(parent.body, child);
invariant(parent.body === child);
return true;
}

Expand All @@ -651,27 +662,27 @@ FPp.firstInStatement = function () {
s[i + 1] === "expressions" &&
childName === 0
) {
assert.strictEqual(parent.expressions[0], child);
invariant(parent.expressions[0] === child);
continue;
}

if (n.CallExpression.check(parent) && childName === "callee") {
assert.strictEqual(parent.callee, child);
invariant(parent.callee === child);
continue;
}

if (n.MemberExpression.check(parent) && childName === "object") {
assert.strictEqual(parent.object, child);
invariant(parent.object === child);
continue;
}

if (n.ConditionalExpression.check(parent) && childName === "test") {
assert.strictEqual(parent.test, child);
invariant(parent.test === child);
continue;
}

if (isBinary(parent) && childName === "left") {
assert.strictEqual(parent.left, child);
invariant(parent.left === child);
continue;
}

Expand All @@ -680,7 +691,7 @@ FPp.firstInStatement = function () {
!parent.prefix &&
childName === "argument"
) {
assert.strictEqual(parent.argument, child);
invariant(parent.argument === child);
continue;
}

Expand Down
46 changes: 23 additions & 23 deletions lib/lines.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import assert from "assert";
import invariant from "tiny-invariant";
import sourceMap from "source-map";
import { normalize as normalizeOptions, Options } from "./options";
import { namedTypes } from "ast-types";
Expand Down Expand Up @@ -30,7 +30,7 @@ export class Lines {
private cachedTabWidth: number | void = void 0;

constructor(private infos: LineInfo[], sourceFileName: string | null = null) {
assert.ok(infos.length > 0);
invariant(infos.length > 0);
this.length = infos.length;
this.name = sourceFileName || null;

Expand Down Expand Up @@ -96,7 +96,7 @@ export class Lines {
) {
const sourceChar = mapping.sourceLines.charAt(sourceCursor);
const targetChar = targetLines.charAt(targetCursor);
assert.strictEqual(sourceChar, targetChar);
invariant(sourceChar === targetChar);

const sourceName = mapping.sourceLines.name;

Expand Down Expand Up @@ -124,9 +124,9 @@ export class Lines {
}

bootstrapCharAt(pos: Pos) {
assert.strictEqual(typeof pos, "object");
assert.strictEqual(typeof pos.line, "number");
assert.strictEqual(typeof pos.column, "number");
invariant(typeof pos === "object");
invariant(typeof pos.line === "number");
invariant(typeof pos.column === "number");

const line = pos.line,
column = pos.column,
Expand All @@ -143,9 +143,9 @@ export class Lines {
}

charAt(pos: Pos) {
assert.strictEqual(typeof pos, "object");
assert.strictEqual(typeof pos.line, "number");
assert.strictEqual(typeof pos.column, "number");
invariant(typeof pos === "object");
invariant(typeof pos.line === "number");
invariant(typeof pos.column === "number");

let line = pos.line,
column = pos.column,
Expand All @@ -171,7 +171,7 @@ export class Lines {
stripMargin(width: number, skipFirstLine: boolean) {
if (width === 0) return this;

assert.ok(width > 0, "negative margin: " + width);
invariant(width > 0, "negative margin: " + width);

if (skipFirstLine && this.length === 1) return this;

Expand All @@ -189,7 +189,7 @@ export class Lines {

if (this.mappings.length > 0) {
const newMappings = lines.mappings;
assert.strictEqual(newMappings.length, 0);
invariant(newMappings.length === 0);
this.mappings.forEach(function (mapping: any) {
newMappings.push(mapping.indent(width, skipFirstLine, true));
});
Expand Down Expand Up @@ -217,7 +217,7 @@ export class Lines {

if (this.mappings.length > 0) {
const newMappings = lines.mappings;
assert.strictEqual(newMappings.length, 0);
invariant(newMappings.length === 0);
this.mappings.forEach(function (mapping: any) {
newMappings.push(mapping.indent(by));
});
Expand Down Expand Up @@ -250,7 +250,7 @@ export class Lines {

if (this.mappings.length > 0) {
const newMappings = lines.mappings;
assert.strictEqual(newMappings.length, 0);
invariant(newMappings.length === 0);
this.mappings.forEach(function (mapping: any) {
newMappings.push(mapping.indent(by, true));
});
Expand All @@ -273,7 +273,7 @@ export class Lines {
}

getIndentAt(line: number) {
assert.ok(line >= 1, "no line " + line + " (line numbers start from 1)");
invariant(line >= 1, "no line " + line + " (line numbers start from 1)");
return Math.max(this.infos[line - 1].indent, 0);
}

Expand Down Expand Up @@ -525,7 +525,7 @@ export class Lines {
if (start.line === end.line) {
sliced[0] = sliceInfo(sliced[0], start.column, end.column);
} else {
assert.ok(start.line < end.line);
invariant(start.line < end.line);
sliced[0] = sliceInfo(sliced[0], start.column);
sliced.push(sliceInfo(sliced.pop(), 0, end.column));
}
Expand All @@ -534,7 +534,7 @@ export class Lines {

if (this.mappings.length > 0) {
const newMappings = lines.mappings;
assert.strictEqual(newMappings.length, 0);
invariant(newMappings.length === 0);
this.mappings.forEach(function (this: any, mapping: any) {
const sliced = mapping.slice(this, start, end);
if (sliced) {
Expand Down Expand Up @@ -695,7 +695,7 @@ export class Lines {
concat(...args: (string | Lines)[]) {
const list: typeof args = [this];
list.push.apply(list, args);
assert.strictEqual(list.length, args.length + 1);
invariant(list.length === args.length + 1);
return emptyLines.join(list);
}
}
Expand All @@ -712,8 +712,8 @@ export function countSpaces(spaces: any, tabWidth?: number) {
switch (spaces.charCodeAt(i)) {
case 9: {
// '\t'
assert.strictEqual(typeof tabWidth, "number");
assert.ok(tabWidth! > 0);
invariant(typeof tabWidth === "number");
invariant(tabWidth! > 0);

const next = Math.ceil(count / tabWidth!) * tabWidth!;
if (next === count) {
Expand Down Expand Up @@ -761,7 +761,7 @@ export function fromString(string: string | Lines, options?: Options): Lines {
const tabless = string.indexOf("\t") < 0;
const cacheable = !options && tabless && string.length <= maxCacheKeyLen;

assert.ok(
invariant(
tabWidth || tabless,
"No tab width specified but encountered tabs in string\n" + string,
);
Expand Down Expand Up @@ -826,9 +826,9 @@ function sliceInfo(info: any, startCol: number, endCol?: number) {
sliceStart += startCol;
}

assert.ok(indent >= 0);
assert.ok(sliceStart <= sliceEnd);
assert.strictEqual(lineLength, indent + sliceEnd - sliceStart);
invariant(indent >= 0);
invariant(sliceStart <= sliceEnd);
invariant(lineLength === indent + sliceEnd - sliceStart);

if (
info.indent === indent &&
Expand Down
Loading

0 comments on commit aba90c6

Please sign in to comment.