From d205d23b982292d1de7a78d9cbcddd085289533d Mon Sep 17 00:00:00 2001 From: Jordan Harband Date: Thu, 19 Sep 2024 14:13:21 -0700 Subject: [PATCH] [Breaking] condense some booleans into enum `type` arg --- api.mjs | 42 ++++++++++++++++++++++++++---------------- help.txt | 22 ++++++++++------------ 2 files changed, 36 insertions(+), 28 deletions(-) diff --git a/api.mjs b/api.mjs index 4a80d75..70dd3d6 100755 --- a/api.mjs +++ b/api.mjs @@ -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, @@ -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(','))); @@ -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); } } @@ -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(); }); @@ -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(); diff --git a/help.txt b/help.txt index 2f8f575..2b514e0 100644 --- a/help.txt +++ b/help.txt @@ -1,20 +1,18 @@ Usage: es-shim-api [options] 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 File path to write output to. If omitted, output will be printed to stdout. - [string] + --ignore-dirs File path to write output to. + [string] If omitted, output will be printed to stdout. \ No newline at end of file