Skip to content

Commit

Permalink
Merge pull request facebook#6804 from dmitriiabramov/update_typescript
Browse files Browse the repository at this point in the history
Update typescript
  • Loading branch information
zpao committed May 20, 2016
2 parents e1e3427 + efb6d80 commit 6a3e9d5
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 16 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@
"run-sequence": "^1.1.4",
"through2": "^2.0.0",
"tmp": "~0.0.28",
"typescript": "~1.4.0",
"typescript": "~1.8.10",
"uglify-js": "^2.5.0",
"uglifyify": "^3.0.1"
},
Expand Down
37 changes: 22 additions & 15 deletions scripts/jest/ts-preprocessor.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,15 @@ var fs = require('fs');
var path = require('path');
var ts = require('typescript');

var tsOptions = {module: 'commonjs'};
var tsOptions = {
module: ts.ModuleKind.CommonJS,
jsx: ts.JsxEmit.React,
};

function formatErrorMessage(error) {
return (
error.file.filename + '(' +
error.file.getLineAndCharacterFromPosition(error.start).line +
error.file.getLineAndCharacterOfPosition(error.start).line +
'): ' +
error.messageText
);
Expand All @@ -18,7 +21,7 @@ function formatErrorMessage(error) {
function compile(content, contentFilename) {
var output = null;
var compilerHost = {
getSourceFile: function(filename, languageVersion) {
getSourceFile(filename, languageVersion) {
var source;

// `path.normalize` and `path.join` are used to turn forward slashes in
Expand All @@ -28,11 +31,13 @@ function compile(content, contentFilename) {
path.join('/', '(?:React|ReactDOM)(?:\.d)?\.ts$')
);

var jestRegex = /jest\.d\.ts/;

if (filename === 'lib.d.ts') {
source = fs.readFileSync(
require.resolve('typescript/bin/lib.d.ts')
require.resolve('typescript/lib/lib.d.ts')
).toString();
} else if (filename === 'jest.d.ts') {
} else if (filename.match(jestRegex)) {
source = fs.readFileSync(
path.join(__dirname, 'jest.d.ts')
).toString();
Expand All @@ -55,34 +60,36 @@ function compile(content, contentFilename) {
}
return ts.createSourceFile(filename, source, 'ES5', '0');
},
writeFile: function(name, text, writeByteOrderMark) {
writeFile(name, text, writeByteOrderMark) {
if (output === null) {
output = text;
} else {
throw new Error('Expected only one dependency.');
}
},
getCanonicalFileName: function(filename) {
getCanonicalFileName(filename) {
return filename;
},
getCurrentDirectory: function() {
getCurrentDirectory() {
return '';
},
getNewLine: function() {
getNewLine() {
return '\n';
},
fileExists(filename) {
return ts.sys.fileExists(filename);
},
useCaseSensitiveFileNames() {
return ts.sys.useCaseSensitiveFileNames;
},
};
var program = ts.createProgram([
'lib.d.ts',
'jest.d.ts',
contentFilename,
], tsOptions, compilerHost);
var errors = program.getDiagnostics();
if (!errors.length) {
var checker = program.getTypeChecker(true);
errors = checker.getDiagnostics();
checker.emitFiles();
}
var emitResult = program.emit();
var errors = ts.getPreEmitDiagnostics(program).concat(emitResult.diagnostics);
if (errors.length) {
throw new Error(errors.map(formatErrorMessage).join('\n'));
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
/// <reference path="../React.d.ts" />
/// <reference path="../ReactDOM.d.ts" />

/*!
* Copyright 2015-present, Facebook, Inc.
* All rights reserved.
Expand Down

0 comments on commit 6a3e9d5

Please sign in to comment.