-
Notifications
You must be signed in to change notification settings - Fork 8.6k
feat(eslint): add no_sync_import_from_plugin rule (closes #171080) #266636
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
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
f92f7db
feat(eslint): add no_sync_import_from_plugin rule (#171080)
afharo 979cc40
chore(eslint): enable no_sync_import_from_plugin as error in CI
afharo d9edca7
Merge branch 'main' into issue-171080-eslint-no-sync-plugin
elasticmachine be22f87
Merge branch 'main' into issue-171080-eslint-no-sync-plugin
afharo 35a29e8
Merge branch 'main' of github.com:elastic/kibana into issue-171080-es…
afharo 0f1269b
Fix new ofenses
afharo File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
105 changes: 105 additions & 0 deletions
105
packages/kbn-eslint-plugin-eslint/rules/no_sync_import_from_plugin.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,105 @@ | ||
| /* | ||
| * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
| * or more contributor license agreements. Licensed under the "Elastic License | ||
| * 2.0", the "GNU Affero General Public License v3.0 only", and the "Server Side | ||
| * Public License v 1"; you may not use this file except in compliance with, at | ||
| * your election, the "Elastic License 2.0", the "GNU Affero General Public | ||
| * License v3.0 only", or the "Server Side Public License, v 1". | ||
| */ | ||
|
|
||
| /** @typedef {import("eslint").Rule.RuleModule} Rule */ | ||
|
|
||
| const MESSAGE = | ||
| "Do not statically import or re-export values from './plugin' in the server plugin entry. Use `import type` for types only, move shared config to `config.ts` when needed, and load the implementation with `await import('./plugin')` so disabled plugins do not load this module. See https://github.com/elastic/kibana/pull/170856."; | ||
|
|
||
| /** | ||
| * @param {string | undefined} source | ||
| * @returns {boolean} | ||
| */ | ||
| function isPluginModuleSpecifier(source) { | ||
| return source === './plugin' || source === '.\\plugin'; | ||
| } | ||
|
|
||
| /** @type {Rule} */ | ||
| module.exports = { | ||
| meta: { | ||
| type: 'problem', | ||
| docs: { | ||
| description: | ||
| 'Disallow static value imports and value re-exports from `./plugin` in plugin server entry files.', | ||
| }, | ||
| schema: [], | ||
| messages: { | ||
| noSyncImportFromPlugin: MESSAGE, | ||
| }, | ||
| }, | ||
|
|
||
| create(context) { | ||
| return { | ||
| ImportDeclaration(node) { | ||
| if (!node.source || !isPluginModuleSpecifier(node.source.value)) { | ||
| return; | ||
| } | ||
|
|
||
| if (node.importKind === 'type') { | ||
| return; | ||
| } | ||
|
|
||
| if (node.specifiers.length === 0) { | ||
| context.report({ node, messageId: 'noSyncImportFromPlugin' }); | ||
| return; | ||
| } | ||
|
|
||
| for (const spec of node.specifiers) { | ||
| if (spec.type === 'ImportSpecifier' && spec.importKind === 'type') { | ||
| continue; | ||
| } | ||
| context.report({ node: spec, messageId: 'noSyncImportFromPlugin' }); | ||
| return; | ||
| } | ||
| }, | ||
|
|
||
| ExportNamedDeclaration(node) { | ||
| if (!node.source || !isPluginModuleSpecifier(node.source.value)) { | ||
| return; | ||
| } | ||
|
|
||
| if (node.exportKind === 'type') { | ||
| return; | ||
| } | ||
|
|
||
| for (const spec of node.specifiers) { | ||
| if (spec.type === 'ExportSpecifier' && spec.exportKind === 'type') { | ||
| continue; | ||
| } | ||
| context.report({ node: spec, messageId: 'noSyncImportFromPlugin' }); | ||
| return; | ||
| } | ||
| }, | ||
|
|
||
| ExportAllDeclaration(node) { | ||
| if (!node.source || !isPluginModuleSpecifier(node.source.value)) { | ||
| return; | ||
| } | ||
|
|
||
| if (node.exportKind === 'type') { | ||
| return; | ||
| } | ||
|
|
||
| context.report({ node, messageId: 'noSyncImportFromPlugin' }); | ||
| }, | ||
|
|
||
| CallExpression(node) { | ||
| if ( | ||
| node.callee.type === 'Identifier' && | ||
| node.callee.name === 'require' && | ||
| node.arguments[0]?.type === 'Literal' && | ||
| typeof node.arguments[0].value === 'string' && | ||
| isPluginModuleSpecifier(node.arguments[0].value) | ||
| ) { | ||
| context.report({ node, messageId: 'noSyncImportFromPlugin' }); | ||
| } | ||
| }, | ||
| }; | ||
| }, | ||
| }; |
95 changes: 95 additions & 0 deletions
95
packages/kbn-eslint-plugin-eslint/rules/no_sync_import_from_plugin.test.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,95 @@ | ||
| /* | ||
| * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
| * or more contributor license agreements. Licensed under the "Elastic License | ||
| * 2.0", the "GNU Affero General Public License v3.0 only", and the "Server Side | ||
| * Public License v 1"; you may not use this file except in compliance with, at | ||
| * your election, the "Elastic License 2.0", the "GNU Affero General Public | ||
| * License v3.0 only", or the "Server Side Public License, v 1". | ||
| */ | ||
|
|
||
| const { RuleTester } = require('eslint'); | ||
| const rule = require('./no_sync_import_from_plugin'); | ||
| const dedent = require('dedent'); | ||
|
|
||
| const ruleTester = new RuleTester({ | ||
| parser: require.resolve('@typescript-eslint/parser'), | ||
| parserOptions: { | ||
| sourceType: 'module', | ||
| ecmaVersion: 2020, | ||
| ecmaFeatures: { | ||
| jsx: true, | ||
| }, | ||
| }, | ||
| }); | ||
|
|
||
| const ERR = { messageId: 'noSyncImportFromPlugin' }; | ||
|
|
||
| ruleTester.run('@kbn/eslint/no_sync_import_from_plugin', rule, { | ||
| valid: [ | ||
| { | ||
| code: dedent` | ||
| import type { PluginInitializerContext } from '@kbn/core/server'; | ||
| import type { MyPluginSetup } from './plugin'; | ||
| export const plugin = async (ctx: PluginInitializerContext) => { | ||
| const { MyPlugin } = await import('./plugin'); | ||
| return new MyPlugin(ctx); | ||
| }; | ||
| `, | ||
| }, | ||
| { | ||
| code: dedent` | ||
| import { config } from './config'; | ||
| export const plugin = async () => { | ||
| const { Plugin } = await import('./plugin'); | ||
| return new Plugin(); | ||
| }; | ||
| export { config }; | ||
| `, | ||
| }, | ||
| { | ||
| code: dedent` | ||
| export type { FooSetup } from './plugin'; | ||
| `, | ||
| }, | ||
| { | ||
| code: dedent` | ||
| import type { Foo } from './other'; | ||
| `, | ||
| }, | ||
| { | ||
| code: dedent` | ||
| const m = await import('./plugin'); | ||
| `, | ||
| }, | ||
| ], | ||
|
|
||
| invalid: [ | ||
| { | ||
| code: `import { FooPlugin } from './plugin';`, | ||
| errors: [ERR], | ||
| }, | ||
| { | ||
| code: dedent` | ||
| import type { Setup } from './plugin'; | ||
| import { FooPlugin } from './plugin'; | ||
| `, | ||
| errors: [ERR], | ||
| }, | ||
| { | ||
| code: `import './plugin';`, | ||
| errors: [ERR], | ||
| }, | ||
| { | ||
| code: `export { FooPlugin } from './plugin';`, | ||
| errors: [ERR], | ||
| }, | ||
| { | ||
| code: `export * from './plugin';`, | ||
| errors: [ERR], | ||
| }, | ||
| { | ||
| code: `require('./plugin');`, | ||
| errors: [ERR], | ||
| }, | ||
| ], | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
The
examples/**/server/index.tsglob only matches the repo-rootexamples/tree, so the 5 plugin entries underx-pack/examples/(e.g.alerting_example,screenshotting_example,triggers_actions_ui_example,gen_ai_streaming_response_example,third_party_vis_lens_example) are not enforced. If example plugins are intended to follow the same pattern, consider also coveringx-pack/examples/.