Skip to content

Commit

Permalink
chore: cleanup types (elastic#1404)
Browse files Browse the repository at this point in the history
- remove unnecessary `// @ts-ignore` comments
- add descriptions to legit `// @ts-ignore` comments
- add [rule](https://github.com/typescript-eslint/typescript-eslint/blob/v4.31.1/packages/eslint-plugin/docs/rules/ban-ts-comment.md) to error on all ts-comments with no description
- remove `global.d.ts` in favor of `declarations/`
- fix type issue related to `jest-extended` and custom matchers
- fix types around `sortSeriesBy` hidden setting
- fix type errors around required `domain` options
- fix type errors around union brush events
- remove deprecated [`msSaveBlob`](https://developer.mozilla.org/en-US/docs/Web/API/Navigator/msSaveBlob) usage
- add step to ci to check `yarn typecheck:all` as type errors somehow snuck into `master`
  • Loading branch information
nickofthyme authored Sep 23, 2021
1 parent 6373a60 commit ffe45c8
Show file tree
Hide file tree
Showing 61 changed files with 222 additions and 151 deletions.
2 changes: 0 additions & 2 deletions .ci/global_setup.sh
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,6 @@ yarn config set yarn-offline-mirror "$cacheDir/yarn-offline-cache"
yarnGlobalDir="$(yarn global bin)"
export PATH="$PATH:$yarnGlobalDir"

# avoid download puppeteer locally (we use the dockerized version)
export PUPPETEER_SKIP_CHROMIUM_DOWNLOAD=true
###
### install dependencies
###
Expand Down
11 changes: 10 additions & 1 deletion .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,16 @@ module.exports = {
'@typescript-eslint/ban-ts-ignore': 0,
'@typescript-eslint/indent': 0,
'@typescript-eslint/no-inferrable-types': 0,
'@typescript-eslint/ban-ts-comment': 1,
'@typescript-eslint/ban-ts-comment': [
2,
{
'ts-expect-error': 'allow-with-description',
'ts-ignore': 'allow-with-description',
'ts-nocheck': 'allow-with-description',
'ts-check': 'allow-with-description',
minimumDescriptionLength: 3,
},
],
'@typescript-eslint/no-unused-vars': [
'error',
{
Expand Down
14 changes: 2 additions & 12 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,6 @@ jobs:
uses: bahmutov/npm-install@v1
with:
useRollingCache: true
env:
PUPPETEER_SKIP_CHROMIUM_DOWNLOAD: true

eslint:
name: Eslint
Expand All @@ -71,8 +69,6 @@ jobs:
uses: bahmutov/npm-install@v1
with:
useRollingCache: true
env:
PUPPETEER_SKIP_CHROMIUM_DOWNLOAD: true
# -----------------------------------

- name: Eslint check
Expand All @@ -96,8 +92,6 @@ jobs:
uses: bahmutov/npm-install@v1
with:
useRollingCache: true
env:
PUPPETEER_SKIP_CHROMIUM_DOWNLOAD: true
# -----------------------------------

- name: Prettier check
Expand All @@ -121,10 +115,10 @@ jobs:
uses: bahmutov/npm-install@v1
with:
useRollingCache: true
env:
PUPPETEER_SKIP_CHROMIUM_DOWNLOAD: true
# -----------------------------------

- name: Run global typecheck
run: yarn typecheck:all
- name: Run API-Extractor
run: yarn api:check
- name: Handle API-Extractor failure
Expand Down Expand Up @@ -154,8 +148,6 @@ jobs:
uses: bahmutov/npm-install@v1
with:
useRollingCache: true
env:
PUPPETEER_SKIP_CHROMIUM_DOWNLOAD: true
# -----------------------------------

- name: TimeZone tests
Expand All @@ -182,8 +174,6 @@ jobs:
uses: bahmutov/npm-install@v1
with:
useRollingCache: true
env:
PUPPETEER_SKIP_CHROMIUM_DOWNLOAD: true
# -----------------------------------

- name: Building storybook
Expand Down
4 changes: 0 additions & 4 deletions .github/workflows/release.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,6 @@ jobs:
uses: bahmutov/npm-install@v1
with:
useRollingCache: true
env:
PUPPETEER_SKIP_CHROMIUM_DOWNLOAD: true
- name: Lint check
run: yarn lint
- name: Prettier check
Expand Down Expand Up @@ -91,8 +89,6 @@ jobs:
uses: bahmutov/npm-install@v1
with:
useRollingCache: true
env:
PUPPETEER_SKIP_CHROMIUM_DOWNLOAD: true
- name: Build library
run: yarn build

Expand Down
33 changes: 33 additions & 0 deletions declarations/@types/jest.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0 and the Server Side Public License, v 1; you may not use this file except
* in compliance with, at your election, the Elastic License 2.0 or the Server
* Side Public License, v 1.
*/

export * from 'jest-extended'; // https://github.com/jest-community/jest-extended

/**
* Final Matcher type with `this` and `received` args removed from jest matcher function
*/
type MatcherParameters<T extends (this: any, received: any, ...args: any[]) => any> = T extends (
this: any,
received: any,
...args: infer P
) => any
? P
: never;

declare global {
namespace jest {
interface Matchers<R> {
/**
* Expect array to be filled with value, and optionally length
*/
toEqualArrayOf(...args: MatcherParameters<typeof toEqualArrayOf>): R;
}
}
}

export {}; // ensure this is parsed as a module.
5 changes: 3 additions & 2 deletions global.d.ts → declarations/@types/node.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@
* Side Public License, v 1.
*/

import 'jest-extended'; // https://github.com/jest-community/jest-extended

// Super flaky not sure why
declare global {
namespace NodeJS {
interface ProcessEnv {
Expand Down Expand Up @@ -56,3 +55,5 @@ declare global {
}
}
}

export {}; // ensure this is parsed as a module.
2 changes: 1 addition & 1 deletion integration/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import { getStorybook, configure } from '@storybook/react';

import { Rotation } from '../packages/charts/src';
import { ThemeId } from '../storybook/use_base_theme';
// @ts-ignore
// @ts-ignore - no type declarations
import { isLegacyVRTServer } from './config';

export type StoryInfo = [string, string, number];
Expand Down
2 changes: 1 addition & 1 deletion integration/jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ const tsPreset = require('ts-jest/jest-preset');
const { debug } = require('./config');

module.exports = {
setupFilesAfterEnv: ['<rootDir>/jest_env_setup.ts'],
setupFilesAfterEnv: ['<rootDir>/jest_env_setup.ts', 'jest-extended'],
globals: {
'ts-jest': {
tsconfig: '<rootDir>/tsconfig.json',
Expand Down
5 changes: 2 additions & 3 deletions integration/page_objects/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,13 @@ import Url from 'url';
import { AXNode } from 'puppeteer';

import { DRAG_DETECTION_TIMEOUT } from '../../packages/charts/src/state/reducers/interactions';
// @ts-ignore
// @ts-ignore - no type declarations
import { port, hostname, debug, isLegacyVRTServer } from '../config';
import { toMatchImageSnapshot } from '../jest_env_setup';

const legacyBaseUrl = `http://${hostname}:${port}/iframe.html`;

// Use to log console statements from within the page.evaluate blocks
// @ts-ignore
// @ts-ignore - used to log console statements from within the page.evaluate blocks
// page.on('console', (msg) => (msg._type === 'log' ? console.log('PAGE LOG:', msg._text) : null)); // eslint-disable-line no-console

expect.extend({ toMatchImageSnapshot });
Expand Down
10 changes: 5 additions & 5 deletions integration/server/mocks/@storybook/addon-knobs/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,14 +45,14 @@ export function text(name: string, dftValue: string, groupId?: string) {

export function array(name: string, dftValues: unknown[], options: any, groupId?: string) {
const params = getParams();
const values = [];
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
for (const [key, value] of params) {
const values: string[] = [];

params.forEach((value, key) => {
if (key.startsWith(`${getKnobKey(name, groupId)}[`)) {
values.push(value);
}
}
});

if (values.length === 0) {
return dftValues;
}
Expand Down
2 changes: 1 addition & 1 deletion integration/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{
"extends": "../tsconfig",
"include": ["./**/*", "../**/*.d.ts", "../scripts/custom_matchers.ts"]
"include": ["./**/*", "../scripts/custom_matchers.mock.ts"]
}
6 changes: 5 additions & 1 deletion jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,11 @@ module.exports = {
testMatch: ['**/?(*.)+(test).[jt]s?(x)'],
roots: ['<rootDir>/packages/charts/src'],
preset: 'ts-jest',
setupFilesAfterEnv: ['<rootDir>/scripts/setup_enzyme.ts', '<rootDir>/scripts/custom_matchers.ts'],
setupFilesAfterEnv: [
'<rootDir>/scripts/setup_enzyme.ts',
'<rootDir>/scripts/custom_matchers.mock.ts',
'jest-extended',
],
coveragePathIgnorePatterns: [
'<rootDir>/packages/charts/src/mocks',
'<rootDir>/packages/charts/src/utils/d3-delaunay',
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-ignore - remove in workcloud refactor
import d3TagCloud from 'd3-cloud';
import React from 'react';
import { connect } from 'react-redux';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,8 @@
* Side Public License, v 1.
*/

// eslint-disable-next-line eslint-comments/disable-enable-pair
/* eslint-disable jest/no-conditional-expect */

import 'jest-extended';
import React from 'react';
import { Store } from 'redux';

Expand Down Expand Up @@ -1356,3 +1354,5 @@ describe('Clickable annotations', () => {
expect(onAnnotationClick).toBeCalled();
});
});

/* eslint-enable jest/no-conditional-expect */
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ export const computeLegendSelector = createCustomCachedSelector(
siDataSeriesMap,
deselectedDataSeries,
chartTheme,
// @ts-ignore
// @ts-ignore - hidden method for vislib usage only
settings.sortSeriesBy,
);
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ export const computeSeriesDomainsSelector = createCustomCachedSelector(
deselectedDataSeries,
settingsSpec.orderOrdinalBinsBy,
smallMultiples,
// @ts-ignore
// @ts-ignore - hidden method for vislib usage only
settingsSpec.sortSeriesBy,
);
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,8 @@
*/

import { DateTime } from 'luxon';
// @ts-ignore
import moment from 'moment-timezone';

import 'jest-extended';
import { ChartType } from '../..';
import { MockGlobalSpec, MockSeriesSpec } from '../../../mocks/specs/specs';
import { MockStore } from '../../../mocks/store/store';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -504,13 +504,13 @@ describe('Fit Function', () => {
describe('sorting', () => {
const spies: jest.SpyInstance[] = [];
const mockArray: any[] = [];
// @ts-ignore
// @ts-ignore - mocking array prototype method
jest.spyOn(mockArray, 'sort');

beforeAll(() => {
// @ts-ignore
// @ts-ignore - mocking array prototype method
spies.push(jest.spyOn(dataSeries.data, 'sort'));
// @ts-ignore
// @ts-ignore - mocking array prototype method
spies.push(jest.spyOn(dataSeries.data, 'slice').mockReturnValue(mockArray));
});

Expand Down
2 changes: 1 addition & 1 deletion packages/charts/src/components/portal/tooltip_portal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ const TooltipPortalComponent = ({
*/
const popper = useRef<Instance | null>(null);
const popperSettings = useMemo(
// @ts-ignore
// @ts-ignore - nesting limitation
() => mergePartial(DEFAULT_POPPER_SETTINGS, settings, { mergeOptionalPartialValues: true }),
[settings],
);
Expand Down
2 changes: 1 addition & 1 deletion packages/charts/src/mocks/specs/specs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -343,7 +343,7 @@ export class MockGlobalSpec {
};

static settings(partial?: Partial<SettingsSpec>): SettingsSpec {
// @ts-ignore
// @ts-ignore - nesting limitation
return mergePartial<SettingsSpec>(MockGlobalSpec.settingsBase, partial, { mergeOptionalPartialValues: true });
}

Expand Down
2 changes: 1 addition & 1 deletion packages/charts/src/scales/scale_continuous.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -498,7 +498,7 @@ describe('Scale Continuous', () => {
});

it('should throw for NaN values', () => {
// @ts-ignore
// @ts-ignore - d3Scale method
jest.spyOn(scale, 'd3Scale').mockImplementationOnce(() => NaN);
expect(() => scale.scaleOrThrow(1)).toThrow();
});
Expand Down
4 changes: 2 additions & 2 deletions packages/charts/src/utils/common.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,14 +77,14 @@ describe('common utilities', () => {
});

it('should return second partial if partial is undefined', () => {
// @ts-ignore
// @ts-ignore - nesting limitation
const result = getPartialValue(base, undefined, [undefined, partial]);

expect(result).toBe(partial);
});

it('should return base if no partials are defined', () => {
// @ts-ignore
// @ts-ignore - nesting limitation
const result = getPartialValue(base, undefined, [undefined, undefined]);

expect(result).toBe(base);
Expand Down
6 changes: 3 additions & 3 deletions packages/charts/src/utils/fast_deep_equal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,16 +51,16 @@ export function deepEqual(a: any, b: any, partial = false): boolean {
}
if (a instanceof Map && b instanceof Map) {
if (a.size !== b.size) return false;
// @ts-ignore
// @ts-ignore - 3rd party code
for (i of a.entries()) if (!b.has(i[0])) return false;
// @ts-ignore
// @ts-ignore - 3rd party code
for (i of a.entries()) if (!deepEqual(i[1], b.get(i[0]))) return false;
return true;
}

if (a instanceof Set && b instanceof Set) {
if (a.size !== b.size) return false;
// @ts-ignore
// @ts-ignore - 3rd party code
for (i of a.entries()) if (!b.has(i[0])) return false;
return true;
}
Expand Down
Loading

0 comments on commit ffe45c8

Please sign in to comment.