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

test: add ts eval snapshots #56358

Closed
wants to merge 3 commits into from
Closed
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
63 changes: 22 additions & 41 deletions lib/internal/process/execution.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,38 +84,19 @@ function evalScript(name, body, breakFirstLine, print, shouldLoadESM = false) {
evalModuleEntryPoint(body, print);
}

const runScript = () => {
// Create wrapper for cache entry
const script = `
globalThis.module = module;
globalThis.exports = exports;
globalThis.__dirname = __dirname;
globalThis.require = require;
return (main) => main();
`;
globalThis.__filename = name;
RegExpPrototypeExec(/^/, ''); // Necessary to reset RegExp statics before user code runs.
const result = module._compile(script, `${name}-wrapper`)(() => {
const compiledScript = compileScript(name, body, baseUrl);
return runScriptInThisContext(compiledScript, true, !!breakFirstLine);
});
if (print) {
const { log } = require('internal/console/global');

process.on('exit', () => {
log(result);
});
}

if (origModule !== undefined)
globalThis.module = origModule;
};
const evalFunction = () => runScriptInContext(name,
body,
breakFirstLine,
print,
module,
baseUrl,
undefined,
origModule);
Comment on lines +87 to +94
Copy link
Member

Choose a reason for hiding this comment

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

What the heck is happening here?

Suggested change
const evalFunction = () => runScriptInContext(name,
body,
breakFirstLine,
print,
module,
baseUrl,
undefined,
origModule);
const evalFunction = () => runScriptInContext(
name,
body,
breakFirstLine,
print,
module,
baseUrl,
undefined,
origModule,
);

Copy link
Member Author

Choose a reason for hiding this comment

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

Its the formatter 🤷 I run make lint-js-fix and it formats it that way


if (shouldLoadESM) {
require('internal/modules/run_main').runEntryPointWithESMLoader(runScript);
return;
return require('internal/modules/run_main').runEntryPointWithESMLoader(evalFunction);
}
runScript();
evalFunction();
}

const exceptionHandlerState = {
Expand Down Expand Up @@ -301,19 +282,19 @@ function evalTypeScript(name, source, breakFirstLine, print, shouldLoadESM = fal
}
}

const evalFunction = () => runScriptInContext(name,
sourceToRun,
breakFirstLine,
print,
module,
baseUrl,
compiledScript,
origModule);
Comment on lines +285 to +292
Copy link
Member

Choose a reason for hiding this comment

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

Same

Suggested change
const evalFunction = () => runScriptInContext(name,
sourceToRun,
breakFirstLine,
print,
module,
baseUrl,
compiledScript,
origModule);
const evalFunction = () => runScriptInContext(
name,
sourceToRun,
breakFirstLine,
print,
module,
baseUrl,
compiledScript,
origModule,
);


if (shouldLoadESM) {
return require('internal/modules/run_main').runEntryPointWithESMLoader(
() => runScriptInContext(name,
sourceToRun,
breakFirstLine,
print,
module,
baseUrl,
compiledScript,
origModule));
return require('internal/modules/run_main').runEntryPointWithESMLoader(evalFunction);
}

runScriptInContext(name, sourceToRun, breakFirstLine, print, module, baseUrl, compiledScript, origModule);
evalFunction();
}

/**
Expand Down Expand Up @@ -476,7 +457,7 @@ function runScriptInContext(name, body, breakFirstLine, print, module, baseUrl,
const result = module._compile(script, `${name}-wrapper`)(() => {
// If the script was already compiled, use it.
return runScriptInThisContext(
compiledScript,
compiledScript ?? compileScript(name, body, baseUrl),
true, !!breakFirstLine);
});
if (print) {
Expand Down
26 changes: 0 additions & 26 deletions test/fixtures/eval/eval_messages.snapshot
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,11 @@ with(this){__filename}
: ^^^^
`----


Caused by:
failed to parse

SyntaxError: Strict mode code may not include a with statement





Node.js *
42
42
Expand All @@ -27,27 +22,13 @@ throw new Error("hello")

Error: hello








Node.js *
[eval]:1
throw new Error("hello")
^

Error: hello








Node.js *
100
[eval]:1
Expand All @@ -56,13 +37,6 @@ var x = 100; y = x;

ReferenceError: y is not defined








Node.js *

[eval]:1
Expand Down
25 changes: 25 additions & 0 deletions test/fixtures/eval/eval_typescript.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
'use strict';

require('../../common');

const spawnSync = require('child_process').spawnSync;

const queue = [
'enum Foo{};',
'throw new SyntaxError("hello")',
'const foo;',
'let x: number = 100;x;',
'const foo: string = 10;',
'function foo(){};foo<Number>(1);',
'interface Foo{};const foo;',
'function foo(){ await Promise.resolve(1)};',
Copy link
Member

Choose a reason for hiding this comment

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

nit: this seems a bit convoluted

Suggested change
'function foo(){ await Promise.resolve(1)};',
'async function foo(){ return 1 };',

or

Suggested change
'function foo(){ await Promise.resolve(1)};',
'const foo = async () => 1;',

Copy link
Member Author

Choose a reason for hiding this comment

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

Its not the same thing, its convoluted to create a syntax error

];

for (const cmd of queue) {
const args = ['--disable-warning=ExperimentalWarning', '-p', cmd];
const result = spawnSync(process.execPath, args, {
stdio: 'pipe'
});
process.stdout.write(result.stdout);
process.stdout.write(result.stderr);
}
51 changes: 51 additions & 0 deletions test/fixtures/eval/eval_typescript.snapshot
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
[eval]:1
enum Foo{};
^^^^
x TypeScript enum is not supported in strip-only mode
,----
1 | enum Foo{};
: ^^^^^^^^^^
`----

SyntaxError: Unexpected reserved word

Node.js *
[eval]:1
throw new SyntaxError("hello")
^

SyntaxError: hello

Node.js *
[eval]:1
const foo;
^^^

SyntaxError: Missing initializer in const declaration

Node.js *
100
undefined
false
[eval]:1
;const foo;
^^^

SyntaxError: Missing initializer in const declaration

Node.js *
[eval]:1
function foo(){ await Promise.resolve(1)};
^^^^^
x await isn't allowed in non-async function
,----
1 | function foo(){ await Promise.resolve(1)};
: ^^^^^^^
`----

Caused by:
failed to parse

SyntaxError: await is only valid in async functions and the top level bodies of modules

Node.js *
39 changes: 0 additions & 39 deletions test/fixtures/eval/stdin_messages.snapshot
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,11 @@ with(this){__filename}
: ^^^^
`----


Caused by:
failed to parse

SyntaxError: Strict mode code may not include a with statement









Node.js *
42
42
Expand All @@ -31,33 +22,13 @@ throw new Error("hello")

Error: hello











Node.js *
[stdin]:1
throw new Error("hello")
^

Error: hello











Node.js *
100
[stdin]:1
Expand All @@ -66,16 +37,6 @@ let x = 100; y = x;

ReferenceError: y is not defined











Node.js *

[stdin]:1
Expand Down
Loading
Loading