Skip to content

Commit

Permalink
fix(plugins/dom): add popover api props to 'no-unknown-property', clo… (
Browse files Browse the repository at this point in the history
  • Loading branch information
Rel1cx authored Nov 22, 2024
1 parent eaa5b04 commit 87fcdc4
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 14 deletions.
1 change: 1 addition & 0 deletions packages/plugins/eslint-plugin-react-dom/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@
"@typescript-eslint/scope-manager": "^8.15.0",
"@typescript-eslint/types": "^8.15.0",
"@typescript-eslint/utils": "^8.15.0",
"compare-versions": "^6.1.1",
"ts-pattern": "^5.5.0"
},
"devDependencies": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -655,6 +655,25 @@ ruleTester.run(RULE_NAME, rule, {
<div popover id="my-popover">Greetings, one and all!</div>
</div>
`,
settings: {
"react-x": {
version: "18.3.1",
},
},
},
{
code: `
<div>
<button popoverTarget="my-popover" popoverTargetAction="toggle">Open Popover</button>
<div popover id="my-popover">Greetings, one and all!</div>
</div>
`,
settings: {
"react-x": {
version: "19.0.0-rc.0",
},
},
},
],
});
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@
// Ported from https://github.com/jsx-eslint/eslint-plugin-react/blob/master/lib/rules/no-unknown-property.js
// TODO: Port to TypeScript

import { decodeSettings, normalizeSettings } from "@eslint-react/shared";
import { createRule } from "../utils";
import { compare, compareVersions } from "compare-versions";

// ------------------------------------------------------------------------------
// Constants
Expand Down Expand Up @@ -419,10 +421,6 @@ const DOM_PROPERTY_NAMES_ONE_WORD = [
"security",
// Video specific
"controls",
// popovers
"popover",
"popovertarget",
"popovertargetaction",
];

const DOM_PROPERTY_NAMES_TWO_WORDS = [
Expand Down Expand Up @@ -890,16 +888,29 @@ const REACT_ON_PROPS = [
"onPointerUpCapture",
];

const POPOVER_API_PROPS = [
"popover",
"popoverTarget",
"popoverTargetAction",
"onToggle",
"onBeforeToggle",
];

function getDOMPropertyNames(context) {
const ALL_DOM_PROPERTY_NAMES = DOM_PROPERTY_NAMES_TWO_WORDS.concat(DOM_PROPERTY_NAMES_ONE_WORD);
// this was removed in React v16.1+, see https://github.com/facebook/react/pull/10823
if (!testReactVersion(context, ">= 16.1.0")) {
return ALL_DOM_PROPERTY_NAMES.concat("allowTransparency");
if (testReactVersion(context, "<=", "16.1.0")) {
ALL_DOM_PROPERTY_NAMES.push("allowTransparency");
return ALL_DOM_PROPERTY_NAMES;
}
// these were added in React v16.4.0, see https://reactjs.org/blog/2018/05/23/react-v-16-4.html and https://github.com/facebook/react/pull/12507
if (testReactVersion(context, ">= 16.4.0")) {
return ALL_DOM_PROPERTY_NAMES.concat(REACT_ON_PROPS);
if (testReactVersion(context, ">=", "16.4.0")) {
ALL_DOM_PROPERTY_NAMES.push(...REACT_ON_PROPS);
}
// these were added in React v19.0.0-rc.0, see https://github.com/facebook/react/pull/27981
testReactVersion(context, ">=", "19.0.0-rc.0")
? ALL_DOM_PROPERTY_NAMES.push(...POPOVER_API_PROPS)
: ALL_DOM_PROPERTY_NAMES.push(...POPOVER_API_PROPS.map((prop) => prop.toLowerCase()));
return ALL_DOM_PROPERTY_NAMES;
}

Expand Down Expand Up @@ -1185,6 +1196,8 @@ function report(context, message, messageId, data) {
});
}

function testReactVersion(context, version) {
return true;
function testReactVersion(context, comparator, version) {
const { version: localVersion } = normalizeSettings(decodeSettings(context.settings));
console.log(localVersion, version, comparator);
return compare(localVersion, version, comparator);
}
2 changes: 1 addition & 1 deletion packages/plugins/eslint-plugin-react-x/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@
"@typescript-eslint/types": "^8.15.0",
"@typescript-eslint/utils": "^8.15.0",
"compare-versions": "^6.1.1",
"is-immutable-type": "5.0.0",
"is-immutable-type": "^5.0.0",
"ts-pattern": "^5.5.0"
},
"devDependencies": {
Expand Down
9 changes: 6 additions & 3 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 87fcdc4

Please sign in to comment.