module: improve typescript error message format · nodejs/node@fe7994e · GitHub
Skip to content

Commit fe7994e

Browse files
marco-ippolitoRafaelGSS
authored andcommitted
module: improve typescript error message format
PR-URL: #57687 Backport-PR-URL: #57298 Fixes: #56830 Reviewed-By: Chengzhong Wu <legendecas@gmail.com> Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com> Reviewed-By: Rafael Gonzaga <rafael.nunu@hotmail.com> PR-URL: #56350 Fixes: nodejs/typescript#17
1 parent c898491 commit fe7994e

7 files changed

Lines changed: 58 additions & 76 deletions

File tree

lib/internal/modules/typescript.js

Lines changed: 20 additions & 4 deletions

lib/internal/process/execution.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@ const {
3939
// communication with JS.
4040
const { shouldAbortOnUncaughtToggle } = internalBinding('util');
4141

42+
const kEvalTag = '[eval]';
43+
4244
function tryGetCwd() {
4345
try {
4446
return process.cwd();
@@ -346,7 +348,7 @@ function evalTypeScriptModuleEntryPoint(source, print) {
346348
*/
347349
function parseAndEvalModuleTypeScript(source, print) {
348350
// We know its a TypeScript module, we can safely emit the experimental warning.
349-
const strippedSource = stripTypeScriptModuleTypes(source, getEvalModuleUrl());
351+
const strippedSource = stripTypeScriptModuleTypes(source, kEvalTag);
350352
evalModuleEntryPoint(strippedSource, print);
351353
}
352354

@@ -361,7 +363,7 @@ function parseAndEvalModuleTypeScript(source, print) {
361363
*/
362364
function parseAndEvalCommonjsTypeScript(name, source, breakFirstLine, print, shouldLoadESM = false) {
363365
// We know its a TypeScript module, we can safely emit the experimental warning.
364-
const strippedSource = stripTypeScriptModuleTypes(source, getEvalModuleUrl());
366+
const strippedSource = stripTypeScriptModuleTypes(source, kEvalTag);
365367
evalScript(name, strippedSource, breakFirstLine, print, shouldLoadESM);
366368
}
367369

test/es-module/test-typescript-eval.mjs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -264,3 +264,23 @@ test('should not allow declare module keyword', async () => {
264264
match(result.stderr, /ERR_UNSUPPORTED_TYPESCRIPT_SYNTAX/);
265265
strictEqual(result.code, 1);
266266
});
267+
268+
// TODO (marco-ippolito) Remove the extra padding from the error message
269+
// The padding comes from swc it will be removed in a future amaro release
270+
test('the error message should not contain extra padding', async () => {
271+
const result = await spawnPromisified(process.execPath, [
272+
'--input-type=module-typescript',
273+
'--eval',
274+
'declare module F { export type x = number }']);
275+
strictEqual(result.stdout, '');
276+
// Windows uses \r\n as line endings
277+
const lines = result.stderr.replace(/\r\n/g, '\n').split('\n');
278+
// The extra padding at the end should not be present
279+
strictEqual(lines[0], '[eval]:1 ');
280+
// The extra padding at the beginning should not be present
281+
strictEqual(lines[2], ' declare module F { export type x = number }');
282+
strictEqual(lines[3], ' ^^^^^^^^');
283+
strictEqual(lines[5], 'SyntaxError [ERR_UNSUPPORTED_TYPESCRIPT_SYNTAX]:' +
284+
' `module` keyword is not supported. Use `namespace` instead.');
285+
strictEqual(result.code, 1);
286+
});

test/fixtures/eval/eval_messages.snapshot

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,7 @@
22
[eval]:1
33
with(this){__filename}
44
^^^^
5-
x The 'with' statement is not supported. All symbols in a 'with' block will have type 'any'.
6-
,----
7-
1 | with(this){__filename}
8-
: ^^^^
9-
`----
5+
The 'with' statement is not supported. All symbols in a 'with' block will have type 'any'.
106

117
SyntaxError: Strict mode code may not include a with statement
128

test/fixtures/eval/eval_typescript.snapshot

Lines changed: 4 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,7 @@
11
[eval]:1
22
enum Foo{};
33
^^^^
4-
x TypeScript enum is not supported in strip-only mode
5-
,----
6-
1 | enum Foo{};
7-
: ^^^^^^^^^^
8-
`----
4+
TypeScript enum is not supported in strip-only mode
95

106
SyntaxError: Unexpected reserved word
117

@@ -20,11 +16,7 @@ Node.js *
2016
[eval]:1
2117
const foo;
2218
^^^
23-
x 'const' declarations must be initialized
24-
,----
25-
1 | const foo;
26-
: ^^^
27-
`----
19+
'const' declarations must be initialized
2820

2921
SyntaxError: Missing initializer in const declaration
3022

@@ -35,23 +27,15 @@ false
3527
[eval]:1
3628
interface Foo{};const foo;
3729
^^^
38-
x 'const' declarations must be initialized
39-
,----
40-
1 | interface Foo{};const foo;
41-
: ^^^
42-
`----
30+
'const' declarations must be initialized
4331

4432
SyntaxError: Unexpected identifier 'Foo'
4533

4634
Node.js *
4735
[eval]:1
4836
function foo(){ await Promise.resolve(1)};
4937
^^^^^
50-
x await isn't allowed in non-async function
51-
,----
52-
1 | function foo(){ await Promise.resolve(1)};
53-
: ^^^^^^^
54-
`----
38+
await isn't allowed in non-async function
5539

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

test/fixtures/eval/stdin_messages.snapshot

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,7 @@
22
[stdin]:1
33
with(this){__filename}
44
^^^^
5-
x The 'with' statement is not supported. All symbols in a 'with' block will have type 'any'.
6-
,----
7-
1 | with(this){__filename}
8-
: ^^^^
9-
`----
5+
The 'with' statement is not supported. All symbols in a 'with' block will have type 'any'.
106

117
SyntaxError: Strict mode code may not include a with statement
128

test/fixtures/eval/stdin_typescript.snapshot

Lines changed: 8 additions & 40 deletions

0 commit comments

Comments
 (0)