react/scripts/print-warnings/print-warnings.js
Flarnie Marchan e6af1580fa Update print warning script for low priority warning (#9756)
* Add back caught error and other checks to 'lowPriorityWarning'

**what is the change?:**
This change makes 'lowPriorityWarning' an exact copy of 'warning.js' from
e66ba20ad5/packages/fbjs/src/__forks__/warning.js
where before we had skipped some checks from that module.

- Adds an error which we catch, in order to let people find the error and resulting stack trace when using devtools with 'pause on caught errors' checked.
- Adds check that 'format' argument is passed

**why make this change?:**
- To maintain a closer fork to 'warning.js'
- To allow easier debugging using 'pause on caught errors'
- To validate inputs to 'lowPriorityWarning'

**test plan:**
`yarn test`

**issue:**

* Update 'print-warnings' script to include 'lowPriorityWarning' output

**what is the change?:**
We print the logs from 'lowPriorityWarning' as well as 'warning' from the 'print-warnings' script.

NOTE: This PR is branching off of https://github.com/facebook/react/pull/9754

**why make this change?:**
We want to use the same process of white/blacklisting warnings with 'lowPriorityWarning' that we do with 'warning'.

**test plan:**
This is not super easy to test unless we are doing a sync with FB afaik. I plan on running a sync in the next few days, or next week at latest, for the sake of not landing big things on a Friday. That will be the actual test of this.

**issue:**
https://github.com/facebook/react/issues/9398
2017-05-26 07:47:49 -07:00

84 lines
2.4 KiB
JavaScript

/**
* Copyright (c) 2013-present, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*/
'use strict';
const babylon = require('babylon');
const fs = require('fs');
const through = require('through2');
const traverse = require('babel-traverse').default;
const gs = require('glob-stream');
const Bundles = require('../rollup/bundles');
const Modules = require('../rollup/modules');
const evalToString = require('../shared/evalToString');
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',
],
};
const warnings = new Set();
function transform(file, enc, cb) {
fs.readFile(file.path, 'utf8', function(err, source) {
if (err) {
cb(err);
return;
}
const ast = babylon.parse(source, babylonOptions);
traverse(ast, {
CallExpression: {
exit: function(astPath) {
const callee = astPath.get('callee');
if (
callee.isIdentifier({name: 'warning'}) ||
callee.isIdentifier({name: 'lowPriorityWarning'})
) {
const node = astPath.node;
// warning messages can be concatenated (`+`) at runtime, so here's
// a trivial partial evaluator that interprets the literal value
const warningMsgLiteral = evalToString(node.arguments[1]);
warnings.add(JSON.stringify(warningMsgLiteral));
}
},
},
});
cb(null);
});
}
const sourcePaths = Bundles.bundles
.filter(
bundle =>
bundle.bundleTypes.indexOf(Bundles.bundleTypes.FB_DEV) !== -1 ||
bundle.bundleTypes.indexOf(Bundles.bundleTypes.FB_PROD) !== -1
)
.reduce((allPaths, bundle) => allPaths.concat(bundle.paths), [])
.concat(Modules.getExcludedHasteGlobs().map(glob => `!${glob}`));
gs(sourcePaths).pipe(
through.obj(transform, cb => {
process.stdout.write(Array.from(warnings).sort().join('\n') + '\n');
cb();
})
);