Skip to content

Commit 9072420

Browse files
committed
chore: 🤖 add tslint and fix tslint errors
1 parent 5834775 commit 9072420

9 files changed

+66
-45
lines changed

‎package.json

+5-2
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,9 @@
1717
"semantic-release": "semantic-release",
1818
"prettier": "prettier --ignore-path .gitignore --write 'src/**/*.{ts,js}'",
1919
"prettier:diff": "prettier -l 'src/**/*.{ts,js}'",
20+
"tslint": "tslint 'src/**/*.ts' -t verbose",
2021
"precommit": "pretty-quick --staged",
21-
"prepush": "yarn prettier:diff"
22+
"prepush": "yarn prettier:diff && yarn tslint"
2223
},
2324
"repository": {
2425
"type": "git",
@@ -44,7 +45,9 @@
4445
"typescript": "3.1.6",
4546
"prettier": "1.15.1",
4647
"pretty-quick": "1.8.0",
47-
"husky": "1.1.3"
48+
"husky": "1.1.3",
49+
"tslint": "5.11.0",
50+
"tslint-config-common": "1.2.0"
4851
},
4952
"config": {
5053
"commitizen": {

‎src/__tests__/index.test.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,10 @@ describe('memfs', () => {
2727
expect(typeof memfs._toUnixTimestamp).toBe('function');
2828
});
2929
it("Exports all Node's filesystem API methods", () => {
30-
for (let method of fsSyncMethods) {
30+
for (const method of fsSyncMethods) {
3131
expect(typeof memfs[method]).toBe('function');
3232
}
33-
for (let method of fsAsyncMethods) {
33+
for (const method of fsAsyncMethods) {
3434
expect(typeof memfs[method]).toBe('function');
3535
}
3636
});

‎src/__tests__/promises.test.ts

-1
Original file line numberDiff line numberDiff line change
@@ -478,7 +478,6 @@ describe('Promises API', () => {
478478
});
479479
vol.symlinkSync('/foo/baz', '/foo/qux');
480480
it('Return real path of existing file', async () => {
481-
console.log((await promises.realpath('/foo/bar/../qux')).toString());
482481
expect((await promises.realpath('/foo/bar/../qux')).toString()).toEqual('/foo/baz');
483482
});
484483
it('Reject when file does not exist', () => {

‎src/__tests__/volume.test.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -506,8 +506,8 @@ describe('volume', () => {
506506
const fd = vol.openSync('/multi.txt', 'w+');
507507
const datas = ['hello', ' ', 'world', '!'];
508508
let bytes = 0;
509-
for (let data of datas) {
510-
let b = vol.writeSync(fd, data);
509+
for (const data of datas) {
510+
const b = vol.writeSync(fd, data);
511511
expect(b).toBe(data.length);
512512
bytes += b;
513513
}

‎src/internal/errors.js

+9-3
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,9 @@ class AssertionError extends Error {
3030
if (options.message) {
3131
super(options.message);
3232
} else {
33-
if (util === null) util = require('util');
33+
if (util === null) {
34+
util = require('util');
35+
}
3436
super(
3537
`${util.inspect(options.actual).slice(0, 128)} ` +
3638
`${options.operator} ${util.inspect(options.expected).slice(0, 128)}`,
@@ -48,7 +50,9 @@ class AssertionError extends Error {
4850
}
4951

5052
function message(key, args) {
51-
if (assert === null) assert = require('assert');
53+
if (assert === null) {
54+
assert = require('assert');
55+
}
5256
assert.strictEqual(typeof key, 'string');
5357
// const msg = messages.get(key);
5458
const msg = messages[key];
@@ -57,7 +61,9 @@ function message(key, args) {
5761
if (typeof msg === 'function') {
5862
fmt = msg;
5963
} else {
60-
if (util === null) util = require('util');
64+
if (util === null) {
65+
util = require('util');
66+
}
6167
fmt = util.format;
6268
if (args === undefined || args.length === 0) return msg;
6369
args.unshift(msg);

‎src/node.ts

+1-4
Original file line numberDiff line numberDiff line change
@@ -245,7 +245,7 @@ export class Link extends EventEmitter {
245245
node: Node = null;
246246

247247
// "i-node" number of the node.
248-
ino: Number = 0;
248+
ino: number = 0;
249249

250250
// Number of children.
251251
length: number = 0;
@@ -338,9 +338,6 @@ export class Link extends EventEmitter {
338338
}
339339

340340
toJSON() {
341-
for (let ch in this.children) {
342-
console.log('ch', ch);
343-
}
344341
return {
345342
steps: this.steps,
346343
ino: this.ino,

‎src/promises.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -33,15 +33,15 @@ function promisify(
3333
});
3434
}
3535

36-
export type TFileHandleReadResult = {
36+
export interface TFileHandleReadResult {
3737
bytesRead: number;
3838
buffer: Buffer | Uint8Array;
39-
};
39+
}
4040

41-
export type TFileHandleWriteResult = {
41+
export interface TFileHandleWriteResult {
4242
bytesWritten: number;
4343
buffer: Buffer | Uint8Array;
44-
};
44+
}
4545

4646
export interface IFileHandle {
4747
fd: number;

‎src/volume.ts

+31-27
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import { resolve as resolveCrossPlatform } from 'path';
21
import * as pathModule from 'path';
32
import { Node, Link, File } from './node';
43
import Stats from './Stats';
@@ -16,6 +15,7 @@ import extend = require('fast-extend');
1615
import util = require('util');
1716
import createPromisesApi from './promises';
1817

18+
const resolveCrossPlatform = pathModule.resolve;
1919
const {
2020
O_RDONLY,
2121
O_WRONLY,
@@ -30,7 +30,8 @@ const {
3030
COPYFILE_FICLONE_FORCE,
3131
} = constants;
3232

33-
let sep, relative;
33+
let sep;
34+
let relative;
3435
if (pathModule.posix) {
3536
const { posix } = pathModule;
3637

@@ -214,7 +215,7 @@ function getOptions<T extends IOptions>(defaults: T, options?: T | string): T {
214215
let opts: T;
215216
if (!options) return defaults;
216217
else {
217-
var tipeof = typeof options;
218+
const tipeof = typeof options;
218219
switch (tipeof) {
219220
case 'string':
220221
opts = extend({}, defaults, { encoding: options as string });
@@ -362,10 +363,10 @@ function getPathFromURLPosix(url) {
362363
if (url.hostname !== '') {
363364
return new errors.TypeError('ERR_INVALID_FILE_URL_HOST', process.platform);
364365
}
365-
let pathname = url.pathname;
366+
const pathname = url.pathname;
366367
for (let n = 0; n < pathname.length; n++) {
367368
if (pathname[n] === '%') {
368-
let third = pathname.codePointAt(n + 2) | 0x20;
369+
const third = pathname.codePointAt(n + 2) | 0x20;
369370
if (pathname[n + 1] === '2' && third === 102) {
370371
return new errors.TypeError('ERR_INVALID_FILE_URL_PATH', 'must not include encoded / characters');
371372
}
@@ -462,6 +463,7 @@ function validateFd(fd) {
462463
// converts Date or number to a fractional UNIX timestamp
463464
export function toUnixTimestamp(time) {
464465
if (typeof time === 'string' && +time == (time as any)) {
466+
// tslint:disable-line triple-equals
465467
return +time;
466468
}
467469
if (time instanceof Date) {
@@ -578,7 +580,7 @@ export class Volume {
578580
const root = this.createLink();
579581
root.setNode(this.createNode(true));
580582

581-
const self = this;
583+
const self = this; // tslint:disable-line no-this-assignment
582584

583585
this.StatWatcher = class extends StatWatcher {
584586
constructor() {
@@ -688,7 +690,7 @@ export class Volume {
688690
let link = this.root;
689691
let i = 0;
690692
while (i < steps.length) {
691-
let step = steps[i];
693+
const step = steps[i];
692694
link = link.getChild(step);
693695
if (!link) return null;
694696

@@ -708,7 +710,7 @@ export class Volume {
708710

709711
// Just like `getLinkOrThrow`, but also dereference/resolves symbolic links.
710712
getResolvedLinkOrThrow(filename: string, funcName?: string): Link {
711-
let link = this.getResolvedLink(filename);
713+
const link = this.getResolvedLink(filename);
712714
if (!link) throwError(ENOENT, funcName, filename);
713715
return link;
714716
}
@@ -793,11 +795,11 @@ export class Volume {
793795
private _toJSON(link = this.root, json = {}, path?: string) {
794796
let isEmpty = true;
795797

796-
for (let name in link.children) {
798+
for (const name in link.children) {
797799
isEmpty = false;
798800

799-
let child = link.getChild(name);
800-
let node = child.getNode();
801+
const child = link.getChild(name);
802+
const node = child.getNode();
801803
if (node.isFile()) {
802804
let filename = child.getPath();
803805
if (path) filename = relative(path, filename);
@@ -819,11 +821,11 @@ export class Volume {
819821
}
820822

821823
toJSON(paths?: TFilePath | TFilePath[], json = {}, isRelative = false) {
822-
let links: Link[] = [];
824+
const links: Link[] = [];
823825

824826
if (paths) {
825827
if (!(paths instanceof Array)) paths = [paths];
826-
for (let path of paths) {
828+
for (const path of paths) {
827829
const filename = pathToFilename(path);
828830
const link = this.getResolvedLink(filename);
829831
if (!link) continue;
@@ -834,7 +836,7 @@ export class Volume {
834836
}
835837

836838
if (!links.length) return json;
837-
for (let link of links) this._toJSON(link, json, isRelative ? link.getPath() : '');
839+
for (const link of links) this._toJSON(link, json, isRelative ? link.getPath() : '');
838840
return json;
839841
}
840842

@@ -1011,7 +1013,7 @@ export class Volume {
10111013
// This `if` branch is from Node.js
10121014
if (length === 0) {
10131015
return process.nextTick(() => {
1014-
callback && callback(null, 0, buffer);
1016+
if (callback) callback(null, 0, buffer);
10151017
});
10161018
}
10171019

@@ -1029,7 +1031,7 @@ export class Volume {
10291031
let result: Buffer | string;
10301032

10311033
const isUserFd = typeof id === 'number';
1032-
let userOwnsFd: boolean = isUserFd && isFd(id);
1034+
const userOwnsFd: boolean = isUserFd && isFd(id);
10331035
let fd: number;
10341036

10351037
if (userOwnsFd) fd = id as number;
@@ -1211,7 +1213,7 @@ export class Volume {
12111213
let position = flagsNum & O_APPEND ? null : 0;
12121214
try {
12131215
while (length > 0) {
1214-
let written = this.writeSync(fd, buf, offset, length, position);
1216+
const written = this.writeSync(fd, buf, offset, length, position);
12151217
offset += written;
12161218
length -= written;
12171219
if (position !== null) position += written;
@@ -1585,7 +1587,7 @@ export class Volume {
15851587

15861588
if (options.withFileTypes) {
15871589
const list: Dirent[] = [];
1588-
for (let name in link.children) {
1590+
for (const name in link.children) {
15891591
list.push(Dirent.build(link.children[name], options.encoding));
15901592
}
15911593
if (!isWin && options.encoding !== 'buffer')
@@ -1598,7 +1600,7 @@ export class Volume {
15981600
}
15991601

16001602
const list: TDataOut[] = [];
1601-
for (let name in link.children) {
1603+
for (const name in link.children) {
16021604
list.push(strToEncoding(name, options.encoding));
16031605
}
16041606

@@ -1807,7 +1809,7 @@ export class Volume {
18071809
}
18081810

18091811
private mkdtempBase(prefix: string, encoding: TEncodingExtended, retry: number = 5): TDataOut {
1810-
let filename = prefix + this.genRndStr();
1812+
const filename = prefix + this.genRndStr();
18111813
try {
18121814
this.mkdirBase(filename, MODE.DIR);
18131815
return strToEncoding(filename, encoding);
@@ -2051,6 +2053,7 @@ export class Volume {
20512053
options = null;
20522054
}
20532055

2056+
// tslint:disable-next-line prefer-const
20542057
let { persistent, recursive, encoding }: IWatchOptions = getDefaultOpts(options);
20552058
if (persistent === undefined) persistent = true;
20562059
if (recursive === undefined) recursive = false;
@@ -2120,6 +2123,7 @@ export class StatWatcher extends EventEmitter {
21202123
}
21212124
}
21222125

2126+
/* tslint:disable no-var-keyword prefer-const */
21232127
// ---------------------------------------- ReadStream
21242128

21252129
export interface IReadStream extends Readable {
@@ -2188,8 +2192,8 @@ function FsReadStream(vol, path, options) {
21882192
}
21892193

21902194
FsReadStream.prototype.open = function() {
2191-
var self = this;
2192-
this._vol.open(this.path, this.flags, this.mode, function(er, fd) {
2195+
var self = this; // tslint:disable-line no-this-assignment
2196+
this._vol.open(this.path, this.flags, this.mode, (er, fd) => {
21932197
if (er) {
21942198
if (self.autoClose) {
21952199
if (self.destroy) self.destroy();
@@ -2233,7 +2237,7 @@ FsReadStream.prototype._read = function(n) {
22332237
if (toRead <= 0) return this.push(null);
22342238

22352239
// the actual read.
2236-
var self = this;
2240+
var self = this; // tslint:disable-line no-this-assignment
22372241
this._vol.read(this.fd, pool, pool.used, toRead, this.pos, onread);
22382242

22392243
// move the pool positions, and internal position for reading.
@@ -2259,7 +2263,7 @@ FsReadStream.prototype._read = function(n) {
22592263
};
22602264

22612265
FsReadStream.prototype._destroy = function(err, cb) {
2262-
this.close(function(err2) {
2266+
this.close(err2 => {
22632267
cb(err || err2);
22642268
});
22652269
};
@@ -2373,8 +2377,8 @@ FsWriteStream.prototype._write = function(data, encoding, cb) {
23732377
});
23742378
}
23752379

2376-
var self = this;
2377-
this._vol.write(this.fd, data, 0, data.length, this.pos, function(er, bytes) {
2380+
var self = this; // tslint:disable-line no-this-assignment
2381+
this._vol.write(this.fd, data, 0, data.length, this.pos, (er, bytes) => {
23782382
if (er) {
23792383
if (self.autoClose && self.destroy) {
23802384
self.destroy();
@@ -2395,7 +2399,7 @@ FsWriteStream.prototype._writev = function(data, cb) {
23952399
});
23962400
}
23972401

2398-
const self = this;
2402+
const self = this; // tslint:disable-line no-this-assignment
23992403
const len = data.length;
24002404
const chunks = new Array(len);
24012405
var size = 0;

‎tslint.json

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{
2+
"extends": "tslint-config-common",
3+
"rules": {
4+
"no-invalid-this": false,
5+
"variable-name": false,
6+
"no-inferrable-types": false,
7+
"curly": false,
8+
"forin": false,
9+
"no-dynamic-delete": false,
10+
"unified-signatures": false
11+
}
12+
}

0 commit comments

Comments
 (0)