Skip to content

Commit

Permalink
[Breaking] condense some booleans into enum type arg
Browse files Browse the repository at this point in the history
  • Loading branch information
ljharb committed Sep 19, 2024
1 parent 3caf579 commit d205d23
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 28 deletions.
42 changes: 26 additions & 16 deletions api.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -26,23 +26,22 @@ const help = readFileSync(join(import.meta.dirname, './help.txt'), 'utf8');
const {
positionals,
values: {
bound: isBound,
property: isProperty,
type,
'skip-shim-returns-polyfill': skipShimPolyfill,
'skip-auto-shim': skipAutoShim,
multi,
'ignore-dirs': rawIgnoreDirs,
},
// eslint-disable-next-line no-extra-parens, max-len
} = /** @type {{ positionals: string[], values: { bound: boolean, property: boolean, 'skip-shim-returns-polyfill': boolean, 'skip-auto-shim': boolean, 'ignore-dirs': string[], multi: boolean } }} */ (
} = /** @type {{ positionals: string[], values: { type: 'method' | 'function' | 'property' | 'constructor' | 'multi', 'skip-shim-returns-polyfill': boolean, 'skip-auto-shim': boolean, 'ignore-dirs': string[], multi: boolean } }} */ (
pargs(help, import.meta.filename, {
allowPositionals: true,
options: {
bound: { type: 'boolean' },
property: { type: 'boolean' },
type: {
type: 'string',
default: 'method',
},
'skip-shim-returns-polyfill': { type: 'boolean' },
'skip-auto-shim': { type: 'boolean' },
multi: { type: 'boolean' },
'ignore-dirs': {
type: 'string',
multiple: true,
Expand All @@ -52,7 +51,7 @@ const {
})
);

let isMulti = multi;
let isMulti = type === 'multi';

const ignoreDirs = ['node_modules', 'coverage', 'helpers', 'test', 'aos'].concat(rawIgnoreDirs.flatMap((x) => x.split(',')));

Expand Down Expand Up @@ -81,12 +80,23 @@ if (moduleNames.length < 1) {

const mainIsJSON = path.extname(require.resolve(process.cwd())) === '.json';
if (isMulti && !mainIsJSON) {
console.error('Error: --multi requires package.json main to be a JSON file');
console.error('Error: --type=multi requires package.json main to be a JSON file');
process.exit(3);
}
if (!isMulti && mainIsJSON) {
isMulti = true;
console.error('# automatic `--multi` mode enabled');
console.error('# automatic `--type=multi` mode enabled');
}

if (
type !== 'property'
&& type !== 'method'
&& type !== 'constructor'
&& type !== 'function'
&& type !== 'multi'
) {
console.error('`type` must be one of `method`, `function`, `property`, `constructor`, or `multi`');
process.exit(4);
}
}

Expand Down Expand Up @@ -133,19 +143,19 @@ const doValidation = function doActualValidation(t, packageDir, name) {
const prefix = isMulti ? `${path.basename(packageDir)}: ` : '';

t.test(`${prefix}export`, (st) => {
if (isProperty) {
if (type === 'property') {
st.comment('# SKIP module that is a data property need not be a function');
} else if (isMulti) {
st.notEqual(typeof module, 'undefined', 'module is not `undefined`');
} else {
st.equal(typeof module, 'function', 'module is a function (pass `--property` to skip this test)');
st.equal(typeof module, 'function', 'module is a function (pass `--type=property` to skip this test)');
}

st.test('module is NOT bound (pass `--bound` to skip this test)', { skip: isBound }, (st2) => {
st.test('module is NOT bound (pass `--type=method` to skip this test)', { skip: type === 'method' }, (st2) => {
st2.equal(module, getPolyfill(), 'module.exports === getPolyfill()');
st2.end();
});
st.test('module is bound (do not pass `--bound` to skip this test)', { skip: !isBound }, (st2) => {
st.test('module is bound (pass a `--type=` other than `method` to skip this test)', { skip: type !== 'method' }, (st2) => {
st2.notEqual(module, getPolyfill(), 'module.exports !== getPolyfill()');
st2.end();
});
Expand All @@ -160,12 +170,12 @@ const doValidation = function doActualValidation(t, packageDir, name) {
{ skip: isMulti },
);

if (isProperty) {
if (type === 'property') {
st.comment('# SKIP implementation that is a data property need not be a function');
} else if (isMulti) {
st.notEqual(typeof implementation, 'undefined', 'implementation is not `undefined`');
} else {
st.equal(typeof implementation, 'function', 'implementation is a function (pass `--property` to skip this test)');
st.equal(typeof implementation, 'function', 'implementation is a function (pass `--type=property` to skip this test)');
}

st.end();
Expand Down
22 changes: 10 additions & 12 deletions help.txt
Original file line number Diff line number Diff line change
@@ -1,20 +1,18 @@
Usage: es-shim-api [options] <optional space-separated module names>

Options:
--multi indicate that the package contains multiple shims
[boolean]

--bound indicate that the function the package is implementing depends on having a receiver (a “this” value)
[boolean]
--type indicate which type of polyfill/shim this is:
- `method`: receiver-sensitive method (default)
- `function`: non-receiver-sensitive function
- `property`: non-function data property
- `constructor`: constructor
- `multi`: a package that contains multiple shims

--property indicate that the polyfill is a normal property, instead of a function
[boolean]

--skip-shim-returns-polyfill indicate that the `shim` module does not return the same value as `polyfill`, by design
[boolean]
--skip-shim-returns-polyfill indicate that `shim` does not return the same
[boolean] value as `polyfill`, by design

--skip-auto-shim skip testing that `auto` invokes `shim`
[boolean]

--ignore-dirs <path> File path to write output to. If omitted, output will be printed to stdout.
[string]
--ignore-dirs <path> File path to write output to.
[string] If omitted, output will be printed to stdout.

0 comments on commit d205d23

Please sign in to comment.