Skip to content

Commit

Permalink
Use TypeScript Compiler API Directly
Browse files Browse the repository at this point in the history
In 1.4.0 we can use the TypeScript API directly to preprocess our files.
This lets us get rid of a dependency.

https://github.com/Microsoft/TypeScript/wiki/Using-the-Compiler-API

We can also use this to provide our default libraries so that we don't
need to keep the references in the test file.
  • Loading branch information
sebmarkbage committed Feb 2, 2015
1 parent 905bfce commit f6fd4a8
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 19 deletions.
24 changes: 9 additions & 15 deletions jest/preprocessor.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,28 +3,22 @@
var ReactTools = require('../main.js');

var coffee = require('coffee-script');
var ts = require('ts-compiler');
var tsPreprocessor = require('./ts-preprocessor');

var defaultLibraries = [
require.resolve('./jest.d.ts'),
require.resolve('../src/modern/class/React.d.ts')
];

var ts = tsPreprocessor(defaultLibraries);

module.exports = {
process: function(src, path) {
if (path.match(/\.coffee$/)) {
return coffee.compile(src, {'bare': true});
}
if (path.match(/\.ts$/) && !path.match(/\.d\.ts$/)) {
ts.compile([path], {
skipWrite: true,
module: 'commonjs'
}, function(err, results) {
if (err) {
throw err;
}
results.forEach(function(file) {
// This is gross, but jest doesn't provide an asynchronous way to
// process a module, and ts currently runs syncronously.
src = file.text;
});
});
return src;
return ts.compile(src, path);
}
return ReactTools.transform(src, {harmony: true});
}
Expand Down
67 changes: 67 additions & 0 deletions jest/ts-preprocessor.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
'use strict';

var fs = require('fs');
var path = require('path');
var ts = require('typescript');

var tsOptions = { module: 'commonjs' };

function formatErrorMessage(error) {
return (
error.file.filename + '(' +
error.file.getLineAndCharacterFromPosition(error.start).line +
'): ' +
error.messageText
);
}

function compile(defaultLib, content, contentFilename) {
var output = null;
var compilerHost = {
getSourceFile: function(filename, languageVersion) {
if (filename === contentFilename) {
return ts.createSourceFile(filename, content, 'ES5', '0');
}
return defaultLib;
},
writeFile: function(name, text, writeByteOrderMark) {
if (output === null) {
output = text;
} else {
throw new Error('Expected only one dependency.');
}
},
getCanonicalFileName: function(filename) { return filename; },
getCurrentDirectory: function() { return ''; },
getNewLine: function() { return '\n'; }
};
var program = ts.createProgram([contentFilename], tsOptions, compilerHost);
var errors = program.getDiagnostics();
if (!errors.length) {
var checker = program.getTypeChecker(true);
errors = checker.getDiagnostics();
checker.emitFiles();
}
if (errors.length) {
throw new Error(errors.map(formatErrorMessage).join('\n'));
}
return output;
}

module.exports = function(defaultLibs) {
var defaultLibSource = fs.readFileSync(
path.join(path.dirname(require.resolve('typescript')), 'lib.d.ts')
);

for (var i = 0; i < defaultLibs.length; i++) {
defaultLibSource += '\n' + fs.readFileSync(defaultLibs[i]);
}

var defaultLibSourceFile = ts.createSourceFile(
'lib.d.ts', defaultLibSource, 'ES5'
);

return {
compile: compile.bind(null, defaultLibSourceFile)
};
};
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@
"recast": "^0.9.11",
"sauce-tunnel": "~1.1.0",
"tmp": "~0.0.18",
"ts-compiler": "^2.0.0",
"typescript": "^1.4.0",
"uglify-js": "~2.4.0",
"uglifyify": "^2.4.0",
"wd": "~0.2.6"
Expand Down
3 changes: 0 additions & 3 deletions src/modern/class/__tests__/ReactTypeScriptClass-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,6 @@
* of patent rights can be found in the PATENTS file in the same directory.
*/

///<reference path='../../../../jest/jest.d.ts'/>
///<reference path='../React.d.ts'/>

import React = require('React');

// Before Each
Expand Down

0 comments on commit f6fd4a8

Please sign in to comment.