Skip to content

Commit d2c9bef

Browse files
committed
[Fix] no-unknown-property: avoid warning on fbt nodes entirely
Fixes #3391
1 parent 18b2b59 commit d2c9bef

File tree

3 files changed

+49
-38
lines changed

3 files changed

+49
-38
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,11 @@ This change log adheres to standards from [Keep a CHANGELOG](https://keepachange
55

66
## Unreleased
77

8+
### Fixed
9+
* [`no-unknown-property`]: avoid warning on `fbt` nodes entirely ([#3391][] @ljharb)
10+
11+
[#3391]: https://github.com/jsx-eslint/eslint-plugin-react/issues/3391
12+
813
## [7.31.6] - 2022.09.04
914

1015
### Fixed

lib/rules/no-unknown-property.js

Lines changed: 41 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -502,60 +502,63 @@ module.exports = {
502502

503503
const tagName = getTagName(node);
504504

505-
// Let's dive deeper into tags that are HTML/DOM elements (`<button>`), and not React components (`<Button />`)
506-
if (isValidHTMLTagInJSX(node)) {
507-
// Some attributes are allowed on some tags only
508-
const allowedTags = has(ATTRIBUTE_TAGS_MAP, name) ? ATTRIBUTE_TAGS_MAP[name] : null;
509-
if (tagName && allowedTags) {
510-
// Scenario 1A: Allowed attribute found where not supposed to, report it
511-
if (allowedTags.indexOf(tagName) === -1) {
512-
report(context, messages.invalidPropOnTag, 'invalidPropOnTag', {
513-
node,
514-
data: {
515-
name,
516-
tagName,
517-
allowedTags: allowedTags.join(', '),
518-
},
519-
});
520-
}
521-
// Scenario 1B: There are allowed attributes on allowed tags, no need to report it
522-
return;
523-
}
524-
525-
// Let's see if the attribute is a close version to some standard property name
526-
const standardName = getStandardName(name, context);
505+
if (tagName === 'fbt') { return; } // fbt nodes are bonkers, let's not go there
527506

528-
const hasStandardNameButIsNotUsed = standardName && standardName !== name;
529-
const usesStandardName = standardName && standardName === name;
507+
if (!isValidHTMLTagInJSX(node)) { return; }
530508

531-
if (usesStandardName) {
532-
// Scenario 2A: The attribute name is the standard name, no need to report it
533-
return;
534-
}
509+
// Let's dive deeper into tags that are HTML/DOM elements (`<button>`), and not React components (`<Button />`)
535510

536-
if (hasStandardNameButIsNotUsed) {
537-
// Scenario 2B: The name of the attribute is close to a standard one, report it with the standard name
538-
report(context, messages.unknownPropWithStandardName, 'unknownPropWithStandardName', {
511+
// Some attributes are allowed on some tags only
512+
const allowedTags = has(ATTRIBUTE_TAGS_MAP, name) ? ATTRIBUTE_TAGS_MAP[name] : null;
513+
if (tagName && allowedTags) {
514+
// Scenario 1A: Allowed attribute found where not supposed to, report it
515+
if (allowedTags.indexOf(tagName) === -1) {
516+
report(context, messages.invalidPropOnTag, 'invalidPropOnTag', {
539517
node,
540518
data: {
541519
name,
542-
standardName,
543-
},
544-
fix(fixer) {
545-
return fixer.replaceText(node.name, standardName);
520+
tagName,
521+
allowedTags: allowedTags.join(', '),
546522
},
547523
});
548-
return;
549524
}
525+
// Scenario 1B: There are allowed attributes on allowed tags, no need to report it
526+
return;
527+
}
528+
529+
// Let's see if the attribute is a close version to some standard property name
530+
const standardName = getStandardName(name, context);
550531

551-
// Scenario 3: We have an attribute that is unknown, report it
552-
report(context, messages.unknownProp, 'unknownProp', {
532+
const hasStandardNameButIsNotUsed = standardName && standardName !== name;
533+
const usesStandardName = standardName && standardName === name;
534+
535+
if (usesStandardName) {
536+
// Scenario 2A: The attribute name is the standard name, no need to report it
537+
return;
538+
}
539+
540+
if (hasStandardNameButIsNotUsed) {
541+
// Scenario 2B: The name of the attribute is close to a standard one, report it with the standard name
542+
report(context, messages.unknownPropWithStandardName, 'unknownPropWithStandardName', {
553543
node,
554544
data: {
555545
name,
546+
standardName,
547+
},
548+
fix(fixer) {
549+
return fixer.replaceText(node.name, standardName);
556550
},
557551
});
552+
return;
558553
}
554+
555+
// Scenario 3: We have an attribute that is unknown, report it
556+
report(context, messages.unknownProp, 'unknownProp', {
557+
node,
558+
data: {
559+
name,
560+
},
561+
});
559562
},
560563
};
561564
},

tests/lib/rules/no-unknown-property.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,9 @@ ruleTester.run('no-unknown-property', rule, {
112112
{ code: '<link as="audio">Audio content</link>' },
113113
{ code: '<video controls={this.controls} loop={true} muted={false} src={this.videoSrc} playsInline={true}></video>' },
114114
{ code: '<audio controls={this.controls} crossOrigin="anonymous" disableRemotePlayback loop muted preload="none" src="something" onAbort={this.abort} onDurationChange={this.durationChange} onEmptied={this.emptied} onEnded={this.end} onError={this.error}></audio>' },
115+
116+
// fbt
117+
{ code: '<fbt desc="foo" doNotExtract />;' },
115118
]),
116119
invalid: parsers.all([
117120
{

0 commit comments

Comments
 (0)