Files
react/scripts/jest/ts-preprocessor.js
Brian Vaughn 2beec2f308 createClass + PropTypes + checkPropTypes warnings (#9399)
(Temporarily) re-adds getters with deprecation warnings for React.PropTypes, React.checkPropTypes, and React.createClass.

* 08bd020: Replace all references to React.PropTypes with prop-types to avoid triggering our own warning message.
* ef5b5c6: Removed several references to React.createClass that appeared after rebasing this branch. (reviewed by @flarnie)
* 524ce20: Added getters for createClass and PropTypes to the main React isomorphic object, behind one-time warning messages. (reviewed by @spicyj)
* db48f54: Fixed Rollup bundles to inline 'prop-types' and 'create-react-class' for UMD builds only. (reviewed by @spicyj, @trueadm )
* cf49cfd: Updated tests-passing.txt to remove tests that were deleted in this branch.
* d34109a: Responses to PR feedback from @spicyj. (Added package.json dependencies to packages/react and packages/react-dom. Renamed a var. Expanded on an inline comment.)
* 488c8d2: Added warning for moved package to React.checkPropTypes accessor too and updated build script.
* 83bcb29: Wordsmithing for deprecation notices (added fb.me links).
* afdc9d2: Tweaked legacy module inlining to remove order-of-deps constraint
* d1348b9: Removed $FlowFixMe.
* 7dbc3e7: More wordsmithing of deprecation notices based on Dan's feedback.
2017-04-11 14:28:03 -07:00

95 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;
var jestRegex = /jest\.d\.ts/;
var reactRegex = /(?:React|ReactDOM|PropTypes)(?:\.d)?\.ts$/;
// `path.normalize` is used to turn forward slashes in
// the file path into backslashes on Windows.
filename = path.normalize(filename);
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,
};