Skip to content
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

feat: use invariant for assertions #1387

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 13 additions & 13 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,28 +2,28 @@ 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: ['12.x', '14.x', '16.x', '18.x', '19.x', '20.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 test
12 changes: 6 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 Down
26 changes: 13 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 @@ -402,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 @@ -636,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 @@ -662,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 @@ -691,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
22 changes: 10 additions & 12 deletions lib/mapping.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 { comparePos } from "./util";
import { namedTypes } from "ast-types";
import { Lines } from "./lines";
Expand Down Expand Up @@ -26,7 +26,7 @@ export default class Mapping {
if (name === "end") {
targetToPos = end;
} else {
assert.strictEqual(name, "start");
invariant(name === "start");
}

return skipChars(
Expand Down Expand Up @@ -194,17 +194,16 @@ function skipChars(
sourceCursor.column = 0;
targetCursor.column = 0;
} else {
assert.strictEqual(lineDiff, 0);
invariant(lineDiff === 0);
}

while (
comparePos(targetCursor, targetToPos) < 0 &&
targetLines.nextPos(targetCursor, true)
) {
assert.ok(sourceLines.nextPos(sourceCursor, true));
assert.strictEqual(
sourceLines.charAt(sourceCursor),
targetLines.charAt(targetCursor),
invariant(sourceLines.nextPos(sourceCursor, true));
invariant(
sourceLines.charAt(sourceCursor) === targetLines.charAt(targetCursor),
);
}
} else {
Expand All @@ -224,17 +223,16 @@ function skipChars(
sourceCursor.column = sourceLines.getLineLength(sourceCursor.line);
targetCursor.column = targetLines.getLineLength(targetCursor.line);
} else {
assert.strictEqual(lineDiff, 0);
invariant(lineDiff === 0);
}

while (
comparePos(targetToPos, targetCursor) < 0 &&
targetLines.prevPos(targetCursor, true)
) {
assert.ok(sourceLines.prevPos(sourceCursor, true));
assert.strictEqual(
sourceLines.charAt(sourceCursor),
targetLines.charAt(targetCursor),
invariant(sourceLines.prevPos(sourceCursor, true));
invariant(
sourceLines.charAt(sourceCursor) === targetLines.charAt(targetCursor),
);
}
}
Expand Down
4 changes: 2 additions & 2 deletions lib/parser.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 b = types.builders;
const isObject = types.builtInTypes.object;
Expand Down Expand Up @@ -143,7 +143,7 @@ const TreeCopier = function TreeCopier(
lines: any,
tokens: any,
) {
assert.ok(this instanceof TreeCopier);
invariant(this instanceof TreeCopier);
this.lines = lines;
this.tokens = tokens;
this.startTokenIndex = 0;
Expand Down
Loading
Loading