Skip to content
This repository has been archived by the owner on Apr 16, 2020. It is now read-only.

Commit

Permalink
esm: merge both 'type mismatch' errors into one
Browse files Browse the repository at this point in the history
  • Loading branch information
GeoffreyBooth committed Feb 27, 2019
1 parent 916080a commit c5052c6
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 38 deletions.
20 changes: 7 additions & 13 deletions doc/api/errors.md
Original file line number Diff line number Diff line change
Expand Up @@ -2213,22 +2213,16 @@ while trying to read and parse it.
The `--type=...` flag is not compatible with the Node.js REPL.

<a id="ERR_INVALID_TYPE_EXTENSION"></a>
### ERR_INVALID_TYPE_EXTENSION
<a id="ERR_TYPE_MISMATCH"></a>
### ERR_TYPE_MISMATCH

> Stability: 1 - Experimental
Attempted to execute a `.cjs` file with the `--type=module` flag,
or an `.mjs` file with the `--type=commonjs` flag.

<a id="ERR_INVALID_TYPE_IN_PACKAGE_SCOPE"></a>
### ERR_INVALID_TYPE_IN_PACKAGE_SCOPE

> Stability: 1 - Experimental
Attempted to execute a `.js` file with the `--type=commonjs` flag where the
nearest `package.json` contains `"type": "module"`; or a `.js` file with the
`--type=module` flag where the nearest `package.json` either lacks a `"type"`
The `--type=commonjs` flag was used to attempt to execute an `.mjs` file or
a `.js` file where the nearest parent `package.json` contains
`"type": "module"`; or
the `--type=module` flag was used to attempt to execute a `.cjs` file or
a `.js` file where the nearest parent `package.json` either lacks a `"type"`
field or contains `"type": "commonjs"`.

<a id="ERR_INVALID_TYPE_FLAG"></a>
Expand Down
22 changes: 10 additions & 12 deletions lib/internal/errors.js
Original file line number Diff line number Diff line change
Expand Up @@ -814,21 +814,9 @@ E('ERR_INVALID_SYNC_FORK_INPUT',
TypeError);
E('ERR_INVALID_THIS', 'Value of "this" must be of type %s', TypeError);
E('ERR_INVALID_TUPLE', '%s must be an iterable %s tuple', TypeError);
E('ERR_INVALID_TYPE_EXTENSION', (extension, flagValue) => {
if (flagValue === 'module')
flagValue = 'module or -m';
return `${extension} is not supported for --type=${flagValue}`;
}, TypeError);
E('ERR_INVALID_TYPE_FLAG',
'Type flag must be one of "module", "commonjs". Received --type=%s',
TypeError);
E('ERR_INVALID_TYPE_IN_PACKAGE_SCOPE', (flagValue, scopeType) => {
if (flagValue === 'module')
flagValue = 'module or -m';
return `Cannot use --type=${flagValue} because nearest parent package.json` +
` includes "type": "${scopeType}"${(scopeType === 'commonjs') ?
' (or lacks the "type" field)' : ''}`;
}, TypeError);
E('ERR_INVALID_URI', 'URI malformed', URIError);
E('ERR_INVALID_URL', 'Invalid URL: %s', TypeError);
E('ERR_INVALID_URL_SCHEME',
Expand Down Expand Up @@ -967,6 +955,16 @@ E('ERR_TRANSFORM_ALREADY_TRANSFORMING',
E('ERR_TRANSFORM_WITH_LENGTH_0',
'Calling transform done when writableState.length != 0', Error);
E('ERR_TTY_INIT_FAILED', 'TTY initialization failed', SystemError);
E('ERR_TYPE_MISMATCH', (flagValue, extension, scopeType) => {
if (flagValue === 'module')
flagValue = 'module or -m';
if (extension) // Mismatch between --type and file extension
return `${extension} is not supported for --type=${flagValue}`;
else // Mismatch between --type and package.json "type" field
return `Cannot use --type=${flagValue} because nearest parent ` +
`package.json includes "type": "${scopeType}"${(scopeType === 'commonjs') ?
' (or lacks the "type" field)' : ''}`;
}, TypeError);
E('ERR_UNCAUGHT_EXCEPTION_CAPTURE_ALREADY_SET',
'`process.setupUncaughtExceptionCapture()` was called while a capture ' +
'callback was already active',
Expand Down
9 changes: 4 additions & 5 deletions lib/internal/modules/cjs/loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,8 @@ const { compileFunction } = internalBinding('contextify');

const {
ERR_INVALID_ARG_VALUE,
ERR_INVALID_TYPE_EXTENSION,
ERR_INVALID_TYPE_IN_PACKAGE_SCOPE,
ERR_REQUIRE_ESM
ERR_REQUIRE_ESM,
ERR_TYPE_MISMATCH
} = require('internal/errors').codes;
const { validateString } = require('internal/validators');

Expand Down Expand Up @@ -873,10 +872,10 @@ Module.runMain = function() {
(!isModule && asyncESM.typeFlag === 'module')) {
if (ext === '.js') {
// Conflict between package scope type and --type
throw new ERR_INVALID_TYPE_IN_PACKAGE_SCOPE(asyncESM.typeFlag, format);
throw new ERR_TYPE_MISMATCH(asyncESM.typeFlag, undefined, format);
} else {
// Conflict between explicit extension (.mjs, .cjs) and --type
throw new ERR_INVALID_TYPE_EXTENSION(ext, asyncESM.typeFlag);
throw new ERR_TYPE_MISMATCH(asyncESM.typeFlag, ext, undefined);
}
}

Expand Down
16 changes: 8 additions & 8 deletions test/es-module/test-esm-type-flag-errors.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,19 +20,19 @@ expect('', packageTypeCommonJsMain, 'package-type-commonjs');
expect('', packageWithoutTypeMain, 'package-without-type');

// Check that running with conflicting --type flags throws errors
expect('--type=commonjs', mjsFile, 'ERR_INVALID_TYPE_EXTENSION', true);
expect('--type=module', cjsFile, 'ERR_INVALID_TYPE_EXTENSION', true);
expect('-m', cjsFile, 'ERR_INVALID_TYPE_EXTENSION', true);
expect('--type=commonjs', mjsFile, 'ERR_TYPE_MISMATCH', true);
expect('--type=module', cjsFile, 'ERR_TYPE_MISMATCH', true);
expect('-m', cjsFile, 'ERR_TYPE_MISMATCH', true);
expect('--type=commonjs', packageTypeModuleMain,
'ERR_INVALID_TYPE_IN_PACKAGE_SCOPE', true);
'ERR_TYPE_MISMATCH', true);
expect('--type=module', packageTypeCommonJsMain,
'ERR_INVALID_TYPE_IN_PACKAGE_SCOPE', true);
'ERR_TYPE_MISMATCH', true);
expect('-m', packageTypeCommonJsMain,
'ERR_INVALID_TYPE_IN_PACKAGE_SCOPE', true);
'ERR_TYPE_MISMATCH', true);
expect('--type=module', packageWithoutTypeMain,
'ERR_INVALID_TYPE_IN_PACKAGE_SCOPE', true);
'ERR_TYPE_MISMATCH', true);
expect('-m', packageWithoutTypeMain,
'ERR_INVALID_TYPE_IN_PACKAGE_SCOPE', true);
'ERR_TYPE_MISMATCH', true);

function expect(opt = '', inputFile, want, wantsError = false) {
// TODO: Remove when --experimental-modules is unflagged
Expand Down

0 comments on commit c5052c6

Please sign in to comment.