Skip to content

Commit

Permalink
fix no-restricted-imports (elastic#195456)
Browse files Browse the repository at this point in the history
## Summary

I noticed that our `no-restricted-imports` rules were not working on
some parts of the codebase. Turns our the rule was overriden by mistake.
This PR fixes the rules and places that were not following them:

- lodash set for safety
- react-use for a bit smaller bundles
- router for context annoncement (`useExecutionContext`) and hopefully
easier upgrade to newer version

(cherry picked from commit 1055120)

# Conflicts:
#	.github/CODEOWNERS
  • Loading branch information
Dosant committed Oct 16, 2024
1 parent ddcecd6 commit b904a17
Show file tree
Hide file tree
Showing 78 changed files with 129 additions and 117 deletions.
13 changes: 6 additions & 7 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -1014,6 +1014,7 @@ module.exports = {
'error',
{
patterns: ['**/legacy_uptime/*'],
paths: RESTRICTED_IMPORTS,
},
],
},
Expand Down Expand Up @@ -1055,6 +1056,7 @@ module.exports = {
{
// prevents UI code from importing server side code and then webpack including it when doing builds
patterns: ['**/server/*'],
paths: RESTRICTED_IMPORTS,
},
],
},
Expand Down Expand Up @@ -1113,6 +1115,7 @@ module.exports = {
{
// prevents UI code from importing server side code and then webpack including it when doing builds
patterns: ['**/server/*'],
paths: RESTRICTED_IMPORTS,
},
],
},
Expand Down Expand Up @@ -1184,13 +1187,7 @@ module.exports = {
// to help deprecation and prevent accidental re-use/continued use of code we plan on removing. If you are
// finding yourself turning this off a lot for "new code" consider renaming the file and functions if it is has valid uses.
patterns: ['*legacy*'],
paths: [
{
name: 'react-router-dom',
importNames: ['Route'],
message: "import { Route } from '@kbn/kibana-react-plugin/public'",
},
],
paths: RESTRICTED_IMPORTS,
},
],
},
Expand Down Expand Up @@ -1348,6 +1345,7 @@ module.exports = {
{
// prevents UI code from importing server side code and then webpack including it when doing builds
patterns: ['**/server/*'],
paths: RESTRICTED_IMPORTS,
},
],
},
Expand Down Expand Up @@ -1525,6 +1523,7 @@ module.exports = {
// to help deprecation and prevent accidental re-use/continued use of code we plan on removing. If you are
// finding yourself turning this off a lot for "new code" consider renaming the file and functions if it has valid uses.
patterns: ['*legacy*'],
paths: RESTRICTED_IMPORTS,
},
],
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ import { useLoadConnectors } from '../connectorland/use_load_connectors';

import { DefinedUseQueryResult, UseQueryResult } from '@tanstack/react-query';

import { useLocalStorage, useSessionStorage } from 'react-use';
import useLocalStorage from 'react-use/lib/useLocalStorage';
import useSessionStorage from 'react-use/lib/useSessionStorage';
import { QuickPrompts } from './quick_prompts/quick_prompts';
import { mockAssistantAvailability, TestProviders } from '../mock/test_providers/test_providers';
import { useFetchCurrentUserConversations } from './api';
Expand All @@ -26,7 +27,8 @@ import { AIConnector } from '../connectorland/connector_selector';

jest.mock('../connectorland/use_load_connectors');
jest.mock('../connectorland/connector_setup');
jest.mock('react-use');
jest.mock('react-use/lib/useLocalStorage');
jest.mock('react-use/lib/useSessionStorage');

jest.mock('./quick_prompts/quick_prompts', () => ({ QuickPrompts: jest.fn() }));
jest.mock('./api/conversations/use_fetch_current_user_conversations');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,15 +32,12 @@ const testTitle = 'SPL_QUERY_CONVERSION_TITLE';
const testPrompt = 'SPL_QUERY_CONVERSION_PROMPT';
const customTitle = 'A_CUSTOM_OPTION';

jest.mock('react-use', () => ({
...jest.requireActual('react-use'),
useMeasure: () => [
() => {},
{
width: 500,
},
],
}));
jest.mock('react-use/lib/useMeasure', () => () => [
() => {},
{
width: 500,
},
]);

jest.mock('../../assistant_context', () => ({
...jest.requireActual('../../assistant_context'),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import {
EuiButtonIcon,
EuiButtonEmpty,
} from '@elastic/eui';
import { useMeasure } from 'react-use';
import useMeasure from 'react-use/lib/useMeasure';

import { css } from '@emotion/react';
import {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,11 @@
import { renderHook } from '@testing-library/react-hooks';

import { useAssistantContext } from '.';
import { useLocalStorage } from 'react-use';
import useLocalStorage from 'react-use/lib/useLocalStorage';
import { TestProviders } from '../mock/test_providers/test_providers';

jest.mock('react-use', () => ({
useLocalStorage: jest.fn().mockReturnValue(['456', jest.fn()]),
useSessionStorage: jest.fn().mockReturnValue(['456', jest.fn()]),
}));
jest.mock('react-use/lib/useLocalStorage', () => jest.fn().mockReturnValue(['456', jest.fn()]));
jest.mock('react-use/lib/useSessionStorage', () => jest.fn().mockReturnValue(['456', jest.fn()]));

describe('AssistantContext', () => {
beforeEach(() => jest.clearAllMocks());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ import { omit } from 'lodash/fp';
import React, { useCallback, useMemo, useState, useRef } from 'react';
import type { IToasts } from '@kbn/core-notifications-browser';
import { ActionTypeRegistryContract } from '@kbn/triggers-actions-ui-plugin/public';
import { useLocalStorage, useSessionStorage } from 'react-use';
import useLocalStorage from 'react-use/lib/useLocalStorage';
import useSessionStorage from 'react-use/lib/useSessionStorage';
import type { DocLinksStart } from '@kbn/core-doc-links-browser';
import { AssistantFeatures, defaultAssistantFeatures } from '@kbn/elastic-assistant-common';
import { NavigateToAppOptions, UserProfileService } from '@kbn/core/public';
Expand Down
2 changes: 1 addition & 1 deletion x-pack/plugins/cases/common/types/domain/user/v1.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* 2.0.
*/

import { set } from 'lodash';
import { set } from '@kbn/safer-lodash-set';
import { UserRt, UserWithProfileInfoRt, UsersRt, CaseUserProfileRt, CaseAssigneesRt } from './v1';

describe('User', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* 2.0.
*/

import { set } from 'lodash';
import { set } from '@kbn/safer-lodash-set';
import React from 'react';
import { screen } from '@testing-library/react';
import type { AppMockRenderer } from '../../common/mock';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ import { loggingSystemMock } from '@kbn/core-logging-server-mocks';

import { CasesOracleService } from './cases_oracle_service';
import { CASE_RULES_SAVED_OBJECT } from '../../../common/constants';
import { isEmpty, set } from 'lodash';
import { isEmpty } from 'lodash';
import { set } from '@kbn/safer-lodash-set';

describe('CasesOracleService', () => {
const savedObjectsClient = savedObjectsClientMock.create();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@
import { createHash } from 'node:crypto';
import stringify from 'json-stable-stringify';

import { isEmpty, set } from 'lodash';
import { isEmpty } from 'lodash';
import { set } from '@kbn/safer-lodash-set';
import { CasesService } from './cases_service';

describe('CasesService', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
* 2.0.
*/

import { set, omit, unset } from 'lodash';
import { omit, unset } from 'lodash';
import { set } from '@kbn/safer-lodash-set';
import { loggerMock } from '@kbn/logging-mocks';
import { savedObjectsClientMock } from '@kbn/core/server/mocks';
import type {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ import { createSavedObjectsSerializerMock } from '../../../client/mocks';
import { savedObjectsClientMock } from '@kbn/core-saved-objects-api-server-mocks';
import { loggerMock } from '@kbn/logging-mocks';
import { auditLoggerMock } from '@kbn/security-plugin/server/audit/mocks';
import { set, unset } from 'lodash';
import { unset } from 'lodash';
import { set } from '@kbn/safer-lodash-set';
import { createConnectorObject } from '../../test_utils';
import { UserActionPersister } from './create';
import { createUserActionSO } from '../test_utils';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
*/

import { useMemo } from 'react';
import { useObservable } from 'react-use';
import useObservable from 'react-use/lib/useObservable';
import { MINIMUM_LICENSE_TYPE } from '../../../common/constants';
import { useKibana } from './use_kibana';
import type { RenderUpselling } from '../../services';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
* 2.0.
*/
import React from 'react';
import { Redirect, Switch } from 'react-router-dom';
import { Route } from '@kbn/shared-ux-router';
import { Redirect } from 'react-router-dom';
import { Route, Routes } from '@kbn/shared-ux-router';
import { KibanaContextProvider } from '@kbn/kibana-react-plugin/public';
import type { Services } from '../../services';
import { TelemetryContextProvider } from './telemetry';
Expand All @@ -33,7 +33,7 @@ const CreateIntegrationRouter = React.memo(() => {
const { canUseIntegrationAssistant, canUseIntegrationUpload } = useRoutesAuthorization();
const isAvailable = useIsAvailable();
return (
<Switch>
<Routes>
{isAvailable && canUseIntegrationAssistant && (
<Route path={PagePath[Page.assistant]} exact component={CreateIntegrationAssistant} />
)}
Expand All @@ -44,7 +44,7 @@ const CreateIntegrationRouter = React.memo(() => {
<Route path={PagePath[Page.landing]} exact component={CreateIntegrationLanding} />

<Route render={() => <Redirect to={PagePath[Page.landing]} />} />
</Switch>
</Routes>
);
});
CreateIntegrationRouter.displayName = 'CreateIntegrationRouter';
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import { useDispatch, useSelector } from 'react-redux';
import { EuiButton, EuiButtonEmpty, EuiCallOut, EuiMarkdownFormat, EuiSpacer } from '@elastic/eui';
import { syntheticsSettingsLocatorID } from '@kbn/observability-plugin/common';
import { useFetcher } from '@kbn/observability-shared-plugin/public';
import { useSessionStorage } from 'react-use';
import useSessionStorage from 'react-use/lib/useSessionStorage';
import { i18n } from '@kbn/i18n';
import { isEmpty } from 'lodash';
import { useKibana } from '@kbn/kibana-react-plugin/public';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import React, { useCallback, useState } from 'react';
import { EuiFormRow, EuiFormRowProps } from '@elastic/eui';
import { useSelector } from 'react-redux';
import { useDebounce } from 'react-use';
import useDebounce from 'react-use/lib/useDebounce';
import { ControllerRenderProps, ControllerFieldState, useFormContext } from 'react-hook-form';
import { useKibanaSpace, useIsEditFlow } from '../hooks';
import { selectServiceLocationsState } from '../../../state';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

import { useCallback, useEffect, useMemo, useState } from 'react';
import { useSelector, useDispatch } from 'react-redux';
import { useDebounce } from 'react-use';
import useDebounce from 'react-use/lib/useDebounce';
import { useLocation } from 'react-router-dom';

import { useSyntheticsRefreshContext } from '../../../contexts/synthetics_refresh_context';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

import { useCallback, useEffect, useRef } from 'react';
import { useDispatch, useSelector } from 'react-redux';
import { useDebounce } from 'react-use';
import useDebounce from 'react-use/lib/useDebounce';
import { useMonitorFiltersState } from '../common/monitor_filters/use_filters';
import {
fetchMonitorListAction,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import {
import React, { useState } from 'react';
import { i18n } from '@kbn/i18n';
import { useSelector } from 'react-redux';
import { useKey } from 'react-use';
import useKey from 'react-use/lib/useKey';
import { FlyoutParamProps } from '../types';
import { OverviewLoader } from '../overview_loader';
import { useFilteredGroupMonitors } from './use_filtered_group_monitors';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import { i18n } from '@kbn/i18n';
import { FormattedMessage } from '@kbn/i18n-react';
import { useKibana } from '@kbn/kibana-react-plugin/public';
import { EuiBasicTableColumn } from '@elastic/eui/src/components/basic_table/basic_table';
import { useDebounce } from 'react-use';
import useDebounce from 'react-use/lib/useDebounce';
import { TableTitle } from '../../common/components/table_title';
import { ParamsText } from './params_text';
import { SyntheticsParams } from '../../../../../../common/runtime_types';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import React, {
FC,
} from 'react';
import useLocalStorage from 'react-use/lib/useLocalStorage';
import { useEvent } from 'react-use';
import useEvent from 'react-use/lib/useEvent';
import moment from 'moment';
import { Subject } from 'rxjs';
import { i18n } from '@kbn/i18n';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { ChromeBreadcrumb } from '@kbn/core/public';
import { render } from '../utils/testing';
import React from 'react';
import { i18n } from '@kbn/i18n';
import { Route } from 'react-router-dom';
import { Route } from '@kbn/shared-ux-router';
import { OVERVIEW_ROUTE } from '../../../../common/constants';
import { KibanaContextProvider } from '@kbn/kibana-react-plugin/public';
import {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

import { useMemo, useState } from 'react';
import { useParams } from 'react-router-dom';
import { useDebounce } from 'react-use';
import useDebounce from 'react-use/lib/useDebounce';
import { useFetcher } from '@kbn/observability-shared-plugin/public';

import { fetchMonitorManagementList, getMonitorListPageStateWithDefaults } from '../state';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import moment from 'moment';
import { Moment } from 'moment-timezone';
import * as redux from 'react-redux';
// eslint-disable-next-line no-restricted-imports
import * as reactRouterDom from 'react-router-dom';

export function mockMoment() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@
/* eslint-disable max-classes-per-file */

import type { DeepPartial } from 'utility-types';
import { merge, set } from 'lodash';
import { merge } from 'lodash';
import { set } from '@kbn/safer-lodash-set';
import { gte } from 'semver';
import type { EndpointCapabilities } from '../service/response_actions/constants';
import { BaseDataGenerator } from './base_data_generator';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ import {
checkIfPopupMessagesContainCustomNotifications,
resetCustomNotifications,
} from './policy_config_helpers';
import { get, merge, set } from 'lodash';
import { get, merge } from 'lodash';
import { set } from '@kbn/safer-lodash-set';

describe('Policy Config helpers', () => {
describe('disableProtections', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
* 2.0.
*/

import { get, set } from 'lodash';
import { get } from 'lodash';
import { set } from '@kbn/safer-lodash-set';
import { DefaultPolicyNotificationMessage } from './policy_config';
import type { PolicyConfig } from '../types';
import { PolicyOperatingSystem, ProtectionModes, AntivirusRegistrationModes } from '../types';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
* 2.0.
*/

import { merge, setWith } from 'lodash';
import { merge } from 'lodash';
import { setWith } from '@kbn/safer-lodash-set';

/*
* Expands an object with "dotted" fields to a nested object with unflattened fields.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import { createAddToTimelineCellActionFactory } from './add_to_timeline';
import type { CellActionExecutionContext } from '@kbn/cell-actions';
import { GEO_FIELD_TYPE } from '../../../../timelines/components/timeline/body/renderers/constants';
import { createStartServicesMock } from '../../../../common/lib/kibana/kibana_react.mock';
import { set } from 'lodash/fp';
import { set } from '@kbn/safer-lodash-set/fp';
import { KBN_FIELD_TYPES } from '@kbn/field-types';

const services = createStartServicesMock();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,7 @@ const data = {

const mockUseObservable = jest.fn();

jest.mock('react-use', () => ({
...jest.requireActual('react-use'),
useObservable: () => mockUseObservable(),
}));
jest.mock('react-use/lib/useObservable', () => () => mockUseObservable());

jest.mock('../../../common/lib/kibana', () => {
const original = jest.requireActual('../../../common/lib/kibana');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import React, { useCallback } from 'react';
import { EuiWrappingPopover } from '@elastic/eui';
import { useLocation } from 'react-router-dom';
import { useObservable } from 'react-use';
import useObservable from 'react-use/lib/useObservable';
import { StatefulTopN } from '../../../common/components/top_n';
import { getScopeFromPath } from '../../../sourcerer/containers/sourcerer_paths';
import { useSourcererDataView } from '../../../sourcerer/containers';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import { once } from 'lodash/fp';
import type { HttpSetup } from '@kbn/core-http-browser';
import type { Message } from '@kbn/elastic-assistant-common';
import { loadAllActions as loadConnectors } from '@kbn/triggers-actions-ui-plugin/public/common/constants';
import { useObservable } from 'react-use';
import useObservable from 'react-use/lib/useObservable';
import { APP_ID } from '../../common';
import { useBasePath, useKibana } from '../common/lib/kibana';
import { useAssistantTelemetry } from './use_assistant_telemetry';
Expand Down
Loading

0 comments on commit b904a17

Please sign in to comment.