Skip to content

Commit

Permalink
fix: improve typescript defs
Browse files Browse the repository at this point in the history
  • Loading branch information
43081j committed Oct 15, 2022
1 parent 651e578 commit 6552700
Show file tree
Hide file tree
Showing 6 changed files with 235 additions and 75 deletions.
8 changes: 6 additions & 2 deletions lib/Resolver.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ const {

/** @typedef {import("./ResolverFactory").ResolveOptions} ResolveOptions */

/** @typedef {Error & {details?: string}} ErrorWithDetail */

/** @typedef {(err: ErrorWithDetail|null, res?: string|false, req?: ResolveRequest) => void} ResolveCallback */

/**
* @typedef {Object} FileSystemStats
* @property {function(): boolean} isDirectory
Expand Down Expand Up @@ -254,7 +258,7 @@ class Resolver {
* @param {string} path context path
* @param {string} request request string
* @param {ResolveContext} resolveContext resolve context
* @param {function(Error | null, (string|false)=, ResolveRequest=): void} callback callback function
* @param {ResolveCallback} callback callback function
* @returns {void}
*/
resolve(context, path, request, resolveContext, callback) {
Expand Down Expand Up @@ -304,7 +308,7 @@ class Resolver {

const finishWithoutResolve = log => {
/**
* @type {Error & {details?: string}}
* @type {ErrorWithDetail}
*/
const error = new Error("Can't " + message);
error.details = log.join("\n");
Expand Down
106 changes: 79 additions & 27 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ const ResolverFactory = require("./ResolverFactory");
/** @typedef {import("./PnpPlugin").PnpApiImpl} PnpApi */
/** @typedef {import("./Resolver")} Resolver */
/** @typedef {import("./Resolver").FileSystem} FileSystem */
/** @typedef {import("./Resolver").ResolveCallback} ResolveCallback */
/** @typedef {import("./Resolver").ResolveContext} ResolveContext */
/** @typedef {import("./Resolver").ResolveRequest} ResolveRequest */
/** @typedef {import("./ResolverFactory").Plugin} Plugin */
Expand All @@ -28,41 +29,86 @@ const asyncResolver = ResolverFactory.createResolver({
extensions: [".js", ".json", ".node"],
fileSystem: nodeFileSystem
});
function resolve(context, path, request, resolveContext, callback) {
if (typeof context === "string") {
callback = resolveContext;
resolveContext = request;
request = path;
path = context;
context = nodeContext;
}
if (typeof callback !== "function") {
callback = resolveContext;
}
asyncResolver.resolve(context, path, request, resolveContext, callback);
}

/**
* @type {{
* (context: object, path: string, request: string, resolveContext: ResolveContext, callback: ResolveCallback): void;
* (context: object, path: string, request: string, callback: ResolveCallback): void;
* (path: string, request: string, resolveContext: ResolveContext, callback: ResolveCallback): void;
* (path: string, request: string, callback: ResolveCallback): void;
* }}
*/
const resolve =
/**
* @param {object|string} context
* @param {string} path
* @param {string|ResolveContext|ResolveCallback} request
* @param {ResolveContext|ResolveCallback=} resolveContext
* @param {ResolveCallback=} callback
*/
(context, path, request, resolveContext, callback) => {
if (typeof context === "string") {
callback = /** @type {ResolveCallback} */ (resolveContext);
resolveContext = /** @type {ResolveContext} */ (request);
request = path;
path = context;
context = nodeContext;
}
if (typeof callback !== "function") {
callback = /** @type {ResolveCallback} */ (resolveContext);
}
asyncResolver.resolve(
context,
path,
/** @type {string} */ (request),
/** @type {ResolveContext} */ (resolveContext),
/** @type {ResolveCallback} */ (callback)
);
};

const syncResolver = ResolverFactory.createResolver({
conditionNames: ["node"],
extensions: [".js", ".json", ".node"],
useSyncFileSystemCalls: true,
fileSystem: nodeFileSystem
});
function resolveSync(context, path, request) {
if (typeof context === "string") {
request = path;
path = context;
context = nodeContext;
}
return syncResolver.resolveSync(context, path, request);
}

/**
* @type {{
* (context: object, path: string, request: string): string|false;
* (path: string, request: string): string|false;
* }}
*/
const resolveSync =
/**
* @param {object|string} context
* @param {string} path
* @param {string=} request
*/
(context, path, request) => {
if (typeof context === "string") {
request = path;
path = context;
context = nodeContext;
}
return syncResolver.resolveSync(
context,
path,
/** @type {string} */ (request)
);
};

/** @typedef {Omit<ResolveOptions, "fileSystem"> & Partial<Pick<ResolveOptions, "fileSystem">>} ResolveOptionsOptionalFS */

/**
* @param {ResolveOptionsOptionalFS} options Resolver options
* @returns {typeof resolve} Resolver function
*/
function create(options) {
options = {
const resolver = ResolverFactory.createResolver({
fileSystem: nodeFileSystem,
...options
};
const resolver = ResolverFactory.createResolver(options);
});
return function (context, path, request, resolveContext, callback) {
if (typeof context === "string") {
callback = resolveContext;
Expand All @@ -78,13 +124,19 @@ function create(options) {
};
}

/**
* @param {ResolveOptionsOptionalFS} options Resolver options
* @returns {{
* (context: object, path: string, request: string): string|false;
* (path: string, request: string): string|false;
* }} Resolver function
*/
function createSync(options) {
options = {
const resolver = ResolverFactory.createResolver({
useSyncFileSystemCalls: true,
fileSystem: nodeFileSystem,
...options
};
const resolver = ResolverFactory.createResolver(options);
});
return function (context, path, request) {
if (typeof context === "string") {
request = path;
Expand Down
53 changes: 36 additions & 17 deletions test/resolve.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ function testResolve(name, context, moduleName, result) {
resolve(context, moduleName, function (err, filename) {
if (err) return done(err);
should.exist(filename);
filename.should.equal(result);
/** @type {string} */ (filename).should.equal(result);
done();
});
});
Expand All @@ -48,7 +48,7 @@ function testResolveContext(name, context, moduleName, result) {
asyncContextResolve(context, moduleName, function (err, filename) {
if (err) done(err);
should.exist(filename);
filename.should.equal(result);
/** @type {string} */ (filename).should.equal(result);
done();
});
});
Expand Down Expand Up @@ -269,7 +269,7 @@ describe("resolve", function () {
function (err, filename) {
if (err) done(err);
should.exist(filename);
filename.should.equal(
/** @type {string} */ (filename).should.equal(
path.resolve(issue238, "./src/common/config/myObjectFile.js")
);
done();
Expand All @@ -281,7 +281,9 @@ describe("resolve", function () {
preferRelativeResolve(fixtures, "main1.js", function (err, filename) {
if (err) done(err);
should.exist(filename);
filename.should.equal(path.join(fixtures, "main1.js"));
/** @type {string} */ (filename).should.equal(
path.join(fixtures, "main1.js")
);
done();
});
});
Expand All @@ -290,29 +292,46 @@ describe("resolve", function () {
preferRelativeResolve(fixtures, "m1/a.js", function (err, filename) {
if (err) done(err);
should.exist(filename);
filename.should.equal(path.join(fixtures, "node_modules", "m1", "a.js"));
/** @type {string} */ (filename).should.equal(
path.join(fixtures, "node_modules", "m1", "a.js")
);
done();
});
});

it("should not crash when passing undefined as path", done => {
resolve(fixtures, undefined, err => {
err.should.be.instanceof(Error);
done();
});
resolve(
fixtures,
/** @type {string} */ (/** @type {unknown} */ (undefined)),
err => {
/** @type {Error} */ (err).should.be.instanceof(Error);
done();
}
);
});

it("should not crash when passing undefined as context", done => {
resolve({}, undefined, "./test/resolve.js", err => {
err.should.be.instanceof(Error);
done();
});
resolve(
{},
/** @type {string} */ (/** @type {unknown} */ (undefined)),
"./test/resolve.js",
err => {
/** @type {Error} */ (err).should.be.instanceof(Error);
done();
}
);
});

it("should not crash when passing undefined everywhere", done => {
resolve(undefined, undefined, undefined, undefined, err => {
err.should.be.instanceof(Error);
done();
});
resolve(
/** @type {object} */ (undefined),
/** @type {string} */ (/** @type {unknown} */ (undefined)),
/** @type {string} */ (/** @type {unknown} */ (undefined)),
/** @type {object} */ (/** @type {unknown} */ (undefined)),
err => {
/** @type {Error} */ (err).should.be.instanceof(Error);
done();
}
);
});
});
6 changes: 4 additions & 2 deletions test/simple.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,10 @@ describe("simple", function () {
new Error([err.message, err.stack, err.details].join("\n"))
);
should.exist(filename);
filename.should.have.type("string");
filename.should.be.eql(path.join(__dirname, "..", "lib", "index.js"));
/** @type {string} */ (filename).should.have.type("string");
/** @type {string} */ (filename).should.be.eql(
path.join(__dirname, "..", "lib", "index.js")
);
done();
});
});
Expand Down
12 changes: 8 additions & 4 deletions test/symlink.js
Original file line number Diff line number Diff line change
Expand Up @@ -214,15 +214,19 @@ describe("symlink", function () {
resolve(pathToIt[0], pathToIt[1], function (err, filename) {
if (err) return done(err);
should.exist(filename);
filename.should.have.type("string");
filename.should.be.eql(path.join(__dirname, "..", "lib", "index.js"));
/** @type {string} */ (filename).should.have.type("string");
/** @type {string} */ (filename).should.be.eql(
path.join(__dirname, "..", "lib", "index.js")
);
resolveWithoutSymlinks(pathToIt[0], pathToIt[1], function (
err,
filename
) {
if (err) return done(err);
filename.should.have.type("string");
filename.should.be.eql(path.resolve(pathToIt[0], pathToIt[1]));
/** @type {string} */ (filename).should.have.type("string");
/** @type {string} */ (filename).should.be.eql(
path.resolve(pathToIt[0], pathToIt[1])
);
done();
});
});
Expand Down
Loading

0 comments on commit 6552700

Please sign in to comment.