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

Advanced Settings management => typescript #54477

Merged
merged 63 commits into from
Jan 23, 2020

Conversation

mattkime
Copy link
Contributor

@mattkime mattkime commented Jan 10, 2020

Summary

Conversion of advanced settings management to typescript.

Checklist

Use strikethroughs to remove checklist items you don't feel are applicable to this PR.

For maintainers

@mattkime mattkime requested a review from a team as a code owner January 10, 2020 16:57
Copy link
Contributor

@streamich streamich left a comment

Choose a reason for hiding this comment

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

Code LGTM, just a couple of comments about //@ts-ignore usage.

@@ -764,6 +774,7 @@ export class Field extends PureComponent {
helpText={this.renderHelpText(setting)}
describedByIds={[`${setting.name}-aria`]}
className="mgtAdvancedSettings__fieldRow"
// @ts-ignore
hasChildLabel={setting.type !== 'boolean'}
Copy link
Contributor

Choose a reason for hiding this comment

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

Can we cast here a variable to the required type, instead of using // @ts-ignore.

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 here due to incomplete eui typescript type defs.

Comment on lines 62 to 65
// @ts-ignore
if (!component.displayName) {
// @ts-ignore
component.displayName = id;
Copy link
Contributor

Choose a reason for hiding this comment

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

Instead of disabling TypeScript compiler for these lines, is it possible to cast here?

if (!(component as any).displayName) {

Copy link
Contributor

Choose a reason for hiding this comment

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

Why is this actually needed ? The property seems present

    interface FunctionComponent<P = {}> {
        (props: PropsWithChildren<P>, context?: any): ReactElement | null;
        propTypes?: WeakValidationMap<P>;
        contextTypes?: ValidationMap<any>;
        defaultProps?: Partial<P>;
        displayName?: string;
    }

Copy link
Contributor Author

@mattkime mattkime Jan 19, 2020

Choose a reason for hiding this comment

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

I removed the tests for this code as its just for debugging and it was giving me some trouble.

Copy link
Contributor

@streamich streamich left a comment

Choose a reason for hiding this comment

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

Code LGTM, just a couple of comments about // @ts-ignore usage.

@elasticmachine
Copy link
Contributor

Pinging @elastic/kibana-app-arch (Team:AppArch)

@mattkime mattkime changed the title Advanced Settings management => typescript Advanced Settings management => typescript - WIP Jan 10, 2020
@mattkime mattkime added the release_note:skip Skip the PR/issue when compiling release notes label Jan 10, 2020
@mattkime
Copy link
Contributor Author

@elasticmachine merge upstream

@mattkime mattkime requested a review from a team as a code owner January 14, 2020 05:07
Copy link
Contributor

@pgayvallet pgayvallet left a comment

Choose a reason for hiding this comment

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

Nothing seems to be impacting platform code, but saw it too late, so here's some NITs 😄

Comment on lines 22 to 23
const registry: { [key: string]: any } = {};
type Id = string;
Copy link
Contributor

@pgayvallet pgayvallet Jan 14, 2020

Choose a reason for hiding this comment

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

NIT: if Id aliases string, registry should probably be { [key: Id]: any } or even { [key: Id]: FunctionComponent }

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Addressed.

Comment on lines +25 to +26
const settingPartial = {
name: 'name',
Copy link
Contributor

Choose a reason for hiding this comment

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

Should this be explicitly typed?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I attempted doing this and it seemed like more work than its worth - the types are verified when the functions are called. But I might be misunderstanding something.

@mattkime mattkime requested a review from a team as a code owner January 15, 2020 17:25
import { UiSettingsType } from 'src/core/server/types';
import { FieldSetting } from '../types';

export function getValType(def: Partial<FieldSetting>, value?: any): UiSettingsType {
Copy link
Contributor Author

@mattkime mattkime Jan 22, 2020

Choose a reason for hiding this comment

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

Changing the return type from string to UiSettingsType is causing the current test failures.

There are a number of nonconforming def values being passed. whyyyyyyy

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Allowing this to return undefined seems to allow everything to work. Will take a look at master to verify this is preexisting behavior and not something I introduced.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Verified that this is behavior on master in this pr - #55551

Copy link
Contributor

@streamich streamich left a comment

Choose a reason for hiding this comment

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

LGTM. Only component types can be improved and would be nice to not use //@ts-ignore.

@@ -26,7 +29,7 @@ const registry = {};
* @param {*} id the id of the component to register
* @param {*} component the component
*/
export function tryRegisterSettingsComponent(id, component) {
export function tryRegisterSettingsComponent(id: Id, component: FunctionComponent) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
export function tryRegisterSettingsComponent(id: Id, component: FunctionComponent) {
export function tryRegisterSettingsComponent(id: Id, component: React.ComponentType) {

import { FunctionComponent } from 'react';

type Id = string;
const registry: Record<Id, FunctionComponent> = {};
Copy link
Contributor

@streamich streamich Jan 22, 2020

Choose a reason for hiding this comment

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

Suggested change
const registry: Record<Id, FunctionComponent> = {};
const registry: Record<Id, React.ComponentType> = {};

export function registerSettingsComponent(id, component, allowOverride = false) {
export function registerSettingsComponent(
id: Id,
component: FunctionComponent<Record<string, any>>,
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
component: FunctionComponent<Record<string, any>>,
component: ComponentType,

@@ -65,7 +72,7 @@ export function registerSettingsComponent(id, component, allowOverride = false)
*
* @param {*} id the ID of the component to retrieve
*/
export function getSettingsComponent(id) {
export function getSettingsComponent(id: Id): FunctionComponent<Record<string, any>> {
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
export function getSettingsComponent(id: Id): FunctionComponent<Record<string, any>> {
export function getSettingsComponent(id: Id): ComponentType {

@@ -263,6 +276,7 @@ describe('Field', () => {
/>
);
const select = findTestSubject(component, `advancedSetting-editField-${setting.name}`);
// @ts-ignore
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: Can we remove this and use casting, if needed?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'm happy to do this if you can show me which type is needed as I've been unable to find a good example.

Copy link
Contributor

Choose a reason for hiding this comment

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

const labels = select.find('option').map((option: any) => option.text());

or

const labels = select.find('option').map((option: { text: () => string[] }) => option.text());

Copy link
Contributor Author

@mattkime mattkime Jan 22, 2020

Choose a reason for hiding this comment

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

I'm a little confused about this. Is this useful aside from removing // @ts-ignore? Is a very incomplete type better than an ignore statement?

Copy link
Contributor

Choose a reason for hiding this comment

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

No, my comment was just about removing // @ts-ignore. It is just a nit.

@@ -280,13 +294,14 @@ describe('Field', () => {
/>
);
const select = findTestSubject(component, `advancedSetting-editField-${setting.name}`);
// @ts-ignore
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: Can we remove this and use casting, if needed?

Copy link
Contributor

@lizozom lizozom left a comment

Choose a reason for hiding this comment

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

Tested on Ubuntu \ Chrome.
I still see some minor comments that haven't been addressed, but overall LGTM

@@ -88,7 +88,16 @@ export interface DeprecationSettings {
* UI element type to represent the settings.
* @public
* */
export type UiSettingsType = 'json' | 'markdown' | 'number' | 'select' | 'boolean' | 'string';
export type UiSettingsType =
| 'undefined' // I don't know why malformed UiSettings objects exist
Copy link
Contributor

@lizozom lizozom Jan 22, 2020

Choose a reason for hiding this comment

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

This actually makes it unneeded, right?
https://github.com/elastic/kibana/pull/55551/files

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sorry, I should have been more clear - that PR will be deleted. It was to prove that the undefined UiSettingsType is preexisting and not a result of another change in this PR

export interface StringValidation {
export type StringValidation = StringValidationRegex | StringValidationRegexString;

export interface StringValidationRegex {
Copy link
Contributor

Choose a reason for hiding this comment

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

// nit why not create StringVlalidaiton as a compound type?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Are you asking why StringValidationRegex and StringValidationRegexString are individually exported? I do refer to them individually.

@kibanamachine
Copy link
Contributor

💚 Build Succeeded

History

To update your PR or re-run it, just comment with:
@elasticmachine merge upstream

@mattkime mattkime merged commit 0c25cb5 into elastic:master Jan 23, 2020
mattkime added a commit to mattkime/kibana that referenced this pull request Jan 23, 2020
mattkime added a commit that referenced this pull request Jan 23, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
release_note:skip Skip the PR/issue when compiling release notes v7.7.0 v8.0.0
Projects
None yet
Development

Successfully merging this pull request may close these issues.

8 participants