From 5eb2767bd586f18d9e965c490f99567a9dd4371b Mon Sep 17 00:00:00 2001 From: streamich Date: Mon, 8 Jul 2019 21:00:24 +0200 Subject: [PATCH 1/8] =?UTF-8?q?chore:=20=F0=9F=A4=96=20forbid=20importing?= =?UTF-8?q?=20from=20ui/**=20from=20within=20shims?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .eslintrc.js | 13 +++++++++++++ .../rules/no_restricted_paths.js | 15 +++++++++++---- 2 files changed, 24 insertions(+), 4 deletions(-) diff --git a/.eslintrc.js b/.eslintrc.js index c3cfeb6a384a..e3ca38845b2b 100644 --- a/.eslintrc.js +++ b/.eslintrc.js @@ -156,6 +156,19 @@ module.exports = { ], allowSameFolder: true, }, + { + from: ['src/legacy/ui/**/*', 'ui/**/*'], + target: [ + 'src/legacy/core_plugins/**/public/shim/**/*', + 'src/legacy/core_plugins/**/server/shim/**/*', + ], + allowSameFolder: true, + errorMessage: + 'Shim should not import from /src/legacy/ui/** folder. ' + + 'Instead of importing from /src/legacy/ui/** deeply withing a shim, ' + + 'import those things once at the top level of your shim and pass those down, just ' + + 'like you pass down `core` and `plugins` objects.', + }, ], }, ], diff --git a/packages/kbn-eslint-plugin-eslint/rules/no_restricted_paths.js b/packages/kbn-eslint-plugin-eslint/rules/no_restricted_paths.js index 4248544c2ea5..2a64b25279fc 100644 --- a/packages/kbn-eslint-plugin-eslint/rules/no_restricted_paths.js +++ b/packages/kbn-eslint-plugin-eslint/rules/no_restricted_paths.js @@ -97,11 +97,16 @@ module.exports = { } function checkForRestrictedImportPath(importPath, node) { - const absoluteImportPath = resolve(importPath, context); - if (!absoluteImportPath) return; + let absoluteImportPath; + if (importPath.indexOf('ui/') === 0) { + absoluteImportPath = importPath; + } else { + absoluteImportPath = resolve(importPath, context); + if (!absoluteImportPath) return; + } const currentFilename = context.getFilename(); - for (const { target, from, allowSameFolder } of zones) { + for (const { target, from, allowSameFolder, errorMessage = '' } of zones) { const srcFilePath = resolve(currentFilename, context); const relativeSrcFile = path.relative(basePath, srcFilePath); @@ -116,7 +121,9 @@ module.exports = { context.report({ node, - message: `Unexpected path "${importPath}" imported in restricted zone.`, + message: `Unexpected path "${importPath}" imported in restricted zone.${ + errorMessage ? ' ' + errorMessage : '' + }`, }); } } From 9f9770c3c14972ca8a7317b10c2fbec225d49a33 Mon Sep 17 00:00:00 2001 From: streamich Date: Mon, 8 Jul 2019 22:52:03 +0200 Subject: [PATCH 2/8] =?UTF-8?q?chore:=20=F0=9F=A4=96=20allow=20importing?= =?UTF-8?q?=20platfrom=20in=20dashboard=20embeddable=20sihm?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../dashboard_embeddable_container/public/shim/index.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/src/legacy/core_plugins/dashboard_embeddable_container/public/shim/index.ts b/src/legacy/core_plugins/dashboard_embeddable_container/public/shim/index.ts index 7868ce7f06cf..fa81a30db374 100644 --- a/src/legacy/core_plugins/dashboard_embeddable_container/public/shim/index.ts +++ b/src/legacy/core_plugins/dashboard_embeddable_container/public/shim/index.ts @@ -18,6 +18,7 @@ */ import { PluginInitializerContext } from 'kibana/public'; +// eslint-disable-next-line @kbn/eslint/no-restricted-paths import { npSetup, npStart } from 'ui/new_platform'; import { embeddablePlugin } from '../../../embeddable_api/public'; import { Plugin } from './plugin'; From 1813c9d9c909d503579dd34f6bd40a10635515d7 Mon Sep 17 00:00:00 2001 From: streamich Date: Fri, 12 Jul 2019 18:45:18 +0200 Subject: [PATCH 3/8] =?UTF-8?q?chore:=20=F0=9F=A4=96=20use=20np=5Fready=20?= =?UTF-8?q?folder,=20improve=20rule?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .eslintrc.js | 6 ++++-- .../kbn-eslint-plugin-eslint/rules/no_restricted_paths.js | 8 ++++++-- 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/.eslintrc.js b/.eslintrc.js index e3ca38845b2b..dd7fa83b2884 100644 --- a/.eslintrc.js +++ b/.eslintrc.js @@ -159,8 +159,10 @@ module.exports = { { from: ['src/legacy/ui/**/*', 'ui/**/*'], target: [ - 'src/legacy/core_plugins/**/public/shim/**/*', - 'src/legacy/core_plugins/**/server/shim/**/*', + 'src/legacy/core_plugins/**/public/np_ready/**/*', + 'src/legacy/core_plugins/**/server/np_ready/**/*', + 'x-pack/legacy/plugins/**/public/np_ready/**/*', + 'x-pack/legacy/plugins/**/server/np_ready/**/*', ], allowSameFolder: true, errorMessage: diff --git a/packages/kbn-eslint-plugin-eslint/rules/no_restricted_paths.js b/packages/kbn-eslint-plugin-eslint/rules/no_restricted_paths.js index 2a64b25279fc..d04aee3f174e 100644 --- a/packages/kbn-eslint-plugin-eslint/rules/no_restricted_paths.js +++ b/packages/kbn-eslint-plugin-eslint/rules/no_restricted_paths.js @@ -98,8 +98,12 @@ module.exports = { function checkForRestrictedImportPath(importPath, node) { let absoluteImportPath; - if (importPath.indexOf('ui/') === 0) { - absoluteImportPath = importPath; + const [one, two, ...rest] = importPath.split('/'); + if (one === 'ui') { + absoluteImportPath = path.join( + basePath, + `src/legacy/${one}/public/${two}${rest.length ? `/${rest.join('/')}` : ''}` + ); } else { absoluteImportPath = resolve(importPath, context); if (!absoluteImportPath) return; From f042466c80dd41a0a0860ae50b0fb88f2cae003e Mon Sep 17 00:00:00 2001 From: streamich Date: Fri, 12 Jul 2019 18:49:49 +0200 Subject: [PATCH 4/8] =?UTF-8?q?chore:=20=F0=9F=A4=96=20update=20ESLint=20e?= =?UTF-8?q?rror=20message?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .eslintrc.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/.eslintrc.js b/.eslintrc.js index dd7fa83b2884..6801358b4bcc 100644 --- a/.eslintrc.js +++ b/.eslintrc.js @@ -166,9 +166,9 @@ module.exports = { ], allowSameFolder: true, errorMessage: - 'Shim should not import from /src/legacy/ui/** folder. ' + - 'Instead of importing from /src/legacy/ui/** deeply withing a shim, ' + - 'import those things once at the top level of your shim and pass those down, just ' + + 'NP-ready code should not import from /src/legacy/ui/** folder. ' + + 'Instead of importing from /src/legacy/ui/** deeply within a np_ready folder, ' + + 'import those things once at the top level of your plugin and pass those down, just ' + 'like you pass down `core` and `plugins` objects.', }, ], From 070d8366a58a0fa3ad4dc63a3fe98740150afff1 Mon Sep 17 00:00:00 2001 From: streamich Date: Mon, 15 Jul 2019 19:19:27 +0200 Subject: [PATCH 5/8] =?UTF-8?q?feat:=20=F0=9F=8E=B8=20improve=20eslint=20p?= =?UTF-8?q?lugin=20rule?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../rules/no_restricted_paths.js | 16 ++++------------ .../panel_actions/add_panel/add_panel_flyout.tsx | 1 + 2 files changed, 5 insertions(+), 12 deletions(-) diff --git a/packages/kbn-eslint-plugin-eslint/rules/no_restricted_paths.js b/packages/kbn-eslint-plugin-eslint/rules/no_restricted_paths.js index d04aee3f174e..9074c35d27c7 100644 --- a/packages/kbn-eslint-plugin-eslint/rules/no_restricted_paths.js +++ b/packages/kbn-eslint-plugin-eslint/rules/no_restricted_paths.js @@ -97,24 +97,16 @@ module.exports = { } function checkForRestrictedImportPath(importPath, node) { - let absoluteImportPath; - const [one, two, ...rest] = importPath.split('/'); - if (one === 'ui') { - absoluteImportPath = path.join( - basePath, - `src/legacy/${one}/public/${two}${rest.length ? `/${rest.join('/')}` : ''}` - ); - } else { - absoluteImportPath = resolve(importPath, context); - if (!absoluteImportPath) return; - } + const absoluteImportPath = importPath[0] === '.' ? resolve(importPath, context) : undefined; const currentFilename = context.getFilename(); for (const { target, from, allowSameFolder, errorMessage = '' } of zones) { const srcFilePath = resolve(currentFilename, context); const relativeSrcFile = path.relative(basePath, srcFilePath); - const relativeImportFile = path.relative(basePath, absoluteImportPath); + const relativeImportFile = absoluteImportPath + ? path.relative(basePath, absoluteImportPath) + : importPath; if ( !mm([relativeSrcFile], target).length || diff --git a/src/legacy/core_plugins/embeddable_api/public/panel/panel_header/panel_actions/add_panel/add_panel_flyout.tsx b/src/legacy/core_plugins/embeddable_api/public/panel/panel_header/panel_actions/add_panel/add_panel_flyout.tsx index 1683f05c1d6b..42fca6c721b0 100644 --- a/src/legacy/core_plugins/embeddable_api/public/panel/panel_header/panel_actions/add_panel/add_panel_flyout.tsx +++ b/src/legacy/core_plugins/embeddable_api/public/panel/panel_header/panel_actions/add_panel/add_panel_flyout.tsx @@ -38,6 +38,7 @@ import { EuiText, } from '@elastic/eui'; +// eslint-disable-next-line @kbn/eslint/no-restricted-paths import { SavedObjectAttributes } from 'src/core/server/saved_objects'; import { EmbeddableFactoryNotFoundError } from '../../../../embeddables/embeddable_factory_not_found_error'; import { IContainer } from '../../../../containers'; From 7726152fe3a16f09b19de1c393d6efc280d4ae90 Mon Sep 17 00:00:00 2001 From: streamich Date: Mon, 15 Jul 2019 19:30:57 +0200 Subject: [PATCH 6/8] =?UTF-8?q?test:=20=F0=9F=92=8D=20add=20tests=20for=20?= =?UTF-8?q?restricted=20paths=20linter=20rule?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../rules/__tests__/no_restricted_paths.js | 74 +++++++++++++++++++ 1 file changed, 74 insertions(+) diff --git a/packages/kbn-eslint-plugin-eslint/rules/__tests__/no_restricted_paths.js b/packages/kbn-eslint-plugin-eslint/rules/__tests__/no_restricted_paths.js index 11f01a1777ab..c821aafb0928 100644 --- a/packages/kbn-eslint-plugin-eslint/rules/__tests__/no_restricted_paths.js +++ b/packages/kbn-eslint-plugin-eslint/rules/__tests__/no_restricted_paths.js @@ -295,5 +295,79 @@ ruleTester.run('@kbn/eslint/no-restricted-paths', rule, { }, ], }, + + { + // Does not allow to import deeply within Core, using "src/core/..." Webpack alias. + code: 'const d = require("src/core/server/saved_objects")', + filename: path.join(__dirname, './files/no_restricted_paths/client/a.js'), + options: [ + { + basePath: __dirname, + zones: [ + { + target: 'files/no_restricted_paths/**/*', + from: 'src/core/server/**/*', + }, + ], + }, + ], + errors: [ + { + message: 'Unexpected path "src/core/server/saved_objects" imported in restricted zone.', + line: 1, + column: 19, + }, + ], + }, + + { + // Does not allow to import "ui/kfetch". + code: 'const d = require("ui/kfetch")', + filename: path.join(__dirname, './files/no_restricted_paths/client/a.js'), + options: [ + { + basePath: __dirname, + zones: [ + { + from: ['src/legacy/ui/**/*', 'ui/**/*'], + target: 'files/no_restricted_paths/**/*', + allowSameFolder: true, + }, + ], + }, + ], + errors: [ + { + message: 'Unexpected path "ui/kfetch" imported in restricted zone.', + line: 1, + column: 19, + }, + ], + }, + + { + // Does not allow to import deeply "ui/kfetch/public/index". + code: 'const d = require("ui/kfetch/public/index")', + filename: path.join(__dirname, './files/no_restricted_paths/client/a.js'), + options: [ + { + basePath: __dirname, + zones: [ + { + from: ['src/legacy/ui/**/*', 'ui/**/*'], + target: 'files/no_restricted_paths/**/*', + allowSameFolder: true, + }, + ], + }, + ], + errors: [ + { + message: 'Unexpected path "ui/kfetch/public/index" imported in restricted zone.', + line: 1, + column: 19, + }, + ], + }, ], }); From a767f6fd45726f9363b6d283876ee81087527c9b Mon Sep 17 00:00:00 2001 From: streamich Date: Tue, 16 Jul 2019 12:02:12 +0200 Subject: [PATCH 7/8] =?UTF-8?q?test:=20=F0=9F=92=8D=20add=20ESLint=20packa?= =?UTF-8?q?ge=20to=20CI=20testing=20suite?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/dev/mocha/run_mocha_cli.js | 1 + 1 file changed, 1 insertion(+) diff --git a/src/dev/mocha/run_mocha_cli.js b/src/dev/mocha/run_mocha_cli.js index 299a910127d4..d2bb9ef9e018 100644 --- a/src/dev/mocha/run_mocha_cli.js +++ b/src/dev/mocha/run_mocha_cli.js @@ -74,6 +74,7 @@ export function runMochaCli() { 'packages/elastic-datemath/test/**/*.js', 'packages/kbn-dev-utils/src/**/__tests__/**/*.js', 'packages/kbn-es-query/src/**/__tests__/**/*.js', + 'packages/kbn-eslint-plugin-eslint/**/__tests__/**/*.js', 'tasks/**/__tests__/**/*.js', ], { cwd: resolve(__dirname, '../../..'), From 5ceef7c5c9c66cf03332e5f709301b5677c2f7ee Mon Sep 17 00:00:00 2001 From: streamich Date: Tue, 16 Jul 2019 12:03:50 +0200 Subject: [PATCH 8/8] =?UTF-8?q?chore:=20=F0=9F=A4=96=20fix=20linter=20erro?= =?UTF-8?q?rs?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/legacy/ui/public/chrome/api/badge.test.ts | 1 + .../server/lib/adapters/database/__tests__/kibana.ts | 1 + x-pack/legacy/plugins/code/server/__tests__/multi_node.ts | 1 + x-pack/legacy/plugins/encrypted_saved_objects/server/plugin.ts | 1 + .../spaces/server/routes/api/__fixtures__/create_test_handler.ts | 1 + .../upgrade_assistant/server/lib/telemetry/usage_collector.ts | 1 + 6 files changed, 6 insertions(+) diff --git a/src/legacy/ui/public/chrome/api/badge.test.ts b/src/legacy/ui/public/chrome/api/badge.test.ts index 02fc1e39d397..6a19afb609f8 100644 --- a/src/legacy/ui/public/chrome/api/badge.test.ts +++ b/src/legacy/ui/public/chrome/api/badge.test.ts @@ -19,6 +19,7 @@ import * as Rx from 'rxjs'; +// eslint-disable-next-line @kbn/eslint/no-restricted-paths import { ChromeBadge } from 'src/core/public/chrome'; import { newPlatformChrome } from './badge.test.mocks'; import { initChromeBadgeApi } from './badge'; diff --git a/x-pack/legacy/plugins/beats_management/server/lib/adapters/database/__tests__/kibana.ts b/x-pack/legacy/plugins/beats_management/server/lib/adapters/database/__tests__/kibana.ts index 58e4488647d5..e3587afb7271 100644 --- a/x-pack/legacy/plugins/beats_management/server/lib/adapters/database/__tests__/kibana.ts +++ b/x-pack/legacy/plugins/beats_management/server/lib/adapters/database/__tests__/kibana.ts @@ -7,6 +7,7 @@ // @ts-ignore import { createEsTestCluster } from '@kbn/test'; +// eslint-disable-next-line @kbn/eslint/no-restricted-paths import { Root } from 'src/core/server/root'; // @ts-ignore import * as kbnTestServer from '../../../../../../../../../src/test_utils/kbn_server'; diff --git a/x-pack/legacy/plugins/code/server/__tests__/multi_node.ts b/x-pack/legacy/plugins/code/server/__tests__/multi_node.ts index 8036f3940919..549a10a2d32f 100644 --- a/x-pack/legacy/plugins/code/server/__tests__/multi_node.ts +++ b/x-pack/legacy/plugins/code/server/__tests__/multi_node.ts @@ -6,6 +6,7 @@ import getPort from 'get-port'; import { resolve } from 'path'; +// eslint-disable-next-line @kbn/eslint/no-restricted-paths import { Root } from 'src/core/server/root'; import { diff --git a/x-pack/legacy/plugins/encrypted_saved_objects/server/plugin.ts b/x-pack/legacy/plugins/encrypted_saved_objects/server/plugin.ts index 0117e0a87884..ad38ca0f7a88 100644 --- a/x-pack/legacy/plugins/encrypted_saved_objects/server/plugin.ts +++ b/x-pack/legacy/plugins/encrypted_saved_objects/server/plugin.ts @@ -6,6 +6,7 @@ import crypto from 'crypto'; import { Legacy, Server } from 'kibana'; +// eslint-disable-next-line @kbn/eslint/no-restricted-paths import { SavedObjectsRepository } from 'src/core/server/saved_objects/service'; import { SavedObjectsBaseOptions, SavedObject, SavedObjectAttributes } from 'src/core/server'; import { diff --git a/x-pack/legacy/plugins/spaces/server/routes/api/__fixtures__/create_test_handler.ts b/x-pack/legacy/plugins/spaces/server/routes/api/__fixtures__/create_test_handler.ts index f84194160971..0e90e2c47118 100644 --- a/x-pack/legacy/plugins/spaces/server/routes/api/__fixtures__/create_test_handler.ts +++ b/x-pack/legacy/plugins/spaces/server/routes/api/__fixtures__/create_test_handler.ts @@ -8,6 +8,7 @@ import * as Rx from 'rxjs'; import { Server } from 'hapi'; import { Legacy } from 'kibana'; import { KibanaConfig } from 'src/legacy/server/kbn_server'; +// eslint-disable-next-line @kbn/eslint/no-restricted-paths import { httpServiceMock, elasticsearchServiceMock } from 'src/core/server/mocks'; import { createOptionalPlugin } from '../../../../../../server/lib/optional_plugin'; import { SpacesClient } from '../../../lib/spaces_client'; diff --git a/x-pack/legacy/plugins/upgrade_assistant/server/lib/telemetry/usage_collector.ts b/x-pack/legacy/plugins/upgrade_assistant/server/lib/telemetry/usage_collector.ts index 8c01db82a707..3a9b11a57f07 100644 --- a/x-pack/legacy/plugins/upgrade_assistant/server/lib/telemetry/usage_collector.ts +++ b/x-pack/legacy/plugins/upgrade_assistant/server/lib/telemetry/usage_collector.ts @@ -5,6 +5,7 @@ */ import { set } from 'lodash'; +// eslint-disable-next-line @kbn/eslint/no-restricted-paths import { SavedObjectsRepository } from 'src/core/server/saved_objects/service/lib/repository'; import { UPGRADE_ASSISTANT_DOC_ID,