From 1cfad73abd41f7f2c6ac27b733955112d90d1d4c Mon Sep 17 00:00:00 2001 From: Frank Hassanabad Date: Tue, 28 May 2019 11:14:17 -0600 Subject: [PATCH] [SIEM] Low impact linter rules to start with (#37137) ## Summary Low impact linter rules to help catch bugs for back-porting as well as moving into the future. We will slowly turn the warns into errors and fix the errors as we go along. This helps us avoid a "big bang" switch over approach to avoid back-port issues as we are porting bugs aggressively at the beginning. Added a custom pattern detection rule for determining if the UI is trying to import anything from the server backend since that would cause web-pack front to pull in backend code. Also forbid the use of NodeJS imports on the front end. Double checked that I was not duplicating rules from: * https://github.com/elastic/kibana/blob/master/packages/eslint-config-kibana/typescript.js * https://github.com/elastic/kibana/blob/master/packages/eslint-config-kibana/javascript.js ### Checklist Use ~~strikethroughs~~ to remove checklist items you don't feel are applicable to this PR. ~- [ ] This was checked for cross-browser compatibility, [including a check against IE11](https://github.com/elastic/kibana/blob/master/CONTRIBUTING.md#cross-browser-compatibility)~ ~- [ ] Any text added follows [EUI's writing guidelines](https://elastic.github.io/eui/#/guidelines/writing), uses sentence case text and includes [i18n support](https://github.com/elastic/kibana/blob/master/packages/kbn-i18n/README.md)~ ~- [ ] [Documentation](https://github.com/elastic/kibana/blob/master/CONTRIBUTING.md#writing-documentation) was added for features that require explanation or tutorials~ ~- [ ] [Unit or functional tests](https://github.com/elastic/kibana/blob/master/CONTRIBUTING.md#cross-browser-compatibility) were updated or added to match the most common scenarios~ ~- [ ] This was checked for [keyboard-only and screenreader accessibility](https://developer.mozilla.org/en-US/docs/Learn/Tools_and_testing/Cross_browser_testing/Accessibility#Accessibility_testing_checklist)~ ### For maintainers ~- [ ] This was checked for breaking API changes and was [labeled appropriately](https://github.com/elastic/kibana/blob/master/CONTRIBUTING.md#release-notes-process)~ ~- [ ] This includes a feature addition or change that requires a release note and was [labeled appropriately](https://github.com/elastic/kibana/blob/master/CONTRIBUTING.md#release-notes-process)~ --- .eslintrc.js | 179 +++++++++++++++++- .../autocomplete_field/index.test.tsx | 3 +- .../components/open_timeline/index.test.tsx | 4 +- .../open_timeline_modal/index.test.tsx | 4 +- .../public/components/url_state/index.tsx | 4 + 5 files changed, 189 insertions(+), 5 deletions(-) diff --git a/.eslintrc.js b/.eslintrc.js index 956259b1edfe5..885397a37d563 100644 --- a/.eslintrc.js +++ b/.eslintrc.js @@ -400,10 +400,185 @@ module.exports = { * SIEM overrides */ { - files: ['x-pack/plugins/siem/**/*.ts'], + // front end typescript and javascript files only + files: ['x-pack/plugins/siem/public/**/*.{js,ts,tsx}'], rules: { + 'import/no-nodejs-modules': 'error', + 'no-restricted-imports': [ + 'error', + { + // prevents UI code from importing server side code and then webpack including it when doing builds + patterns: ['**/server/*'], + }, + ], + }, + }, + { + // typescript only for front and back end + files: ['x-pack/plugins/siem/**/*.{ts,tsx}'], + rules: { + // This will be turned on after bug fixes are complete + // '@typescript-eslint/explicit-member-accessibility': 'warn', + '@typescript-eslint/no-this-alias': 'error', '@typescript-eslint/no-explicit-any': 'error', - 'import/order': 'error', + '@typescript-eslint/no-useless-constructor': 'error', + // This will be turned on after bug fixes are complete + // '@typescript-eslint/no-object-literal-type-assertion': 'warn', + '@typescript-eslint/unified-signatures': 'error', + + // eventually we want this to be a warn and then an error since this is a recommended linter rule + // for now, keeping it commented out to avoid too much IDE noise until the other linter issues + // are fixed in the next release or two + // '@typescript-eslint/explicit-function-return-type': 'warn', + + // these rules cannot be turned on and tested at the moment until this issue is resolved: + // https://github.com/prettier/prettier-eslint/issues/201 + // '@typescript-eslint/await-thenable': 'error', + // '@typescript-eslint/no-non-null-assertion': 'error' + // '@typescript-eslint/no-unnecessary-type-assertion': 'error', + // '@typescript-eslint/no-unused-vars': 'error', + // '@typescript-eslint/prefer-includes': 'error', + // '@typescript-eslint/prefer-string-starts-ends-with': 'error', + // '@typescript-eslint/promise-function-async': 'error', + // '@typescript-eslint/prefer-regexp-exec': 'error', + // '@typescript-eslint/promise-function-async': 'error', + // '@typescript-eslint/require-array-sort-compare': 'error', + // '@typescript-eslint/restrict-plus-operands': 'error', + // '@typescript-eslint/unbound-method': 'error', + }, + }, + { + // typescript and javascript for front and back end + files: ['x-pack/plugins/siem/**/*.{js,ts,tsx}'], + plugins: ['react'], + rules: { + 'accessor-pairs': 'error', + 'array-callback-return': 'error', + 'no-array-constructor': 'error', + // This will be turned on after bug fixes are mostly completed + // 'arrow-body-style': ['warn', 'as-needed'], + complexity: 'warn', + // This will be turned on after bug fixes are mostly completed + // 'consistent-return': 'warn', + // This will be turned on after bug fixes are mostly completed + // 'func-style': ['warn', 'expression'], + // These will be turned on after bug fixes are mostly completed and we can + // run a fix-lint + /* + 'import/order': [ + 'warn', + { + groups: ['builtin', 'external', 'internal', 'parent', 'sibling', 'index'], + 'newlines-between': 'always', + }, + ], + */ + 'no-bitwise': 'error', + 'no-continue': 'error', + 'no-dupe-keys': 'error', + 'no-duplicate-case': 'error', + // This will be turned on after bug fixes are mostly completed + // 'no-duplicate-imports': 'warn', + 'no-empty-character-class': 'error', + 'no-empty-pattern': 'error', + 'no-ex-assign': 'error', + 'no-extend-native': 'error', + 'no-extra-bind': 'error', + 'no-extra-boolean-cast': 'error', + 'no-extra-label': 'error', + 'no-floating-decimal': 'error', + 'no-func-assign': 'error', + 'no-implicit-globals': 'error', + 'no-implied-eval': 'error', + 'no-invalid-regexp': 'error', + 'no-inner-declarations': 'error', + 'no-lone-blocks': 'error', + 'no-multi-assign': 'error', + 'no-misleading-character-class': 'error', + 'no-new-symbol': 'error', + 'no-obj-calls': 'error', + // This will be turned on after bug fixes are mostly complete + // 'no-param-reassign': 'warn', + 'no-process-exit': 'error', + 'no-prototype-builtins': 'error', + // This will be turned on after bug fixes are mostly complete + // 'no-return-await': 'warn', + 'no-self-compare': 'error', + 'no-shadow-restricted-names': 'error', + 'no-sparse-arrays': 'error', + 'no-this-before-super': 'error', + // This will be turned on after bug fixes are mostly complete + // 'no-undef': 'warn', + 'no-unreachable': 'error', + 'no-unsafe-finally': 'error', + 'no-useless-call': 'error', + // This will be turned on after bug fixes are mostly complete + // 'no-useless-catch': 'warn', + 'no-useless-concat': 'error', + 'no-useless-computed-key': 'error', + // This will be turned on after bug fixes are mostly complete + // 'no-useless-escape': 'warn', + 'no-useless-rename': 'error', + // This will be turned on after bug fixes are mostly complete + // 'no-useless-return': 'warn', + // This will be turned on after bug fixers are mostly complete + // 'no-void': 'warn', + 'one-var-declaration-per-line': 'error', + 'prefer-object-spread': 'error', + 'prefer-promise-reject-errors': 'error', + 'prefer-rest-params': 'error', + 'prefer-spread': 'error', + // This style will be turned on after most bugs are fixed + // 'prefer-template': 'warn', + // This style will be turned on after most bugs are fixed + // quotes: ['warn', 'single', { avoidEscape: true }], + 'react/boolean-prop-naming': 'error', + 'react/button-has-type': 'error', + 'react/forbid-dom-props': 'error', + // This will go from warn to error when this is fixed: + // https://github.com/elastic/ingest-dev/issues/468 + 'react/no-access-state-in-setstate': 'warn', + // This style will be turned on after most bugs are fixed + // 'react/no-children-prop': 'warn', + 'react/no-danger-with-children': 'error', + 'react/no-deprecated': 'error', + 'react/no-did-mount-set-state': 'error', + // This will go from warn to error when this is fixed: + // https://github.com/elastic/ingest-dev/issues/468 + 'react/no-did-update-set-state': 'warn', + 'react/no-direct-mutation-state': 'error', + 'react/no-find-dom-node': 'error', + 'react/no-redundant-should-component-update': 'error', + 'react/no-render-return-value': 'error', + 'react/no-typos': 'error', + 'react/no-string-refs': 'error', + 'react/no-this-in-sfc': 'error', + // This can go from warn to error once this is fixed + // https://github.com/elastic/ingest-dev/issues/467 + 'react/no-unescaped-entities': 'warn', + 'react/no-unsafe': 'error', + 'react/no-unused-prop-types': 'error', + 'react/no-unused-state': 'error', + // will introduced after the other warns are fixed + // 'react/sort-comp': 'error', + 'react/void-dom-elements-no-children': 'error', + 'react/jsx-boolean-value': ['error', 'warn'], + // will introduced after the other warns are fixed + // 'react/jsx-no-bind': 'error', + 'react/jsx-no-comment-textnodes': 'error', + // will be introduced to fix missing i18n keys + // 'react/jsx-no-literals': 'warn', + 'react/jsx-no-target-blank': 'error', + 'react/jsx-fragments': 'error', + 'react/jsx-sort-default-props': 'error', + // might be introduced after the other warns are fixed + // 'react/jsx-sort-props': 'error', + 'react/jsx-tag-spacing': 'error', + 'require-atomic-updates': 'error', + 'rest-spread-spacing': ['error', 'never'], + 'symbol-description': 'error', + 'template-curly-spacing': 'error', + 'vars-on-top': 'error', }, }, diff --git a/x-pack/plugins/siem/public/components/autocomplete_field/index.test.tsx b/x-pack/plugins/siem/public/components/autocomplete_field/index.test.tsx index 7fc23e057d02f..9f8bc2b656806 100644 --- a/x-pack/plugins/siem/public/components/autocomplete_field/index.test.tsx +++ b/x-pack/plugins/siem/public/components/autocomplete_field/index.test.tsx @@ -246,7 +246,8 @@ describe('Autocomplete', () => { const wrapperAutocompleteField = wrapper.find(AutocompleteField); wrapperAutocompleteField.setState({ selectedIndex: null }); const wrapperFixedEuiFieldSearch = wrapper.find(EuiFieldSearch); - // eslint-disable-next-line @typescript-eslint/no-explicit-any -- TODO: FixedEuiFieldSearch fails to import + // TODO: FixedEuiFieldSearch fails to import + // eslint-disable-next-line @typescript-eslint/no-explicit-any (wrapperFixedEuiFieldSearch as any).props().onSearch(); expect(onSubmit).toHaveBeenCalled(); }); diff --git a/x-pack/plugins/siem/public/components/open_timeline/index.test.tsx b/x-pack/plugins/siem/public/components/open_timeline/index.test.tsx index c102a64549824..62f9531b52f45 100644 --- a/x-pack/plugins/siem/public/components/open_timeline/index.test.tsx +++ b/x-pack/plugins/siem/public/components/open_timeline/index.test.tsx @@ -19,8 +19,10 @@ import { NotePreviews } from './note_previews'; import { OPEN_TIMELINE_CLASS_NAME } from './helpers'; const getStateChildComponent = ( + // eslint-disable-next-line @typescript-eslint/no-explicit-any wrapper: ReactWrapper, React.Component<{}, {}, any>> -): React.Component<{}, {}, any> => +): // eslint-disable-next-line @typescript-eslint/no-explicit-any +React.Component<{}, {}, any> => wrapper .childAt(0) .childAt(0) diff --git a/x-pack/plugins/siem/public/components/open_timeline/open_timeline_modal/index.test.tsx b/x-pack/plugins/siem/public/components/open_timeline/open_timeline_modal/index.test.tsx index 40cff3465bd51..de049a18baed2 100644 --- a/x-pack/plugins/siem/public/components/open_timeline/open_timeline_modal/index.test.tsx +++ b/x-pack/plugins/siem/public/components/open_timeline/open_timeline_modal/index.test.tsx @@ -17,8 +17,10 @@ import * as i18n from '../translations'; import { OpenTimelineModalButton } from '.'; const getStateChildComponent = ( + // eslint-disable-next-line @typescript-eslint/no-explicit-any wrapper: ReactWrapper, React.Component<{}, {}, any>> -): React.Component<{}, {}, any> => +): // eslint-disable-next-line @typescript-eslint/no-explicit-any +React.Component<{}, {}, any> => wrapper .childAt(0) .childAt(0) diff --git a/x-pack/plugins/siem/public/components/url_state/index.tsx b/x-pack/plugins/siem/public/components/url_state/index.tsx index 771d4161f995f..b56ed51d888e2 100644 --- a/x-pack/plugins/siem/public/components/url_state/index.tsx +++ b/x-pack/plugins/siem/public/components/url_state/index.tsx @@ -57,6 +57,7 @@ type KeyUrlState = keyof UrlState; interface UrlStateProps { indexPattern: StaticIndexPattern; + // eslint-disable-next-line @typescript-eslint/no-explicit-any mapToUrlState?: (value: any) => UrlState; onChange?: (urlState: UrlState, previousUrlState: UrlState) => void; onInitialize?: (urlState: UrlState) => void; @@ -394,6 +395,7 @@ export const UrlStateContainer = connect( // @ts-ignore )(UrlStateComponents); +// eslint-disable-next-line @typescript-eslint/no-explicit-any export const decodeRisonUrlState = (value: string | undefined): RisonValue | any | undefined => { try { return value ? decode(value) : undefined; @@ -405,6 +407,7 @@ export const decodeRisonUrlState = (value: string | undefined): RisonValue | any } }; +// eslint-disable-next-line @typescript-eslint/no-explicit-any const encodeRisonUrlState = (state: any) => encode(state); export const getQueryStringFromLocation = (location: Location) => location.search.substring(1); @@ -414,6 +417,7 @@ export const getParamFromQueryString = (queryString: string, key: string): strin return Array.isArray(queryParam) ? queryParam[0] : queryParam; }; +// eslint-disable-next-line @typescript-eslint/no-explicit-any export const replaceStateKeyInQueryString = ( stateKey: string, urlState: UrlState | undefined