Turn your tests into types
Transform JavaScript files to TypeScript by collecting V8 runtime type information during test runs.
# Using Yarn:
yarn add @esmbly/transformer-v8
# Or, using NPM:
npm install @esmbly/transformer-v8 --save
Check out Using the V8 transformer for a step-by-step guide on how to get started with @esmbly/transformer-v8
and the command-line interface.
The @esmbly/transformer-v8
transforms JavaScript files to TypeScript based on type information collected from V8. In order to use it, make sure you have @esmbly/cli
installed. Under the hood, @esmbly/transformer-v8
uses the experimental Inspector API and the V8 inspector protocol which requires you to have at least Node.js version 10 installed.
In order to collect a V8 type profile about a JavaScript program - the program needs to be executed. When the program is executed, type information can be collected from V8 via the V8 inspector protocol. If a program is covered by a test suite, the test suite can be executed in order to run the program and retrieve a type profile from V8.
The following Esmbly configuration will run jest
in order to extract a type profile, and then transform all JavaScript files located in the src
directory to TypeScript. Files are outputted to the dist
directory.
// esmbly.config.js
const V8 = require('@esmbly/transformer-v8');
module.exports = {
input: ['./src/**/*.js'],
transformers: [
V8.createTransformer({
testCommand: `jest`,
}),
],
output: [
{
format: '.ts',
outDir: 'dist',
rootDir: 'src'
},
],
};
The createTransformer()
method accepts an optional configuration object (see the V8TransformerOptions interface). The following options are possible.
Option | Description | Type | Default |
---|---|---|---|
testCommand | The test command to run in order to extract a type profile, e.g. jest --runInBand |
string |
|
debug | Debug whats being printed to stdout/stderr during the test run. | boolean | false |
customRules (optional) | An object containing any custom rules which should be applied (existing rules can be overridden). Check out the custom-rule example for further details. | CustomRules |
The long term goal is to support any test runner, including simply running node
to invoke a program that executes the specified input files. Currently, the following test runners are supported:
- jest
- jasmine
- tape
The type information that can be retrieved from V8 is somewhat experimental. All primitive types are supported, as well as custom classes. However, V8 does provide any detailed information about arrays or objects literals which will be interpreted as any[]
and Object
.
- Add: Transforming a simple JavaScript program that is covered by a test suite to WebAssembly by chaining
@esmbly/transformer-v8
and@esmbly/transformer-wasm
. - Radians: Transforming a utility program for transforming between radians and degrees to WebAssembly by chaining
@esmbly/transformer-v8
and@esmbly/transformer-wasm
. - Repeat: Transforming a utility program for repeating strings to WebAssembly by chaining
@esmbly/transformer-v8
and@esmbly/transformer-wasm
.
All types of contributions are very much welcome. Check out our Contributing Guide for instructions on how to get started.