-
Notifications
You must be signed in to change notification settings - Fork 30.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
module: add --experimental-enable-transformation for strip-types
- Loading branch information
1 parent
3cbeed8
commit c5da92e
Showing
13 changed files
with
217 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
import { skip, spawnPromisified } from '../common/index.mjs'; | ||
import * as fixtures from '../common/fixtures.mjs'; | ||
import { match, strictEqual } from 'node:assert'; | ||
import { test } from 'node:test'; | ||
|
||
if (!process.config.variables.node_use_amaro) skip('Requires Amaro'); | ||
|
||
test('execute a TypeScript file with transformation enabled', async () => { | ||
const result = await spawnPromisified(process.execPath, [ | ||
'--experimental-enable-transformation', | ||
'--no-warnings', | ||
fixtures.path('typescript/ts/transformation/test-enum.ts'), | ||
]); | ||
|
||
strictEqual(result.stderr, ''); | ||
match(result.stdout, /Hello, TypeScript!/); | ||
strictEqual(result.code, 0); | ||
}); | ||
|
||
test('execute a TypeScript file with transformation enabled and sourcemaps', async () => { | ||
const result = await spawnPromisified(process.execPath, [ | ||
'--experimental-enable-transformation', | ||
'--enable-source-maps', | ||
'--no-warnings', | ||
fixtures.path('typescript/ts/transformation/test-enum.ts'), | ||
]); | ||
|
||
strictEqual(result.stderr, ''); | ||
match(result.stdout, /Hello, TypeScript!/); | ||
strictEqual(result.code, 0); | ||
}); | ||
|
||
test('reconstruct error of a TypeScript file with transformation enabled and sourcemaps', async () => { | ||
const result = await spawnPromisified(process.execPath, [ | ||
'--experimental-enable-transformation', | ||
'--enable-source-maps', | ||
'--no-warnings', | ||
fixtures.path('typescript/ts/transformation/test-enum-stacktrace.ts'), | ||
]); | ||
|
||
match(result.stderr, /test-enum-stacktrace\.ts:4:7/); | ||
strictEqual(result.stdout, ''); | ||
strictEqual(result.code, 1); | ||
}); | ||
|
||
test('reconstruct error of a complex TypeScript file with transformation enabled and sourcemaps', async () => { | ||
const result = await spawnPromisified(process.execPath, [ | ||
'--experimental-enable-transformation', | ||
'--enable-source-maps', | ||
'--no-warnings', | ||
fixtures.path('typescript/ts/transformation/test-complex-stacktrace.ts'), | ||
]); | ||
|
||
match(result.stderr, /Calculation failed: Division by zero!/); | ||
match(result.stderr, /test-complex-stacktrace\.ts:64/); | ||
match(result.stderr, /test-complex-stacktrace\.ts:64:19/); | ||
strictEqual(result.stdout, ''); | ||
strictEqual(result.code, 1); | ||
}); | ||
|
||
test('reconstruct error of a complex TypeScript file with transformation enabled without sourcemaps', async () => { | ||
const result = await spawnPromisified(process.execPath, [ | ||
'--experimental-enable-transformation', | ||
'--no-warnings', | ||
fixtures.path('typescript/ts/transformation/test-complex-stacktrace.ts'), | ||
]); | ||
// The stack trace is not reconstructed without sourcemaps. | ||
match(result.stderr, /Calculation failed: Division by zero!/); | ||
match(result.stderr, /test-complex-stacktrace\.ts:50/); | ||
match(result.stderr, /test-complex-stacktrace\.ts:50:19/); | ||
strictEqual(result.stdout, ''); | ||
strictEqual(result.code, 1); | ||
}); |
67 changes: 67 additions & 0 deletions
67
test/fixtures/typescript/ts/transformation/test-complex-stacktrace.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
// Namespace | ||
namespace Mathematics { | ||
// Enum | ||
export enum Operation { | ||
Add, | ||
Subtract, | ||
Multiply, | ||
Divide | ||
} | ||
|
||
// Interface | ||
export interface MathOperation { | ||
perform(a: number, b: number): number; | ||
} | ||
|
||
// Generic function | ||
export function executeOperation<T extends MathOperation>(op: T, x: number, y: number): number { | ||
return op.perform(x, y); | ||
} | ||
|
||
// Class implementing interface | ||
export class Calculator implements MathOperation { | ||
constructor(private op: Operation) { } | ||
|
||
perform(a: number, b: number): number { | ||
switch (this.op) { | ||
case Operation.Add: return a + b; | ||
case Operation.Subtract: return a - b; | ||
case Operation.Multiply: return a * b; | ||
case Operation.Divide: | ||
if (b === 0) throw new Error("Division by zero!"); | ||
return a / b; | ||
default: | ||
throw new Error("Unknown operation"); | ||
} | ||
} | ||
} | ||
} | ||
|
||
// Using the namespace | ||
const calc = new Mathematics.Calculator(Mathematics.Operation.Divide); | ||
|
||
// Union type | ||
type Result = number | string; | ||
|
||
// Function with rest parameters and arrow syntax | ||
const processResults = (...results: Result[]): string => { | ||
return results.map(r => r.toString()).join(", "); | ||
}; | ||
|
||
// Async function with await | ||
async function performAsyncCalculation(a: number, b: number): Promise<number> { | ||
const result = await Promise.resolve(Mathematics.executeOperation(calc, a, b)); | ||
return result; | ||
} | ||
|
||
// IIFE to create a block scope | ||
(async () => { | ||
try { | ||
const result = await performAsyncCalculation(10, 0); | ||
} catch (error) { | ||
if (error instanceof Error) { | ||
// This line will throw an error | ||
throw new Error("Calculation failed: " + error.message); | ||
} | ||
} | ||
})(); |
4 changes: 4 additions & 0 deletions
4
test/fixtures/typescript/ts/transformation/test-enum-stacktrace.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
enum Foo { | ||
A = "Hello, TypeScript!", | ||
} | ||
throw new Error(Foo.A); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
enum Foo { | ||
A = "Hello, TypeScript!", | ||
} | ||
console.log(Foo.A); |