react/scripts/eslint-rules/warning-and-invariant-args.js
Andrew Clark 112168f31b
Lint rule for unminified errors (#15757)
* Lint rule for unminified errors

Add a lint rule that fails if an invariant message is not part of the
error code map.

The goal is to be more disciplined about adding and modifiying
production error codes. Error codes should be consistent across releases
even if their wording changes, for continuity in logs.

Currently, error codes are added to the error code map via an automated
script that runs right before release. The problem with this approach is
that if someone modifies an error message in the source, but neglects to
modify the corresponding message in the error code map, then the message
will be assigned a new error code, instead of reusing the existing one.

Because the error extraction script only runs before a release, people
rarely modify the error code map in practice. By moving the extraction
step to the PR stage, it forces the author to consider whether the
message should be assigned a new error code. It also allows the reviewer
to review the changes.

The trade off is that it requires more effort and context to land new
error messages, or to modify existing ones, particular for new
contributors who are not familiar with our processes.

Since we already expect users to lint their code, I would argue the
additional burden is marginal. Even if they forget to run the lint
command locally, they will get quick feedback from the CI lint job,
which typically finishes within 2-3 minutes.

* Add unreleased error messages to map
2019-05-29 11:29:04 -07:00

112 lines
3.5 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.
*
* @emails react-core
*/
'use strict';
const fs = require('fs');
const path = require('path');
const existingErrorMap = JSON.parse(
fs.readFileSync(path.resolve(__dirname, '../error-codes/codes.json'))
);
const messages = new Set();
Object.keys(existingErrorMap).forEach(key =>
messages.add(existingErrorMap[key])
);
/**
* The warning() and invariant() functions take format strings as their second
* argument.
*/
module.exports = function(context) {
// we also allow literal strings and concatenated literal strings
function getLiteralString(node) {
if (node.type === 'Literal' && typeof node.value === 'string') {
return node.value;
} else if (node.type === 'BinaryExpression' && node.operator === '+') {
const l = getLiteralString(node.left);
const r = getLiteralString(node.right);
if (l !== null && r !== null) {
return l + r;
}
}
return null;
}
return {
CallExpression: function(node) {
// This could be a little smarter by checking context.getScope() to see
// how warning/invariant was defined.
const isWarningOrInvariant =
node.callee.type === 'Identifier' &&
(node.callee.name === 'warning' ||
node.callee.name === 'warningWithoutStack' ||
node.callee.name === 'invariant');
if (!isWarningOrInvariant) {
return;
}
if (node.arguments.length < 2) {
context.report(node, '{{name}} takes at least two arguments', {
name: node.callee.name,
});
return;
}
const format = getLiteralString(node.arguments[1]);
if (format === null) {
context.report(
node,
'The second argument to {{name}} must be a string literal',
{name: node.callee.name}
);
return;
}
if (format.length < 10 || /^[s\W]*$/.test(format)) {
context.report(
node,
'The {{name}} format should be able to uniquely identify this ' +
'{{name}}. Please, use a more descriptive format than: {{format}}',
{name: node.callee.name, format: format}
);
return;
}
// count the number of formatting substitutions, plus the first two args
const expectedNArgs = (format.match(/%s/g) || []).length + 2;
if (node.arguments.length !== expectedNArgs) {
context.report(
node,
'Expected {{expectedNArgs}} arguments in call to {{name}} based on ' +
'the number of "%s" substitutions, but got {{length}}',
{
expectedNArgs: expectedNArgs,
name: node.callee.name,
length: node.arguments.length,
}
);
}
if (node.callee.name === 'invariant') {
if (!messages.has(format)) {
context.report(
node,
'Error message does not have a corresponding production ' +
'error code.\n\n' +
'Run `yarn extract-errors` to add the message to error code ' +
'map, so it can be stripped from the production builds. ' +
"Alternatively, if you're updating an existing error " +
'message, you can modify ' +
'`scripts/error-codes/codes.json` directly.'
);
}
}
},
};
};
module.exports.schema = [];