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

feat: error codes & validate targets #6

Open
wants to merge 3 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
46 changes: 31 additions & 15 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,19 +23,36 @@ function loop(exports, keys) {
}
}

function throws(code, msg) {
let err = Error(msg);
err.code = code;
throw err;
}

/**
* @param {string} name The package name
* @param {string} entry The target entry, eg "."
* @param {number} [condition] Unmatched condition?
*/
function bail(name, entry, condition) {
throw new Error(
condition
function missing(name, entry, condition) {
throws('ERR_PACKAGE_PATH_NOT_EXPORTED', condition
? `No known conditions for "${entry}" entry in "${name}" package`
: `Missing "${entry}" export in "${name}" package`
);
}

/**
* @param {string} name The package name
* @param {string} entry The target entry, eg "."
* @param {string} value The resolved value
* @returns {void|string}
*/
function validate(name, entry, value) {
if (value[0] != '.' || value[1] != '/') throws('ERR_INVALID_PACKAGE_TARGET', `Invalid "${entry}" export in "${name}" package; targets must start with "./"`);
// if (value[value.length - 1] == '/') throws('ERR_UNSUPPORTED_DIR_IMPORT', `Invalid "${entry}" export in "${name}" package; targets must not resolve to a directory`);
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is where ERR_UNSUPPORTED_DIR_IMPORT would throw.

return value;
}

/**
* @param {string} name the package name
* @param {string} entry the target path/import
Expand Down Expand Up @@ -64,7 +81,7 @@ export function resolve(pkg, entry='.', options={}) {
if (target[0] !== '.') target = './' + target;

if (typeof exports === 'string') {
return target === '.' ? exports : bail(name, target);
return target === '.' ? exports : missing(name, target);
}

let allows = new Set(['default', ...conditions]);
Expand All @@ -79,33 +96,32 @@ export function resolve(pkg, entry='.', options={}) {
}

if (isSingle) {
return target === '.'
? loop(exports, allows) || bail(name, target, 1)
: bail(name, target);
if (target !== '.') return missing(name, target);
tmp = loop(exports, allows) || missing(name, target, 1);
return validate(name, target, tmp);
}

if (tmp = exports[target]) {
return loop(tmp, allows) || bail(name, target, 1);
tmp = loop(tmp, allows) || missing(name, target, 1);
return validate(name, target, tmp);
}

for (key in exports) {
tmp = key[key.length - 1];
if (tmp === '/' && target.startsWith(key)) {
return (tmp = loop(exports[key], allows))
? (tmp + target.substring(key.length))
: bail(name, target, 1);
tmp = loop(exports[key], allows) || missing(name, target, 1);
return validate(name, target, tmp + target.substring(key.length));
}
if (tmp === '*' && target.startsWith(key.slice(0, -1))) {
// do not trigger if no *content* to inject
if (target.substring(key.length - 1).length > 0) {
return (tmp = loop(exports[key], allows))
? tmp.replace('*', target.substring(key.length - 1))
: bail(name, target, 1);
tmp = loop(exports[key], allows) || missing(name, target, 1);
return validate(name, target, tmp.replace('*', target.substring(key.length - 1)));
}
}
}

return bail(name, target);
return missing(name, target);
}
}

Expand Down
Loading