Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed invalid prop types error message to be more specific #11308

Merged
merged 22 commits into from
Oct 31, 2017
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions packages/react-dom/src/__tests__/ReactDOMAttribute-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,8 @@ describe('ReactDOM unknown attribute', () => {
expectDev(
normalizeCodeLocInfo(console.error.calls.argsFor(0)[0]),
).toMatch(
'Warning: Received `true` for non-boolean attribute `unknown`. ' +
'If this is expected, cast the value to a string.\n' +
'Warning: Received `true` for non-boolean attribute `unknown`. If this is expected, cast ' +
Copy link
Collaborator

@gaearon gaearon Oct 26, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This change is unnecessary, right? It just reformats the message in a slightly less readable way.

'the value to a string.\n' +
' in div (at **)',
);
expectDev(console.error.calls.count()).toBe(1);
Expand Down
13 changes: 9 additions & 4 deletions packages/react-dom/src/__tests__/ReactDOMComponent-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2042,7 +2042,8 @@ describe('ReactDOMComponent', () => {
expect(el.hasAttribute('whatever')).toBe(false);

expectDev(console.error.calls.argsFor(0)[0]).toContain(
'Warning: Received `true` for non-boolean attribute `whatever`',
'Received `true` for non-boolean attribute `whatever`. If this is expected, cast ' +
Copy link
Collaborator

@gaearon gaearon Oct 26, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would look nicer if If this is expected... started on the new line in test.

'the value to a string.',
);
});

Expand All @@ -2055,7 +2056,8 @@ describe('ReactDOMComponent', () => {
expect(el.hasAttribute('whatever')).toBe(false);

expectDev(console.error.calls.argsFor(0)[0]).toContain(
'Warning: Received `true` for non-boolean attribute `whatever`',
'Received `true` for non-boolean attribute `whatever`. If this is expected, cast ' +
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same

'the value to a string.',
);
});

Expand Down Expand Up @@ -2230,7 +2232,8 @@ describe('ReactDOMComponent', () => {
expect(el.hasAttribute('whatever')).toBe(false);

expectDev(console.error.calls.argsFor(0)[0]).toContain(
'Warning: Received `true` for non-boolean attribute `whatever`.',
'Received `true` for non-boolean attribute `whatever`. If this is expected, cast ' +
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same

'the value to a string.',
);
});

Expand Down Expand Up @@ -2283,7 +2286,9 @@ describe('ReactDOMComponent', () => {

expectDev(console.error.calls.count()).toBe(1);
expectDev(console.error.calls.argsFor(0)[0]).toContain(
'Warning: Received `false` for non-boolean attribute `whatever`.',
'If you mean to conditionally pass an attribute, use a ternary ' +
'expression: `false`={condition ? value : undefined} instead of ' +
'{condition && value}.',
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What does false={condition? value : undefined} mean? Why does it says false?

Please fix this to show the attribute name.

);
});
});
Expand Down
27 changes: 19 additions & 8 deletions packages/react-dom/src/shared/ReactDOMUnknownPropertyHook.js
Original file line number Diff line number Diff line change
Expand Up @@ -181,14 +181,25 @@ if (__DEV__) {
}

if (typeof value === 'boolean') {
warning(
DOMProperty.shouldAttributeAcceptBooleanValue(name),
'Received `%s` for non-boolean attribute `%s`. If this is expected, cast ' +
'the value to a string.%s',
value,
name,
getStackAddendum(),
);
if (value === true) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: we already know it's a boolean so can just write if (value)

warning(
DOMProperty.shouldAttributeAcceptBooleanValue(name),
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we make this warning(false and move out this (negated) condition together with the top one?

Copy link
Contributor Author

@NicBonetto NicBonetto Oct 26, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes :) Just so I have a correct understanding, the refactor will look like:

if (typeof value === 'boolean' && !false) {
  if (value === true) {
    warning(false,
      ...
    );
  } else {
    warning(false,
      ...
    );
  }
  ...
}

Is this the branching logic you prefer? I do not understand the !false condition, so if I am incorrect please explain what should take it's place.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if (typeof value === 'boolean' && !DOMProperty.shouldAttributeAcceptBooleanValue(name)) {
  if (value === true) {
    warning(false,
      ...
    );
  } else {
    warning(false,
      ...
    );
  }
  ...
}

'Received `%s` for non-boolean attribute `%s`. If this is expected, cast ' +
'the value to a string.%s',
value,
name,
getStackAddendum(),
);
} else {
warning(
DOMProperty.shouldAttributeAcceptBooleanValue(name),
'If you mean to conditionally pass an attribute, use a ternary ' +
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can this also start with Received ... for non-boolean attribute ...? I want both messages to look similar, but with different second sentences.

'expression: `%s`={condition ? value : undefined} instead of ' +
'{condition && value}.%s',
value,
getStackAddendum(),
);
}
warnedProperties[name] = true;
return true;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why was this removed?

}
Expand Down