react/scripts/jest/ts-preprocessor.js
Flarnie Marchan 009c9f7201 Update tests to not warn due to moved PropTypes and shallowRenderer (#9486)
* Update tests to not warn due to moved PropTypes and shallowRenderer

We added some warnings in v15.5 for calling `React.PropTypes` and
calling the shallow renderer from the wrong place. These warnings were
causing test failures, and now they are fixed.

Most of these were for the `React.PropTypes` change.

* tweak from running prettier

* Final tweaks to get tests passing

**what is the change?:**
Updated 'PropTypes' and 'shallow renderer' syntax in a couple more
places to get tests passing.

**why make this change?:**
In order to verify any changes to the 15.6 and 15.* branches in general
we should have tests passing.

**test plan:**
`npm run test`

**issue:**
https://github.com/facebook/react/issues/9410
2017-04-24 18:48:20 -07:00

102 lines
2.6 KiB
JavaScript

'use strict';
var fs = require('fs');
var path = require('path');
var ts = require('typescript');
var tsOptions = {
module: ts.ModuleKind.CommonJS,
jsx: ts.JsxEmit.React,
};
function formatErrorMessage(error) {
return (
error.file.filename + '(' +
error.file.getLineAndCharacterOfPosition(error.start).line +
'): ' +
error.messageText
);
}
function compile(content, contentFilename) {
var output = null;
var compilerHost = {
getSourceFile(filename, languageVersion) {
var source;
// `path.normalize` and `path.join` are used to turn forward slashes in
// the file path into backslashes on Windows.
filename = path.normalize(filename);
var reactRegex = new RegExp(
path.join('/', '(?:React|ReactDOM|PropTypes)(?:\.d)?\.ts$')
);
var jestRegex = /jest\.d\.ts/;
if (filename === 'lib.d.ts') {
source = fs.readFileSync(
require.resolve('typescript/lib/lib.d.ts')
).toString();
} else if (filename.match(jestRegex)) {
source = fs.readFileSync(
path.join(__dirname, 'jest.d.ts')
).toString();
} else if (filename === contentFilename) {
source = content;
} else if (reactRegex.test(filename)) {
// TypeScript will look for the .d.ts files in each ancestor directory,
// so there may not be a file at the referenced path as it climbs the
// hierarchy.
try {
source = fs.readFileSync(filename).toString();
} catch (e) {
if (e.code === 'ENOENT') {
return undefined;
}
throw e;
}
} else {
throw new Error('Unexpected filename ' + filename);
}
return ts.createSourceFile(filename, source, 'ES5', '0');
},
writeFile(name, text, writeByteOrderMark) {
if (output === null) {
output = text;
} else {
throw new Error('Expected only one dependency.');
}
},
getCanonicalFileName(filename) {
return filename;
},
getCurrentDirectory() {
return '';
},
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 emitResult = program.emit();
var errors = ts.getPreEmitDiagnostics(program).concat(emitResult.diagnostics);
if (errors.length) {
throw new Error(errors.map(formatErrorMessage).join('\n'));
}
return output;
}
module.exports = {
compile: compile,
};