Skip to content

Commit c9a0fd6

Browse files
committed
fix: 🐛 replace throwError fn w/ inline throw createError() calls
TypeScript treats `throw` the same as `return` in regards to scope, but doesn't apply this to throws inside functions.
1 parent 3e3c992 commit c9a0fd6

File tree

1 file changed

+40
-44
lines changed

1 file changed

+40
-44
lines changed

src/volume.ts

Lines changed: 40 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -152,10 +152,6 @@ function createError(errorCode: string, func = '', path = '', path2 = '', Constr
152152
return error;
153153
}
154154

155-
function throwError(errorCode: string, func = '', path = '', path2 = '', Constructor = Error) {
156-
throw createError(errorCode, func, path, path2, Constructor);
157-
}
158-
159155
// ---------------------------------------- Flags
160156

161157
// List of file `flags` as defined by Node.
@@ -695,12 +691,12 @@ export class Volume {
695691
getLinkOrThrow(filename: string, funcName?: string): Link {
696692
const steps = filenameToSteps(filename);
697693
const link = this.getLink(steps);
698-
if (!link) throwError(ENOENT, funcName, filename);
694+
if (!link) throw createError(ENOENT, funcName, filename);
699695
return link;
700696
}
701697

702698
// Just like `getLink`, but also dereference/resolves symbolic links.
703-
getResolvedLink(filenameOrSteps: string | string[]): Link {
699+
getResolvedLink(filenameOrSteps: string | string[]): Link | null {
704700
let steps: string[] = typeof filenameOrSteps === 'string' ? filenameToSteps(filenameOrSteps) : filenameOrSteps;
705701

706702
let link = this.root;
@@ -727,7 +723,7 @@ export class Volume {
727723
// Just like `getLinkOrThrow`, but also dereference/resolves symbolic links.
728724
getResolvedLinkOrThrow(filename: string, funcName?: string): Link {
729725
const link = this.getResolvedLink(filename);
730-
if (!link) throwError(ENOENT, funcName, filename);
726+
if (!link) throw createError(ENOENT, funcName, filename);
731727
return link;
732728
}
733729

@@ -745,7 +741,7 @@ export class Volume {
745741
// Just like `getLinkOrThrow`, but also verifies that the link is a directory.
746742
private getLinkAsDirOrThrow(filename: string, funcName?: string): Link {
747743
const link = this.getLinkOrThrow(filename, funcName);
748-
if (!link.getNode().isDirectory()) throwError(ENOTDIR, funcName, filename);
744+
if (!link.getNode().isDirectory()) throw createError(ENOTDIR, funcName, filename);
749745
return link;
750746
}
751747

@@ -757,8 +753,8 @@ export class Volume {
757753
private getLinkParentAsDirOrThrow(filenameOrSteps: string | string[], funcName?: string): Link {
758754
const steps = filenameOrSteps instanceof Array ? filenameOrSteps : filenameToSteps(filenameOrSteps);
759755
const link = this.getLinkParent(steps);
760-
if (!link) throwError(ENOENT, funcName, sep + steps.join(sep));
761-
if (!link.getNode().isDirectory()) throwError(ENOTDIR, funcName, sep + steps.join(sep));
756+
if (!link) throw createError(ENOENT, funcName, sep + steps.join(sep));
757+
if (!link.getNode().isDirectory()) throw createError(ENOTDIR, funcName, sep + steps.join(sep));
762758
return link;
763759
}
764760

@@ -769,7 +765,7 @@ export class Volume {
769765
private getFileByFdOrThrow(fd: number, funcName?: string): File {
770766
if (!isFd(fd)) throw TypeError(ERRSTR.FD);
771767
const file = this.getFileByFd(fd);
772-
if (!file) throwError(EBADF, funcName);
768+
if (!file) throw createError(EBADF, funcName);
773769
return file;
774770
}
775771

@@ -793,7 +789,7 @@ export class Volume {
793789
}
794790
}
795791

796-
throwError(ENOENT, 'getNodeByIdOrCreate', pathToFilename(id));
792+
throw createError(ENOENT, 'getNodeByIdOrCreate', pathToFilename(id));
797793
}
798794
}
799795

@@ -901,15 +897,15 @@ export class Volume {
901897
// Resolve symlinks.
902898
let realLink: Link = link;
903899
if (resolveSymlinks) realLink = this.resolveSymlinks(link);
904-
if (!realLink) throwError(ENOENT, 'open', link.getPath());
900+
if (!realLink) throw createError(ENOENT, 'open', link.getPath());
905901

906902
const node = realLink.getNode();
907-
if (node.isDirectory() && flagsNum !== FLAGS.r) throwError(EISDIR, 'open', link.getPath());
903+
if (node.isDirectory() && flagsNum !== FLAGS.r) throw createError(EISDIR, 'open', link.getPath());
908904

909905
// Check node permissions
910906
if (!(flagsNum & O_WRONLY)) {
911907
if (!node.canRead()) {
912-
throwError(EACCES, 'open', link.getPath());
908+
throw createError(EACCES, 'open', link.getPath());
913909
}
914910
}
915911
if (flagsNum & O_RDWR) {
@@ -932,21 +928,21 @@ export class Volume {
932928
if (!link && flagsNum & O_CREAT) {
933929
// const dirLink: Link = this.getLinkParent(steps);
934930
const dirLink: Link = this.getResolvedLink(steps.slice(0, steps.length - 1));
935-
// if(!dirLink) throwError(ENOENT, 'open', filename);
936-
if (!dirLink) throwError(ENOENT, 'open', sep + steps.join(sep));
931+
// if(!dirLink) throw createError(ENOENT, 'open', filename);
932+
if (!dirLink) throw createError(ENOENT, 'open', sep + steps.join(sep));
937933

938934
if (flagsNum & O_CREAT && typeof modeNum === 'number') {
939935
link = this.createLink(dirLink, steps[steps.length - 1], false, modeNum);
940936
}
941937
}
942938

943939
if (link) return this.openLink(link, flagsNum, resolveSymlinks);
944-
throwError(ENOENT, 'open', filename);
940+
throw createError(ENOENT, 'open', filename);
945941
}
946942

947943
private openBase(filename: string, flagsNum: number, modeNum: number, resolveSymlinks: boolean = true): number {
948944
const file = this.openFile(filename, flagsNum, modeNum, resolveSymlinks);
949-
if (!file) throwError(ENOENT, 'open', filename);
945+
if (!file) throw createError(ENOENT, 'open', filename);
950946
return file.fd;
951947
}
952948

@@ -1058,7 +1054,7 @@ export class Volume {
10581054

10591055
if (link) {
10601056
const node = link.getNode();
1061-
if (node.isDirectory()) throwError(EISDIR, 'open', link.getPath());
1057+
if (node.isDirectory()) throw createError(EISDIR, 'open', link.getPath());
10621058
}
10631059

10641060
fd = this.openSync(id as TFilePath, flagsNum);
@@ -1268,18 +1264,18 @@ export class Volume {
12681264
private linkBase(filename1: string, filename2: string) {
12691265
const steps1 = filenameToSteps(filename1);
12701266
const link1 = this.getLink(steps1);
1271-
if (!link1) throwError(ENOENT, 'link', filename1, filename2);
1267+
if (!link1) throw createError(ENOENT, 'link', filename1, filename2);
12721268

12731269
const steps2 = filenameToSteps(filename2);
12741270

12751271
// Check new link directory exists.
12761272
const dir2 = this.getLinkParent(steps2);
1277-
if (!dir2) throwError(ENOENT, 'link', filename1, filename2);
1273+
if (!dir2) throw createError(ENOENT, 'link', filename1, filename2);
12781274

12791275
const name = steps2[steps2.length - 1];
12801276

12811277
// Check if new file already exists.
1282-
if (dir2.getChild(name)) throwError(EEXIST, 'link', filename1, filename2);
1278+
if (dir2.getChild(name)) throw createError(EEXIST, 'link', filename1, filename2);
12831279

12841280
const node = link1.getNode();
12851281
node.nlink++;
@@ -1291,12 +1287,12 @@ export class Volume {
12911287

12921288
if (flags & COPYFILE_EXCL) {
12931289
if (this.existsSync(dest)) {
1294-
throwError(EEXIST, 'copyFile', src, dest);
1290+
throw createError(EEXIST, 'copyFile', src, dest);
12951291
}
12961292
}
12971293

12981294
if (flags & COPYFILE_FICLONE_FORCE) {
1299-
throwError(ENOSYS, 'copyFile', src, dest);
1295+
throw createError(ENOSYS, 'copyFile', src, dest);
13001296
}
13011297

13021298
this.writeFileBase(dest, buf, FLAGS.w, MODE.DEFAULT);
@@ -1346,7 +1342,7 @@ export class Volume {
13461342
private unlinkBase(filename: string) {
13471343
const steps = filenameToSteps(filename);
13481344
const link = this.getLink(steps);
1349-
if (!link) throwError(ENOENT, 'unlink', filename);
1345+
if (!link) throw createError(ENOENT, 'unlink', filename);
13501346

13511347
// TODO: Check if it is file, dir, other...
13521348

@@ -1378,12 +1374,12 @@ export class Volume {
13781374

13791375
// Check if directory exists, where we about to create a symlink.
13801376
const dirLink = this.getLinkParent(pathSteps);
1381-
if (!dirLink) throwError(ENOENT, 'symlink', targetFilename, pathFilename);
1377+
if (!dirLink) throw createError(ENOENT, 'symlink', targetFilename, pathFilename);
13821378

13831379
const name = pathSteps[pathSteps.length - 1];
13841380

13851381
// Check if new file already exists.
1386-
if (dirLink.getChild(name)) throwError(EEXIST, 'symlink', targetFilename, pathFilename);
1382+
if (dirLink.getChild(name)) throw createError(EEXIST, 'symlink', targetFilename, pathFilename);
13871383

13881384
// Create symlink.
13891385
const symlink: Link = dirLink.createChild(name);
@@ -1411,11 +1407,11 @@ export class Volume {
14111407
const steps = filenameToSteps(filename);
14121408
const link: Link = this.getLink(steps);
14131409
// TODO: this check has to be perfomed by `lstat`.
1414-
if (!link) throwError(ENOENT, 'realpath', filename);
1410+
if (!link) throw createError(ENOENT, 'realpath', filename);
14151411

14161412
// Resolve symlinks.
14171413
const realLink = this.resolveSymlinks(link);
1418-
if (!realLink) throwError(ENOENT, 'realpath', filename);
1414+
if (!realLink) throw createError(ENOENT, 'realpath', filename);
14191415

14201416
return strToEncoding(realLink.getPath(), encoding);
14211417
}
@@ -1436,7 +1432,7 @@ export class Volume {
14361432
private lstatBase(filename: string, bigint: true): Stats<bigint>;
14371433
private lstatBase(filename: string, bigint: boolean = false): Stats {
14381434
const link: Link = this.getLink(filenameToSteps(filename));
1439-
if (!link) throwError(ENOENT, 'lstat', filename);
1435+
if (!link) throw createError(ENOENT, 'lstat', filename);
14401436
return Stats.build(link.getNode(), bigint);
14411437
}
14421438

@@ -1459,11 +1455,11 @@ export class Volume {
14591455
private statBase(filename: string, bigint: true): Stats<bigint>;
14601456
private statBase(filename: string, bigint: boolean = false): Stats {
14611457
let link: Link = this.getLink(filenameToSteps(filename));
1462-
if (!link) throwError(ENOENT, 'stat', filename);
1458+
if (!link) throw createError(ENOENT, 'stat', filename);
14631459

14641460
// Resolve symlinks.
14651461
link = this.resolveSymlinks(link);
1466-
if (!link) throwError(ENOENT, 'stat', filename);
1462+
if (!link) throw createError(ENOENT, 'stat', filename);
14671463

14681464
return Stats.build(link.getNode(), bigint);
14691465
}
@@ -1487,7 +1483,7 @@ export class Volume {
14871483
private fstatBase(fd: number, bigint: true): Stats<bigint>;
14881484
private fstatBase(fd: number, bigint: boolean = false): Stats {
14891485
const file = this.getFileByFd(fd);
1490-
if (!file) throwError(EBADF, 'fstat');
1486+
if (!file) throw createError(EBADF, 'fstat');
14911487
return Stats.build(file.node, bigint);
14921488
}
14931489

@@ -1507,15 +1503,15 @@ export class Volume {
15071503

15081504
private renameBase(oldPathFilename: string, newPathFilename: string) {
15091505
const link: Link = this.getLink(filenameToSteps(oldPathFilename));
1510-
if (!link) throwError(ENOENT, 'rename', oldPathFilename, newPathFilename);
1506+
if (!link) throw createError(ENOENT, 'rename', oldPathFilename, newPathFilename);
15111507

15121508
// TODO: Check if it is directory, if non-empty, we cannot move it, right?
15131509

15141510
const newPathSteps = filenameToSteps(newPathFilename);
15151511

15161512
// Check directory exists for the new location.
15171513
const newPathDirLink: Link = this.getLinkParent(newPathSteps);
1518-
if (!newPathDirLink) throwError(ENOENT, 'rename', oldPathFilename, newPathFilename);
1514+
if (!newPathDirLink) throw createError(ENOENT, 'rename', oldPathFilename, newPathFilename);
15191515

15201516
// TODO: Also treat cases with directories and symbolic links.
15211517
// TODO: See: http://man7.org/linux/man-pages/man2/rename.2.html
@@ -1622,10 +1618,10 @@ export class Volume {
16221618
private readdirBase(filename: string, options: IReaddirOptions): TDataOut[] | Dirent[] {
16231619
const steps = filenameToSteps(filename);
16241620
const link: Link = this.getResolvedLink(steps);
1625-
if (!link) throwError(ENOENT, 'readdir', filename);
1621+
if (!link) throw createError(ENOENT, 'readdir', filename);
16261622

16271623
const node = link.getNode();
1628-
if (!node.isDirectory()) throwError(ENOTDIR, 'scandir', filename);
1624+
if (!node.isDirectory()) throw createError(ENOTDIR, 'scandir', filename);
16291625

16301626
if (options.withFileTypes) {
16311627
const list: Dirent[] = [];
@@ -1669,7 +1665,7 @@ export class Volume {
16691665
const link = this.getLinkOrThrow(filename, 'readlink');
16701666
const node = link.getNode();
16711667

1672-
if (!node.isSymlink()) throwError(EINVAL, 'readlink', filename);
1668+
if (!node.isSymlink()) throw createError(EINVAL, 'readlink', filename);
16731669

16741670
const str = sep + node.symlink.join(sep);
16751671
return strToEncoding(str, encoding);
@@ -1790,14 +1786,14 @@ export class Volume {
17901786

17911787
// This will throw if user tries to create root dir `fs.mkdirSync('/')`.
17921788
if (!steps.length) {
1793-
throwError(EISDIR, 'mkdir', filename);
1789+
throw createError(EISDIR, 'mkdir', filename);
17941790
}
17951791

17961792
const dir = this.getLinkParentAsDirOrThrow(filename, 'mkdir');
17971793

17981794
// Check path already exists.
17991795
const name = steps[steps.length - 1];
1800-
if (dir.getChild(name)) throwError(EEXIST, 'mkdir', filename);
1796+
if (dir.getChild(name)) throw createError(EEXIST, 'mkdir', filename);
18011797

18021798
dir.createChild(name, this.createNode(true, modeNum));
18031799
}
@@ -1813,12 +1809,12 @@ export class Volume {
18131809
for (let i = 0; i < steps.length; i++) {
18141810
const step = steps[i];
18151811

1816-
if (!link.getNode().isDirectory()) throwError(ENOTDIR, 'mkdir', link.getPath());
1812+
if (!link.getNode().isDirectory()) throw createError(ENOTDIR, 'mkdir', link.getPath());
18171813

18181814
const child = link.getChild(step);
18191815
if (child) {
18201816
if (child.getNode().isDirectory()) link = child;
1821-
else throwError(ENOTDIR, 'mkdir', child.getPath());
1817+
else throw createError(ENOTDIR, 'mkdir', child.getPath());
18221818
} else {
18231819
link = link.createChild(step, this.createNode(true, modeNum));
18241820
}
@@ -1895,7 +1891,7 @@ export class Volume {
18951891
const link = this.getLinkAsDirOrThrow(filename, 'rmdir');
18961892

18971893
// Check directory is empty.
1898-
if (link.length) throwError(ENOTEMPTY, 'rmdir', filename);
1894+
if (link.length) throw createError(ENOTEMPTY, 'rmdir', filename);
18991895

19001896
this.deleteLink(link);
19011897
}

0 commit comments

Comments
 (0)