Skip to content

Commit f2db90e

Browse files
committed
add some eslint rules, prettify, and prettier script locally
1 parent fd6ceac commit f2db90e

15 files changed

+247
-180
lines changed

.eslintrc.json

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
{
22
"root": true,
33
"plugins": ["node", "@typescript-eslint"],
4-
"extends": ["eslint:recommended", "plugin:node/recommended", "plugin:@typescript-eslint/recommended"],
4+
"extends": [
5+
"eslint:recommended",
6+
"plugin:node/recommended",
7+
"plugin:@typescript-eslint/recommended"
8+
],
59
"env": {
610
"node": true
711
},
@@ -20,6 +24,11 @@
2024
"prefer-const": "error",
2125
"prefer-arrow-callback": "error",
2226
"object-shorthand": "error",
23-
"es-syntax": "false"
27+
"node/no-unsupported-features/es-syntax": 0,
28+
"node/no-missing-require": 0,
29+
"node/no-missing-import": 0,
30+
"node/no-unpublished-import": 0,
31+
"@typescript-eslint/no-var-requires": 0,
32+
"@typescript-eslint/no-explicit-any": 0
2433
}
2534
}

.prettierignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@
22
/dist
33
/node_modules
44
/test/fixtures
5-
CHANGELOG.md
5+
CHANGELOG.md

lib/getHashDigest.ts

Lines changed: 38 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import {Hash} from 'crypto';
1+
import { Hash } from "crypto";
22

33
const baseEncodeTables = {
44
26: "abcdefghijklmnopqrstuvwxyz",
@@ -11,7 +11,15 @@ const baseEncodeTables = {
1111
64: "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ-_",
1212
};
1313

14-
type DigestTypes = "base26" | "base32" | "base36" | "base49" | "base52" | "base58" | "base62" | "base64";
14+
type DigestTypes =
15+
| "base26"
16+
| "base32"
17+
| "base36"
18+
| "base49"
19+
| "base52"
20+
| "base58"
21+
| "base62"
22+
| "base64";
1523
type BaseEncodings = 26 | 32 | 36 | 49 | 52 | 58 | 62 | 64;
1624

1725
/**
@@ -29,8 +37,12 @@ function divmod32(uint32Array: Uint32Array, divisor: number): number {
2937
return carry;
3038
}
3139

32-
function encodeBufferToBase(buffer: Buffer, base: BaseEncodings | number, length: number) {
33-
const encodeTable = baseEncodeTables[(base as keyof typeof baseEncodeTables)];
40+
function encodeBufferToBase(
41+
buffer: Buffer,
42+
base: BaseEncodings | number,
43+
length: number
44+
) {
45+
const encodeTable = baseEncodeTables[base as keyof typeof baseEncodeTables];
3446

3547
if (!encodeTable) {
3648
throw new Error("Unknown encoding base" + base);
@@ -57,13 +69,18 @@ function encodeBufferToBase(buffer: Buffer, base: BaseEncodings | number, length
5769
return output;
5870
}
5971

60-
let crypto: typeof import('crypto')
61-
let createXXHash64: typeof import('./hash/xxhash64').default;
62-
let createMd4: typeof import('./hash/md4').default;
63-
let BatchedHash: typeof import('./hash/BatchedHash').default;
64-
let BulkUpdateDecorator: typeof import('./hash/BulkUpdateDecorator').default;
65-
66-
export default function getHashDigest(buffer: Buffer, algorithm: string | "xxhash64" | "md4" | "native-md4", digestType: DigestTypes | string, maxLength: number) {
72+
let crypto: typeof import("crypto");
73+
let createXXHash64: typeof import("./hash/xxhash64").default;
74+
let createMd4: typeof import("./hash/md4").default;
75+
let BatchedHash: typeof import("./hash/BatchedHash").default;
76+
let BulkUpdateDecorator: typeof import("./hash/BulkUpdateDecorator").default;
77+
78+
export default function getHashDigest(
79+
buffer: Buffer,
80+
algorithm: string | "xxhash64" | "md4" | "native-md4",
81+
digestType: DigestTypes | string,
82+
maxLength: number
83+
) {
6784
algorithm = algorithm || "xxhash64";
6885
maxLength = maxLength || 9999;
6986

@@ -125,10 +142,17 @@ export default function getHashDigest(buffer: Buffer, algorithm: string | "xxhas
125142
digestType === "base58" ||
126143
digestType === "base62"
127144
) {
128-
const digestTypeToDigest: number = digestType.substr(4) as unknown as number;
129-
130-
return encodeBufferToBase(hash.digest() as Buffer, digestTypeToDigest, maxLength);
145+
const digestTypeToDigest: number = digestType.substr(
146+
4
147+
) as unknown as number;
148+
149+
return encodeBufferToBase(
150+
hash.digest() as Buffer,
151+
digestTypeToDigest,
152+
maxLength
153+
);
131154
} else {
155+
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
132156
// @ts-ignore
133157
return hash.digest(digestType || "hex").substr(0, maxLength);
134158
}

lib/hash/BatchedHash.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import type { Hash, Encoding, BinaryToTextEncoding } from "crypto";
2-
import { MAX_SHORT_STRING } from './wasm-hash'
2+
import { MAX_SHORT_STRING } from "./wasm-hash";
33

44
export default class BatchedHash {
55
public string?: string;
@@ -33,7 +33,7 @@ export default class BatchedHash {
3333
if (this.encoding !== undefined) {
3434
this.hash.update(this.string, this.encoding);
3535
} else {
36-
this.hash.update(this.string)
36+
this.hash.update(this.string);
3737
}
3838

3939
this.string = undefined;
@@ -73,7 +73,6 @@ export default class BatchedHash {
7373
} else {
7474
this.hash.update(this.string);
7575
}
76-
7776
}
7877
if (encoding !== undefined) {
7978
return this.hash.digest(encoding);

lib/hash/BulkUpdateDecorator.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,19 @@
11
import type { Hash, Encoding, BinaryToTextEncoding } from "crypto";
22
type HashOrFactory = Hash | (() => Hash);
33

4-
const BULK_SIZE: number = 2000;
4+
const BULK_SIZE = 2000;
55

66
// We are using an object instead of a Map as this will stay static during the runtime
77
// so access to it can be optimized by v8
8-
const digestCaches: {[key: string]: any} = {};
9-
8+
const digestCaches: { [key: string]: any } = {};
109

1110
export default class BulkUpdateDecorator {
1211
/**
1312
* @param {HashOrFactory} hashOrFactory function to create a hash
1413
* @param {string=} hashKey key for caching
1514
*/
1615
hash?: Hash;
17-
hashFactory?: (() => Hash);
16+
hashFactory?: () => Hash;
1817
hashKey: string;
1918
buffer: string;
2019

@@ -45,6 +44,7 @@ export default class BulkUpdateDecorator {
4544
data.length > BULK_SIZE
4645
) {
4746
if (this.hash === undefined) {
47+
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
4848
this.hash = this.hashFactory!();
4949
}
5050

@@ -63,6 +63,7 @@ export default class BulkUpdateDecorator {
6363

6464
if (this.buffer.length > BULK_SIZE) {
6565
if (this.hash === undefined) {
66+
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
6667
this.hash = this.hashFactory!();
6768
}
6869

@@ -101,6 +102,7 @@ export default class BulkUpdateDecorator {
101102
return cacheEntry;
102103
}
103104

105+
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
104106
this.hash = this.hashFactory!();
105107
}
106108

lib/hash/md4.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
Author Tobias Koppers @sokra
44
*/
55

6-
import { create } from './wasm-hash';
6+
import { create } from "./wasm-hash";
77

88
//#region wasm code: md4 (../../../assembly/hash/md4.asm.ts) --initialMemory 1
99
const md4 = new WebAssembly.Module(

lib/hash/wasm-hash.ts

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Hash, BinaryToTextEncoding } from "crypto";
1+
import { BinaryToTextEncoding } from "crypto";
22

33
/*
44
MIT License http://www.opensource.org/licenses/mit-license.php
@@ -25,7 +25,12 @@ export class WasmHash {
2525
chunkSize: number;
2626
digestSize: number;
2727

28-
constructor(instance: WebAssembly.Instance, instancesPool: WebAssembly.Instance[], chunkSize: number, digestSize: number) {
28+
constructor(
29+
instance: WebAssembly.Instance,
30+
instancesPool: WebAssembly.Instance[],
31+
chunkSize: number,
32+
digestSize: number
33+
) {
2934
const exports = instance.exports as any;
3035

3136
exports.init();
@@ -189,7 +194,12 @@ export class WasmHash {
189194
}
190195
}
191196

192-
export const create = (wasmModule: WebAssembly.Module, instancesPool: WasmHash[], chunkSize: number, digestSize: number) => {
197+
export const create = (
198+
wasmModule: WebAssembly.Module,
199+
instancesPool: WasmHash[],
200+
chunkSize: number,
201+
digestSize: number
202+
) => {
193203
if (instancesPool.length > 0) {
194204
const old = instancesPool.pop();
195205

@@ -207,4 +217,3 @@ export const create = (wasmModule: WebAssembly.Module, instancesPool: WasmHash[]
207217
);
208218
}
209219
};
210-

lib/hash/xxhash64.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
MIT License http://www.opensource.org/licenses/mit-license.php
33
Author Tobias Koppers @sokra
44
*/
5-
import { create } from './wasm-hash';
5+
import { create } from "./wasm-hash";
66

77
//#region wasm code: xxhash64 (../../../assembly/hash/xxhash64.asm.ts) --initialMemory 1
88
const xxhash64 = new WebAssembly.Module(

lib/index.ts

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,4 @@ import urlToRequest from "./urlToRequest";
33
import getHashDigest from "./getHashDigest";
44
import interpolateName from "./interpolateName";
55

6-
export {
7-
urlToRequest,
8-
getHashDigest,
9-
interpolateName,
10-
isUrlRequest
11-
}
6+
export { urlToRequest, getHashDigest, interpolateName, isUrlRequest };

lib/interpolateName.ts

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,22 @@
11
import type { LoaderContext } from "webpack";
22
import path from "path";
3-
import getHashDigest from './getHashDigest';
3+
import getHashDigest from "./getHashDigest";
44

55
interface IInterpolateNameOptions {
66
content?: Buffer;
77
context?: string;
88
regExp?: string;
99
}
1010

11-
export default function interpolateName(loaderContext: LoaderContext<{}>, name: string | ((resourcePath: string, resourceQuery?: string) => string), options: IInterpolateNameOptions = {}) {
11+
export default function interpolateName(
12+
loaderContext: LoaderContext<object>,
13+
name: string | ((resourcePath: string, resourceQuery?: string) => string),
14+
options: IInterpolateNameOptions = {}
15+
) {
1216
let filename;
1317

14-
const hasQuery: boolean =
15-
(loaderContext.resourceQuery && loaderContext.resourceQuery.length > 1) as boolean;
18+
const hasQuery: boolean = (loaderContext.resourceQuery &&
19+
loaderContext.resourceQuery.length > 1) as boolean;
1620

1721
if (typeof name === "function") {
1822
filename = name(
@@ -104,13 +108,16 @@ export default function interpolateName(loaderContext: LoaderContext<{}>, name:
104108
}
105109

106110
if (
111+
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
107112
// @ts-ignore LoaderContext doesn't even have options defined on it?
108113
// If we chagned this to be `loaderContext.getOptions()` it would still not have
109114
// the customInterpolateName function defined on it.
110115
typeof loaderContext.options === "object" &&
116+
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
111117
// @ts-ignore
112118
typeof loaderContext.options.customInterpolateName === "function"
113119
) {
120+
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
114121
// @ts-ignore
115122
url = loaderContext.options.customInterpolateName.call(
116123
loaderContext,

lib/isUrlRequest.ts

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
import path from 'path';
1+
import path from "path";
22

3-
const DATA_URI_REGEXP: RegExp = /^data:/i;
4-
const ABOSLUTE_URL_NON_WINDOWS_PATHLIKE_REGEXP: RegExp = /^[a-z][a-z0-9+.-]*:/i;
5-
const POROTCOL_RELATIVE_REGEXP: RegExp = /^\/\//i;
6-
const URL_FOR_TEMPLATE_REGEXP: RegExp = /^#/i;
3+
const DATA_URI_REGEXP = /^data:/i;
4+
const ABOSLUTE_URL_NON_WINDOWS_PATHLIKE_REGEXP = /^[a-z][a-z0-9+.-]*:/i;
5+
const POROTCOL_RELATIVE_REGEXP = /^\/\//i;
6+
const URL_FOR_TEMPLATE_REGEXP = /^#/i;
77

88
export default function isUrlRequest(url: string): boolean {
99
// An URL is not an request if
@@ -14,7 +14,10 @@ export default function isUrlRequest(url: string): boolean {
1414
}
1515

1616
// 2. It's an absolute url and it is not `windows` path like `C:\dir\file`
17-
if (ABOSLUTE_URL_NON_WINDOWS_PATHLIKE_REGEXP.test(url) && !path.win32.isAbsolute(url)) {
17+
if (
18+
ABOSLUTE_URL_NON_WINDOWS_PATHLIKE_REGEXP.test(url) &&
19+
!path.win32.isAbsolute(url)
20+
) {
1821
return false;
1922
}
2023

@@ -30,5 +33,3 @@ export default function isUrlRequest(url: string): boolean {
3033

3134
return true;
3235
}
33-
34-

lib/urlToRequest.ts

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
// we can't use path.win32.isAbsolute because it also matches paths starting with a forward slash
2-
const NATIVE_WIN_32_PATH_REGEXP: RegExp = /^[A-Z]:[/\\]|^\\\\/i;
3-
const MODULE_REQUEST_REGEXP: RegExp = /^[^?]*~/;
4-
const ROOT_RELATIVE_URL_REGEXP: RegExp = /^\//;
2+
const NATIVE_WIN_32_PATH_REGEXP = /^[A-Z]:[/\\]|^\\\\/i;
3+
const MODULE_REQUEST_REGEXP = /^[^?]*~/;
4+
const ROOT_RELATIVE_URL_REGEXP = /^\//;
55

6-
export default function urlToRequest(url: string, root?: string | boolean): string {
6+
export default function urlToRequest(
7+
url: string,
8+
root?: string | boolean
9+
): string {
710
// Do not rewrite an empty url
811
if (url === "") {
912
return "";
@@ -14,7 +17,11 @@ export default function urlToRequest(url: string, root?: string | boolean): stri
1417
if (NATIVE_WIN_32_PATH_REGEXP.test(url)) {
1518
// absolute windows path, keep it
1619
request = url;
17-
} else if (root !== undefined && root !== false && ROOT_RELATIVE_URL_REGEXP.test(url)) {
20+
} else if (
21+
root !== undefined &&
22+
root !== false &&
23+
ROOT_RELATIVE_URL_REGEXP.test(url)
24+
) {
1825
// if root is set and the url is root-relative
1926
switch (typeof root) {
2027
// 1. root is a string: root is prefixed to the url

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@
55
"description": "utils for webpack loaders",
66
"dependencies": {},
77
"scripts": {
8-
"lint": "prettier --list-different . && eslint .",
8+
"lint": "prettier --list-different . && eslint ./lib",
9+
"pretty": "prettier --write .",
910
"build": "tsc",
1011
"pretest": "yarn lint",
1112
"test": "jest",

tsconfig.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
"outDir": "dist",
77
"strict": true,
88
"types": ["node"],
9-
"esModuleInterop": true,
9+
"esModuleInterop": true
1010
},
11-
"include": ["lib/**/*.ts"],
11+
"include": ["lib/**/*.ts"]
1212
}

0 commit comments

Comments
 (0)