react/scripts/error-codes/extract-errors.js
Sophie Alpert d63249d034 Update license headers BSD+Patents -> MIT
Did find and replace in TextMate.

```
find: (?:( \*)( ))?Copyright (?:\(c\) )?(\d{4})\b.+Facebook[\s\S]+(?:this source tree|the same directory)\.$
replace: $1$2Copyright (c) $3-present, Facebook, Inc.\n$1\n$1$2This source code is licensed under the MIT license found in the\n$1$2LICENSE file in the root directory of this source tree.
```
2017-09-25 18:17:44 -07:00

105 lines
2.8 KiB
JavaScript

/**
* Copyright (c) 2013-present, Facebook, Inc.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
'use strict';
const babylon = require('babylon');
const fs = require('fs');
const path = require('path');
const traverse = require('babel-traverse').default;
const evalToString = require('../shared/evalToString');
const invertObject = require('./invertObject');
const babylonOptions = {
sourceType: 'module',
// As a parser, babylon has its own options and we can't directly
// import/require a babel preset. It should be kept **the same** as
// the `babel-plugin-syntax-*` ones specified in
// https://github.com/facebook/fbjs/blob/master/babel-preset/configure.js
plugins: [
'classProperties',
'flow',
'jsx',
'trailingFunctionCommas',
'objectRestSpread',
],
};
module.exports = function(opts) {
if (!opts || !('errorMapFilePath' in opts)) {
throw new Error(
'Missing options. Ensure you pass an object with `errorMapFilePath`.'
);
}
var errorMapFilePath = opts.errorMapFilePath;
var existingErrorMap;
try {
// Using `fs.readFileSync` instead of `require` here, because `require()`
// calls are cached, and the cache map is not properly invalidated after
// file changes.
existingErrorMap = JSON.parse(
fs.readFileSync(
path.join(__dirname, path.basename(errorMapFilePath)),
'utf8'
)
);
} catch (e) {
existingErrorMap = {};
}
var allErrorIDs = Object.keys(existingErrorMap);
var currentID;
if (allErrorIDs.length === 0) {
// Map is empty
currentID = 0;
} else {
currentID = Math.max.apply(null, allErrorIDs) + 1;
}
// Here we invert the map object in memory for faster error code lookup
existingErrorMap = invertObject(existingErrorMap);
function transform(source) {
var ast = babylon.parse(source, babylonOptions);
traverse(ast, {
CallExpression: {
exit: function(astPath) {
if (astPath.get('callee').isIdentifier({name: 'invariant'})) {
var node = astPath.node;
// error messages can be concatenated (`+`) at runtime, so here's a
// trivial partial evaluator that interprets the literal value
var errorMsgLiteral = evalToString(node.arguments[1]);
if (existingErrorMap.hasOwnProperty(errorMsgLiteral)) {
return;
}
existingErrorMap[errorMsgLiteral] = '' + currentID++;
}
},
},
});
}
function flush(cb) {
fs.writeFileSync(
errorMapFilePath,
JSON.stringify(invertObject(existingErrorMap), null, 2) + '\n',
'utf-8'
);
}
return function extractErrors(filepath) {
const source = fs.readFileSync(filepath, 'utf-8');
transform(source);
flush();
};
};