Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

pkg: add type lints. #5

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 9 additions & 4 deletions lib/backend.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,12 @@ const features = require('./features');
* Expose
*/

if (features.HAS_ALL)
module.exports = require('./modern');
else
module.exports = require('./legacy');
if (features.HAS_ALL) {
const modern = require('./modern');

module.exports = modern;
} else {
const legacy = require('./legacy');

module.exports = legacy;
}
61 changes: 52 additions & 9 deletions lib/compat.js
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,10 @@ function mkdirpSync(dir, mode) {
}
}

/**
* @returns {Array} [args, recursive]
*/

function mkdirArgs(path, options) {
let mode = null;
let recursive = false;
Expand Down Expand Up @@ -261,13 +265,24 @@ async function mkdir(path, options) {
return call(fs.mkdir, args);
}

/**
* Synchronously creates a directory.
* @param {fs.PathLike} path
* @param {{
* recursive?: boolean;
* mode?: string | number;
* } | number} [options]
* @returns {string | void}
*/

function mkdirSync(path, options) {
const [args, recursive] = mkdirArgs(path, options);

if (recursive)
return mkdirpSync(...args);

return fs.mkdirSync(...args);
const [argPath, argMode] = args;
return fs.mkdirSync(argPath, argMode);
}

/*
Expand All @@ -281,11 +296,19 @@ async function open(...args) {
return call(fs.open, args);
}

function openSync(...args) {
if (args[1] == null)
args[1] = 'r';
/**
* Synchronously opens a file.
* @param {fs.PathLike} path
* @param {string | number} [flags]
* @param {string | number} [mode]
* @returns {number}
*/

function openSync(path, flags, mode) {
if (flags == null)
flags = 'r';

return fs.openSync(...args);
return fs.openSync(path, flags, mode);
}

/*
Expand All @@ -299,6 +322,7 @@ async function read(...args) {

function readSync(...args) {
args[1] = toBuffer(args[1]);
// @ts-ignore
return fs.readSync(...args);
}

Expand Down Expand Up @@ -330,6 +354,7 @@ async function readdir(...args) {
function readdirSync(...args) {
const [dir, options] = args;
const withFileTypes = options && options.withFileTypes;
// @ts-ignore
const list = fs.readdirSync(...args);

if (!withFileTypes || fs.Dirent)
Expand Down Expand Up @@ -429,7 +454,9 @@ class Dir {
_error() {
const err = new Error('Directory handle was closed');

// @ts-ignore
err.code = 'ERR_DIR_CLOSED';
// @ts-ignore
err.name = `Error [${err.code}]`;

if (Error.captureStackTrace)
Expand Down Expand Up @@ -524,12 +551,26 @@ realpath.native = async function(...args) {
return call(fs.realpath, args);
};

function realpathSync(...args) {
return fs.realpathSync(...args);
/**
* Returns the resolved pathname.
* @param {fs.PathLike} p
* @param {fs.EncodingOption} [options]
* @returns {string | Buffer}
*/

function realpathSync(p, options) {
return fs.realpathSync(p, options);
}

realpathSync.native = function(...args) {
return fs.realpathSync(...args);
/**
* Returns the resolved pathname.
* @param {fs.PathLike} p
* @param {fs.EncodingOption} [options]
* @returns {string | Buffer}
*/

realpathSync.native = function(p, options) {
return fs.realpathSync(p, options);
};

/*
Expand Down Expand Up @@ -825,6 +866,7 @@ function writeSync(...args) {
if (typeof args[1] !== 'string')
args[1] = toBuffer(args[1]);

// @ts-ignore
return fs.writeSync(...args);
}

Expand All @@ -843,6 +885,7 @@ function writeFileSync(...args) {
if (typeof args[1] !== 'string')
args[1] = toBuffer(args[1]);

// @ts-ignore
return fs.writeFileSync(...args);
}

Expand Down
17 changes: 17 additions & 0 deletions lib/error.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,22 @@ class ArgError extends TypeError {
}
}

/**
* TypeError w/ code.
*/

class TypeCodeError extends TypeError {
constructor(msg, code = '') {
super(msg);

this.code = code;
this.name = `TypeError [${this.code}]`;

if (Error.captureStackTrace)
Error.captureStackTrace(this, this.constructor);
}
}

/**
* FSError
*/
Expand Down Expand Up @@ -143,3 +159,4 @@ FSError.EISDIR = {

exports.ArgError = ArgError;
exports.FSError = FSError;
exports.TypeCodeError = TypeCodeError;
Loading