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

[Cases] Align cases lint rules with security solution #117177

Merged
merged 4 commits into from
Nov 4, 2021

Conversation

cnasikas
Copy link
Member

@cnasikas cnasikas commented Nov 2, 2021

Summary

This PR aligns cases eslint rules with security solution eslint rules.

For maintainers

@cnasikas cnasikas added technical debt Improvement of the software architecture and operational architecture release_note:skip Skip the PR/issue when compiling release notes v8.1.0 Team:Threat Hunting:Cases labels Nov 2, 2021
@cnasikas cnasikas self-assigned this Nov 2, 2021
@elasticmachine
Copy link
Contributor

Pinging @elastic/security-threat-hunting-cases (Team:Threat Hunting:Cases)

@cnasikas cnasikas requested a review from a team November 2, 2021 17:24
@cnasikas cnasikas requested a review from a team as a code owner November 3, 2021 12:22
@cnasikas
Copy link
Member Author

cnasikas commented Nov 3, 2021

@elasticmachine merge upstream

@@ -5,6 +5,8 @@
* 2.0.
*/

/* eslint-disable react/display-name */
Copy link
Member Author

Choose a reason for hiding this comment

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

Disabling as it is a mock file

import styled from 'styled-components';
import { Case, SubCase } from '../../containers/types';
import { CasesColumns } from './columns';
import { AssociationType } from '../../../common';

type ExpandedRowMap = Record<string, Element> | {};

const EuiBasicTable: any = _EuiBasicTable;
// @ts-expect-error TS2769
Copy link
Member Author

Choose a reason for hiding this comment

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

I could disable the linting rule about any but I found it better to use @ts-expect-error because we will be warned if it will be fixed and we do not have to declare _EuiBasicTable (better readability)

(i, key) => i.name != null && !i.hasOwnProperty('actions') && checkIt(`${i.name}`, key)
(i, key) =>
i.name != null &&
!Object.prototype.hasOwnProperty.call(i, 'actions') &&
Copy link
Member Author

@cnasikas cnasikas Nov 4, 2021

Choose a reason for hiding this comment

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

Replace hasOwnProperty with Object.prototype.hasOwnProperty.call

@@ -378,7 +381,9 @@ describe('AllCasesGeneric', () => {
})
);
await waitFor(() => {
result.current.map((i) => i.name != null && !i.hasOwnProperty('actions'));
result.current.map(
(i) => i.name != null && !Object.prototype.hasOwnProperty.call(i, 'actions')
Copy link
Member Author

Choose a reason for hiding this comment

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

Replace hasOwnProperty with Object.prototype.hasOwnProperty.call

@@ -60,7 +60,7 @@ export const decodeOrThrow =
const getExcessProps = (props: rt.Props, r: Record<string, unknown>): string[] => {
const ex: string[] = [];
for (const k of Object.keys(r)) {
if (!props.hasOwnProperty(k)) {
if (!Object.prototype.hasOwnProperty.call(props, k)) {
Copy link
Member Author

Choose a reason for hiding this comment

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

Replace hasOwnProperty with Object.prototype.hasOwnProperty.call

Copy link
Contributor

@semd semd Nov 4, 2021

Choose a reason for hiding this comment

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

Is this check really needed? I believe Object.keys() does not return inherited properties.
EDIT: now I see we are checking props, not r. 👍

@@ -9,8 +9,25 @@ import { i18n } from '@kbn/i18n';
import { CaseConnector, CaseConnectorsRegistry } from './types';

export const createCaseConnectorsRegistry = (): CaseConnectorsRegistry => {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
Copy link
Member Author

Choose a reason for hiding this comment

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

TS won this battle

}
return connectors.get(id)!;
const connector = connectors.get(id);
assertConnectorExists(connector, id);
Copy link
Member Author

Choose a reason for hiding this comment

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

Move the logic to an assertion so I can return the connector without having to put ! at the end of the return statement.

const fullName = currentSavedObjectMetaData.getTooltipForSavedObject
? currentSavedObjectMetaData.getTooltipForSavedObject(item.savedObject)
: `${item.title} (${currentSavedObjectMetaData!.name})`;
: `${item.title} (${currentSavedObjectMetaData.name})`;
Copy link
Member Author

Choose a reason for hiding this comment

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

Remove the ! non-null assertion operator. The non-null check is done above

@@ -112,6 +114,7 @@ export const useLensButtonToggle = ({
) {
if (child.type === 'text') break outer; // don't dive into `text` nodes
node = child;
// eslint-disable-next-line no-continue
Copy link
Member Author

Choose a reason for hiding this comment

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

Trying to fix it may introduce bugs.

@@ -5,6 +5,8 @@
* 2.0.
*/

/* eslint-disable @typescript-eslint/no-non-null-assertion */
Copy link
Member Author

Choose a reason for hiding this comment

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

Trying to fix it may introduce bugs.

@@ -16,16 +16,25 @@ interface Props {
disableLinks?: boolean;
}

const withDisabledLinks = (disableLinks?: boolean): React.FC<EuiLinkAnchorProps> => {
Copy link
Member Author

Choose a reason for hiding this comment

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

Create a HOC that returns a MarkdownLink component with disableLinks so we can declare a displayName.

@@ -165,7 +176,9 @@ export const UserActionTree = React.memo(
const { isLoadingIds, patchComment } = useUpdateComment();
const currentUser = useCurrentUser();
const [manageMarkdownEditIds, setManageMarkdownEditIds] = useState<string[]>([]);
const commentRefs = useRef<Record<string, any>>({});
const commentRefs = useRef<
Copy link
Member Author

Choose a reason for hiding this comment

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

Typed refs

@@ -129,6 +129,17 @@ const MyEuiCommentList = styled(EuiCommentList)`
const DESCRIPTION_ID = 'description';
const NEW_ID = 'newComment';

const isAddCommentRef = (
Copy link
Member Author

Choose a reason for hiding this comment

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

Checks if the ref is of type AddCommentRefObject

commentRefs.current &&
commentRefs.current[draftComment.commentId] &&
commentRefs.current[draftComment.commentId].editor?.textarea &&
commentRefs.current[draftComment.commentId].editor?.toolbar
Copy link
Member Author

@cnasikas cnasikas Nov 4, 2021

Choose a reason for hiding this comment

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

toolbar is not part of the editor object. Nevertheless, openLensModal function checks for its existence.

const owner = requiredPrivileges.get(privilege)!;
authorizedOwners.push(owner);
const owner = requiredPrivileges.get(privilege);
if (owner) {
Copy link
Member Author

Choose a reason for hiding this comment

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

Remove the ! non-null assertion operation and check if is empty before pushing.

@@ -83,6 +83,7 @@ const SwimlaneFieldsSchema = schema.object({

const NoneFieldsSchema = schema.nullable(schema.object({}));

// eslint-disable-next-line @typescript-eslint/no-explicit-any
Copy link
Member Author

Choose a reason for hiding this comment

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

Lost this battle with TS again

if (field && !manageDuplicate[alertFieldMapping[alertField].sirFieldKey].has(field)) {
manageDuplicate[alertFieldMapping[alertField].sirFieldKey].add(field);
acc = {
Copy link
Member Author

Choose a reason for hiding this comment

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

Fix for the linting rule prohibiting the direct assignment of function arguments

@@ -8,10 +8,14 @@
import { Boom, isBoom } from '@hapi/boom';
import { Logger } from 'src/core/server';

export interface HTTPError extends Error {
Copy link
Member Author

Choose a reason for hiding this comment

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

Used in x-pack/plugins/cases/server/routes/api/utils.ts

export function wrapError(error: any): CustomHttpResponseOptions<ResponseError> {

export function wrapError(
error: CaseError | Boom | HTTPError | Error
Copy link
Member Author

Choose a reason for hiding this comment

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

Type error instead of any

@@ -4,7 +4,11 @@
* 2.0; you may not use this file except in compliance with the Elastic License
* 2.0.
*/

/* eslint-disable @typescript-eslint/no-explicit-any */
Copy link
Member Author

Choose a reason for hiding this comment

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

As it is a script we do not care about TS errors

Copy link
Contributor

@semd semd left a comment

Choose a reason for hiding this comment

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

LGTM! despite a couple of lost battles against TS (LOL), it is a big improvement.
Thanks Christos, this will help us all.

@kibanamachine
Copy link
Contributor

💚 Build Succeeded

Metrics [docs]

Async chunks

Total size of all lazy-loaded chunks that will be downloaded as the user navigates the app

id before after diff
cases 303.9KB 304.2KB +336.0B

Page load bundle

Size of the bundles that are downloaded on every page load. Target size is below 100kb

id before after diff
cases 80.4KB 80.4KB +53.0B

History

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

cc @cnasikas

@cnasikas cnasikas added the auto-backport Deprecated - use backport:version if exact versions are needed label Nov 4, 2021
@cnasikas cnasikas merged commit d38fb03 into elastic:main Nov 4, 2021
@cnasikas cnasikas deleted the cases_lint branch November 4, 2021 15:51
@kibanamachine
Copy link
Contributor

💔 Backport failed

The backport operation could not be completed due to the following error:
There are no branches to backport to. Aborting.

The backport PRs will be merged automatically after passing CI.

To backport manually run:
node scripts/backport --pr 117177

@cnasikas cnasikas removed the auto-backport Deprecated - use backport:version if exact versions are needed label Nov 4, 2021
@kibanamachine kibanamachine added the backport missing Added to PRs automatically when the are determined to be missing a backport. label Nov 8, 2021
@kibanamachine
Copy link
Contributor

Friendly reminder: Looks like this PR hasn’t been backported yet.
To create backports run node scripts/backport --pr 117177 or prevent reminders by adding the backport:skip label.

@kibanamachine
Copy link
Contributor

Friendly reminder: Looks like this PR hasn’t been backported yet.
To create backports run node scripts/backport --pr 117177 or prevent reminders by adding the backport:skip label.

4 similar comments
@kibanamachine
Copy link
Contributor

Friendly reminder: Looks like this PR hasn’t been backported yet.
To create backports run node scripts/backport --pr 117177 or prevent reminders by adding the backport:skip label.

@kibanamachine
Copy link
Contributor

Friendly reminder: Looks like this PR hasn’t been backported yet.
To create backports run node scripts/backport --pr 117177 or prevent reminders by adding the backport:skip label.

@kibanamachine
Copy link
Contributor

Friendly reminder: Looks like this PR hasn’t been backported yet.
To create backports run node scripts/backport --pr 117177 or prevent reminders by adding the backport:skip label.

@kibanamachine
Copy link
Contributor

Friendly reminder: Looks like this PR hasn’t been backported yet.
To create backports run node scripts/backport --pr 117177 or prevent reminders by adding the backport:skip label.

@cnasikas cnasikas added backport:skip This commit does not require backporting and removed backport missing Added to PRs automatically when the are determined to be missing a backport. labels Nov 16, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
backport:skip This commit does not require backporting release_note:skip Skip the PR/issue when compiling release notes technical debt Improvement of the software architecture and operational architecture v8.1.0
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants