Skip to content

Commit

Permalink
chore: replace no-restricted-globals with `@nx/workspace-no-restric…
Browse files Browse the repository at this point in the history
…ted-globals` in whole monorepo (#32960)
  • Loading branch information
Hotell authored Oct 9, 2024
1 parent b474188 commit 511f956
Show file tree
Hide file tree
Showing 36 changed files with 112 additions and 53 deletions.
2 changes: 1 addition & 1 deletion apps/perf-test-react-components/.eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@
"rules": {
"@griffel/styles-file": "off",
"no-console": "off",
"no-restricted-globals": "off"
"@nx/workspace-no-restricted-globals": "off"
}
}
3 changes: 1 addition & 2 deletions apps/public-docsite-v9/.eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
"@typescript-eslint/no-explicit-any": "off",
"@typescript-eslint/jsx-no-bind": "off",
"deprecation/deprecation": "off",
"import/no-extraneous-dependencies": ["error", { "packageDir": [".", "../.."] }],
"no-restricted-globals": "off"
"import/no-extraneous-dependencies": ["error", { "packageDir": [".", "../.."] }]
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,6 @@ const tabs: SettingsTab[] = [

type OverflowMenuItemProps = {
tab: SettingsTab;
// FIXME: This should be a consistent callback type
// eslint-disable-next-line @nx/workspace-consistent-callback-type
onClick: React.MouseEventHandler<HTMLDivElement>;
};

Expand All @@ -91,8 +89,6 @@ const OverflowMenuItem = (props: OverflowMenuItemProps) => {
};

type OverflowMenuProps = {
// FIXME: This should be a consistent callback type
// eslint-disable-next-line @nx/workspace-consistent-callback-type
onTabSelect?: (tabId: string) => void;
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ export const BackLink = () => <ScenariosListLink>Go back to main menu</Scenarios

export const Scenario: React.FunctionComponent<{ pageTitle: string }> = ({ pageTitle, children }) => {
React.useEffect(() => {
// eslint-disable-next-line no-restricted-globals
// eslint-disable-next-line @nx/workspace-no-restricted-globals
document.title = pageTitle + APP_TITLE_SEPARATOR + APP_TITLE;
}, [pageTitle]);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,8 @@ const useStyles = makeStyles({

const regexes = {
onlyNameChars: /^[A-Za-zÀ-ÖØ-öø-ÿěščřžďťňůĚŠČŘŽĎŤŇŮ -]*$/,
// eslint-disable-next-line @fluentui/max-len
startsAndEndsWithLetter:
/^(([A-Za-zÀ-ÖØ-öø-ÿěščřžďťňůĚŠČŘŽĎŤŇŮ][A-Za-zÀ-ÖØ-öø-ÿěščřžďťňůĚŠČŘŽĎŤŇŮ -]*[A-Za-zÀ-ÖØ-öø-ÿěščřžďťňůĚŠČŘŽĎŤŇŮ])|[A-Za-zÀ-ÖØ-öø-ÿěščřžďťňůĚŠČŘŽĎŤŇŮ])?$/,
// eslint-disable-next-line @fluentui/max-len
validEmail:
/^(([^<>()[\]\.,;:\s@\"]+(\.[^<>()[\]\.,;:\s@\"]+)*)|(\".+\"))@(([^<>()[\]\.,;:\s@\"]+\.)+[^<>()[\]\.,;:\s@\"]{2,})$/i,
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ export const useDebounce = (fn: (...args: unknown[]) => void, duration: number)

return React.useCallback(
(...args: unknown[]) => {
clearTimeout(timeoutRef.current);
// eslint-disable-next-line @nx/workspace-no-restricted-globals
window.clearTimeout(timeoutRef.current);
// eslint-disable-next-line @nx/workspace-no-restricted-globals
timeoutRef.current = window.setTimeout(() => {
fn(...args);
}, duration);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* eslint-disable no-restricted-globals, no-restricted-properties */
/* eslint-disable no-restricted-properties */
import * as React from 'react';
import { makeStyles, tokens, mergeClasses, createCSSRuleFromTheme, webLightTheme } from '@fluentui/react-components';

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* eslint-disable no-restricted-globals, no-restricted-properties */
/* eslint-disable no-restricted-properties */
import * as React from 'react';
import {
makeStyles,
Expand Down
2 changes: 1 addition & 1 deletion apps/react-18-tests-v8/.eslintrc.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"extends": ["plugin:@fluentui/eslint-plugin/react"],
"extends": ["plugin:@fluentui/eslint-plugin/react--legacy"],
"root": true,
"rules": {
"no-restricted-globals": "off"
Expand Down
2 changes: 1 addition & 1 deletion apps/react-18-tests-v9/.eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@
"extends": ["plugin:@fluentui/eslint-plugin/react"],
"root": true,
"rules": {
"no-restricted-globals": "off"
"@nx/workspace-no-restricted-globals": "off"
}
}
2 changes: 1 addition & 1 deletion apps/vr-tests-react-components/.eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,6 @@
"@typescript-eslint/jsx-no-bind": "off",
"deprecation/deprecation": "off",
"import/no-extraneous-dependencies": ["error", { "packageDir": [".", "../.."] }],
"no-restricted-globals": "off"
"@nx/workspace-no-restricted-globals": "off"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"type": "minor",
"comment": "feat: replace no-restricted-globals with type aware @nx/workspace-no-restricted-globals in v9",
"packageName": "@fluentui/eslint-plugin",
"email": "martinhochel@microsoft.com",
"dependentChangeType": "patch"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"type": "none",
"comment": "style: replace no-restricted-globals with type aware @nx/workspace-no-restricted-globals in v9",
"packageName": "@fluentui/global-context",
"email": "martinhochel@microsoft.com",
"dependentChangeType": "none"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"type": "none",
"comment": "chore: use react--legacy config in v8 projects",
"packageName": "@fluentui/public-docsite-setup",
"email": "martinhochel@microsoft.com",
"dependentChangeType": "none"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"type": "none",
"comment": "style: replace no-restricted-globals with type aware @nx/workspace-no-restricted-globals in v9",
"packageName": "@fluentui/react-icons-compat",
"email": "martinhochel@microsoft.com",
"dependentChangeType": "none"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"type": "none",
"comment": "style: replace no-restricted-globals with type aware @nx/workspace-no-restricted-globals in v9",
"packageName": "@fluentui/react-shared-contexts",
"email": "martinhochel@microsoft.com",
"dependentChangeType": "none"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"type": "none",
"comment": "style: replace no-restricted-globals with type aware @nx/workspace-no-restricted-globals in v9",
"packageName": "@fluentui/react-table",
"email": "martinhochel@microsoft.com",
"dependentChangeType": "none"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"type": "none",
"comment": "style: replace no-restricted-globals with type aware @nx/workspace-no-restricted-globals in v9",
"packageName": "@fluentui/react-utilities",
"email": "martinhochel@microsoft.com",
"dependentChangeType": "none"
}
2 changes: 1 addition & 1 deletion packages/cra-template/.eslintrc.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"extends": ["plugin:@fluentui/eslint-plugin/react"],
"extends": ["plugin:@fluentui/eslint-plugin/react--legacy"],
"root": true,
"overrides": [
{
Expand Down
4 changes: 0 additions & 4 deletions packages/eslint-plugin/src/configs/react-config.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
const configHelpers = require('../utils/configHelpers');
const { react: restrictedGlobals } = require('./restricted-globals');

/** @type {import("eslint").Linter.Config} */
module.exports = {
Expand Down Expand Up @@ -30,7 +29,6 @@ module.exports = {
'@griffel/no-shorthands': 'error',
'@griffel/pseudo-element-naming': 'error',
'@griffel/styles-file': 'error',
'no-restricted-globals': restrictedGlobals,
/**
* react eslint rules
* @see https://github.com/yannickcr/eslint-plugin-react
Expand Down Expand Up @@ -125,15 +123,13 @@ module.exports = {
files: [...configHelpers.testFiles],
rules: {
'react/jsx-no-bind': 'off',
'no-restricted-globals': 'off',
},
},
{
files: '**/*.stories.tsx',
rules: {
// allow makeStyles calls in stories as examples should be defined in a single file
'@griffel/styles-file': 'off',
'no-restricted-globals': 'off',
// allow arrow functions in stories for now (may want to change this later since using
// constantly-mutating functions can be an anti-pattern which we may not want to demonstrate
// in our converged components docs; it happened to be allowed starting out because .stories
Expand Down
4 changes: 4 additions & 0 deletions packages/eslint-plugin/src/internal.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
const restrictedGlobals = require('./configs/restricted-globals');

function shouldRegisterInternal() {
try {
const hasNxEslintPlugin = require.resolve('@nx/eslint-plugin');
Expand Down Expand Up @@ -26,8 +28,10 @@ const __internal = {
react: shouldRegister
? {
files: ['**/src/**/*.{ts,tsx}'],
excludedFiles: ['*.{test,spec,cy,stories}.{ts,tsx}'],
rules: {
'@nx/workspace-consistent-callback-type': 'error',
'@nx/workspace-no-restricted-globals': restrictedGlobals.react,
},
}
: null,
Expand Down
2 changes: 1 addition & 1 deletion packages/public-docsite-setup/.eslintrc.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"extends": ["plugin:@fluentui/eslint-plugin/react"],
"extends": ["plugin:@fluentui/eslint-plugin/react--legacy"],
"root": true,
"overrides": [
{
Expand Down
1 change: 1 addition & 0 deletions packages/public-docsite-setup/src/types.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/* eslint-disable @typescript-eslint/naming-convention */
/** `IContextualMenuItem`-like interface, compatible with ContextualMenu in versions 5-8. */
export interface VersionMenuItem {
/** Major version number (a unique key for the entry). */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@ import { getMajorVersion } from './utils';
import { GlobalObject } from './types';

const isBrowser = canUseDOM();
// eslint-disable-next-line no-restricted-globals
const globalObject: GlobalObject = isBrowser ? window : global;
const globalObject: GlobalObject = isBrowser
? // eslint-disable-next-line @nx/workspace-no-restricted-globals
window
: global;

// Identifier for the symbol, for easy idenfitifaction of symbols created by this util
// Useful for clearning global object during SSR reloads
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@ import { GlobalObject } from './types';
import { getMajorVersion } from './utils';

const isBrowser = canUseDOM();
// eslint-disable-next-line no-restricted-globals
const globalObject: GlobalObject = isBrowser ? window : global;
const globalObject: GlobalObject = isBrowser
? // eslint-disable-next-line @nx/workspace-no-restricted-globals
window
: global;

// Identifier for the symbol, for easy idenfitifaction of symbols created by this util
// Useful for clearning global object during SSR reloads
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ let _window: Window | undefined = undefined;
// hits a memory leak, whereas aliasing it and calling "typeof _window" does not.
// Caching the window value at the file scope lets us minimize the impact.
try {
// eslint-disable-next-line no-restricted-globals
// eslint-disable-next-line @nx/workspace-no-restricted-globals
_window = window;
} catch (e) {
/* no-op */
Expand Down
28 changes: 14 additions & 14 deletions packages/react-components/react-icons-compat/library/src/icon.ts
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,6 @@ export function setIconOptions(options: Partial<IconOptions>): void {

let _missingIcons: string[] = [];
// TODO: exclude types from this lint rule: https://github.com/microsoft/fluentui/issues/31286
// eslint-disable-next-line no-restricted-globals
let _missingIconsTimer: ReturnType<typeof setTimeout> | undefined = undefined;

function _warnDuplicateIcon(iconName: string): void {
Expand All @@ -209,19 +208,20 @@ function _warnDuplicateIcon(iconName: string): void {
if (!options.disableWarnings) {
_missingIcons.push(iconName);
if (_missingIconsTimer === undefined) {
// eslint-disable-next-line no-restricted-globals
_missingIconsTimer = setTimeout(() => {
// eslint-disable-next-line no-console
console.warn(
`Some icons were re-registered. Applications should only call registerIcons for any given ` +
`icon once. Redefining what an icon is may have unintended consequences. Duplicates ` +
`include: \n` +
_missingIcons.slice(0, maxIconsInMessage).join(', ') +
(_missingIcons.length > maxIconsInMessage ? ` (+ ${_missingIcons.length - maxIconsInMessage} more)` : ''),
);
_missingIconsTimer = undefined;
_missingIcons = [];
}, warningDelay);
_missingIconsTimer =
// eslint-disable-next-line @nx/workspace-no-restricted-globals
setTimeout(() => {
// eslint-disable-next-line no-console
console.warn(
`Some icons were re-registered. Applications should only call registerIcons for any given ` +
`icon once. Redefining what an icon is may have unintended consequences. Duplicates ` +
`include: \n` +
_missingIcons.slice(0, maxIconsInMessage).join(', ') +
(_missingIcons.length > maxIconsInMessage ? ` (+ ${_missingIcons.length - maxIconsInMessage} more)` : ''),
);
_missingIconsTimer = undefined;
_missingIcons = [];
}, warningDelay);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,13 @@ function getMotionFunction(component: PresenceComponent): PresenceMotionFn | nul
export function expectPresenceMotionObject(component: PresenceComponent) {
const presenceMotionFn = getMotionFunction(component);

// eslint-disable-next-line no-restricted-globals
expect(presenceMotionFn?.({ element: document.createElement('div') })).toMatchObject({
expect(
presenceMotionFn?.({
element:
// eslint-disable-next-line @nx/workspace-no-restricted-globals
document.createElement('div'),
}),
).toMatchObject({
enter: expect.objectContaining({
duration: expect.any(Number),
easing: expect.any(String),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ const MotionDurationCard: React.FC<{ animationEnabled: boolean; tokenName: strin
function useAnimationEnabled() {
const [animationEnabled, setAnimationEnabled] = React.useState(() => {
// Heads up/ Not the best way to detect reduced motion as it breaks SSR, but it's good enough for Storybook
// eslint-disable-next-line no-restricted-globals
// eslint-disable-next-line @nx/workspace-no-restricted-globals
const isReducedMotion = window.matchMedia('(prefers-reduced-motion: reduce)').matches;

return !isReducedMotion;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ const ProviderContext = React.createContext<ProviderContextValue | undefined>(
) as React.Context<ProviderContextValue>;

const providerContextDefaultValue: ProviderContextValue = {
// eslint-disable-next-line no-restricted-globals
// eslint-disable-next-line @nx/workspace-no-restricted-globals -- expected ignore ( SSR friendly acquisition of globals )
targetDocument: typeof document === 'object' ? document : undefined,
dir: 'ltr' as const,
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,7 @@ export function useMeasureElement<TElement extends HTMLElement = HTMLElement>()
const [width, setWidth] = React.useState(0);

const container = React.useRef<HTMLElement | undefined>(undefined);
// TODO: exclude types from this lint rule: https://github.com/microsoft/fluentui/issues/31286
// eslint-disable-next-line no-restricted-globals

const resizeObserverRef = React.useRef<ResizeObserver | null>(null);

const { targetDocument } = useFluent();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ export interface Response {

export const mockFetch = (type: string) => {
return new Promise<Response>(resolve => {
// eslint-disable-next-line no-restricted-globals
// eslint-disable-next-line @nx/workspace-no-restricted-globals
setTimeout(() => {
const mockData: Response = {
results: Array.from({ length: 10 }, (_, index) => ({
Expand Down
10 changes: 8 additions & 2 deletions packages/react-components/react-utilities/src/ssr/canUseDOM.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,13 @@
*/
export function canUseDOM(): boolean {
return (
// eslint-disable-next-line deprecation/deprecation, no-restricted-globals
typeof window !== 'undefined' && !!(window.document && window.document.createElement)
/* eslint-disable @nx/workspace-no-restricted-globals -- expected ignore ( SSR friendly acquisition of globals )*/
typeof window !== 'undefined' &&
!!(
window.document &&
// eslint-disable-next-line deprecation/deprecation
window.document.createElement
)
/* eslint-enable @nx/workspace-no-restricted-globals */
);
}
2 changes: 1 addition & 1 deletion packages/react-components/theme-designer/.eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@
"root": true,
"rules": {
"@griffel/styles-file": "off",
"no-restricted-globals": "off"
"@nx/workspace-no-restricted-globals": "off"
}
}
2 changes: 1 addition & 1 deletion packages/react-window-provider/.eslintrc.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{
"extends": ["plugin:@fluentui/eslint-plugin/react"],
"extends": ["plugin:@fluentui/eslint-plugin/react--legacy"],
"root": true
}
2 changes: 1 addition & 1 deletion packages/set-version/.eslintrc.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"extends": ["plugin:@fluentui/eslint-plugin/react"],
"extends": ["plugin:@fluentui/eslint-plugin/react--legacy"],
"root": true,
"rules": {
"deprecation/deprecation": "off",
Expand Down

0 comments on commit 511f956

Please sign in to comment.