Skip to content

Commit 97b4970

Browse files
committed
Remove method name prefix from warnings and errors
This pattern is a petpeeve of mine. I don't consider this best practice and so most don't have these prefixes. Very inconsistent. At best this is useless and noisey that you have to parse because the information is also in the stack trace. At worse these are misleading because they'rehighlighting something internal (like validateDOMNesting) which even suggests an internal bug. Even the ones public to React aren't necessarily what you called because you might be calling a wrapper around it. That would be properly reflected in a stack trace - which can also properly ignore list so that the first stack you see is your callsite, Which might be like render() in react-testing-library rather than createRoot() for example.
1 parent 66c8346 commit 97b4970

39 files changed

+188
-215
lines changed

packages/react-dom-bindings/src/client/validateDOMNesting.js

+4-8
Original file line numberDiff line numberDiff line change
@@ -477,15 +477,14 @@ function validateDOMNesting(
477477
'the browser.';
478478
}
479479
console.error(
480-
'validateDOMNesting(...): %s cannot appear as a child of <%s>.%s',
480+
'%s cannot appear as a child of <%s>.%s',
481481
tagDisplayName,
482482
ancestorTag,
483483
info,
484484
);
485485
} else {
486486
console.error(
487-
'validateDOMNesting(...): %s cannot appear as a descendant of ' +
488-
'<%s>.',
487+
'%s cannot appear as a descendant of ' + '<%s>.',
489488
tagDisplayName,
490489
ancestorTag,
491490
);
@@ -507,13 +506,10 @@ function validateTextNesting(childText: string, parentTag: string): void {
507506
didWarn[warnKey] = true;
508507

509508
if (/\S/.test(childText)) {
510-
console.error(
511-
'validateDOMNesting(...): Text nodes cannot appear as a child of <%s>.',
512-
parentTag,
513-
);
509+
console.error('Text nodes cannot appear as a child of <%s>.', parentTag);
514510
} else {
515511
console.error(
516-
'validateDOMNesting(...): Whitespace text nodes cannot appear as a child of <%s>. ' +
512+
'Whitespace text nodes cannot appear as a child of <%s>. ' +
517513
"Make sure you don't have any extra whitespace between tags on " +
518514
'each line of your source code.',
519515
parentTag,

packages/react-dom/src/__tests__/ReactCompositeComponent-test.js

+13-13
Original file line numberDiff line numberDiff line change
@@ -879,7 +879,7 @@ describe('ReactCompositeComponent', () => {
879879
root.render(<Foo idx="qwe" />);
880880
});
881881
}).toErrorDev(
882-
'Foo(...): When calling super() in `Foo`, make sure to pass ' +
882+
'When calling super() in `Foo`, make sure to pass ' +
883883
"up the same props that your component's constructor was passed.",
884884
);
885885
});
@@ -1233,14 +1233,14 @@ describe('ReactCompositeComponent', () => {
12331233
}).toErrorDev([
12341234
// Expect two errors because invokeGuardedCallback will dispatch an error event,
12351235
// Causing the warning to be logged again.
1236-
'Warning: RenderTextInvalidConstructor(...): No `render` method found on the returned component instance: ' +
1236+
'Warning: No `render` method found on the RenderTextInvalidConstructor instance: ' +
12371237
'did you accidentally return an object from the constructor?',
1238-
'Warning: RenderTextInvalidConstructor(...): No `render` method found on the returned component instance: ' +
1238+
'Warning: No `render` method found on the RenderTextInvalidConstructor instance: ' +
12391239
'did you accidentally return an object from the constructor?',
12401240
// And then two more because we retry errors.
1241-
'Warning: RenderTextInvalidConstructor(...): No `render` method found on the returned component instance: ' +
1241+
'Warning: No `render` method found on the RenderTextInvalidConstructor instance: ' +
12421242
'did you accidentally return an object from the constructor?',
1243-
'Warning: RenderTextInvalidConstructor(...): No `render` method found on the returned component instance: ' +
1243+
'Warning: No `render` method found on the RenderTextInvalidConstructor instance: ' +
12441244
'did you accidentally return an object from the constructor?',
12451245
]);
12461246
});
@@ -1280,16 +1280,16 @@ describe('ReactCompositeComponent', () => {
12801280
}).toErrorDev([
12811281
// Expect two errors because invokeGuardedCallback will dispatch an error event,
12821282
// Causing the warning to be logged again.
1283-
'Warning: RenderTestUndefinedRender(...): No `render` method found on the returned ' +
1284-
'component instance: you may have forgotten to define `render`.',
1285-
'Warning: RenderTestUndefinedRender(...): No `render` method found on the returned ' +
1286-
'component instance: you may have forgotten to define `render`.',
1283+
'Warning: No `render` method found on the RenderTestUndefinedRender instance: ' +
1284+
'you may have forgotten to define `render`.',
1285+
'Warning: No `render` method found on the RenderTestUndefinedRender instance: ' +
1286+
'you may have forgotten to define `render`.',
12871287

12881288
// And then two more because we retry errors.
1289-
'Warning: RenderTestUndefinedRender(...): No `render` method found on the returned ' +
1290-
'component instance: you may have forgotten to define `render`.',
1291-
'Warning: RenderTestUndefinedRender(...): No `render` method found on the returned ' +
1292-
'component instance: you may have forgotten to define `render`.',
1289+
'Warning: No `render` method found on the RenderTestUndefinedRender instance: ' +
1290+
'you may have forgotten to define `render`.',
1291+
'Warning: No `render` method found on the RenderTestUndefinedRender instance: ' +
1292+
'you may have forgotten to define `render`.',
12931293
]);
12941294
});
12951295

packages/react-dom/src/__tests__/ReactDOM-test.js

+6-6
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,7 @@ describe('ReactDOM', () => {
183183
expect(() => {
184184
ReactDOM.render(<A />, myDiv, 'no');
185185
}).toErrorDev(
186-
'render(...): Expected the last optional `callback` argument to be ' +
186+
'Expected the last optional `callback` argument to be ' +
187187
'a function. Instead received: no.',
188188
);
189189
}).toThrowError(
@@ -195,7 +195,7 @@ describe('ReactDOM', () => {
195195
expect(() => {
196196
ReactDOM.render(<A />, myDiv, {foo: 'bar'});
197197
}).toErrorDev(
198-
'render(...): Expected the last optional `callback` argument to be ' +
198+
'Expected the last optional `callback` argument to be ' +
199199
'a function. Instead received: [object Object].',
200200
);
201201
}).toThrowError(
@@ -207,7 +207,7 @@ describe('ReactDOM', () => {
207207
expect(() => {
208208
ReactDOM.render(<A />, myDiv, new Foo());
209209
}).toErrorDev(
210-
'render(...): Expected the last optional `callback` argument to be ' +
210+
'Expected the last optional `callback` argument to be ' +
211211
'a function. Instead received: [object Object].',
212212
);
213213
}).toThrowError(
@@ -236,7 +236,7 @@ describe('ReactDOM', () => {
236236
expect(() => {
237237
ReactDOM.render(<A />, myDiv, 'no');
238238
}).toErrorDev(
239-
'render(...): Expected the last optional `callback` argument to be ' +
239+
'Expected the last optional `callback` argument to be ' +
240240
'a function. Instead received: no.',
241241
);
242242
}).toThrowError(
@@ -249,7 +249,7 @@ describe('ReactDOM', () => {
249249
expect(() => {
250250
ReactDOM.render(<A />, myDiv, {foo: 'bar'});
251251
}).toErrorDev(
252-
'render(...): Expected the last optional `callback` argument to be ' +
252+
'Expected the last optional `callback` argument to be ' +
253253
'a function. Instead received: [object Object].',
254254
);
255255
}).toThrowError(
@@ -262,7 +262,7 @@ describe('ReactDOM', () => {
262262
expect(() => {
263263
ReactDOM.render(<A />, myDiv, new Foo());
264264
}).toErrorDev(
265-
'render(...): Expected the last optional `callback` argument to be ' +
265+
'Expected the last optional `callback` argument to be ' +
266266
'a function. Instead received: [object Object].',
267267
);
268268
}).toThrowError(

packages/react-dom/src/__tests__/ReactDOMComponent-test.js

+7-7
Original file line numberDiff line numberDiff line change
@@ -2188,7 +2188,7 @@ describe('ReactDOMComponent', () => {
21882188
);
21892189
});
21902190
}).toErrorDev([
2191-
'Warning: validateDOMNesting(...): <tr> cannot appear as a child of ' +
2191+
'Warning: <tr> cannot appear as a child of ' +
21922192
'<div>.' +
21932193
'\n in tr (at **)' +
21942194
'\n in div (at **)',
@@ -2208,7 +2208,7 @@ describe('ReactDOMComponent', () => {
22082208
);
22092209
});
22102210
}).toErrorDev(
2211-
'Warning: validateDOMNesting(...): <p> cannot appear as a descendant ' +
2211+
'Warning: <p> cannot appear as a descendant ' +
22122212
'of <p>.' +
22132213
// There is no outer `p` here because root container is not part of the stack.
22142214
'\n in p (at **)' +
@@ -2241,20 +2241,20 @@ describe('ReactDOMComponent', () => {
22412241
root.render(<Foo />);
22422242
});
22432243
}).toErrorDev([
2244-
'Warning: validateDOMNesting(...): <tr> cannot appear as a child of ' +
2244+
'Warning: <tr> cannot appear as a child of ' +
22452245
'<table>. Add a <tbody>, <thead> or <tfoot> to your code to match the DOM tree generated ' +
22462246
'by the browser.' +
22472247
'\n in tr (at **)' +
22482248
'\n in Row (at **)' +
22492249
'\n in table (at **)' +
22502250
'\n in Foo (at **)',
2251-
'Warning: validateDOMNesting(...): Text nodes cannot appear as a ' +
2251+
'Warning: Text nodes cannot appear as a ' +
22522252
'child of <tr>.' +
22532253
'\n in tr (at **)' +
22542254
'\n in Row (at **)' +
22552255
'\n in table (at **)' +
22562256
'\n in Foo (at **)',
2257-
'Warning: validateDOMNesting(...): Whitespace text nodes cannot ' +
2257+
'Warning: Whitespace text nodes cannot ' +
22582258
"appear as a child of <table>. Make sure you don't have any extra " +
22592259
'whitespace between tags on each line of your source code.' +
22602260
'\n in table (at **)' +
@@ -2283,7 +2283,7 @@ describe('ReactDOMComponent', () => {
22832283
root.render(<Foo> </Foo>);
22842284
});
22852285
}).toErrorDev([
2286-
'Warning: validateDOMNesting(...): Whitespace text nodes cannot ' +
2286+
'Warning: Whitespace text nodes cannot ' +
22872287
"appear as a child of <table>. Make sure you don't have any extra " +
22882288
'whitespace between tags on each line of your source code.' +
22892289
'\n in table (at **)' +
@@ -2311,7 +2311,7 @@ describe('ReactDOMComponent', () => {
23112311
);
23122312
});
23132313
}).toErrorDev([
2314-
'Warning: validateDOMNesting(...): Text nodes cannot appear as a ' +
2314+
'Warning: Text nodes cannot appear as a ' +
23152315
'child of <tr>.' +
23162316
'\n in tr (at **)' +
23172317
'\n in Row (at **)' +

packages/react-dom/src/__tests__/ReactDOMFloat-test.js

+10-10
Original file line numberDiff line numberDiff line change
@@ -523,7 +523,7 @@ describe('ReactDOMFloat', () => {
523523
}).toErrorDev(
524524
[
525525
'Cannot render <noscript> outside the main document. Try moving it into the root <head> tag.',
526-
'Warning: validateDOMNesting(...): <noscript> cannot appear as a child of <#document>.',
526+
'Warning: <noscript> cannot appear as a child of <#document>.',
527527
],
528528
{withoutStack: 1},
529529
);
@@ -538,7 +538,7 @@ describe('ReactDOMFloat', () => {
538538
await waitForAll([]);
539539
}).toErrorDev([
540540
'Cannot render <template> outside the main document. Try moving it into the root <head> tag.',
541-
'Warning: validateDOMNesting(...): <template> cannot appear as a child of <html>.',
541+
'Warning: <template> cannot appear as a child of <html>.',
542542
]);
543543

544544
await expect(async () => {
@@ -551,7 +551,7 @@ describe('ReactDOMFloat', () => {
551551
await waitForAll([]);
552552
}).toErrorDev([
553553
'Cannot render a <style> outside the main document without knowing its precedence and a unique href key. React can hoist and deduplicate <style> tags if you provide a `precedence` prop along with an `href` prop that does not conflic with the `href` values used in any other hoisted <style> or <link rel="stylesheet" ...> tags. Note that hoisting <style> tags is considered an advanced feature that most will not use directly. Consider moving the <style> tag to the <head> or consider adding a `precedence="default"` and `href="some unique resource identifier"`, or move the <style> to the <style> tag.',
554-
'Warning: validateDOMNesting(...): <style> cannot appear as a child of <html>.',
554+
'Warning: <style> cannot appear as a child of <html>.',
555555
]);
556556

557557
await expect(async () => {
@@ -574,7 +574,7 @@ describe('ReactDOMFloat', () => {
574574
}).toErrorDev(
575575
[
576576
'Cannot render a <link rel="stylesheet" /> outside the main document without knowing its precedence. Consider adding precedence="default" or moving it into the root <head> tag.',
577-
'Warning: validateDOMNesting(...): <link> cannot appear as a child of <#document>.',
577+
'Warning: <link> cannot appear as a child of <#document>.',
578578
],
579579
{withoutStack: 1},
580580
);
@@ -591,7 +591,7 @@ describe('ReactDOMFloat', () => {
591591
await waitForAll([]);
592592
}).toErrorDev([
593593
'Cannot render a sync or defer <script> outside the main document without knowing its order. Try adding async="" or moving it into the root <head> tag.',
594-
'Warning: validateDOMNesting(...): <script> cannot appear as a child of <html>.',
594+
'Warning: <script> cannot appear as a child of <html>.',
595595
]);
596596

597597
await expect(async () => {
@@ -2552,11 +2552,11 @@ body {
25522552
'Cannot render a <style> outside the main document if it has an `itemProp` prop. `itemProp` suggests the tag belongs to an `itemScope` which can appear anywhere in the DOM. If you were intending for React to hoist this <style> remove the `itemProp` prop. Otherwise, try moving this tag into the <head> or <body> of the Document.',
25532553
'Cannot render a <link> outside the main document if it has an `itemProp` prop. `itemProp` suggests the tag belongs to an `itemScope` which can appear anywhere in the DOM. If you were intending for React to hoist this <link> remove the `itemProp` prop. Otherwise, try moving this tag into the <head> or <body> of the Document.',
25542554
'Cannot render a <script> outside the main document if it has an `itemProp` prop. `itemProp` suggests the tag belongs to an `itemScope` which can appear anywhere in the DOM. If you were intending for React to hoist this <script> remove the `itemProp` prop. Otherwise, try moving this tag into the <head> or <body> of the Document.',
2555-
'validateDOMNesting(...): <meta> cannot appear as a child of <html>',
2556-
'validateDOMNesting(...): <title> cannot appear as a child of <html>',
2557-
'validateDOMNesting(...): <style> cannot appear as a child of <html>',
2558-
'validateDOMNesting(...): <link> cannot appear as a child of <html>',
2559-
'validateDOMNesting(...): <script> cannot appear as a child of <html>',
2555+
'<meta> cannot appear as a child of <html>',
2556+
'<title> cannot appear as a child of <html>',
2557+
'<style> cannot appear as a child of <html>',
2558+
'<link> cannot appear as a child of <html>',
2559+
'<script> cannot appear as a child of <html>',
25602560
]);
25612561
});
25622562

packages/react-dom/src/__tests__/ReactDOMForm-test.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -381,7 +381,7 @@ describe('ReactDOMForm', () => {
381381
);
382382
});
383383
}).toErrorDev([
384-
'Warning: validateDOMNesting(...): <form> cannot appear as a descendant of <form>.' +
384+
'Warning: <form> cannot appear as a descendant of <form>.' +
385385
'\n in form (at **)' +
386386
'\n in form (at **)',
387387
]);

packages/react-dom/src/__tests__/ReactDOMLegacyComponentTree-test.internal.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ describe('ReactDOMComponentTree', () => {
4848
const anotherComponent = <div />;
4949
const instance = ReactDOM.render(component, container);
5050
expect(() => ReactDOM.render(anotherComponent, instance)).toErrorDev(
51-
'render(...): Replacing React-rendered children with a new root ' +
51+
'Replacing React-rendered children with a new root ' +
5252
'component. If you intended to update the children of this node, ' +
5353
'you should instead have the existing children update their state ' +
5454
'and render the new components instead of calling ReactDOM.render.',

packages/react-dom/src/__tests__/ReactDOMLegacyFiber-test.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -1200,7 +1200,7 @@ describe('ReactDOMLegacyFiber', () => {
12001200
expect(() =>
12011201
ReactDOM.render(<div key="2">baz</div>, container),
12021202
).toErrorDev(
1203-
'render(...): ' +
1203+
'' +
12041204
'It looks like the React-rendered content of this container was ' +
12051205
'removed without using React. This is not supported and will ' +
12061206
'cause errors. Instead, call ReactDOM.unmountComponentAtNode ' +
@@ -1218,7 +1218,7 @@ describe('ReactDOMLegacyFiber', () => {
12181218
// then we mess with the DOM before an update
12191219
container.innerHTML = '<div>MEOW.</div>';
12201220
expect(() => ReactDOM.render(<div>baz</div>, container)).toErrorDev(
1221-
'render(...): ' +
1221+
'' +
12221222
'It looks like the React-rendered content of this container was ' +
12231223
'removed without using React. This is not supported and will ' +
12241224
'cause errors. Instead, call ReactDOM.unmountComponentAtNode ' +
@@ -1235,7 +1235,7 @@ describe('ReactDOMLegacyFiber', () => {
12351235
// then we mess with the DOM before an update
12361236
container.innerHTML = '';
12371237
expect(() => ReactDOM.render(<div>baz</div>, container)).toErrorDev(
1238-
'render(...): ' +
1238+
'' +
12391239
'It looks like the React-rendered content of this container was ' +
12401240
'removed without using React. This is not supported and will ' +
12411241
'cause errors. Instead, call ReactDOM.unmountComponentAtNode ' +

packages/react-dom/src/__tests__/ReactDOMOption-test.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ describe('ReactDOMOption', () => {
4646
expect(() => {
4747
node = ReactTestUtils.renderIntoDocument(el);
4848
}).toErrorDev(
49-
'validateDOMNesting(...): <div> cannot appear as a child of <option>.\n' +
49+
'<div> cannot appear as a child of <option>.\n' +
5050
' in div (at **)\n' +
5151
' in option (at **)',
5252
);
@@ -263,7 +263,7 @@ describe('ReactDOMOption', () => {
263263
[
264264
'Warning: Text content did not match. Server: "FooBaz" Client: "Foo"',
265265
'Warning: An error occurred during hydration. The server HTML was replaced with client content in <div>',
266-
'Warning: validateDOMNesting(...): <div> cannot appear as a child of <option>',
266+
'Warning: <div> cannot appear as a child of <option>',
267267
],
268268
{withoutStack: 1},
269269
);

packages/react-dom/src/__tests__/ReactDOMRoot-test.js

+6-6
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ describe('ReactDOMRoot', () => {
7171
const callback = jest.fn();
7272
const root = ReactDOMClient.createRoot(container);
7373
expect(() => root.render(<div>Hi</div>, callback)).toErrorDev(
74-
'render(...): does not support the second callback argument. ' +
74+
'does not support the second callback argument. ' +
7575
'To execute a side effect after rendering, declare it in a component body with useEffect().',
7676
{withoutStack: true},
7777
);
@@ -115,7 +115,7 @@ describe('ReactDOMRoot', () => {
115115
const root = ReactDOMClient.createRoot(container);
116116
root.render(<div>Hi</div>);
117117
expect(() => root.unmount(callback)).toErrorDev(
118-
'unmount(...): does not support a callback argument. ' +
118+
'does not support a callback argument. ' +
119119
'To execute a side effect after rendering, declare it in a component body with useEffect().',
120120
{withoutStack: true},
121121
);
@@ -199,7 +199,7 @@ describe('ReactDOMRoot', () => {
199199
it('throws a good message on invalid containers', () => {
200200
expect(() => {
201201
ReactDOMClient.createRoot(<div>Hi</div>);
202-
}).toThrow('createRoot(...): Target container is not a DOM element.');
202+
}).toThrow('Target container is not a DOM element.');
203203
});
204204

205205
it('warns when creating two roots managing the same container', () => {
@@ -253,7 +253,7 @@ describe('ReactDOMRoot', () => {
253253
expect(() => {
254254
root.render(<div>Hi</div>);
255255
}).toErrorDev(
256-
'render(...): It looks like the React-rendered content of the ' +
256+
'It looks like the React-rendered content of the ' +
257257
'root container was removed without using React. This is not ' +
258258
'supported and will cause errors. Instead, call ' +
259259
"root.unmount() to empty a root's container.",
@@ -446,10 +446,10 @@ describe('ReactDOMRoot', () => {
446446
const commentNode = div.childNodes[0];
447447

448448
expect(() => ReactDOMClient.createRoot(commentNode)).toThrow(
449-
'createRoot(...): Target container is not a DOM element.',
449+
'Target container is not a DOM element.',
450450
);
451451
expect(() => ReactDOMClient.hydrateRoot(commentNode)).toThrow(
452-
'hydrateRoot(...): Target container is not a DOM element.',
452+
'Target container is not a DOM element.',
453453
);
454454
});
455455

packages/react-dom/src/__tests__/ReactDOMServerLifecycles-test.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -260,7 +260,7 @@ describe('ReactDOMServerLifecycles', () => {
260260
'<div>1-2</div>',
261261
);
262262
}).toErrorDev(
263-
'Warning: setState(...): Can only update a mounting component. This ' +
263+
'Warning: Can only update a mounting component. This ' +
264264
'usually means you called setState() outside componentWillMount() on ' +
265265
'the server. This is a no-op.\n\n' +
266266
'Please check the code for the Outer component.',

packages/react-dom/src/__tests__/ReactFunctionComponent-test.js

+1-2
Original file line numberDiff line numberDiff line change
@@ -154,8 +154,7 @@ describe('ReactFunctionComponent', () => {
154154
root.render(<FunctionComponentWithChildContext name="A" />);
155155
});
156156
}).toErrorDev(
157-
'FunctionComponentWithChildContext(...): childContextTypes cannot ' +
158-
'be defined on a function component.',
157+
'childContextTypes cannot ' + 'be defined on a function component.',
159158
);
160159
});
161160

0 commit comments

Comments
 (0)