Skip to content

Commit

Permalink
[no-unknown-property] Support fixing attributes with any input case
Browse files Browse the repository at this point in the history
  • Loading branch information
julienw committed Sep 9, 2020
1 parent 9a569f7 commit c70c6b0
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 8 deletions.
23 changes: 15 additions & 8 deletions lib/rules/no-unknown-property.js
Expand Up @@ -147,7 +147,8 @@ function getDOMPropertyNames(context) {
// ------------------------------------------------------------------------------

/**
* Checks if a node matches the JSX tag convention.
* Checks if a node matches the JSX tag convention. This also checks if a node
* is extended as a webcomponent using the attribute "is".
* @param {Object} node - JSX element being tested.
* @returns {boolean} Whether or not the node name match the JSX tag convention.
*/
Expand Down Expand Up @@ -194,7 +195,7 @@ function tagNameHasDot(node) {
* Get the standard name of the attribute.
* @param {String} name - Name of the attribute.
* @param {String} context - eslint context
* @returns {String} The standard name of the attribute.
* @returns {String | null} The standard name of the attribute, or null if no standard name was found.
*/
function getStandardName(name, context) {
if (DOM_ATTRIBUTE_NAMES[name]) {
Expand All @@ -205,11 +206,11 @@ function getStandardName(name, context) {
}
let i = -1;
const names = getDOMPropertyNames(context);
const found = names.some((element, index) => {
i = index;
return element.toLowerCase() === name;
});
return found ? names[i] : null;
// Let's find a possible attribute match with a case-insensitive search.
const foundName = names.find(
(element, index) => element.toLowerCase() === name.toLowerCase()
);
return foundName === undefined ? null : foundName;
}

// ------------------------------------------------------------------------------
Expand Down Expand Up @@ -259,6 +260,8 @@ module.exports = {
}

const tagName = getTagName(node);

// 1. Some attributes are allowed on some tags only.
const allowedTags = ATTRIBUTE_TAGS_MAP[name];
if (tagName && allowedTags && /[^A-Z]/.test(tagName.charAt(0)) && allowedTags.indexOf(tagName) === -1) {
context.report({
Expand All @@ -272,8 +275,12 @@ module.exports = {
});
}

// 2. Otherwise, we'll try to find if the attribute is a close version
// of what we should normally have with React. If yes, we'll report an
// error. We don't want to report if the input attribute name is the
// standard name though!
const standardName = getStandardName(name, context);
if (!isTagName(node) || !standardName) {
if (!isTagName(node) || !standardName || standardName === name) {
return;
}
context.report({
Expand Down
4 changes: 4 additions & 0 deletions tests/lib/rules/no-unknown-property.js
Expand Up @@ -75,6 +75,10 @@ ruleTester.run('no-unknown-property', rule, {
code: '<div onmousedown="bar"></div>;',
output: '<div onMouseDown="bar"></div>;',
errors: [{message: 'Unknown property \'onmousedown\' found, use \'onMouseDown\' instead'}]
}, {
code: '<div onMousedown="bar"></div>;',
output: '<div onMouseDown="bar"></div>;',
errors: [{message: 'Unknown property \'onMousedown\' found, use \'onMouseDown\' instead'}]
}, {
code: '<use xlink:href="bar" />;',
output: '<use xlinkHref="bar" />;',
Expand Down

0 comments on commit c70c6b0

Please sign in to comment.