-
Notifications
You must be signed in to change notification settings - Fork 8.3k
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
[Fleet] Use TS organize imports #92980
Conversation
@elasticmachine merge upstream |
@elasticmachine merge upstream |
Pinging @elastic/fleet (Team:Fleet) |
💚 Build SucceededMetrics [docs]Async chunks
Page load bundle
History
To update your PR or re-run it, just comment with: cc @jfsiii |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is there anyway we can enforce the ts imports with some linting rules maybe?
@nchaulet oh, yeah, pretty sure we can 👍🏻 I've used https://eslint.org/docs/rules/sort-imports before. I think I got fixated on using tsc because there's only one way to do it, versus the many options in the eslint rule. But it's basically impossible to use the tsc version from the command line for now so I think I'll try using eslint so we can have something using automation. I'll try adding the rule and see how it goes. Thanks for the reminder |
## Problem Blocks of 10-15 `import`s are common in the plugin and there a few places which have ~50 lines of `import`s. It makes it more difficult to understand the where/why of what's being imported. We've had instances while files import from the same module in different lines. i.e. ```ts import { a } from './file'; ... 5-10 lines later import { b } from './file'; ``` ## Proposed solution Add a lint rule to enforce a convention on the module `import` order. This can help in the same way Prettier & ESLint help to format type signatures or other code. It makes it easier to understand or notice any changes in the code. It's also able to be fixed automatically (`node scripts/eslint.js --fix` or any existing "format on save" in an editor). ## This PR replaces #92980 (based on #92980 (review)) ### Lint rule f9be98d Add eslint rule to enforce/autofix import group order. Use the same rule as a few other plugins. Groups `import` statements by type as shown in the [lint rule docs](https://github.com/benmosher/eslint-plugin-import/blob/master/docs/rules/order.md#importorder-enforce-a-convention-in-module-import-order ). The order is: 1. node "builtin" modules 2. "external" modules 3. "internal" modules 4. modules from a "parent" directory 5. "sibling" modules from the same or a sibling's directory, "index" of the current directory, everything else e.g. ```typescript import fs from 'fs'; import path from 'path'; import _ from 'lodash'; import chalk from 'chalk'; import foo from 'src/foo'; import foo from '../foo'; import qux from '../../foo/qux'; import bar from './bar'; import baz from './bar/baz'; import main from './'; ``` The [lint rule](https://github.com/benmosher/eslint-plugin-import/blob/master/docs/rules/order.md#importorder-enforce-a-convention-in-module-import-order) is relatively light handed. It only ensures the `imports` are groups together in the given order. It doesn't alphabetize or otherwise sort the order of the files. e.g. imports aren't rewritten to be in alphabetical order. This is fine ```ts import from './c'; import from './a'; import from './b'; ``` The [docs show other options](https://github.com/benmosher/eslint-plugin-import/blob/master/docs/rules/order.md#options) and https://github.com/jfsiii/kibana/blob/2831f02bc7d7d4d6792e980b7733ddcdfc340cea/.eslintrc.js#L1138-L1168 uses many of them ### Newlines option The newlines settings means a change from something like ```typescript import fs from 'fs'; import path from 'path'; import _ from 'lodash'; import chalk from 'chalk'; import foo from 'src/foo'; import foo from '../foo'; import qux from '../../foo/qux'; import bar from './bar'; import baz from './bar/baz'; import main from './'; ``` to ```typescript import fs from 'fs'; import path from 'path'; import _ from 'lodash'; import chalk from 'chalk'; import foo from 'src/foo'; import foo from '../foo'; import qux from '../../foo/qux'; import bar from './bar'; import baz from './bar/baz'; import main from './'; ``` Added it as a separate commit 2831f02 in case we want to avoid it, but I believe it's an improvement overall. Especially on the files with 25+ lines of imports. Even the "worst case" of something like this isn't bad (IMO). Especially since it's an automatic reformat like anything else in prettier ```typescript import fs from 'fs'; import _ from 'lodash'; import foo from '../foo'; import main from './'; ```
## Problem Blocks of 10-15 `import`s are common in the plugin and there a few places which have ~50 lines of `import`s. It makes it more difficult to understand the where/why of what's being imported. We've had instances while files import from the same module in different lines. i.e. ```ts import { a } from './file'; ... 5-10 lines later import { b } from './file'; ``` ## Proposed solution Add a lint rule to enforce a convention on the module `import` order. This can help in the same way Prettier & ESLint help to format type signatures or other code. It makes it easier to understand or notice any changes in the code. It's also able to be fixed automatically (`node scripts/eslint.js --fix` or any existing "format on save" in an editor). ## This PR replaces elastic#92980 (based on elastic#92980 (review)) ### Lint rule f9be98d Add eslint rule to enforce/autofix import group order. Use the same rule as a few other plugins. Groups `import` statements by type as shown in the [lint rule docs](https://github.com/benmosher/eslint-plugin-import/blob/master/docs/rules/order.md#importorder-enforce-a-convention-in-module-import-order ). The order is: 1. node "builtin" modules 2. "external" modules 3. "internal" modules 4. modules from a "parent" directory 5. "sibling" modules from the same or a sibling's directory, "index" of the current directory, everything else e.g. ```typescript import fs from 'fs'; import path from 'path'; import _ from 'lodash'; import chalk from 'chalk'; import foo from 'src/foo'; import foo from '../foo'; import qux from '../../foo/qux'; import bar from './bar'; import baz from './bar/baz'; import main from './'; ``` The [lint rule](https://github.com/benmosher/eslint-plugin-import/blob/master/docs/rules/order.md#importorder-enforce-a-convention-in-module-import-order) is relatively light handed. It only ensures the `imports` are groups together in the given order. It doesn't alphabetize or otherwise sort the order of the files. e.g. imports aren't rewritten to be in alphabetical order. This is fine ```ts import from './c'; import from './a'; import from './b'; ``` The [docs show other options](https://github.com/benmosher/eslint-plugin-import/blob/master/docs/rules/order.md#options) and https://github.com/jfsiii/kibana/blob/2831f02bc7d7d4d6792e980b7733ddcdfc340cea/.eslintrc.js#L1138-L1168 uses many of them ### Newlines option The newlines settings means a change from something like ```typescript import fs from 'fs'; import path from 'path'; import _ from 'lodash'; import chalk from 'chalk'; import foo from 'src/foo'; import foo from '../foo'; import qux from '../../foo/qux'; import bar from './bar'; import baz from './bar/baz'; import main from './'; ``` to ```typescript import fs from 'fs'; import path from 'path'; import _ from 'lodash'; import chalk from 'chalk'; import foo from 'src/foo'; import foo from '../foo'; import qux from '../../foo/qux'; import bar from './bar'; import baz from './bar/baz'; import main from './'; ``` Added it as a separate commit 2831f02 in case we want to avoid it, but I believe it's an improvement overall. Especially on the files with 25+ lines of imports. Even the "worst case" of something like this isn't bad (IMO). Especially since it's an automatic reformat like anything else in prettier ```typescript import fs from 'fs'; import _ from 'lodash'; import foo from '../foo'; import main from './'; ```
## Problem Blocks of 10-15 `import`s are common in the plugin and there a few places which have ~50 lines of `import`s. It makes it more difficult to understand the where/why of what's being imported. We've had instances while files import from the same module in different lines. i.e. ```ts import { a } from './file'; ... 5-10 lines later import { b } from './file'; ``` ## Proposed solution Add a lint rule to enforce a convention on the module `import` order. This can help in the same way Prettier & ESLint help to format type signatures or other code. It makes it easier to understand or notice any changes in the code. It's also able to be fixed automatically (`node scripts/eslint.js --fix` or any existing "format on save" in an editor). ## This PR replaces #92980 (based on #92980 (review)) ### Lint rule f9be98d Add eslint rule to enforce/autofix import group order. Use the same rule as a few other plugins. Groups `import` statements by type as shown in the [lint rule docs](https://github.com/benmosher/eslint-plugin-import/blob/master/docs/rules/order.md#importorder-enforce-a-convention-in-module-import-order ). The order is: 1. node "builtin" modules 2. "external" modules 3. "internal" modules 4. modules from a "parent" directory 5. "sibling" modules from the same or a sibling's directory, "index" of the current directory, everything else e.g. ```typescript import fs from 'fs'; import path from 'path'; import _ from 'lodash'; import chalk from 'chalk'; import foo from 'src/foo'; import foo from '../foo'; import qux from '../../foo/qux'; import bar from './bar'; import baz from './bar/baz'; import main from './'; ``` The [lint rule](https://github.com/benmosher/eslint-plugin-import/blob/master/docs/rules/order.md#importorder-enforce-a-convention-in-module-import-order) is relatively light handed. It only ensures the `imports` are groups together in the given order. It doesn't alphabetize or otherwise sort the order of the files. e.g. imports aren't rewritten to be in alphabetical order. This is fine ```ts import from './c'; import from './a'; import from './b'; ``` The [docs show other options](https://github.com/benmosher/eslint-plugin-import/blob/master/docs/rules/order.md#options) and https://github.com/jfsiii/kibana/blob/2831f02bc7d7d4d6792e980b7733ddcdfc340cea/.eslintrc.js#L1138-L1168 uses many of them ### Newlines option The newlines settings means a change from something like ```typescript import fs from 'fs'; import path from 'path'; import _ from 'lodash'; import chalk from 'chalk'; import foo from 'src/foo'; import foo from '../foo'; import qux from '../../foo/qux'; import bar from './bar'; import baz from './bar/baz'; import main from './'; ``` to ```typescript import fs from 'fs'; import path from 'path'; import _ from 'lodash'; import chalk from 'chalk'; import foo from 'src/foo'; import foo from '../foo'; import qux from '../../foo/qux'; import bar from './bar'; import baz from './bar/baz'; import main from './'; ``` Added it as a separate commit 2831f02 in case we want to avoid it, but I believe it's an improvement overall. Especially on the files with 25+ lines of imports. Even the "worst case" of something like this isn't bad (IMO). Especially since it's an automatic reformat like anything else in prettier ```typescript import fs from 'fs'; import _ from 'lodash'; import foo from '../foo'; import main from './'; ``` Co-authored-by: John Schulz <john.schulz@elastic.co>
Summary
Sort imports using TypeScripts organize imports. Ideally it'd be done by CI or hook, but big bang once year isn't bad either, IMO.
Skipped some long imports which had comments about which file/domain they were from.