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

[OSCI][Enhancement] ts cleanup and precommit #930

Open
wants to merge 9 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
12 changes: 11 additions & 1 deletion .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,17 @@ module.exports = {
root: true,
extends: ["@elastic/eslint-config-kibana", "plugin:@elastic/eui/recommended"],
rules: {
// "@osd/eslint/require-license-header": "off"
// "@osd/eslint/require-license-header": "off",
"import/no-default-export": "off",
"@typescript-eslint/naming-convention": [
"error",
{
selector: "default",
format: ["camelCase", "UPPER_CASE", "PascalCase", "snake_case"],
leadingUnderscore: "allow",
trailingUnderscore: "allow",
},
],
},
overrides: [
{
Expand Down
8 changes: 7 additions & 1 deletion .lintstagedrc
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
{
"*.{ts,tsx,js,jsx,json,css,md}": ["prettier --write", "git add"]
Copy link
Member

Choose a reason for hiding this comment

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

Do we still need this git add here?

Copy link
Contributor Author

@kohinoor98 kohinoor98 Nov 15, 2023

Choose a reason for hiding this comment

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

For lint-staged v10 onwards, we do not need to add git add
We are using:

    "lint-staged": "^10.2.0",

It should be fine. Do you think I should add it as a fail-safe?

Copy link
Member

Choose a reason for hiding this comment

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

Good to know 👍

"*.{ts,tsx}": [
"npx eslint --fix",
"prettier --write"
],
"*.{js,jsx,json,css,md}": [
"prettier --write"
]
}
2 changes: 1 addition & 1 deletion cypress/support/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* SPDX-License-Identifier: Apache-2.0
*/

/// <reference types="cypress" />
// / <reference types="cypress" />

declare namespace Cypress {
interface Chainable<Subject> {
Expand Down
23 changes: 14 additions & 9 deletions models/interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,13 @@ export type DiffableMappingsPropertiesObject = Record<
}
>;

export type MappingsProperties = {
export type MappingsProperties = Array<{
fieldName: string;
type: string;
path?: string;
analyzer?: string;
properties?: MappingsProperties;
}[];
}>;

export interface IndexItem {
index: string;
Expand Down Expand Up @@ -525,14 +525,14 @@ export interface Transform {
enabled_at: number | null;
updated_at: number;
metadata_id: string | null;
aggregations: Map<String, any>;
aggregations: Map<string, any>;
page_size: number;
schedule: IntervalSchedule | CronSchedule;
schema_version: number;
source_index: string;
target_index: string;
roles: String[];
data_selection_query: Map<String, any>;
roles: string[];
data_selection_query: Map<string, any>;
}

export interface TransformMetadata {
Expand Down Expand Up @@ -571,15 +571,15 @@ export interface CronSchedule {
};
}

//Frontend dimension data model
// Frontend dimension data model
export interface DimensionItem {
sequence: number;
field: FieldItem;
aggregationMethod: string;
interval?: number;
}

//Frontend metric data model
// Frontend metric data model
export interface MetricItem {
source_field: FieldItem;
all: boolean;
Expand Down Expand Up @@ -620,18 +620,23 @@ interface HistogramItem {
};
}

//Backend dimension data model
// Backend dimension data model
export type RollupDimensionItem = DateHistogramItem | TermsItem | HistogramItem;

//Backend metric data model
// Backend metric data model
export interface RollupMetricItem {
source_field: string;
metrics: [
{
// eslint-disable-next-line @typescript-eslint/ban-types
min?: Object;
// eslint-disable-next-line @typescript-eslint/ban-types
max?: Object;
// eslint-disable-next-line @typescript-eslint/ban-types
sum?: Object;
// eslint-disable-next-line @typescript-eslint/ban-types
avg?: Object;
// eslint-disable-next-line @typescript-eslint/ban-types
value_count?: Object;
}
];
Expand Down
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
"osd": "node ../../scripts/osd",
"opensearch": "node ../../scripts/opensearch",
"lint": "node ../../scripts/eslint . && node ../../scripts/stylelint",
"eslint:fix": "yarn eslint:run --fix",
"eslint:run": "npx eslint '**/*.{ts,tsx}'",
"plugin-helpers": "node ../../scripts/plugin_helpers",
"test:jest": "../../node_modules/.bin/jest --config ./test/jest.config.js",
"build": "yarn plugin-helpers build",
Expand Down Expand Up @@ -73,4 +75,4 @@
"engines": {
"yarn": "^1.21.1"
}
}
}
4 changes: 2 additions & 2 deletions public/JobHandler/callbacks/force_merge.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
* SPDX-License-Identifier: Apache-2.0
*/

import { CoreSetup } from "opensearch-dashboards/public";
import { callbackForForceMerge, callbackForForceMergeTimeout } from "./force_merge";
import { coreServicesMock, httpClientMock } from "../../../test/mocks";
import { ListenType } from "../../lib/JobScheduler";
import { CoreSetup } from "opensearch-dashboards/public";

const getMockFn = (response = {}, ok = true) => {
return jest.fn().mockResolvedValue({
Expand All @@ -33,7 +33,7 @@ const core = ({
describe("callbackForForceMerge spec", () => {
it("callback when error", async () => {
httpClientMock.fetch = getMockFn({}, false);
let result = await callbackForForceMerge(forceMergeMetaData, {
const result = await callbackForForceMerge(forceMergeMetaData, {
core,
});
expect(result).toBe(false);
Expand Down
4 changes: 2 additions & 2 deletions public/JobHandler/callbacks/force_merge.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@ type ForceMergeTaskResult = TaskResult<{
successful: number;
total: number;
failed: number;
failures?: {
failures?: Array<{
index: string;
status: string;
shard: number;
}[];
}>;
};
}>;

Expand Down
4 changes: 2 additions & 2 deletions public/JobHandler/callbacks/open.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@
* Copyright OpenSearch Contributors
* SPDX-License-Identifier: Apache-2.0
*/
import { CoreSetup } from "opensearch-dashboards/public";
import { callbackForOpen, callbackForOpenTimeout } from "./open";
import { coreServicesMock, httpClientMock } from "../../../test/mocks";
import { ListenType } from "../../lib/JobScheduler";
import { CoreSetup } from "opensearch-dashboards/public";

const getMockFn = (response = {}, ok = true) => {
return jest.fn().mockResolvedValue({
Expand All @@ -32,7 +32,7 @@ const core = ({
describe("callbackForOpen spec", () => {
it("callback when error", async () => {
httpClientMock.fetch = getMockFn({}, false);
let result = await callbackForOpen(openMetaData, {
const result = await callbackForOpen(openMetaData, {
core,
});
expect(result).toBe(false);
Expand Down
1 change: 1 addition & 0 deletions public/JobHandler/callbacks/open.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
* Copyright OpenSearch Contributors
* SPDX-License-Identifier: Apache-2.0
*/

import React from "react";
import { CallbackType, TaskResult } from "../interface";
import { OpenJobMetaData } from "../../models/interfaces";
Expand Down
4 changes: 2 additions & 2 deletions public/JobHandler/callbacks/reindex.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@
* Copyright OpenSearch Contributors
* SPDX-License-Identifier: Apache-2.0
*/
import { CoreSetup } from "opensearch-dashboards/public";
import { callbackForReindex, callbackForReindexTimeout } from "./reindex";
import { coreServicesMock, httpClientMock } from "../../../test/mocks";
import { ListenType } from "../../lib/JobScheduler";
import { CoreSetup } from "opensearch-dashboards/public";

const getMockFn = (response = {}, ok = true) => {
return jest.fn().mockResolvedValue({
Expand Down Expand Up @@ -35,7 +35,7 @@ const core = ({
describe("callbackForOpen spec", () => {
it("callback when error", async () => {
httpClientMock.fetch = getMockFn({}, false);
let result = await callbackForReindex(reindexMetaData, {
const result = await callbackForReindex(reindexMetaData, {
core,
});
expect(result).toBe(false);
Expand Down
4 changes: 2 additions & 2 deletions public/JobHandler/callbacks/reindex.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,12 @@ import { ErrorToastContentForJob } from "../components/ErrorToastContentForJob";

type ReindexTaskResult = TaskResult<{
canceled?: string;
failures: {
failures: Array<{
cause?: {
reason: string;
};
id?: string;
}[];
}>;
}>;

export const callbackForReindex: CallbackType = async (job: ReindexJobMetaData, { core }) => {
Expand Down
4 changes: 2 additions & 2 deletions public/JobHandler/callbacks/shrink.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@
* Copyright OpenSearch Contributors
* SPDX-License-Identifier: Apache-2.0
*/
import { CoreSetup } from "opensearch-dashboards/public";
import { callbackForShrink, callbackForShrinkTimeout } from "./shrink";
import { coreServicesMock, httpClientMock } from "../../../test/mocks";
import { ListenType } from "../../lib/JobScheduler";
import { CoreSetup } from "opensearch-dashboards/public";

const getMockFn = (response = {}, ok = true) => {
return jest.fn().mockResolvedValue({
Expand Down Expand Up @@ -33,7 +33,7 @@ const core = ({
describe("callbackForOpen spec", () => {
it("callback when error", async () => {
httpClientMock.fetch = getMockFn({}, false);
let result = await callbackForShrink(shrinkMetaData, {
const result = await callbackForShrink(shrinkMetaData, {
core,
});
expect(result).toBe(false);
Expand Down
4 changes: 2 additions & 2 deletions public/JobHandler/callbacks/split.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@
* Copyright OpenSearch Contributors
* SPDX-License-Identifier: Apache-2.0
*/
import { CoreSetup } from "opensearch-dashboards/public";
import { callbackForSplit, callbackForSplitTimeout } from "./split";
import { coreServicesMock, httpClientMock } from "../../../test/mocks";
import { ListenType } from "../../lib/JobScheduler";
import { CoreSetup } from "opensearch-dashboards/public";

const getMockFn = (response = {}, ok = true) => {
return jest.fn().mockResolvedValue({
Expand Down Expand Up @@ -33,7 +33,7 @@ const core = ({
describe("callbackForOpen spec", () => {
it("callback when error", async () => {
httpClientMock.fetch = getMockFn({}, false);
let result = await callbackForSplit(splitMetaData, {
const result = await callbackForSplit(splitMetaData, {
core,
});
expect(result).toBe(false);
Expand Down
2 changes: 1 addition & 1 deletion public/JobHandler/components/components.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
*/
import React from "react";
import { render } from "@testing-library/react";
import userEvent from "@testing-library/user-event";
import { DetailLink } from "./DetailLink";
import { ErrorToastContentForJob } from "./ErrorToastContentForJob";
import userEvent from "@testing-library/user-event";
import { FormatResourceWithClusterInfo, FormatResourcesWithClusterInfo } from "./FormatResourceWithClusterInfo";

describe("<DetailLink /> spec", () => {
Expand Down
4 changes: 2 additions & 2 deletions public/JobHandler/interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ export type CallbackType = (
}
) => Promise<boolean>;

export type TaskResult<T = {}> = {
export interface TaskResult<T = {}> {
found: boolean;
_source: {
completed: boolean;
Expand All @@ -22,7 +22,7 @@ export type TaskResult<T = {}> = {
reason: string;
};
};
};
}

export type RecoveryTaskResult = TaskResult<{
acknowledged: boolean;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@

import React from "react";
import { fireEvent, render } from "@testing-library/react";
import AdvancedSettings from "./index";
import userEvent from "@testing-library/user-event";
import AdvancedSettings from "./index";

describe("<FormGenerator /> spec", () => {
it("render the component", () => {
Expand Down
5 changes: 4 additions & 1 deletion public/components/AdvancedSettings/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,11 @@ function AdvancedSettings<T>(props: IAdvancedSettingsProps<T>, ref: React.Ref<IA
parsedValue = editorProps.formatValue(parsedValue);
}
editorRef.current?.setValue(JSON.stringify(parsedValue, null, 2));
propsRef.current.onChange && propsRef.current.onChange(parsedValue);
if (propsRef.current.onChange) {
propsRef.current.onChange(parsedValue);
}
},
// eslint-disable-next-line react-hooks/exhaustive-deps
[props.onChange]
);

Expand Down
2 changes: 1 addition & 1 deletion public/components/AliasSelect/AliasSelect.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@

import React, { useState } from "react";
import { render, waitFor } from "@testing-library/react";
import AliasSelect, { AliasSelectProps } from "./index";
import userEvent from "@testing-library/user-event";
import AliasSelect, { AliasSelectProps } from "./index";

const onChangeMock = jest.fn();

Expand Down
8 changes: 4 additions & 4 deletions public/components/AliasSelect/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,13 @@ import { SYSTEM_ALIAS } from "../../../utils/constants";
export interface AliasSelectProps extends Omit<EuiComboBoxProps<{ label: string; value: string }>, "value" | "onChange"> {
value?: Record<string, {}>;
onChange?: (value: AliasSelectProps["value"]) => void;
refreshOptions: (aliasName: string) => Promise<ServerResponse<{ alias: string; index: string; [key: string]: any }[]>>;
refreshOptions: (aliasName: string) => Promise<ServerResponse<Array<{ alias: string; index: string; [key: string]: any }>>>;
onOptionsChange?: RemoteSelectProps["onOptionsChange"];
}

const AliasSelect = forwardRef((props: AliasSelectProps, ref: React.Ref<HTMLInputElement>) => {
const { value, onChange, refreshOptions: refreshOptionsFromProps, onOptionsChange } = props;
const optionsRef = useRef<{ label: string; [key: string]: any }[]>([]);
const optionsRef = useRef<Array<{ label: string; [key: string]: any }>>([]);
const refreshOptions: RemoteSelectProps["refreshOptions"] = ({ searchValue }) => {
return refreshOptionsFromProps(searchValue || "").then((res) => {
if (res?.ok) {
Expand Down Expand Up @@ -51,18 +51,18 @@ const AliasSelect = forwardRef((props: AliasSelectProps, ref: React.Ref<HTMLInpu
refreshOptions={refreshOptions}
value={Object.keys(value || {})}
onChange={(val) => {
onChange &&
if (onChange) {
onChange(
val
.map((label) => optionsRef.current.find((item) => item.label === label) || { label })
.reduce((total, { label, ...others }) => ({ ...total, [label]: others || {} }), {})
);
}
}}
/>
);
});

AliasSelect.displayName = "AliasSelect";

// @ts-ignore
export default AliasSelect;
4 changes: 2 additions & 2 deletions public/components/BottomBar/BottomBar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
import React, { useEffect, useRef } from "react";
import { EuiBottomBar } from "@elastic/eui";

export type CustomFormRowProps = {
export interface CustomFormRowProps {
children?: React.ReactChild;
};
}

export default function BottomBar(props: CustomFormRowProps) {
const bottomBarRef = useRef(null);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
import React from "react";
import "@testing-library/jest-dom/extend-expect";
import { render } from "@testing-library/react";
import ChannelNotification from "./ChannelNotification";
import { fireEvent, queryByTestId } from "@testing-library/dom";
import ChannelNotification from "./ChannelNotification";

describe("<ChannelNotification /> spec", () => {
it("renders the component", () => {
Expand Down
Loading
Loading