Check warning/invariant calls have good messages
The `warning` implementation checked that the message is long enough to be useful. See commit f5038829d for more information. It makes more sense to move this into a lint rule, and also to apply it for both `warning` and `invariant`. We can safely remove stuff from the `warning` implementation as we replace the function internally anyways. https://github.com/facebook/react/pull/4021#issuecomment-108694976 @spicyj
Benjamin Woodruff committed
Jun 4, 2015 at 10:52 UTC
c91fd8ac145c7fd437ff0fc113ba72641a234250
3 files changed
+45
-12
eslint-rules/__tests__/warning-and-invariant-args-test.js
+32
-1
@@ -58,7 +58,7 @@ eslintTester.addRuleTest('eslint-rules/warning-and-invariant-args', {
58
],
59
},
60
{
61
- code: "warning(true, 'foobar', 'junk argument');",
61
+ code: "warning(true, 'foo is a bar under foobar', 'junk argument');",
62
errors: [
63
{
64
message:
@@ -67,6 +67,37 @@ eslintTester.addRuleTest('eslint-rules/warning-and-invariant-args', {
67
},
68
],
69
},
70
+ {
71
+ code: "invariant(true, 'error!');",
72
+ errors: [
73
+ {
74
+ message:
75
+ 'The invariant format should be able to uniquely identify this ' +
76
+ 'invariant. Please, use a more descriptive format than: error!',
77
+ },
78
+ ],
79
+ },
80
+ {
81
+ code: "warning(true, 'error!');",
82
+ errors: [
83
+ {
84
+ message:
85
+ 'The warning format should be able to uniquely identify this ' +
86
+ 'warning. Please, use a more descriptive format than: error!',
87
+ },
88
+ ],
89
+ },
90
+ {
91
+ code: "warning(true, '%s %s, %s %s: %s (%s)', 1, 2, 3, 4, 5, 6);",
92
+ errors: [
93
+ {
94
+ message:
95
+ 'The warning format should be able to uniquely identify this ' +
96
+ 'warning. Please, use a more descriptive format than: ' +
97
+ '%s %s, %s %s: %s (%s)',
98
+ },
99
+ ],
100
+ },
101
],
102
});
103
eslint-rules/warning-and-invariant-args.js
+13
-4
@@ -49,8 +49,8 @@ module.exports = function(context) {
49
);
50
return;
51
}
52
- var str = getLiteralString(node.arguments[1]);
53
- if (str === null) {
52
+ var format = getLiteralString(node.arguments[1]);
53
+ if (format === null) {
54
context.report(
55
node,
56
'The second argument to {{name}} must be a string literal',
@@ -58,8 +58,17 @@ module.exports = function(context) {
58
);
59
return;
60
}
61
- // count the number of string substitutions, plus the first two args
62
- var expectedNArgs = (str.match(/%s/g) || []).length + 2;
61
+ if (format.length < 10 || /^[s\W]*$/.test(format)) {
62
+ context.report(
63
+ node,
64
+ 'The {{name}} format should be able to uniquely identify this ' +
65
+ '{{name}}. Please, use a more descriptive format than: {{format}}',
66
+ {name: node.callee.name, format: format}
67
+ );
68
+ return;
69
+ }
70
+ // count the number of formating substitutions, plus the first two args
71
+ var expectedNArgs = (format.match(/%s/g) || []).length + 2;
72
if (node.arguments.length !== expectedNArgs) {
73
context.report(
74
node,
src/shared/vendor/core/warning.js
-7
@@ -31,13 +31,6 @@ if (__DEV__) {
31
);
32
}
33
34
- if (format.length < 10 || /^[s\W]*$/.test(format)) {
35
- throw new Error(
36
- 'The warning format should be able to uniquely identify this ' +
37
- 'warning. Please, use a more descriptive format than: ' + format
38
- );
39
- }
40
-
34
if (format.indexOf('Failed Composite propType: ') === 0) {
35
return; // Ignore CompositeComponent proptype check.
36
}