Skip to content

Commit 718a47a

Browse files
committed
esm: add limited support for --print
1 parent c714cda commit 718a47a

File tree

4 files changed

+34
-29
lines changed

4 files changed

+34
-29
lines changed

doc/api/errors.md

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1231,12 +1231,6 @@ provided.
12311231
Encoding provided to `TextDecoder()` API was not one of the
12321232
[WHATWG Supported Encodings][].
12331233

1234-
<a id="ERR_EVAL_ESM_CANNOT_PRINT"></a>
1235-
1236-
### `ERR_EVAL_ESM_CANNOT_PRINT`
1237-
1238-
`--print` cannot be used with ESM input.
1239-
12401234
<a id="ERR_EVENT_RECURSION"></a>
12411235

12421236
### `ERR_EVENT_RECURSION`
@@ -3696,6 +3690,17 @@ removed: v15.0.0
36963690

36973691
The native call from `process.cpuUsage` could not be processed.
36983692

3693+
<a id="ERR_EVAL_ESM_CANNOT_PRINT"></a>
3694+
3695+
### `ERR_EVAL_ESM_CANNOT_PRINT`
3696+
3697+
<!--
3698+
added: v14.3.0
3699+
removed: REPLACEME
3700+
-->
3701+
3702+
`--print` cannot be used with ESM input.
3703+
36993704
[ES Module]: esm.md
37003705
[ICU]: intl.md#internationalization-support
37013706
[JSON Web Key Elliptic Curve Registry]: https://www.iana.org/assignments/jose/jose.xhtml#web-key-elliptic-curve

lib/internal/errors.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1194,7 +1194,6 @@ E('ERR_ENCODING_INVALID_ENCODED_DATA', function(encoding, ret) {
11941194
}, TypeError);
11951195
E('ERR_ENCODING_NOT_SUPPORTED', 'The "%s" encoding is not supported',
11961196
RangeError);
1197-
E('ERR_EVAL_ESM_CANNOT_PRINT', '--print cannot be used with ESM input', Error);
11981197
E('ERR_EVENT_RECURSION', 'The event "%s" is already being dispatched', Error);
11991198
E('ERR_FALSY_VALUE_REJECTION', function(reason) {
12001199
this.reason = reason;

lib/internal/process/execution.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ const {
1212
codes: {
1313
ERR_INVALID_ARG_TYPE,
1414
ERR_UNCAUGHT_EXCEPTION_CAPTURE_ALREADY_SET,
15-
ERR_EVAL_ESM_CANNOT_PRINT,
1615
},
1716
} = require('internal/errors');
1817
const { pathToFileURL } = require('internal/url');
@@ -61,7 +60,7 @@ function getEvalModuleUrl() {
6160
*/
6261
function evalModuleEntryPoint(source, print) {
6362
if (print) {
64-
throw new ERR_EVAL_ESM_CANNOT_PRINT();
63+
source = `console.log((${source}))`;
6564
}
6665
RegExpPrototypeExec(/^/, ''); // Necessary to reset RegExp statics before user code runs.
6766
return require('internal/modules/run_main').runEntryPointWithESMLoader(

test/parallel/test-cli-eval.js

Lines changed: 22 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ const assert = require('assert');
3232
const child = require('child_process');
3333
const path = require('path');
3434
const fixtures = require('../common/fixtures');
35+
const { spawnSyncAndAssert } = require('../common/child_process');
3536
const nodejs = `"${process.execPath}"`;
3637

3738
if (process.argv.length > 2) {
@@ -224,30 +225,31 @@ child.exec(`${nodejs} --use-strict -p process.execArgv`,
224225

225226

226227
// Assert that "42\n" is written to stdout on module eval.
227-
const execOptions = '--input-type module';
228-
child.exec(
229-
`${nodejs} ${execOptions} --eval "console.log(42)"`,
230-
common.mustSucceed((stdout) => {
231-
assert.strictEqual(stdout, '42\n');
232-
}));
228+
const execOptions = '--input-type=module';
229+
spawnSyncAndAssert(process.execPath, [execOptions, '--eval', 'console.log(42)'], {
230+
stdout: '42\n',
231+
stderr: '',
232+
});
233233

234234
// Assert that "42\n" is written to stdout with print option.
235-
child.exec(
236-
`${nodejs} ${execOptions} --print --eval "42"`,
237-
common.mustCall((err, stdout, stderr) => {
238-
assert.ok(err);
239-
assert.strictEqual(stdout, '');
240-
assert.ok(stderr.includes('--print cannot be used with ESM input'));
241-
}));
235+
spawnSyncAndAssert(process.execPath, [execOptions, '--print', '--eval', 42], {
236+
stdout: '42\n',
237+
stderr: '',
238+
});
242239

243240
// Assert that error is written to stderr on invalid input.
244-
child.exec(
245-
`${nodejs} ${execOptions} --eval "!!!!"`,
246-
common.mustCall((err, stdout, stderr) => {
247-
assert.ok(err);
248-
assert.strictEqual(stdout, '');
249-
assert.ok(stderr.indexOf('SyntaxError: Unexpected end of input') > 0);
250-
}));
241+
spawnSyncAndAssert(process.execPath, [execOptions, '--print', '--eval', '!!!!'], {
242+
status: 1,
243+
stdout: '',
244+
stderr: /SyntaxError: Unexpected token '\)'\n/,
245+
});
246+
247+
// Assert that error is written to stderr on unsupported input.
248+
spawnSyncAndAssert(process.execPath, [execOptions, '-p', 'import "node:os"'], {
249+
status: 1,
250+
stdout: '',
251+
stderr: /SyntaxError: Unexpected string\n/,
252+
});
251253

252254
// Assert that require is undefined in ESM support
253255
child.exec(

0 commit comments

Comments
 (0)