Skip to content
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

chore: 🤖 forbid import from ui/** #40537

Merged
merged 15 commits into from
Jul 18, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,21 @@ module.exports = {
],
allowSameFolder: true,
},
{
from: ['src/legacy/ui/**/*', 'ui/**/*'],
target: [
'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:
'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.',
},
],
},
],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
},
],
},
],
});
13 changes: 8 additions & 5 deletions packages/kbn-eslint-plugin-eslint/rules/no_restricted_paths.js
Original file line number Diff line number Diff line change
Expand Up @@ -97,15 +97,16 @@ module.exports = {
}

function checkForRestrictedImportPath(importPath, node) {
const absoluteImportPath = resolve(importPath, context);
if (!absoluteImportPath) return;
const absoluteImportPath = importPath[0] === '.' ? resolve(importPath, context) : undefined;

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);
const relativeImportFile = path.relative(basePath, absoluteImportPath);
const relativeImportFile = absoluteImportPath
? path.relative(basePath, absoluteImportPath)
: importPath;

if (
!mm([relativeSrcFile], target).length ||
Expand All @@ -116,7 +117,9 @@ module.exports = {

context.report({
node,
message: `Unexpected path "${importPath}" imported in restricted zone.`,
message: `Unexpected path "${importPath}" imported in restricted zone.${
errorMessage ? ' ' + errorMessage : ''
}`,
});
}
}
Expand Down
1 change: 1 addition & 0 deletions src/dev/mocha/run_mocha_cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -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, '../../..'),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should we allow importing from ui/new_platform? if yes you can add !ui/new_platform in zone declaration inside .eslintrc

import { embeddablePlugin } from '../../../embeddable_api/public';
import { Plugin } from './plugin';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Copy link
Contributor Author

@streamich streamich Jul 15, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think src/** imports that used Webpack alias, like

import 'src/core/server/saved_objects';

where not covered previously, now this PR fixes it. Added a test for it, too.

import { EmbeddableFactoryNotFoundError } from '../../../../embeddables/embeddable_factory_not_found_error';
import { IContainer } from '../../../../containers';
Expand Down
1 change: 1 addition & 0 deletions src/legacy/ui/public/chrome/api/badge.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down
1 change: 1 addition & 0 deletions x-pack/legacy/plugins/code/server/__tests__/multi_node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

import getPort from 'get-port';
import { resolve } from 'path';
// eslint-disable-next-line @kbn/eslint/no-restricted-paths
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

and tests may import from anywhere. we also can exclude them from the check

import { Root } from 'src/core/server/root';

import {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was under the impression that this was the "correct" way to import mocks. Is there another approach we should take? Are mocks not meant to be used in this way at all?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess it is still as "correct" as it was before. The difference is that before this PR @kbn/eslint/no-restricted-paths linter rule ignored Webpack aliases like src/.., now it processes them as well.


Maybe @elastic/kibana-platform could comment what is the "correct" way to import mocks/types? ^^

Copy link
Contributor

@mshustov mshustov Jul 16, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah, unfortunately linter rule doesn't work with absolute paths yet. #36096
as a temporary solution you can change zones in .eslintrc

- '!src/core/server/mocks.ts',
+ '!src/core/server/mocks',

- '!src/core/public/mocks.ts',
+ '!src/core/public/mocks',

I guess it is still as "correct" as it was before.

right

import { httpServiceMock, elasticsearchServiceMock } from 'src/core/server/mocks';
import { createOptionalPlugin } from '../../../../../../server/lib/optional_plugin';
import { SpacesClient } from '../../../lib/spaces_client';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down