-
Notifications
You must be signed in to change notification settings - Fork 176
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
* Add TypeScript support. Closes #1025 * Add TS leaks and override module target * Fix problems with incorrect double path in sourcemaps * Fix typo * Address comments Co-authored-by: Gil Pedersen <[email protected]>
- Loading branch information
1 parent
2328482
commit 6dd0d2c
Showing
13 changed files
with
186 additions
and
19 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
'use strict'; | ||
|
||
const Typescript = require('typescript'); | ||
|
||
const internals = { | ||
configs: new Map() | ||
}; | ||
|
||
internals.transform = function (content, fileName) { | ||
|
||
const configFile = Typescript.findConfigFile(Typescript.getDirectoryPath(fileName), Typescript.sys.fileExists); | ||
|
||
if (!internals.configs.has(configFile)) { | ||
try { | ||
var { config, error } = Typescript.readConfigFile(configFile, Typescript.sys.readFile); | ||
if (error) { | ||
throw new Error(`TypeScript config error in ${configFile}: ${error.messageText}`); | ||
} | ||
} | ||
catch (err) { | ||
throw new Error(`Cannot find a tsconfig file for ${fileName}`); | ||
} | ||
|
||
const { options } = Typescript.parseJsonConfigFileContent(config, Typescript.sys, Typescript.getDirectoryPath(configFile), {}, configFile); | ||
options.sourceMap = false; | ||
options.inlineSourceMap = true; | ||
options.module = 1; // CommonJS | ||
internals.configs.set(configFile, options); | ||
} | ||
|
||
const compilerOptions = internals.configs.get(configFile); | ||
const { outputText } = Typescript.transpileModule(content, { fileName, compilerOptions }); | ||
return outputText; | ||
}; | ||
|
||
exports.extensions = [ | ||
{ ext: '.ts', transform: internals.transform }, | ||
{ ext: '.tsx', transform: internals.transform } | ||
]; | ||
|
||
|
||
// Adapted from https://github.com/garthk/lab-transform-typescript | ||
// Copyright (C) 2016-2017 Garth Kidd <[email protected]>, MIT Licensed |
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,12 @@ | ||
import { expect } from '@hapi/code'; | ||
import * as _Lab from '../../test_runner'; | ||
|
||
const { describe, it } = exports.lab = _Lab.script(); | ||
|
||
describe('Test CLI', () => { | ||
|
||
it('adds two numbers together', () => { | ||
|
||
expect(1 + 1).to.equal(4); | ||
}); | ||
}); |
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,17 @@ | ||
import { expect } from '@hapi/code'; | ||
import * as _Lab from '../../test_runner'; | ||
|
||
const { describe, it } = exports.lab = _Lab.script(); | ||
|
||
describe('Test CLI', () => { | ||
|
||
it('adds two numbers together', () => { | ||
|
||
expect(1 + 1).to.equal(2); | ||
}); | ||
|
||
it('subtracts two numbers', () => { | ||
|
||
expect(2 - 2).to.equal(0); | ||
}); | ||
}); |
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,7 @@ | ||
{ | ||
"compilerOptions": { | ||
"target": "es2021", | ||
"module": "commonjs", | ||
"moduleResolution": "node" | ||
} | ||
} |
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,40 @@ | ||
'use strict'; | ||
|
||
const Path = require('path'); | ||
|
||
const Code = require('@hapi/code'); | ||
const _Lab = require('../test_runner'); | ||
const RunCli = require('./run_cli'); | ||
|
||
|
||
const internals = { | ||
cwd: process.cwd() | ||
}; | ||
|
||
|
||
const { describe, it, afterEach } = exports.lab = _Lab.script(); | ||
const expect = Code.expect; | ||
|
||
|
||
describe('TypeScript', () => { | ||
|
||
afterEach(() => process.chdir(internals.cwd)); | ||
|
||
it('supports TypeScript', async () => { | ||
|
||
process.chdir(Path.join(__dirname, 'cli_typescript')); | ||
const result = await RunCli(['simple.ts', '-m', '2000', '--typescript']); | ||
expect(result.errorOutput).to.equal(''); | ||
expect(result.code).to.equal(0); | ||
expect(result.output).to.contain('2 tests complete'); | ||
}); | ||
|
||
it('handles errors', async () => { | ||
|
||
process.chdir(Path.join(__dirname, 'cli_typescript')); | ||
const result = await RunCli(['error.ts', '-m', '2000', '--typescript']); | ||
expect(result.errorOutput).to.equal(''); | ||
expect(result.code).to.equal(1); | ||
expect(result.output).to.contain('error.ts:10:26'); | ||
}); | ||
}); |