Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Enterprise Search] Add eslint import/order rules #90530

Merged
merged 9 commits into from
Feb 9, 2021
  •  
  •  
  •  
33 changes: 33 additions & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -1119,6 +1119,39 @@ module.exports = {
// All files
files: ['x-pack/plugins/enterprise_search/**/*.{ts,tsx}'],
rules: {
'import/order': [
'error',
{
groups: ['unknown', ['builtin', 'external'], 'internal', 'parent', 'sibling', 'index'],
pathGroups: [
{
pattern:
'{../../../../../../,../../../../../,../../../../,../../../,../../,../}{common/,*}__mocks__{*,/**}',
Comment on lines +1128 to +1129
Copy link
Contributor Author

@cee-chen cee-chen Feb 5, 2021

Choose a reason for hiding this comment

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

Yes this is absolutely horrifying. No there is no way else to do this. You would think that **/__mocks__/** would work. It absolutely does not for whatever reason (.. doesn't count as **).

See: import-js/eslint-plugin-import#1632

So yeah, I guess let's hope we don't go 7 folders in deep from a __mocks__ file, or we're adding yet another another , of 7x ../s :dead_inside:

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Y'all are also free to test this yourselves (glob tester links are in the PR description, under resources) but I can also reassure you I spent several hours of my life yesterday and today trying to get this pathGroup work and this was the only way it did.

group: 'unknown',
},
{
pattern: '{**,.}/*.mock',
group: 'unknown',
},
{
pattern: 'react*',
Copy link
Contributor Author

@cee-chen cee-chen Feb 5, 2021

Choose a reason for hiding this comment

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

I opted to group React-adjacent imports (e.g. react-router-dom and react-redux) in with React itself. Alphabetizing wise React should still remain at the top.

I don't feel super strongly if people would rather have only the main react lib in this group, feel free to comment if you do.

group: 'external',
position: 'before',
},
{
pattern: '{@elastic/**,@kbn/**,src/**}',
group: 'internal',
},
],
pathGroupsExcludedImportTypes: [],
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is required for the React external rule to work. See import-js/eslint-plugin-import#1874

alphabetize: {
order: 'asc',
caseInsensitive: true,
Copy link
Contributor Author

@cee-chen cee-chen Feb 5, 2021

Choose a reason for hiding this comment

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

I listed caseInsensitive as a reason to upgrade from eslint-plugin-import v2.19 to v2.22, then I realized Kibana enforces snake_case on files anyway so it's not doing much 🤦‍♀️

I left it in anyway as I think we would still probably want this rule if we were on the old ent-search repo with PascalCased component filenames, but would be curious to hear what others think and if my assumption was wrong there

},
'newlines-between': 'always-and-inside-groups',
Copy link
Contributor Author

@cee-chen cee-chen Feb 5, 2021

Choose a reason for hiding this comment

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

(Docs link) I opted for always-and-inside-groups instead of always because I wanted the option to sub-organize within groups for large amount of imports. Mock example:

import { SharedHelperA } from '../../../shared/a'
import { SharedHelperB } from '../../../shared/b'
import { SharedHelperC } from '../../../shared/c'

import { ProductSpecificHelperA } from '../app_search/utils/util_a';
import { ProductSpecificHelperB } from '../app_search/utils/util_b';

Actual example of newlines within a group being used to make code more readable in our server/plugin.ts file:

import { registerTelemetryUsageCollector as registerASTelemetryUsageCollector } from './collectors/app_search/telemetry';
import { registerTelemetryUsageCollector as registerESTelemetryUsageCollector } from './collectors/enterprise_search/telemetry';
import { registerTelemetryUsageCollector as registerWSTelemetryUsageCollector } from './collectors/workplace_search/telemetry';

import { checkAccess } from './lib/check_access';
import {
  EnterpriseSearchRequestHandler,
  IEnterpriseSearchRequestHandler,
} from './lib/enterprise_search_request_handler';

import { registerAppSearchRoutes } from './routes/app_search';
import { registerConfigDataRoute } from './routes/enterprise_search/config_data';
import { registerTelemetryRoute } from './routes/enterprise_search/telemetry';
import { registerWorkplaceSearchRoutes } from './routes/workplace_search';

import { appSearchTelemetryType } from './saved_objects/app_search/telemetry';
import { enterpriseSearchTelemetryType } from './saved_objects/enterprise_search/telemetry';
import { workplaceSearchTelemetryType } from './saved_objects/workplace_search/telemetry';

},
],
'import/newline-after-import': 'error',
'react-hooks/exhaustive-deps': 'off',
'react/jsx-boolean-value': ['error', 'never'],
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@
* NOTE: These variable names MUST start with 'mock*' in order for
* Jest to accept its use within a jest.mock()
*/
import { mockFlashMessagesValues, mockFlashMessagesActions } from './flash_messages_logic.mock';
import { mockHttpValues } from './http_logic.mock';
import { mockKibanaValues } from './kibana_logic.mock';
import { mockLicensingValues } from './licensing_logic.mock';
import { mockHttpValues } from './http_logic.mock';
import { mockTelemetryActions } from './telemetry_logic.mock';
import { mockFlashMessagesValues, mockFlashMessagesActions } from './flash_messages_logic.mock';

export const mockAllValues = {
...mockKibanaValues,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
*/

import { chartPluginMock } from '../../../../../../src/plugins/charts/public/mocks';

import { mockHistory } from './';

export const mockKibanaValues = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@
*/

import React from 'react';
import { act } from 'react-dom/test-utils';

import { mount, ReactWrapper } from 'enzyme';
import { act } from 'react-dom/test-utils';

import { mountWithIntl } from './';

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@
*/

import React from 'react';

import { mount } from 'enzyme';

import { I18nProvider } from '@kbn/i18n/react';

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import React from 'react';

import { shallow, mount, ReactWrapper } from 'enzyme';

import { I18nProvider, __IntlProvider } from '@kbn/i18n/react';

// Use fake component to extract `intl` property to use in tests.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
* 2.0.
*/

import { DEFAULT_INITIAL_APP_DATA } from '../../../common/__mocks__';
import { LogicMounter } from '../__mocks__';

import { DEFAULT_INITIAL_APP_DATA } from '../../../common/__mocks__';
import { AppLogic } from './app_logic';

describe('AppLogic', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import { kea, MakeLogicType } from 'kea';

import { InitialAppData } from '../../../common/types';

import { ConfiguredLimits, Account, Role } from './types';

import { getRoleAbilities } from './utils/role';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,15 @@ import { mockKibanaValues, setMockValues, setMockActions, rerender } from '../..

import React from 'react';
import { useParams } from 'react-router-dom';

import { shallow } from 'enzyme';

import { Loading } from '../../../shared/loading';
import { FlashMessages } from '../../../shared/flash_messages';
import { Loading } from '../../../shared/loading';
import { LogRetentionCallout } from '../log_retention';
import { AnalyticsHeader, AnalyticsUnavailable } from './components';

import { AnalyticsLayout } from './analytics_layout';
import { AnalyticsHeader, AnalyticsUnavailable } from './components';

describe('AnalyticsLayout', () => {
const { history } = mockKibanaValues;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,21 @@

import React, { useEffect } from 'react';
import { useParams } from 'react-router-dom';

import { useValues, useActions } from 'kea';

import { EuiSpacer } from '@elastic/eui';

import { KibanaLogic } from '../../../shared/kibana';
import { FlashMessages } from '../../../shared/flash_messages';
import { KibanaLogic } from '../../../shared/kibana';
import { Loading } from '../../../shared/loading';

import { LogRetentionCallout, LogRetentionOptions } from '../log_retention';

import { AnalyticsLogic } from './';
import { AnalyticsHeader, AnalyticsUnavailable } from './components';

import { AnalyticsLogic } from './';

interface Props {
title: string;
isQueryView?: boolean;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ jest.mock('../engine', () => ({
import { nextTick } from '@kbn/test/jest';

import { DEFAULT_START_DATE, DEFAULT_END_DATE } from './constants';

import { AnalyticsLogic } from './';

describe('AnalyticsLogic', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@
import { kea, MakeLogicType } from 'kea';
import queryString from 'query-string';

import { KibanaLogic } from '../../../shared/kibana';
import { HttpLogic } from '../../../shared/http';
import { flashAPIErrors } from '../../../shared/flash_messages';
import { HttpLogic } from '../../../shared/http';
import { KibanaLogic } from '../../../shared/kibana';
import { EngineLogic } from '../engine';

import { DEFAULT_START_DATE, DEFAULT_END_DATE } from './constants';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,11 @@
import '../../__mocks__/engine_logic.mock';

import React from 'react';
import { shallow } from 'enzyme';

import { Route, Switch } from 'react-router-dom';

import { shallow } from 'enzyme';

import { AnalyticsRouter } from './';

describe('AnalyticsRouter', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@
*/

import React from 'react';

import { shallow } from 'enzyme';

import { EuiStat } from '@elastic/eui';

import { AnalyticsCards } from './';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
*/

import React from 'react';

import { EuiFlexGroup, EuiFlexItem, EuiPanel, EuiStat } from '@elastic/eui';

interface Props {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@
import { mockKibanaValues } from '../../../../__mocks__';

import React from 'react';

import { shallow } from 'enzyme';

import { Chart, Settings, LineSeries, Axis } from '@elastic/charts';

import { AnalyticsChart } from './';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,11 @@
*/

import React from 'react';

import { useValues } from 'kea';

import moment from 'moment';

import { Chart, Settings, LineSeries, CurveType, Axis } from '@elastic/charts';

import { KibanaLogic } from '../../../../shared/kibana';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,16 @@
import { setMockValues, mockKibanaValues } from '../../../../__mocks__';

import React, { ReactElement } from 'react';

import { shallow, ShallowWrapper } from 'enzyme';
import moment, { Moment } from 'moment';

import { EuiPageHeader, EuiSelect, EuiDatePickerRange, EuiButton } from '@elastic/eui';

import { LogRetentionTooltip } from '../../log_retention';

import { DEFAULT_START_DATE, DEFAULT_END_DATE } from '../constants';

import { AnalyticsHeader } from './';

describe('AnalyticsHeader', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,11 @@
*/

import React, { useState } from 'react';
import { useValues } from 'kea';

import queryString from 'query-string';
import { useValues } from 'kea';
import moment from 'moment';
import queryString from 'query-string';

import { i18n } from '@kbn/i18n';
import {
EuiPageHeader,
EuiPageHeaderSection,
Expand All @@ -23,11 +22,12 @@ import {
EuiDatePicker,
EuiButton,
} from '@elastic/eui';
import { i18n } from '@kbn/i18n';

import { AnalyticsLogic } from '../';
import { KibanaLogic } from '../../../../shared/kibana';
import { LogRetentionTooltip, LogRetentionOptions } from '../../log_retention';

import { AnalyticsLogic } from '../';
import { DEFAULT_START_DATE, DEFAULT_END_DATE, SERVER_DATE_FORMAT } from '../constants';
import { convertTagsToSelectOptions } from '../utils';

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@ import { mockKibanaValues } from '../../../../__mocks__';
import '../../../__mocks__/engine_logic.mock';

import React from 'react';

import { shallow } from 'enzyme';

import { EuiFieldSearch } from '@elastic/eui';

import { AnalyticsSearch } from './';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,11 @@
*/

import React, { useState } from 'react';

import { useValues } from 'kea';

import { i18n } from '@kbn/i18n';
import { EuiFlexGroup, EuiFlexItem, EuiFieldSearch, EuiButton, EuiSpacer } from '@elastic/eui';
import { i18n } from '@kbn/i18n';

import { KibanaLogic } from '../../../../shared/kibana';
import { ENGINE_ANALYTICS_QUERY_DETAIL_PATH } from '../../../routes';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
*/

import React from 'react';

import { shallow } from 'enzyme';

import { AnalyticsSection } from './';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { mountWithIntl, mockKibanaValues } from '../../../../../__mocks__';
import '../../../../__mocks__/engine_logic.mock';

import React from 'react';

import { EuiBasicTable, EuiBadge, EuiEmptyPrompt } from '@elastic/eui';

import { AnalyticsTable } from './';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,12 @@
*/

import React from 'react';
import { i18n } from '@kbn/i18n';

import { EuiBasicTable, EuiBasicTableColumn, EuiEmptyPrompt } from '@elastic/eui';
import { i18n } from '@kbn/i18n';

import { Query } from '../../types';

import {
TERM_COLUMN_PROPS,
TAGS_COLUMN,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@
*/

import React from 'react';

import { shallow } from 'enzyme';

import { EuiBadge, EuiToolTip } from '@elastic/eui';

import { InlineTagsList } from './inline_tags_list';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@
*/

import React from 'react';
import { i18n } from '@kbn/i18n';

import { EuiBadgeGroup, EuiBadge, EuiToolTip } from '@elastic/eui';
import { i18n } from '@kbn/i18n';

import { Query } from '../../types';

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { mountWithIntl } from '../../../../../__mocks__';
import '../../../../__mocks__/engine_logic.mock';

import React from 'react';

import { EuiBasicTable, EuiLink, EuiBadge, EuiEmptyPrompt } from '@elastic/eui';

import { QueryClicksTable } from './';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,16 @@

import React from 'react';

import { i18n } from '@kbn/i18n';
import { EuiBasicTable, EuiBasicTableColumn, EuiEmptyPrompt } from '@elastic/eui';
import { i18n } from '@kbn/i18n';

import { EuiLinkTo } from '../../../../../shared/react_router_helpers';
import { ENGINE_DOCUMENT_DETAIL_PATH } from '../../../../routes';
import { generateEnginePath } from '../../../engine';
import { DOCUMENTS_TITLE } from '../../../documents';
import { generateEnginePath } from '../../../engine';

import { QueryClick } from '../../types';

import { FIRST_COLUMN_PROPS, TAGS_COLUMN, COUNT_COLUMN_PROPS } from './shared_columns';

interface Props {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { mountWithIntl, mockKibanaValues } from '../../../../../__mocks__';
import '../../../../__mocks__/engine_logic.mock';

import React from 'react';

import { EuiBasicTable, EuiBadge, EuiEmptyPrompt } from '@elastic/eui';

import { RecentQueriesTable } from './';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,12 @@

import React from 'react';

import { EuiBasicTable, EuiBasicTableColumn, EuiEmptyPrompt } from '@elastic/eui';
import { i18n } from '@kbn/i18n';
import { FormattedDate, FormattedTime } from '@kbn/i18n/react';
import { EuiBasicTable, EuiBasicTableColumn, EuiEmptyPrompt } from '@elastic/eui';

import { RecentQuery } from '../../types';

import {
TERM_COLUMN_PROPS,
TAGS_COLUMN,
Expand Down
Loading