Skip to content

Commit 90ceb71

Browse files
authored
fix: Case-insensitive globals matching (#684) (#685)
* Use case-insensitive objectname matching only for matching browserglobals
1 parent 44f6be3 commit 90ceb71

File tree

2 files changed

+175
-4
lines changed

2 files changed

+175
-4
lines changed

src/helpers.ts

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
/* eslint no-nested-ternary: off */
22
import browserslist from "browserslist";
3+
import globals from "globals";
34
import { AstNodeTypes, TargetNameMappings } from "./constants";
45
import {
56
AstMetadataApiWithTargetsResolver,
@@ -165,6 +166,8 @@ function protoChainFromMemberExpression(node: ESLintNode): string[] {
165166
return [...protoChain, node.property!.name];
166167
}
167168

169+
const browserGlobals = new Set(Object.keys(globals.browser));
170+
168171
export function lintMemberExpression(
169172
context: Context,
170173
handleFailingRule: HandleFailingRule,
@@ -200,11 +203,20 @@ export function lintMemberExpression(
200203
} else {
201204
const objectName = node.object.name;
202205
const propertyName = node.property.name;
203-
const failingRule = rules.find(
204-
(rule) =>
205-
rule.object.toLowerCase() === objectName.toLowerCase() &&
206+
const isBrowserGlobal = browserGlobals.has(objectName);
207+
const objectNameLower = objectName.toLowerCase();
208+
209+
const failingRule = rules.find((rule) => {
210+
// Match case-insensitively IF the objectName was case-sentively found in browserGlobals
211+
const objectNameMatches = isBrowserGlobal
212+
? rule.object.toLowerCase() === objectNameLower
213+
: rule.object === objectName;
214+
return (
215+
objectNameMatches &&
206216
(rule.property == null || rule.property === propertyName)
207-
);
217+
);
218+
});
219+
208220
if (failingRule)
209221
checkNotInsideIfStatementAndReport(
210222
context,

test/e2e.spec.ts

Lines changed: 159 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,51 @@ ruleTester.run("compat", rule, {
9393
code: "document.fonts()",
9494
settings: { browsers: ["edge 79"] },
9595
},
96+
{
97+
code: `
98+
import * as serviceWorker from './serviceWorker';
99+
serviceWorker.register(false);
100+
`,
101+
settings: { browsers: ["chrome 52", "android 145"] },
102+
},
103+
{
104+
code: `
105+
navigator.permissions
106+
.query({ name: 'local-network-access' })
107+
.then((permissionStatus) => {
108+
permissionStatus.addEventListener('change', () => {});
109+
});
110+
`,
111+
settings: { browsers: ["chrome 52", "android 145"] },
112+
},
113+
{
114+
code: `
115+
const abortController = new AbortController();
116+
abortController.abort();
117+
`,
118+
settings: { browsers: ["chrome 70"] },
119+
},
120+
{
121+
code: `
122+
const mutationObserver = new MutationObserver(() => {});
123+
mutationObserver.observe(document.body, { childList: true });
124+
`,
125+
settings: { browsers: ["chrome 52"] },
126+
},
127+
{
128+
code: `
129+
const intersectionObserver = new IntersectionObserver(() => {});
130+
intersectionObserver.observe(document.body);
131+
`,
132+
settings: { browsers: ["chrome 70"] },
133+
},
134+
{
135+
code: `
136+
const IntersectionObserver = "test";
137+
IntersectionObserver.trim();
138+
`,
139+
settings: { browsers: ["chrome 30"] },
140+
},
96141
// Import cases
97142
{
98143
code: `
@@ -730,5 +775,119 @@ ruleTester.run("compat", rule, {
730775
},
731776
],
732777
},
778+
{
779+
code: "[].includes()",
780+
settings: { browsers: ["ie 11"] },
781+
errors: [
782+
{
783+
message: "Array.includes() is not supported in IE 11",
784+
},
785+
],
786+
},
787+
{
788+
code: "'strsd'.includes()",
789+
settings: { browsers: ["ie 11"] },
790+
errors: [
791+
{
792+
message: "String.includes() is not supported in IE 11",
793+
},
794+
],
795+
},
796+
{
797+
code: "[1, 2, [3, 4]].flat()",
798+
settings: { browsers: ["ie 11"] },
799+
errors: [
800+
{
801+
message: "Array.flat() is not supported in IE 11",
802+
},
803+
],
804+
},
805+
{
806+
code: "[1,2,3].flatMap(x => [x, x])",
807+
settings: { browsers: ["chrome 68"] },
808+
errors: [
809+
{
810+
message: "Array.flatMap() is not supported in Chrome 68",
811+
},
812+
],
813+
},
814+
{
815+
code: "Object.fromEntries([])",
816+
settings: { browsers: ["chrome 72"] },
817+
errors: [
818+
{
819+
message: "Object.fromEntries() is not supported in Chrome 72",
820+
},
821+
],
822+
},
823+
{
824+
code: "'text'.replaceAll('x', 's')",
825+
settings: { browsers: ["chrome 84"] },
826+
errors: [
827+
{
828+
message: "String.replaceAll() is not supported in Chrome 84",
829+
},
830+
],
831+
},
832+
{
833+
code: `navigator.serviceWorker.register("/service_worker.js");`,
834+
settings: { browsers: ["chrome 39"] },
835+
errors: [
836+
{
837+
message: "navigator.serviceWorker() is not supported in Chrome 39",
838+
},
839+
],
840+
},
841+
{
842+
code: `
843+
const abortController = new AbortController();
844+
abortController.abort();
845+
`,
846+
settings: { browsers: ["chrome 65"] },
847+
errors: [
848+
{
849+
message: "AbortController is not supported in Chrome 65",
850+
},
851+
],
852+
},
853+
{
854+
code: `
855+
const mutationObserver = new MutationObserver(() => {});
856+
mutationObserver.observe(document.body, { childList: true });
857+
`,
858+
settings: { browsers: ["chrome 25"] },
859+
errors: [
860+
{
861+
message: "MutationObserver is not supported in Chrome 25",
862+
},
863+
],
864+
},
865+
{
866+
code: `
867+
const intersectionObserver = new IntersectionObserver(() => {});
868+
intersectionObserver.observe(document.body);
869+
`,
870+
settings: { browsers: ["chrome 50"] },
871+
errors: [
872+
{
873+
message: "IntersectionObserver is not supported in Chrome 50",
874+
},
875+
],
876+
},
877+
{
878+
code: `
879+
navigator.permissions
880+
.query({ name: 'local-network-access' })
881+
.then((permissionStatus) => {
882+
permissionStatus.addEventListener('change', () => {});
883+
});
884+
`,
885+
settings: { browsers: ["chrome 41"] },
886+
errors: [
887+
{
888+
message: "navigator.permissions() is not supported in Chrome 41",
889+
},
890+
],
891+
},
733892
],
734893
});

0 commit comments

Comments
 (0)