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

group warnings for unknown DOM properties #7153

Merged
merged 1 commit into from
Jun 30, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
11 changes: 11 additions & 0 deletions src/renderers/dom/shared/__tests__/ReactDOMComponent-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,17 @@ describe('ReactDOMComponent', function() {
);
});

it('should group multiple unknown prop warnings together', function() {
spyOn(console, 'error');
var container = document.createElement('div');
ReactDOM.render(<div foo="bar" baz="qux" />, container);
expect(console.error.calls.count(0)).toBe(1);
expect(normalizeCodeLocInfo(console.error.calls.argsFor(0)[0])).toBe(
'Warning: Unknown props `foo`, `baz` on <div> tag. Remove these props from the element. ' +
'For details, see https://fb.me/react-unknown-prop\n in div (at **)'
);
});

it('should warn for onDblClick prop', function() {
spyOn(console, 'error');
var container = document.createElement('div');
Expand Down
58 changes: 43 additions & 15 deletions src/renderers/dom/shared/devtools/ReactDOMUnknownPropertyDevtool.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,16 +36,16 @@ if (__DEV__) {
};
var warnedProperties = {};

var warnUnknownProperty = function(tagName, name, debugID) {
var validateProperty = function(tagName, name, debugID) {
if (DOMProperty.properties.hasOwnProperty(name) || DOMProperty.isCustomAttribute(name)) {
return;
return true;
}
if (reactProps.hasOwnProperty(name) && reactProps[name] ||
warnedProperties.hasOwnProperty(name) && warnedProperties[name]) {
return;
return true;
}
if (EventPluginRegistry.registrationNameModules.hasOwnProperty(name)) {
return;
return true;
}
warnedProperties[name] = true;
var lowerCasedName = name.toLowerCase();
Expand Down Expand Up @@ -75,6 +75,7 @@ if (__DEV__) {
standardName,
ReactComponentTreeDevtool.getStackAddendumByID(debugID)
);
return true;
} else if (registrationName != null) {
warning(
registrationName == null,
Expand All @@ -83,32 +84,59 @@ if (__DEV__) {
registrationName,
ReactComponentTreeDevtool.getStackAddendumByID(debugID)
);
return true;
} else {
// We were unable to guess which prop the user intended.
// It is likely that the user was just blindly spreading/forwarding props
// Components should be careful to only render valid props/attributes.
warning(
false,
'Unknown prop `%s` on <%s> tag. Remove this prop from the element. ' +
'For details, see https://fb.me/react-unknown-prop%s',
name,
tagName,
ReactComponentTreeDevtool.getStackAddendumByID(debugID)
);
// Warning will be invoked in warnUnknownProperties to allow grouping.
return false;
}
};
}

var warnUnknownProperties = function(debugID, element) {
var unknownProps = [];
for (var key in element.props) {
var isValid = validateProperty(element.type, key, debugID);
if (!isValid) {
unknownProps.push(key);
}
}

var unknownPropString = unknownProps
.map(prop => '`' + prop + '`')
.join(', ');

if (unknownProps.length === 1) {
warning(
false,
'Unknown prop %s on <%s> tag. Remove this prop from the element. ' +
'For details, see https://fb.me/react-unknown-prop%s',
unknownPropString,
element.type,
ReactComponentTreeDevtool.getStackAddendumByID(debugID)
);
} else if (unknownProps.length > 1) {
warning(
false,
'Unknown props %s on <%s> tag. Remove these props from the element. ' +
'For details, see https://fb.me/react-unknown-prop%s',
unknownPropString,
element.type,
ReactComponentTreeDevtool.getStackAddendumByID(debugID)
);
}
};

function handleElement(debugID, element) {
if (element == null || typeof element.type !== 'string') {
return;
}
if (element.type.indexOf('-') >= 0 || element.props.is) {
return;
}
for (var key in element.props) {
warnUnknownProperty(element.type, key, debugID);
}
warnUnknownProperties(debugID, element);
}

var ReactDOMUnknownPropertyDevtool = {
Expand Down