|
| 1 | +/* |
| 2 | + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one |
| 3 | + * or more contributor license agreements. Licensed under the "Elastic License |
| 4 | + * 2.0", the "GNU Affero General Public License v3.0 only", and the "Server Side |
| 5 | + * Public License v 1"; you may not use this file except in compliance with, at |
| 6 | + * your election, the "Elastic License 2.0", the "GNU Affero General Public |
| 7 | + * License v3.0 only", or the "Server Side Public License, v 1". |
| 8 | + */ |
| 9 | + |
| 10 | +/** @typedef {import("eslint").Rule.RuleModule} Rule */ |
| 11 | + |
| 12 | +const MESSAGE = |
| 13 | + "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."; |
| 14 | + |
| 15 | +/** |
| 16 | + * @param {string | undefined} source |
| 17 | + * @returns {boolean} |
| 18 | + */ |
| 19 | +function isPluginModuleSpecifier(source) { |
| 20 | + return source === './plugin' || source === '.\\plugin'; |
| 21 | +} |
| 22 | + |
| 23 | +/** @type {Rule} */ |
| 24 | +module.exports = { |
| 25 | + meta: { |
| 26 | + type: 'problem', |
| 27 | + docs: { |
| 28 | + description: |
| 29 | + 'Disallow static value imports and value re-exports from `./plugin` in plugin server entry files.', |
| 30 | + }, |
| 31 | + schema: [], |
| 32 | + messages: { |
| 33 | + noSyncImportFromPlugin: MESSAGE, |
| 34 | + }, |
| 35 | + }, |
| 36 | + |
| 37 | + create(context) { |
| 38 | + return { |
| 39 | + ImportDeclaration(node) { |
| 40 | + if (!node.source || !isPluginModuleSpecifier(node.source.value)) { |
| 41 | + return; |
| 42 | + } |
| 43 | + |
| 44 | + if (node.importKind === 'type') { |
| 45 | + return; |
| 46 | + } |
| 47 | + |
| 48 | + if (node.specifiers.length === 0) { |
| 49 | + context.report({ node, messageId: 'noSyncImportFromPlugin' }); |
| 50 | + return; |
| 51 | + } |
| 52 | + |
| 53 | + for (const spec of node.specifiers) { |
| 54 | + if (spec.type === 'ImportSpecifier' && spec.importKind === 'type') { |
| 55 | + continue; |
| 56 | + } |
| 57 | + context.report({ node: spec, messageId: 'noSyncImportFromPlugin' }); |
| 58 | + return; |
| 59 | + } |
| 60 | + }, |
| 61 | + |
| 62 | + ExportNamedDeclaration(node) { |
| 63 | + if (!node.source || !isPluginModuleSpecifier(node.source.value)) { |
| 64 | + return; |
| 65 | + } |
| 66 | + |
| 67 | + if (node.exportKind === 'type') { |
| 68 | + return; |
| 69 | + } |
| 70 | + |
| 71 | + for (const spec of node.specifiers) { |
| 72 | + if (spec.type === 'ExportSpecifier' && spec.exportKind === 'type') { |
| 73 | + continue; |
| 74 | + } |
| 75 | + context.report({ node: spec, messageId: 'noSyncImportFromPlugin' }); |
| 76 | + return; |
| 77 | + } |
| 78 | + }, |
| 79 | + |
| 80 | + ExportAllDeclaration(node) { |
| 81 | + if (!node.source || !isPluginModuleSpecifier(node.source.value)) { |
| 82 | + return; |
| 83 | + } |
| 84 | + |
| 85 | + if (node.exportKind === 'type') { |
| 86 | + return; |
| 87 | + } |
| 88 | + |
| 89 | + context.report({ node, messageId: 'noSyncImportFromPlugin' }); |
| 90 | + }, |
| 91 | + |
| 92 | + CallExpression(node) { |
| 93 | + if ( |
| 94 | + node.callee.type === 'Identifier' && |
| 95 | + node.callee.name === 'require' && |
| 96 | + node.arguments[0]?.type === 'Literal' && |
| 97 | + typeof node.arguments[0].value === 'string' && |
| 98 | + isPluginModuleSpecifier(node.arguments[0].value) |
| 99 | + ) { |
| 100 | + context.report({ node, messageId: 'noSyncImportFromPlugin' }); |
| 101 | + } |
| 102 | + }, |
| 103 | + }; |
| 104 | + }, |
| 105 | +}; |
0 commit comments