Skip to content

Commit

Permalink
Add more strict eslint rules for discover (#99241)
Browse files Browse the repository at this point in the history
  • Loading branch information
Tim Roes authored May 5, 2021
1 parent b2df82f commit 349b7e9
Show file tree
Hide file tree
Showing 65 changed files with 217 additions and 132 deletions.
16 changes: 16 additions & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -1256,6 +1256,22 @@ module.exports = {
},
},

/**
* Discover overrides
*/
{
files: ['src/plugins/discover/**/*.{ts,tsx}'],
rules: {
'@typescript-eslint/no-explicit-any': 'error',
'@typescript-eslint/ban-ts-comment': [
'error',
{
'ts-expect-error': false,
},
],
},
},

/**
* Enterprise Search overrides
* NOTE: We also have a single rule at the bottom of the file that
Expand Down
2 changes: 1 addition & 1 deletion src/plugins/discover/public/__mocks__/index_pattern.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ const indexPattern = ({

indexPattern.flattenHit = indexPatterns.flattenHitWrapper(indexPattern, indexPattern.metaFields);
indexPattern.isTimeBased = () => !!indexPattern.timeFieldName;
indexPattern.formatField = (hit: Record<string, any>, fieldName: string) => {
indexPattern.formatField = (hit: Record<string, unknown>, fieldName: string) => {
return fieldName === '_source' ? hit._source : indexPattern.flattenHit(hit)[fieldName];
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,10 @@ import { getServices } from '../../../../kibana_services';

export type SurrDocType = 'successors' | 'predecessors';
export interface EsHitRecord {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
fields: Record<string, any>;
sort: number[];
// eslint-disable-next-line @typescript-eslint/no-explicit-any
_source: Record<string, any>;
_id: string;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,11 @@ export function getEsQuerySearchAfter(
// already surrounding docs -> first or last record is used
const afterTimeRecIdx = type === 'successors' && documents.length ? documents.length - 1 : 0;
const afterTimeDoc = documents[afterTimeRecIdx];
let afterTimeValue = afterTimeDoc.sort[0];
let afterTimeValue: string | number = afterTimeDoc.sort[0];
if (nanoSeconds) {
afterTimeValue = useNewFieldsApi
? afterTimeDoc.fields[timeFieldName][0]
: afterTimeDoc._source[timeFieldName];
? (afterTimeDoc.fields[timeFieldName] as Array<string | number>)[0]
: (afterTimeDoc._source[timeFieldName] as string | number);
}
return [afterTimeValue, afterTimeDoc.sort[1]];
}
Expand All @@ -42,8 +42,8 @@ export function getEsQuerySearchAfter(
searchAfter[0] = anchor.sort[0];
if (nanoSeconds) {
searchAfter[0] = useNewFieldsApi
? anchor.fields[timeFieldName][0]
: anchor._source[timeFieldName];
? (anchor.fields[timeFieldName] as Array<string | number>)[0]
: (anchor._source[timeFieldName] as string | number);
}
searchAfter[1] = anchor.sort[1];
return searchAfter;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ export function getFirstSortableField(indexPattern: IndexPattern, fieldNames: st
const sortableFields = fieldNames.filter(
(fieldName) =>
META_FIELD_NAMES.includes(fieldName) ||
// @ts-ignore
(indexPattern.fields.getByName(fieldName) || { sortable: false }).sortable
);
return sortableFields[0];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import { getAngularModule } from '../../../../../kibana_services';
import { ActionBar } from './action_bar';

// eslint-disable-next-line @typescript-eslint/no-explicit-any
getAngularModule().directive('contextActionBar', function (reactDirective: any) {
return reactDirective(ActionBar);
});
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
* Side Public License, v 1.
*/

// @ts-ignore
// @ts-expect-error
import { getQueryParameterActions } from './actions';
import { FilterManager } from '../../../../../../data/public';
import { coreMock } from '../../../../../../../core/public/mocks';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,17 @@ import { createBrowserHistory, History } from 'history';
import { FilterManager, Filter } from '../../../../data/public';
import { coreMock } from '../../../../../core/public/mocks';
import { SEARCH_FIELDS_FROM_SOURCE } from '../../../common';

const setupMock = coreMock.createSetup();

describe('Test Discover Context State', () => {
let history: History;
let state: any;
let state: ReturnType<typeof getState>;
const getCurrentUrl = () => history.createHref(history.location);
beforeEach(async () => {
history = createBrowserHistory();
history.push('/');
state = await getState({
state = getState({
defaultStepSize: '4',
timeFieldName: 'time',
history,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ import {
createStateContainer,
createKbnUrlStateStorage,
syncStates,
BaseStateContainer,
withNotifyOnErrors,
ReduxLikeStateContainer,
} from '../../../../kibana_utils/public';
import { esFilters, FilterManager, Filter, Query } from '../../../../data/public';
import { handleSourceColumnState } from './helpers';
Expand Down Expand Up @@ -85,11 +85,11 @@ interface GetStateReturn {
/**
* Global state, the _g part of the URL
*/
globalState: BaseStateContainer<GlobalState>;
globalState: ReduxLikeStateContainer<GlobalState>;
/**
* App state, the _a part of the URL
*/
appState: BaseStateContainer<AppState>;
appState: ReduxLikeStateContainer<AppState>;
/**
* Start sync between state and URL
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import 'angular-mocks';
import 'angular-sanitize';
import 'angular-route';

// @ts-ignore
// @ts-expect-error
import { createDebounceProviderTimeout } from './debounce';
import { coreMock } from '../../../../../../../core/public/mocks';
import { initializeInnerAngularModule } from '../../../../get_inner_angular';
Expand All @@ -21,6 +21,7 @@ import { dataPluginMock } from '../../../../../../data/public/mocks';
import { initAngularBootstrap } from '../../../../../../kibana_legacy/public';

describe('debounce service', function () {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
let debounce: (fn: () => void, timeout: number, options?: any) => any;
let $timeout: ITimeoutService;
let spy: SinonSpy;
Expand Down
8 changes: 5 additions & 3 deletions src/plugins/discover/public/application/angular/doc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,17 @@
*/

import { getAngularModule, getServices } from '../../kibana_services';
// @ts-ignore
import { getRootBreadcrumbs } from '../helpers/breadcrumbs';
import html from './doc.html';
import { Doc } from '../components/doc/doc';

interface LazyScope extends ng.IScope {
[key: string]: any;
[key: string]: unknown;
}

const { timefilter } = getServices();
const app = getAngularModule();
// eslint-disable-next-line @typescript-eslint/no-explicit-any
app.directive('discoverDoc', function (reactDirective: any) {
return reactDirective(
Doc,
Expand All @@ -31,6 +31,7 @@ app.directive('discoverDoc', function (reactDirective: any) {
);
});

// eslint-disable-next-line @typescript-eslint/no-explicit-any
app.config(($routeProvider: any) => {
$routeProvider
.when('/doc/:indexPattern/:index/:type', {
Expand All @@ -39,7 +40,7 @@ app.config(($routeProvider: any) => {
// the new route, es 7 deprecated types, es 8 removed them
.when('/doc/:indexPattern/:index', {
// have to be written as function expression, because it's not compiled in dev mode
// eslint-disable-next-line object-shorthand
// eslint-disable-next-line @typescript-eslint/no-explicit-any, object-shorthand
controller: function ($scope: LazyScope, $route: any) {
timefilter.disableAutoRefreshSelector();
timefilter.disableTimeRangeSelector();
Expand All @@ -49,6 +50,7 @@ app.config(($routeProvider: any) => {
$scope.indexPatternService = getServices().indexPatterns;
},
template: html,
// eslint-disable-next-line @typescript-eslint/no-explicit-any
k7Breadcrumbs: ($route: any) => [
...getRootBreadcrumbs(),
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,12 @@
import { ToolBarPagerText } from './tool_bar_pager_text';
import { ToolBarPagerButtons } from './tool_bar_pager_buttons';

// eslint-disable-next-line @typescript-eslint/no-explicit-any
export function createToolBarPagerTextDirective(reactDirective: any) {
return reactDirective(ToolBarPagerText);
}

// eslint-disable-next-line @typescript-eslint/no-explicit-any
export function createToolBarPagerButtonsDirective(reactDirective: any) {
return reactDirective(ToolBarPagerButtons);
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { getServices } from '../../../../kibana_services';
import { SORT_DEFAULT_ORDER_SETTING, DOC_HIDE_TIME_COLUMN_SETTING } from '../../../../../common';
import { UI_SETTINGS } from '../../../../../../data/public';

// eslint-disable-next-line @typescript-eslint/no-explicit-any
export function createTableHeaderDirective(reactDirective: any) {
const { uiSettings: config } = getServices();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@

import React from 'react';
import { IndexPattern } from '../../../../../kibana_services';
// @ts-ignore
import { TableHeaderColumn } from './table_header_column';
import { SortOrder, getDisplayedColumns } from './helpers';
import { getDefaultSort } from '../../lib/get_default_sort';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ export function noWhiteSpace(html: string): string {
const MIN_LINE_LENGTH = 20;

interface LazyScope extends ng.IScope {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
[key: string]: any;
}

Expand Down Expand Up @@ -94,6 +95,7 @@ export function createTableRowDirective($compile: ng.ICompileService) {
createSummaryRow($scope.row);
});

// eslint-disable-next-line @typescript-eslint/no-explicit-any
$scope.inlineFilter = function inlineFilter($event: any, type: string) {
const column = $($event.currentTarget).data().column;
const field = $scope.indexPattern.fields.getByName(column);
Expand All @@ -119,6 +121,7 @@ export function createTableRowDirective($compile: ng.ICompileService) {
};

// create a tr element that lists the value for each *column*
// eslint-disable-next-line @typescript-eslint/no-explicit-any
function createSummaryRow(row: any) {
const indexPattern = $scope.indexPattern;
$scope.flattenedRow = indexPattern.flattenHit(row);
Expand Down Expand Up @@ -188,7 +191,7 @@ export function createTableRowDirective($compile: ng.ICompileService) {
const $cell = $cells.eq(i);
if ($cell.data('discover:html') === html) return;

const reuse = find($cells.slice(i + 1), function (cell: any) {
const reuse = find($cells.slice(i + 1), (cell) => {
return $.data(cell, 'discover:html') === html;
});

Expand Down Expand Up @@ -222,6 +225,7 @@ export function createTableRowDirective($compile: ng.ICompileService) {
/**
* Fill an element with the value of a field
*/
// eslint-disable-next-line @typescript-eslint/no-explicit-any
function _displayField(row: any, fieldName: string, truncate = false) {
const indexPattern = $scope.indexPattern;
const text = indexPattern.formatField(row, fieldName);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ export type AngularScope = IScope & { renderProps?: DocTableLegacyProps };
export async function injectAngularElement(
domNode: Element,
template: string,
// eslint-disable-next-line @typescript-eslint/no-explicit-any
renderProps: any,
injector: auto.IInjectorService
) {
Expand All @@ -64,6 +65,7 @@ export async function injectAngularElement(
return newScope;
}

// eslint-disable-next-line @typescript-eslint/no-explicit-any
function getRenderFn(domNode: Element, props: any) {
const directive = {
template: `<doc-table
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,17 @@
import html from './doc_table.html';
import { dispatchRenderComplete } from '../../../../../kibana_utils/public';
import { SAMPLE_SIZE_SETTING } from '../../../../common';
// @ts-ignore
// @ts-expect-error
import { getLimitedSearchResultsMessage } from './doc_table_strings';
import { getServices } from '../../../kibana_services';
import './index.scss';

export interface LazyScope extends ng.IScope {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
[key: string]: any;
}

// eslint-disable-next-line @typescript-eslint/no-explicit-any
export function createDocTableDirective(pagerFactory: any, $filter: any) {
return {
restrict: 'E',
Expand Down Expand Up @@ -63,7 +65,7 @@ export function createDocTableDirective(pagerFactory: any, $filter: any) {
$scope.limit = Math.max(minimumVisibleRows || 50, $scope.limit || 50);
});

$scope.$watch('hits', (hits: any) => {
$scope.$watch('hits', (hits: unknown[]) => {
if (!hits) return;

// Reset infinite scroll limit
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import $ from 'jquery';

interface LazyScope extends ng.IScope {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
[key: string]: any;
}

Expand All @@ -19,6 +20,7 @@ export function createInfiniteScrollDirective() {
more: '=',
},
link: ($scope: LazyScope, $element: JQuery) => {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
let checkTimer: any;
/**
* depending on which version of Discover is displayed, different elements are scrolling
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
*/

import { getDefaultSort } from './get_default_sort';
// @ts-ignore
// @ts-expect-error
import FixturesStubbedLogstashIndexPatternProvider from '../../../../__fixtures__/stubbed_logstash_index_pattern';
import { IndexPattern } from '../../../../kibana_services';

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

import { getSort, getSortArray } from './get_sort';
// @ts-ignore
// @ts-expect-error
import FixturesStubbedLogstashIndexPatternProvider from '../../../../__fixtures__/stubbed_logstash_index_pattern';
import { IndexPattern } from '../../../../kibana_services';

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

import { getSortForSearchSource } from './get_sort_for_search_source';
// @ts-ignore
// @ts-expect-error
import FixturesStubbedLogstashIndexPatternProvider from '../../../../__fixtures__/stubbed_logstash_index_pattern';
import { IndexPattern } from '../../../../kibana_services';
import { SortOrder } from '../components/table_header/helpers';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
* Side Public License, v 1.
*/

// @ts-ignore
// @ts-expect-error
import { Pager } from './pager';

export function createPagerFactory() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,10 @@
import React from 'react';
import { DocViewer } from '../components/doc_viewer/doc_viewer';

// eslint-disable-next-line @typescript-eslint/no-explicit-any
export function createDocViewerDirective(reactDirective: any) {
return reactDirective(
(props: any) => {
(props: React.ComponentProps<typeof DocViewer>) => {
return <DocViewer {...props} />;
},
[
Expand Down
Loading

0 comments on commit 349b7e9

Please sign in to comment.