Skip to content
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
3 changes: 2 additions & 1 deletion apps/perf-test/.eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
"extends": ["plugin:@fluentui/eslint-plugin/react"],
"root": true,
"rules": {
"no-console": "off"
"no-console": "off",
"no-restricted-globals": "off"
}
}
3 changes: 2 additions & 1 deletion apps/pr-deploy-site/.eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@
"curly": "off",
"no-var": "off",
"vars-on-top": "off",
"prefer-arrow-callback": "off"
"prefer-arrow-callback": "off",
"no-restricted-globals": "off"
}
}
]
Expand Down
3 changes: 2 additions & 1 deletion apps/public-docsite-resources/.eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
"@typescript-eslint/no-explicit-any": "off",
"@typescript-eslint/explicit-member-accessibility": "off",
"@typescript-eslint/member-ordering": "off",
"deprecation/deprecation": "off"
"deprecation/deprecation": "off",
"no-restricted-globals": "off"
}
}
3 changes: 2 additions & 1 deletion apps/public-docsite/.eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
"deprecation/deprecation": "off",
"import/no-webpack-loader-syntax": "off", // ok in this project
"prefer-const": "off",
"react/jsx-no-bind": "off"
"react/jsx-no-bind": "off",
"no-restricted-globals": "off"
}
}
3 changes: 2 additions & 1 deletion apps/theming-designer/.eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
"rules": {
"@typescript-eslint/no-explicit-any": "off",
"deprecation/deprecation": "off",
"prefer-const": "off"
"prefer-const": "off",
"no-restricted-globals": "off"
}
}
3 changes: 2 additions & 1 deletion apps/vr-tests/.eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
"@typescript-eslint/no-explicit-any": "off",
"@typescript-eslint/jsx-no-bind": "off",
"deprecation/deprecation": "off",
"import/no-extraneous-dependencies": ["error", { "packageDir": [".", "../.."] }]
"import/no-extraneous-dependencies": ["error", { "packageDir": [".", "../.."] }],
"no-restricted-globals": "off"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"type": "patch",
"comment": "chore: disallow document and window access",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

tip for future: chore != patch, this added new api on public surface while refactoring under the hood. it should have been type:minor and feat

"packageName": "@fluentui/dom-utilities",
"email": "[email protected]",
"dependentChangeType": "patch"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"type": "patch",
"comment": "chore: add restricted globals for @fluentui/react and related packages",
"packageName": "@fluentui/eslint-plugin",
"email": "[email protected]",
"dependentChangeType": "patch"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"type": "patch",
"comment": "chore: disallow document and window access",
"packageName": "@fluentui/merge-styles",
"email": "[email protected]",
"dependentChangeType": "patch"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"type": "patch",
"comment": "chore: disallow document and window access",
"packageName": "@fluentui/react",
"email": "[email protected]",
"dependentChangeType": "patch"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"type": "patch",
"comment": "chore: disallow document and window access",
"packageName": "@fluentui/react-file-type-icons",
"email": "[email protected]",
"dependentChangeType": "patch"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"type": "patch",
"comment": "chore: disallow document and window access",
"packageName": "@fluentui/react-focus",
"email": "[email protected]",
"dependentChangeType": "patch"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"type": "patch",
"comment": "chore: clean up exports",
"packageName": "@fluentui/react-window-provider",
"email": "[email protected]",
"dependentChangeType": "patch"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"type": "patch",
"comment": "chore: disallow document and window access",
"packageName": "@fluentui/utilities",
"email": "[email protected]",
"dependentChangeType": "patch"
}
7 changes: 3 additions & 4 deletions packages/dom-utilities/etc/dom-utilities.api.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@ export const DATA_PORTAL_ATTRIBUTE = "data-portal-element";
export function elementContains(parent: HTMLElement | null, child: HTMLElement | null, allowVirtualParents?: boolean): boolean;

// @public
export function elementContainsAttribute(element: HTMLElement, attribute: string): string | null;
export function elementContainsAttribute(element: HTMLElement, attribute: string, doc?: Document): string | null;

// @public
export function findElementRecursive(element: HTMLElement | null, matchFunction: (element: HTMLElement) => boolean): HTMLElement | null;
export function findElementRecursive(element: HTMLElement | null, matchFunction: (element: HTMLElement) => boolean, doc?: Document): HTMLElement | null;

// @public
export function getChildren(parent: HTMLElement, allowVirtualChildren?: boolean): HTMLElement[];
Expand All @@ -38,15 +38,14 @@ export interface IVirtualElement extends HTMLElement {
}

// @public
export function portalContainsElement(target: HTMLElement, parent?: HTMLElement): boolean;
export function portalContainsElement(target: HTMLElement, parent?: HTMLElement, doc?: Document): boolean;

// @public
export function setPortalAttribute(element: HTMLElement): void;

// @public
export function setVirtualParent(child: HTMLElement, parent: HTMLElement | null): void;


// (No @packageDocumentation comment for this package)

```
8 changes: 6 additions & 2 deletions packages/dom-utilities/src/elementContainsAttribute.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,12 @@ import { findElementRecursive } from './findElementRecursive';
* @param attribute - the attribute to search for
* @returns the value of the first instance found
*/
export function elementContainsAttribute(element: HTMLElement, attribute: string): string | null {
const elementMatch = findElementRecursive(element, (testElement: HTMLElement) => testElement.hasAttribute(attribute));
export function elementContainsAttribute(element: HTMLElement, attribute: string, doc?: Document): string | null {
const elementMatch = findElementRecursive(
element,
(testElement: HTMLElement) => testElement.hasAttribute(attribute),
doc,
);

return elementMatch && elementMatch.getAttribute(attribute);
}
5 changes: 4 additions & 1 deletion packages/dom-utilities/src/findElementRecursive.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,11 @@ import { getParent } from './getParent';
export function findElementRecursive(
element: HTMLElement | null,
matchFunction: (element: HTMLElement) => boolean,
doc?: Document,
): HTMLElement | null {
if (!element || element === document.body) {
// eslint-disable-next-line no-restricted-globals
doc ??= document;
if (!element || element === doc.body) {
return null;
}
return matchFunction(element) ? element : findElementRecursive(getParent(element), matchFunction);
Expand Down
3 changes: 2 additions & 1 deletion packages/dom-utilities/src/portalContainsElement.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,11 @@ import { DATA_PORTAL_ATTRIBUTE } from './setPortalAttribute';
* @param parent - Optional parent perspective. Search for containing portal stops at parent
* (or root if parent is undefined or invalid.)
*/
export function portalContainsElement(target: HTMLElement, parent?: HTMLElement): boolean {
export function portalContainsElement(target: HTMLElement, parent?: HTMLElement, doc?: Document): boolean {
const elementMatch = findElementRecursive(
target,
(testElement: HTMLElement) => parent === testElement || testElement.hasAttribute(DATA_PORTAL_ATTRIBUTE),
doc,
);
return elementMatch !== null && elementMatch.hasAttribute(DATA_PORTAL_ATTRIBUTE);
}
16 changes: 13 additions & 3 deletions packages/eslint-plugin/src/configs/react-legacy.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// @ts-check

const configHelpers = require('../utils/configHelpers');
const path = require('path');
const { reactLegacy: restrictedGlobals } = require('./restricted-globals');

/** @type {import("eslint").Linter.Config} */
module.exports = {
Expand All @@ -9,7 +10,16 @@ module.exports = {
rules: {
'jsdoc/check-tag-names': 'off',
'@griffel/no-shorthands': 'off',
'no-restricted-globals': 'off',
'no-restricted-globals': restrictedGlobals,
},
overrides: [],
overrides: [
{
// Test overrides
files: [...configHelpers.testFiles, '**/*.stories.tsx'],
rules: {
'no-restricted-globals': 'off',
'react/jsx-no-bind': 'off',
},
},
],
};
13 changes: 13 additions & 0 deletions packages/eslint-plugin/src/configs/restricted-globals.js
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,18 @@
// 'onwheel',
// ];

const reactLegacy = [
'error',
{
name: 'window',
message: 'Get a reference to `window` via context.',
},
{
name: 'document',
message: 'Get a reference to `document` via context.',
},
];

const react = [
'error',
{
Expand All @@ -258,5 +270,6 @@ const react = [
];

module.exports = {
reactLegacy,
react,
};
3 changes: 3 additions & 0 deletions packages/merge-styles/src/StyleOptionsState.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,11 @@ export function setRTL(isRTL: boolean): void {
export function getRTL(): boolean {
if (_rtl === undefined) {
_rtl =
// eslint-disable-next-line no-restricted-globals
typeof document !== 'undefined' &&
// eslint-disable-next-line no-restricted-globals
!!document.documentElement &&
// eslint-disable-next-line no-restricted-globals
document.documentElement.getAttribute('dir') === 'rtl';
}
return _rtl;
Expand Down
3 changes: 3 additions & 0 deletions packages/merge-styles/src/Stylesheet.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
/* eslint no-restricted-globals: 0 */
// globals in stylesheets will be addressed as part of shadow DOM work.
// See: https://github.com/microsoft/fluentui/issues/28058
import { IStyle } from './IStyle';

export const InjectionMode = {
Expand Down
1 change: 1 addition & 0 deletions packages/merge-styles/src/getVendorSettings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ let _vendorSettings: IVendorSettings | undefined;

export function getVendorSettings(): IVendorSettings {
if (!_vendorSettings) {
// eslint-disable-next-line no-restricted-globals
const doc = typeof document !== 'undefined' ? document : undefined;
const nav = typeof navigator !== 'undefined' ? navigator : undefined;
const userAgent = nav?.userAgent?.toLowerCase();
Expand Down
5 changes: 4 additions & 1 deletion packages/react-charting/.eslintrc.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
{
"extends": ["plugin:@fluentui/eslint-plugin/react--legacy"],
"root": true
"root": true,
"rules": {
"no-restricted-globals": "off"
}
}
3 changes: 2 additions & 1 deletion packages/react-docsite-components/.eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
"extends": ["plugin:@fluentui/eslint-plugin/react--legacy"],
"root": true,
"rules": {
"deprecation/deprecation": "off"
"deprecation/deprecation": "off",
"no-restricted-globals": "off"
}
}
3 changes: 2 additions & 1 deletion packages/react-examples/.eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
"rules": {
"import/no-webpack-loader-syntax": "off",
"@typescript-eslint/no-explicit-any": "off",
"no-alert": "off"
"no-alert": "off",
"no-restricted-globals": "off"
}
}
3 changes: 2 additions & 1 deletion packages/react-experiments/.eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
"extends": ["plugin:@fluentui/eslint-plugin/react--legacy"],
"root": true,
"rules": {
"@typescript-eslint/no-explicit-any": "off"
"@typescript-eslint/no-explicit-any": "off",
"no-restricted-globals": "off"
}
}
10 changes: 8 additions & 2 deletions packages/react-file-type-icons/src/getFileTypeIconProps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -160,8 +160,14 @@ export function getFileTypeIconNameFromExtensionOrType(
return iconBaseName || GENERIC_FILE;
}

export function getFileTypeIconSuffix(size: FileTypeIconSize, imageFileType: ImageFileType = 'svg'): string {
let devicePixelRatio: number = window.devicePixelRatio;
export function getFileTypeIconSuffix(
size: FileTypeIconSize,
imageFileType: ImageFileType = 'svg',
win?: Window,
): string {
// eslint-disable-next-line no-restricted-globals
win ??= window;
let devicePixelRatio: number = win.devicePixelRatio;
let devicePixelRatioSuffix = ''; // Default is 1x

// SVGs scale well, so you can generally use the default image.
Expand Down
2 changes: 2 additions & 0 deletions packages/react-focus/src/components/FocusZone/FocusZone.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -59,12 +59,14 @@ function raiseClickFromKeyboardEvent(target: Element, ev?: React.KeyboardEvent<H
cancelable: ev?.cancelable,
});
} else {
// eslint-disable-next-line no-restricted-globals
event = document.createEvent('MouseEvents');
// eslint-disable-next-line deprecation/deprecation
event.initMouseEvent(
'click',
ev ? ev.bubbles : false,
ev ? ev.cancelable : false,
// eslint-disable-next-line no-restricted-globals
window, // not using getWindow() since this can only be run client side
0, // detail
0, // screen x
Expand Down
3 changes: 2 additions & 1 deletion packages/react-monaco-editor/.eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
"extends": ["plugin:@fluentui/eslint-plugin/react--legacy"],
"root": true,
"rules": {
"import/no-webpack-loader-syntax": "off" // ok in this project
"import/no-webpack-loader-syntax": "off", // ok in this project
"no-restricted-globals": "off"
}
}
9 changes: 8 additions & 1 deletion packages/react-window-provider/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
export * from './WindowProvider';
export {
// eslint-disable-next-line @fluentui/ban-context-export
WindowContext,
useWindow,
useDocument,
WindowProvider,
} from './WindowProvider';
export type { WindowProviderProps } from './WindowProvider';

import './version';
Loading