mirror of
https://github.com/zebrajr/react.git
synced 2026-01-15 12:15:22 +00:00
Update typescript package and jest preprocessor (#17839)
This commit is contained in:
committed by
GitHub
parent
50eafef07e
commit
3bd6adceda
@@ -10,29 +10,46 @@ const tsOptions = {
|
||||
};
|
||||
|
||||
function formatErrorMessage(error) {
|
||||
return (
|
||||
error.file.filename +
|
||||
'(' +
|
||||
error.file.getLineAndCharacterOfPosition(error.start).line +
|
||||
'): ' +
|
||||
error.messageText
|
||||
);
|
||||
if (error.file) {
|
||||
const message = ts.flattenDiagnosticMessageText(error.messageText, '\n');
|
||||
return (
|
||||
error.file.fileName +
|
||||
'(' +
|
||||
error.file.getLineAndCharacterOfPosition(error.start).line +
|
||||
'): ' +
|
||||
message
|
||||
);
|
||||
} else {
|
||||
return ts.flattenDiagnosticMessageText(error.messageText, '\n');
|
||||
}
|
||||
}
|
||||
|
||||
function compile(content, contentFilename) {
|
||||
let output = null;
|
||||
const compilerHost = {
|
||||
fileExists(filename) {
|
||||
return ts.sys.fileExists(filename);
|
||||
},
|
||||
getCanonicalFileName(filename) {
|
||||
return filename;
|
||||
},
|
||||
getCurrentDirectory() {
|
||||
return '';
|
||||
},
|
||||
getDefaultLibFileName: () => 'lib.d.ts',
|
||||
getNewLine: () => ts.sys.newLine,
|
||||
getSourceFile(filename, languageVersion) {
|
||||
let source;
|
||||
const libRegex = /lib\.(.+\.)?d\.ts$/;
|
||||
const jestRegex = /jest\.d\.ts/;
|
||||
const 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') {
|
||||
if (filename.match(libRegex)) {
|
||||
source = fs
|
||||
.readFileSync(require.resolve('typescript/lib/lib.d.ts'))
|
||||
.readFileSync(require.resolve('typescript/lib/' + filename))
|
||||
.toString();
|
||||
} else if (filename.match(jestRegex)) {
|
||||
source = fs.readFileSync(path.join(__dirname, 'jest.d.ts')).toString();
|
||||
@@ -55,6 +72,12 @@ function compile(content, contentFilename) {
|
||||
}
|
||||
return ts.createSourceFile(filename, source, 'ES5', '0');
|
||||
},
|
||||
readFile(filename) {
|
||||
return ts.sys.readFile(filename);
|
||||
},
|
||||
useCaseSensitiveFileNames() {
|
||||
return ts.sys.useCaseSensitiveFileNames;
|
||||
},
|
||||
writeFile(name, text, writeByteOrderMark) {
|
||||
if (output === null) {
|
||||
output = text;
|
||||
@@ -62,21 +85,6 @@ function compile(content, contentFilename) {
|
||||
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;
|
||||
},
|
||||
};
|
||||
const program = ts.createProgram(
|
||||
['lib.d.ts', 'jest.d.ts', contentFilename],
|
||||
|
||||
Reference in New Issue
Block a user