Skip to content

Commit

Permalink
refactor: replace any type annotations in Lines
Browse files Browse the repository at this point in the history
Many of these could just be left inferred.
  • Loading branch information
eventualbuddha committed Jun 18, 2024
1 parent aff2dea commit 8d8d34b
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 43 deletions.
44 changes: 24 additions & 20 deletions lib/lines.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ type LineInfo = {
readonly sliceEnd: number;
};

type MutableLineInfo = {
-readonly [K in keyof LineInfo]: LineInfo[K];
}

export class Lines {
public readonly length: number;
public readonly name: string | null;
Expand Down Expand Up @@ -176,7 +180,7 @@ export class Lines {
if (skipFirstLine && this.length === 1) return this;

const lines = new Lines(
this.infos.map(function (info: any, i: any) {
this.infos.map(function (info, i) {
if (info.line && (i > 0 || !skipFirstLine)) {
info = {
...info,
Expand All @@ -190,7 +194,7 @@ export class Lines {
if (this.mappings.length > 0) {
const newMappings = lines.mappings;
invariant(newMappings.length === 0);
this.mappings.forEach(function (mapping: any) {
this.mappings.forEach(function (mapping) {
newMappings.push(mapping.indent(width, skipFirstLine, true));
});
}
Expand All @@ -204,7 +208,7 @@ export class Lines {
}

const lines = new Lines(
this.infos.map(function (info: any) {
this.infos.map(function (info) {
if (info.line && !info.locked) {
info = {
...info,
Expand All @@ -218,7 +222,7 @@ export class Lines {
if (this.mappings.length > 0) {
const newMappings = lines.mappings;
invariant(newMappings.length === 0);
this.mappings.forEach(function (mapping: any) {
this.mappings.forEach(function (mapping) {
newMappings.push(mapping.indent(by));
});
}
Expand All @@ -236,7 +240,7 @@ export class Lines {
}

const lines = new Lines(
this.infos.map(function (info: any, i: any) {
this.infos.map(function (info, i) {
if (i > 0 && info.line && !info.locked) {
info = {
...info,
Expand All @@ -251,7 +255,7 @@ export class Lines {
if (this.mappings.length > 0) {
const newMappings = lines.mappings;
invariant(newMappings.length === 0);
this.mappings.forEach(function (mapping: any) {
this.mappings.forEach(function (mapping) {
newMappings.push(mapping.indent(by, true));
});
}
Expand All @@ -265,7 +269,7 @@ export class Lines {
}

return new Lines(
this.infos.map((info: any, i: any) => ({
this.infos.map((info, i) => ({
...info,
locked: i > 0,
})),
Expand All @@ -282,7 +286,7 @@ export class Lines {
return this.cachedTabWidth;
}

const counts: any[] = []; // Sparse array.
const counts: number[] = []; // Sparse array.
let lastIndent = 0;

for (let line = 1, last = this.length; line <= last; ++line) {
Expand Down Expand Up @@ -527,15 +531,15 @@ export class Lines {
} else {
invariant(start.line < end.line);
sliced[0] = sliceInfo(sliced[0], start.column);
sliced.push(sliceInfo(sliced.pop(), 0, end.column));
sliced.push(sliceInfo(sliced.pop()!, 0, end.column));
}

const lines = new Lines(sliced);

if (this.mappings.length > 0) {
const newMappings = lines.mappings;
invariant(newMappings.length === 0);
this.mappings.forEach(function (this: any, mapping: any) {
this.mappings.forEach(function (this: Lines, mapping) {
const sliced = mapping.slice(this, start, end);
if (sliced) {
newMappings.push(sliced);
Expand Down Expand Up @@ -618,9 +622,9 @@ export class Lines {

join(elements: (string | Lines)[]) {
const separator = this;
const infos: any[] = [];
const mappings: any[] = [];
let prevInfo: any;
const infos: LineInfo[] = [];
const mappings: Mapping[] = [];
let prevInfo: MutableLineInfo | undefined;

function appendLines(linesOrNull: Lines | null) {
if (linesOrNull === null) {
Expand Down Expand Up @@ -648,15 +652,15 @@ export class Lines {
prevInfo.sliceEnd = prevInfo.line.length;

if (linesOrNull.mappings.length > 0) {
linesOrNull.mappings.forEach(function (mapping: any) {
linesOrNull.mappings.forEach(function (mapping) {
mappings.push(mapping.add(prevLine, prevColumn));
});
}
} else if (linesOrNull.mappings.length > 0) {
mappings.push.apply(mappings, linesOrNull.mappings);
}

linesOrNull.infos.forEach(function (info: any, i: any) {
linesOrNull.infos.forEach(function (info, i) {
if (!prevInfo || i > 0) {
prevInfo = { ...info };
infos.push(prevInfo);
Expand All @@ -670,7 +674,7 @@ export class Lines {
}

elements
.map(function (elem: any) {
.map(function (elem) {
const lines = fromString(elem);
if (lines.isEmpty()) return null;
return lines;
Expand Down Expand Up @@ -700,11 +704,11 @@ export class Lines {
}
}

const fromStringCache: any = {};
const fromStringCache: Record<string, Lines> = {};
const hasOwn = fromStringCache.hasOwnProperty;
const maxCacheKeyLen = 10;

export function countSpaces(spaces: any, tabWidth?: number) {
export function countSpaces(spaces: string, tabWidth?: number) {
let count = 0;
const len = spaces.length;

Expand Down Expand Up @@ -794,7 +798,7 @@ function isOnlyWhitespace(string: string) {
return !/\S/.test(string);
}

function sliceInfo(info: any, startCol: number, endCol?: number) {
function sliceInfo(info: LineInfo, startCol: number, endCol?: number) {
let sliceStart = info.sliceStart;
let sliceEnd = info.sliceEnd;
let indent = Math.max(info.indent, 0);
Expand Down Expand Up @@ -848,7 +852,7 @@ function sliceInfo(info: any, startCol: number, endCol?: number) {
};
}

export function concat(elements: any) {
export function concat(elements: (string | Lines)[]) {
return emptyLines.join(elements);
}

Expand Down
50 changes: 27 additions & 23 deletions test/lines.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,20 @@ import fs from "fs";
import path from "path";
import { fromString, concat, countSpaces, Lines } from "../lib/lines";
import { EOL as eol } from "os";
import { namedTypes } from "ast-types";

function check(a: any, b: any) {
assert.strictEqual(
a.toString({
lineTerminator: eol,
}),
b.toString({
lineTerminator: eol,
}),
);
type LinesInput = string | Lines;

function toString(s: LinesInput): string {
if (typeof s === 'string') {
return s;
}

return s.toString({ lineTerminator: eol });
}

function check(a: LinesInput, b: LinesInput) {
assert.strictEqual(toString(a), toString(b));
}

describe("lines", function () {
Expand All @@ -37,7 +41,7 @@ describe("lines", function () {
});

it("FromString", function () {
function checkIsCached(s: any) {
function checkIsCached(s: LinesInput) {
assert.strictEqual(fromString(s), fromString(s));
check(fromString(s), s);
}
Expand Down Expand Up @@ -66,19 +70,19 @@ describe("lines", function () {
check(lines.indentTail(5).indentTail(-7).indentTail(2), code);
});

function testEachPosHelper(lines: any, code: any) {
function testEachPosHelper(lines: Lines, code: string) {
check(lines, code);

const chars: any[] = [];
const chars: string[] = [];
let emptyCount = 0;

function iterator(pos: any) {
function iterator(pos: namedTypes.Position) {
const ch = lines.charAt(pos);
if (ch === "") emptyCount += 1;
chars.push(ch);
}

lines.eachPos(iterator, null);
lines.eachPos(iterator, undefined);

// The character at the position just past the end (as returned by
// lastPos) should be the only empty string.
Expand All @@ -94,7 +98,7 @@ describe("lines", function () {

const withoutSpaces = code.replace(/\s+/g, "");
chars.length = emptyCount = 0;
lines.eachPos(iterator, null, true); // Skip spaces this time.
lines.eachPos(iterator, undefined, true); // Skip spaces this time.
assert.strictEqual(emptyCount, 0);
joined = chars.join("");
assert.strictEqual(joined.length, withoutSpaces.length);
Expand Down Expand Up @@ -123,7 +127,7 @@ describe("lines", function () {
const code = String(CharAtTest).replace(/\r\n/g, "\n");
const lines = fromString(code);

function compare(pos: any) {
function compare(pos: namedTypes.Position) {
assert.strictEqual(lines.charAt(pos), lines.bootstrapCharAt(pos));
}

Expand All @@ -133,7 +137,7 @@ describe("lines", function () {
indented = original.indentTail(4),
reference = fromString(" ab" + eol + " c");

function compareIndented(pos: any) {
function compareIndented(pos: namedTypes.Position) {
const c = indented.charAt(pos);
check(c, reference.charAt(pos));
check(c, indented.bootstrapCharAt(pos));
Expand Down Expand Up @@ -197,7 +201,7 @@ describe("lines", function () {
});

it("Empty", function () {
function c(s: any) {
function c(s: LinesInput) {
const lines = fromString(s);
check(lines, s);
assert.strictEqual(lines.isEmpty(), s.length === 0);
Expand Down Expand Up @@ -254,9 +258,9 @@ describe("lines", function () {
checkAllSlices(lines);
});

function checkAllSlices(lines: any) {
lines.eachPos(function (start: any) {
lines.eachPos(function (end: any) {
function checkAllSlices(lines: Lines) {
lines.eachPos(function (start) {
lines.eachPos(function (end) {
check(lines.slice(start, end), lines.bootstrapSlice(start, end));
check(
lines.sliceString(start, end),
Expand All @@ -266,15 +270,15 @@ describe("lines", function () {
});
}

function getSourceLocation(lines: any) {
function getSourceLocation(lines: Lines) {
return { start: lines.firstPos(), end: lines.lastPos() };
}

it("GetSourceLocation", function GetSourceLocationTest() {
const code = String(GetSourceLocationTest),
lines = fromString(code);

function verify(indent: any) {
function verify(indent: number) {
const indented = lines.indentTail(indent),
loc = getSourceLocation(indented),
string = indented.toString(),
Expand Down

0 comments on commit 8d8d34b

Please sign in to comment.