Skip to content

Commit

Permalink
chore: enable single-var-declarator lint rule (#4488)
Browse files Browse the repository at this point in the history
  • Loading branch information
gabelluardo authored Mar 14, 2024
1 parent 469c3c6 commit 2990aa6
Show file tree
Hide file tree
Showing 15 changed files with 160 additions and 154 deletions.
10 changes: 6 additions & 4 deletions assert/_diff.ts
Original file line number Diff line number Diff line change
Expand Up @@ -351,7 +351,8 @@ export function diffstr(A: string, B: string) {
tokenize(`${unescape(B)}\n`),
);

const added = [], removed = [];
const added = [];
const removed = [];
for (const result of diffResult) {
if (result.type === DiffType.added) {
added.push(result);
Expand All @@ -366,8 +367,8 @@ export function diffstr(A: string, B: string) {
const aLines = hasMoreRemovedLines ? added : removed;
const bLines = hasMoreRemovedLines ? removed : added;
for (const a of aLines) {
let tokens = [] as Array<DiffResult<string>>,
b: undefined | DiffResult<string>;
let tokens = [] as Array<DiffResult<string>>;
let b: undefined | DiffResult<string>;
// Search another diff line with at least one common token
while (bLines.length) {
b = bLines.shift();
Expand Down Expand Up @@ -437,7 +438,8 @@ export function buildMessage(
diffResult: ReadonlyArray<DiffResult<string>>,
{ stringDiff = false } = {},
): string[] {
const messages: string[] = [], diffMessages: string[] = [];
const messages: string[] = [];
const diffMessages: string[] = [];
messages.push("");
messages.push("");
messages.push(
Expand Down
8 changes: 3 additions & 5 deletions bytes/ends_with.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,9 @@
* ```
*/
export function endsWith(source: Uint8Array, suffix: Uint8Array): boolean {
for (
let srci = source.length - 1, sfxi = suffix.length - 1;
sfxi >= 0;
srci--, sfxi--
) {
let srci = source.length - 1;
let sfxi = suffix.length - 1;
for (; sfxi >= 0; srci--, sfxi--) {
if (source[srci] !== suffix[sfxi]) return false;
}
return true;
Expand Down
2 changes: 1 addition & 1 deletion bytes/starts_with.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
* ```
*/
export function startsWith(source: Uint8Array, prefix: Uint8Array): boolean {
for (let i = 0, max = prefix.length; i < max; i++) {
for (let i = 0; i < prefix.length; i++) {
if (source[i] !== prefix[i]) return false;
}
return true;
Expand Down
4 changes: 2 additions & 2 deletions csv/stringify.ts
Original file line number Diff line number Diff line change
Expand Up @@ -147,8 +147,8 @@ type NormalizedColumn = Omit<ColumnDetails, "header" | "prop"> & {
};

function normalizeColumn(column: Column): NormalizedColumn {
let header: NormalizedColumn["header"],
prop: NormalizedColumn["prop"];
let header: NormalizedColumn["header"];
let prop: NormalizedColumn["prop"];

if (typeof column === "object") {
if (Array.isArray(column)) {
Expand Down
3 changes: 2 additions & 1 deletion deno.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,8 @@
"rules": {
"include": [
"camelcase",
"no-sync-fn-in-async-fn"
"no-sync-fn-in-async-fn",
"single-var-declarator"
]
}
}
Expand Down
22 changes: 11 additions & 11 deletions encoding/ascii85.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,24 +65,24 @@ export function encodeAscii85(
let uint8 = validateBinaryLike(data);

const standard = options?.standard ?? "Adobe";
let output: string[] = [],
v: number,
n = 0,
difference = 0;
let output: string[] = [];
let v: number;
let n = 0;
let difference = 0;
if (uint8.length % 4 !== 0) {
const tmp = uint8;
difference = 4 - (tmp.length % 4);
uint8 = new Uint8Array(tmp.length + difference);
uint8.set(tmp);
}
const view = new DataView(uint8.buffer, uint8.byteOffset, uint8.byteLength);
for (let i = 0, len = uint8.length; i < len; i += 4) {
for (let i = 0; i < uint8.length; i += 4) {
v = view.getUint32(i);
// Adobe and btoa standards compress 4 zeroes to single "z" character
if (
(standard === "Adobe" || standard === "btoa") &&
v === 0 &&
i < len - difference - 3
i < uint8.length - difference - 3
) {
output[n++] = "z";
continue;
Expand Down Expand Up @@ -164,12 +164,12 @@ export function decodeAscii85(
}
//remove all invalid characters
ascii85 = ascii85.replaceAll(/[^!-u]/g, "");
const len = ascii85.length,
output = new Uint8Array(len + 4 - (len % 4));
const len = ascii85.length;
const output = new Uint8Array(len + 4 - (len % 4));
const view = new DataView(output.buffer);
let v = 0,
n = 0,
max = 0;
let v = 0;
let n = 0;
let max = 0;
for (let i = 0; i < len;) {
for (max += 5; i < max; i++) {
v = v * 85 + (i < len ? ascii85.charCodeAt(i) : 117) - 33;
Expand Down
4 changes: 2 additions & 2 deletions encoding/base64.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,8 +95,8 @@ const base64abc = [
export function encodeBase64(data: ArrayBuffer | Uint8Array | string): string {
// CREDIT: https://gist.github.com/enepomnyaschih/72c423f727d395eeaa09697058238727
const uint8 = validateBinaryLike(data);
let result = "",
i;
let result = "";
let i;
const l = uint8.length;
for (i = 2; i < l; i += 3) {
result += base64abc[(uint8[i - 2]!) >> 2];
Expand Down
13 changes: 6 additions & 7 deletions encoding/varint.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,12 +112,11 @@ export function decode(buf: Uint8Array, offset = 0): [bigint, number] {
* from the returned new `offset`.
*/
export function decode32(buf: Uint8Array, offset = 0): [number, number] {
let shift = 0;
let decoded = 0;
for (
let i = offset,
len = Math.min(buf.length, offset + MaxVarIntLen32),
shift = 0,
decoded = 0;
i <= len;
let i = offset;
i <= Math.min(buf.length, offset + MaxVarIntLen32);
i += 1, shift += SHIFT
) {
const byte = buf[i]!;
Expand Down Expand Up @@ -148,8 +147,8 @@ export function encode(
num = BigInt(num);
if (num < 0n) throw new RangeError("signed input given");
for (
let i = offset, len = Math.min(buf.length, MaxVarIntLen64);
i <= len;
let i = offset;
i <= Math.min(buf.length, MaxVarIntLen64);
i += 1
) {
if (num < MSBN) {
Expand Down
10 changes: 6 additions & 4 deletions expect/_diff.ts
Original file line number Diff line number Diff line change
Expand Up @@ -351,7 +351,8 @@ export function diffstr(A: string, B: string) {
tokenize(`${unescape(B)}\n`),
);

const added = [], removed = [];
const added = [];
const removed = [];
for (const result of diffResult) {
if (result.type === DiffType.added) {
added.push(result);
Expand All @@ -366,8 +367,8 @@ export function diffstr(A: string, B: string) {
const aLines = hasMoreRemovedLines ? added : removed;
const bLines = hasMoreRemovedLines ? removed : added;
for (const a of aLines) {
let tokens = [] as Array<DiffResult<string>>,
b: undefined | DiffResult<string>;
let tokens = [] as Array<DiffResult<string>>;
let b: undefined | DiffResult<string>;
// Search another diff line with at least one common token
while (bLines.length) {
b = bLines.shift();
Expand Down Expand Up @@ -437,7 +438,8 @@ export function buildMessage(
diffResult: ReadonlyArray<DiffResult<string>>,
{ stringDiff = false } = {},
): string[] {
const messages: string[] = [], diffMessages: string[] = [];
const messages: string[] = [];
const diffMessages: string[] = [];
messages.push("");
messages.push("");
messages.push(
Expand Down
4 changes: 2 additions & 2 deletions path/_common/normalize_string.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ export function normalizeString(
let lastSlash = -1;
let dots = 0;
let code: number | undefined;
for (let i = 0, len = path.length; i <= len; ++i) {
if (i < len) code = path.charCodeAt(i);
for (let i = 0; i <= path.length; ++i) {
if (i < path.length) code = path.charCodeAt(i);
else if (isPathSeparator(code!)) break;
else code = CHAR_FORWARD_SLASH;

Expand Down
2 changes: 1 addition & 1 deletion path/posix/join.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ export function join(...paths: string[]): string {
if (paths.length === 0) return ".";

let joined: string | undefined;
for (let i = 0, len = paths.length; i < len; ++i) {
for (let i = 0; i < paths.length; ++i) {
const path = paths[i]!;
assertPath(path);
if (path.length > 0) {
Expand Down
16 changes: 8 additions & 8 deletions testing/mock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -546,10 +546,10 @@ function methodSpy<
}

const original = self[property] as unknown as (
this: Self,
...args: Args
) => Return,
calls: SpyCall<Self, Args, Return>[] = [];
this: Self,
...args: Args
) => Return;
const calls: SpyCall<Self, Args, Return>[] = [];
let restored = false;
const spy = function (this: Self, ...args: Args): Return {
const call: SpyCall<Self, Args, Return> = { args };
Expand Down Expand Up @@ -799,10 +799,10 @@ export function stub<
const fake = func ?? (() => {}) as (this: Self, ...args: Args) => Return;

const original = self[property] as unknown as (
this: Self,
...args: Args
) => Return,
calls: SpyCall<Self, Args, Return>[] = [];
this: Self,
...args: Args
) => Return;
const calls: SpyCall<Self, Args, Return>[] = [];
let restored = false;
const stub = function (this: Self, ...args: Args): Return {
const call: SpyCall<Self, Args, Return> = { args };
Expand Down
70 changes: 36 additions & 34 deletions yaml/_dumper/dumper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,12 +99,12 @@ function encodeHex(character: number): string {

// Indents every line in a string. Empty lines (\n only) are not indented.
function indentString(string: string, spaces: number): string {
const ind = common.repeat(" ", spaces),
length = string.length;
let position = 0,
next = -1,
result = "",
line: string;
const ind = common.repeat(" ", spaces);
const length = string.length;
let position = 0;
let next = -1;
let result = "";
let line: string;

while (position < length) {
next = string.indexOf("\n", position);
Expand Down Expand Up @@ -209,11 +209,11 @@ function needIndentIndicator(string: string): boolean {
return leadingSpaceRe.test(string);
}

const STYLE_PLAIN = 1,
STYLE_SINGLE = 2,
STYLE_LITERAL = 3,
STYLE_FOLDED = 4,
STYLE_DOUBLE = 5;
const STYLE_PLAIN = 1;
const STYLE_SINGLE = 2;
const STYLE_LITERAL = 3;
const STYLE_FOLDED = 4;
const STYLE_DOUBLE = 5;

// Determines which scalar styles are possible and returns the preferred style.
// lineWidth = -1 => no limit.
Expand All @@ -230,13 +230,14 @@ function chooseScalarStyle(
testAmbiguousType: (...args: Any[]) => Any,
): number {
const shouldTrackWidth = lineWidth !== -1;
let hasLineBreak = false,
hasFoldableLine = false, // only checked if shouldTrackWidth
previousLineBreak = -1, // count the first line correctly
plain = isPlainSafeFirst(string.charCodeAt(0)) &&
!isWhitespace(string.charCodeAt(string.length - 1));

let char: number, i: number;
let hasLineBreak = false;
let hasFoldableLine = false; // only checked if shouldTrackWidth
let previousLineBreak = -1; // count the first line correctly
let plain = isPlainSafeFirst(string.charCodeAt(0)) &&
!isWhitespace(string.charCodeAt(string.length - 1));

let char: number;
let i: number;
if (singleLineOnly) {
// Case: no block styles.
// Check for disallowed characters to rule out plain and single.
Expand Down Expand Up @@ -300,10 +301,10 @@ function foldLine(line: string, width: number): string {
const breakRe = / [^ ]/g; // note: the match index will always be <= length-2.
let match;
// start is an inclusive index. end, curr, and next are exclusive.
let start = 0,
end,
curr = 0,
next = 0;
let start = 0;
let end;
let curr = 0;
let next = 0;
let result = "";

// Invariants: 0 <= start <= length-1.
Expand Down Expand Up @@ -365,8 +366,8 @@ function foldString(string: string, width: number): string {
let match;
// tslint:disable-next-line:no-conditional-assignment
while ((match = lineRe.exec(string))) {
const prefix = match[1],
line = match[2] || "";
const prefix = match[1];
const line = match[2] || "";
moreIndented = line[0] === " ";
result += prefix +
(!prevMoreIndented && !moreIndented && line !== "" ? "\n" : "") +
Expand All @@ -380,7 +381,8 @@ function foldString(string: string, width: number): string {
// Escapes a double-quoted string.
function escapeString(string: string): string {
let result = "";
let char, nextChar;
let char;
let nextChar;
let escapeSeq;

for (let i = 0; i < string.length; i++) {
Expand Down Expand Up @@ -508,7 +510,7 @@ function writeFlowSequence(
let _result = "";
const _tag = state.tag;

for (let index = 0, length = object.length; index < length; index += 1) {
for (let index = 0; index < object.length; index += 1) {
// Write only valid elements.
if (writeNode(state, level, object[index], false, false)) {
if (index !== 0) _result += `,${!state.condenseFlow ? " " : ""}`;
Expand All @@ -529,7 +531,7 @@ function writeBlockSequence(
let _result = "";
const _tag = state.tag;

for (let index = 0, length = object.length; index < length; index += 1) {
for (let index = 0; index < object.length; index += 1) {
// Write only valid elements.
if (writeNode(state, level + 1, object[index], true, true)) {
if (!compact || index !== 0) {
Expand All @@ -556,8 +558,8 @@ function writeFlowMapping(
object: Any,
) {
let _result = "";
const _tag = state.tag,
objectKeyList = Object.keys(object);
const _tag = state.tag;
const objectKeyList = Object.keys(object);

for (const [index, objectKey] of objectKeyList.entries()) {
let pairBuffer = state.condenseFlow ? '"' : "";
Expand Down Expand Up @@ -596,8 +598,8 @@ function writeBlockMapping(
object: Any,
compact = false,
) {
const _tag = state.tag,
objectKeyList = Object.keys(object);
const _tag = state.tag;
const objectKeyList = Object.keys(object);
let _result = "";

// Allow sorting keys so that the output file is deterministic
Expand Down Expand Up @@ -810,7 +812,7 @@ function inspectNode(
objects.push(object);

if (Array.isArray(object)) {
for (let idx = 0, length = object.length; idx < length; idx += 1) {
for (let idx = 0; idx < object.length; idx += 1) {
inspectNode(object[idx], objects, duplicatesIndexes);
}
} else {
Expand All @@ -826,8 +828,8 @@ function getDuplicateReferences(
object: Record<string, unknown>,
state: DumperState,
) {
const objects: Any[] = [],
duplicatesIndexes: number[] = [];
const objects: Any[] = [];
const duplicatesIndexes: number[] = [];

inspectNode(object, objects, duplicatesIndexes);

Expand Down
Loading

0 comments on commit 2990aa6

Please sign in to comment.