react/scripts/print-warnings/print-warnings.js
Sunil Pai d9b4c55d53
unify deprecated/unsafe lifecycle warnings, pass tests (#16103)
- redoes #15431 from scratch, taking on the feedback there
- unifies the messaging between "deprecated" and UNSAFE_ lifecycle messages. It reorganizes ReactStrictModeWarnings.js to capture and flush all the lifecycle warnings in one procedure each.
- matches the warning from ReactPartialRenderer to match the above change
- passes all the tests
- this also turns on `warnAboutDeprecatedLifecycles` for the test renderer. I think we missed doing so it previously. In a future PR, I'll remove the feature flag altogether.
- this DOES NOT do the same treatment for context warnings, I'll do that in another PR too
2019-07-15 20:56:44 +01:00

90 lines
2.3 KiB
JavaScript

/**
* Copyright (c) Facebook, Inc. and its affiliates.
*
* 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 through = require('through2');
const traverse = require('babel-traverse').default;
const gs = require('glob-stream');
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/packages/babel-preset-fbjs/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: 'warningWithoutStack'}) ||
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
try {
const warningMsgLiteral = evalToString(node.arguments[1]);
warnings.add(JSON.stringify(warningMsgLiteral));
} catch (error) {
console.error(
'Failed to extract warning message from',
file.path
);
console.error(astPath.node.loc);
throw error;
}
}
},
},
});
cb(null);
});
}
gs([
'packages/**/*.js',
'!packages/shared/warning.js',
'!**/__tests__/**/*.js',
'!**/__mocks__/**/*.js',
]).pipe(
through.obj(transform, cb => {
process.stdout.write(
Array.from(warnings)
.sort()
.join('\n') + '\n'
);
cb();
})
);