Skip to content

Commit feed406

Browse files
author
Tyler Smalley
authored
[eslint] no_restricted_paths config cleanup (#63741)
Major cleanup of the no_restricted_paths rule for imports of core. For relative imports, we use eslint-module-utils/resolve which resolves to the full filesystem path. So, to support relative and absolute imports from the src alias we need to define both the directory and the index including file extension. This rule was handling both core imports, as well as imports from other plugins. Imports from other plugins are being used much more liberally allowed through the exceptions in tests. I choose to break these up, removing this exception for tests for core imports. Fixes: Absolute imports of src/core/server/mocks were not allowed in src. This was not an issue in x-pack due to the target excluding !x-pack/**/*.test.* and !x-pack/test/**/*. Non-top-level public and server imports were allowed from X-Pack tests to the previously mentioned exclusion. Signed-off-by: Tyler Smalley <tyler.smalley@elastic.co>
1 parent 9e11bcb commit feed406

File tree

48 files changed

+171
-137
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

48 files changed

+171
-137
lines changed

.eslintrc.js

Lines changed: 24 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -185,31 +185,40 @@ module.exports = {
185185
zones: [
186186
{
187187
target: [
188-
'src/legacy/**/*',
189-
'x-pack/**/*',
190-
'!x-pack/**/*.test.*',
191-
'!x-pack/test/**/*',
188+
'(src|x-pack)/legacy/**/*',
192189
'(src|x-pack)/plugins/**/(public|server)/**/*',
193-
'src/core/(public|server)/**/*',
194190
'examples/**/*',
195191
],
196192
from: [
197193
'src/core/public/**/*',
198-
'!src/core/public/index.ts',
199-
'!src/core/public/mocks.ts',
200-
'!src/core/public/*.test.mocks.ts',
194+
'!src/core/public/index.ts', // relative import
195+
'!src/core/public/mocks{,.ts}',
196+
'!src/core/server/types{,.ts}',
201197
'!src/core/public/utils/**/*',
198+
'!src/core/public/*.test.mocks{,.ts}',
202199

203200
'src/core/server/**/*',
204-
'!src/core/server/index.ts',
205-
'!src/core/server/mocks.ts',
206-
'!src/core/server/types.ts',
207-
'!src/core/server/test_utils.ts',
201+
'!src/core/server/index.ts', // relative import
202+
'!src/core/server/mocks{,.ts}',
203+
'!src/core/server/types{,.ts}',
204+
'!src/core/server/test_utils',
208205
// for absolute imports until fixed in
209206
// https://github.com/elastic/kibana/issues/36096
210-
'!src/core/server/types',
211-
'!src/core/server/*.test.mocks.ts',
212-
207+
'!src/core/server/*.test.mocks{,.ts}',
208+
],
209+
allowSameFolder: true,
210+
errorMessage:
211+
'Plugins may only import from top-level public and server modules in core.',
212+
},
213+
{
214+
target: [
215+
'(src|x-pack)/legacy/**/*',
216+
'(src|x-pack)/plugins/**/(public|server)/**/*',
217+
'examples/**/*',
218+
'!(src|x-pack)/**/*.test.*',
219+
'!(x-pack/)?test/**/*',
220+
],
221+
from: [
213222
'(src|x-pack)/plugins/**/(public|server)/**/*',
214223
'!(src|x-pack)/plugins/**/(public|server)/(index|mocks).{js,ts,tsx}',
215224
],

src/core/public/http/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,5 @@
1818
*/
1919

2020
export { HttpService } from './http_service';
21+
export { HttpFetchError } from './http_fetch_error';
2122
export * from './types';

src/core/public/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,7 @@ export {
143143
export {
144144
HttpHeadersInit,
145145
HttpRequestInit,
146+
HttpFetchError,
146147
HttpFetchOptions,
147148
HttpFetchOptionsWithPath,
148149
HttpFetchQuery,

src/core/public/public.api.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -593,6 +593,23 @@ export type HandlerFunction<T extends object> = (context: T, ...args: any[]) =>
593593
// @public
594594
export type HandlerParameters<T extends HandlerFunction<any>> = T extends (context: any, ...args: infer U) => any ? U : never;
595595

596+
// @internal (undocumented)
597+
export class HttpFetchError extends Error implements IHttpFetchError {
598+
constructor(message: string, name: string, request: Request, response?: Response | undefined, body?: any);
599+
// (undocumented)
600+
readonly body?: any;
601+
// (undocumented)
602+
readonly name: string;
603+
// (undocumented)
604+
readonly req: Request;
605+
// (undocumented)
606+
readonly request: Request;
607+
// (undocumented)
608+
readonly res?: Response;
609+
// (undocumented)
610+
readonly response?: Response | undefined;
611+
}
612+
596613
// @public
597614
export interface HttpFetchOptions extends HttpRequestInit {
598615
asResponse?: boolean;

src/core/server/mocks.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ export { elasticsearchServiceMock } from './elasticsearch/elasticsearch_service.
4545
export { httpServiceMock } from './http/http_service.mock';
4646
export { loggingServiceMock } from './logging/logging_service.mock';
4747
export { savedObjectsRepositoryMock } from './saved_objects/service/lib/repository.mock';
48+
export { savedObjectsServiceMock } from './saved_objects/saved_objects_service.mock';
4849
export { typeRegistryMock as savedObjectsTypeRegistryMock } from './saved_objects/saved_objects_type_registry.mock';
4950
export { uiSettingsServiceMock } from './ui_settings/ui_settings_service.mock';
5051
export { metricsServiceMock } from './metrics/metrics_service.mock';

x-pack/legacy/plugins/uptime/public/components/overview/empty_state/__tests__/empty_state.test.tsx

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,7 @@
77
import React from 'react';
88
import { EmptyStateComponent } from '../empty_state';
99
import { StatesIndexStatus } from '../../../../../common/runtime_types';
10-
import { IHttpFetchError } from '../../../../../../../../../target/types/core/public/http';
11-
import { HttpFetchError } from '../../../../../../../../../src/core/public/http/http_fetch_error';
10+
import { HttpFetchError, IHttpFetchError } from 'src/core/public';
1211
import { mountWithRouter, shallowWithRouter } from '../../../../lib';
1312

1413
describe('EmptyState component', () => {

x-pack/legacy/plugins/uptime/public/state/api/__tests__/snapshot.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
import { fetchSnapshotCount } from '../snapshot';
88
import { apiService } from '../utils';
9-
import { HttpFetchError } from '../../../../../../../../src/core/public/http/http_fetch_error';
9+
import { HttpFetchError } from 'src/core/public';
1010

1111
describe('snapshot API', () => {
1212
let fetchMock: jest.SpyInstance<Partial<unknown>>;

x-pack/legacy/plugins/uptime/public/state/effects/__tests__/fetch_effect.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
import { call, put } from 'redux-saga/effects';
88
import { fetchEffectFactory } from '../fetch_effect';
99
import { indexStatusAction } from '../../actions';
10-
import { HttpFetchError } from '../../../../../../../../src/core/public/http/http_fetch_error';
10+
import { HttpFetchError } from 'src/core/public';
1111
import { StatesIndexStatus } from '../../../../common/runtime_types';
1212
import { fetchIndexStatus } from '../../api';
1313

x-pack/plugins/actions/server/routes/create.test.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
* you may not use this file except in compliance with the Elastic License.
55
*/
66
import { createActionRoute } from './create';
7-
import { mockRouter, RouterMock } from '../../../../../src/core/server/http/router/router.mock';
7+
import { httpServiceMock } from 'src/core/server/mocks';
88
import { licenseStateMock } from '../lib/license_state.mock';
99
import { verifyApiAccess, ActionTypeDisabledError } from '../lib';
1010
import { mockHandlerArguments } from './_mock_handler_arguments';
@@ -20,7 +20,7 @@ beforeEach(() => {
2020
describe('createActionRoute', () => {
2121
it('creates an action with proper parameters', async () => {
2222
const licenseState = licenseStateMock.create();
23-
const router: RouterMock = mockRouter.create();
23+
const router = httpServiceMock.createRouter();
2424

2525
createActionRoute(router, licenseState);
2626

@@ -83,7 +83,7 @@ describe('createActionRoute', () => {
8383

8484
it('ensures the license allows creating actions', async () => {
8585
const licenseState = licenseStateMock.create();
86-
const router: RouterMock = mockRouter.create();
86+
const router = httpServiceMock.createRouter();
8787

8888
createActionRoute(router, licenseState);
8989

@@ -107,7 +107,7 @@ describe('createActionRoute', () => {
107107

108108
it('ensures the license check prevents creating actions', async () => {
109109
const licenseState = licenseStateMock.create();
110-
const router: RouterMock = mockRouter.create();
110+
const router = httpServiceMock.createRouter();
111111

112112
(verifyApiAccess as jest.Mock).mockImplementation(() => {
113113
throw new Error('OMG');
@@ -135,7 +135,7 @@ describe('createActionRoute', () => {
135135

136136
it('ensures the action type gets validated for the license', async () => {
137137
const licenseState = licenseStateMock.create();
138-
const router: RouterMock = mockRouter.create();
138+
const router = httpServiceMock.createRouter();
139139

140140
createActionRoute(router, licenseState);
141141

x-pack/plugins/actions/server/routes/delete.test.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
* you may not use this file except in compliance with the Elastic License.
55
*/
66
import { deleteActionRoute } from './delete';
7-
import { mockRouter, RouterMock } from '../../../../../src/core/server/http/router/router.mock';
7+
import { httpServiceMock } from 'src/core/server/mocks';
88
import { licenseStateMock } from '../lib/license_state.mock';
99
import { verifyApiAccess } from '../lib';
1010
import { mockHandlerArguments } from './_mock_handler_arguments';
@@ -20,7 +20,7 @@ beforeEach(() => {
2020
describe('deleteActionRoute', () => {
2121
it('deletes an action with proper parameters', async () => {
2222
const licenseState = licenseStateMock.create();
23-
const router: RouterMock = mockRouter.create();
23+
const router = httpServiceMock.createRouter();
2424

2525
deleteActionRoute(router, licenseState);
2626

@@ -65,7 +65,7 @@ describe('deleteActionRoute', () => {
6565

6666
it('ensures the license allows deleting actions', async () => {
6767
const licenseState = licenseStateMock.create();
68-
const router: RouterMock = mockRouter.create();
68+
const router = httpServiceMock.createRouter();
6969

7070
deleteActionRoute(router, licenseState);
7171

@@ -86,7 +86,7 @@ describe('deleteActionRoute', () => {
8686

8787
it('ensures the license check prevents deleting actions', async () => {
8888
const licenseState = licenseStateMock.create();
89-
const router: RouterMock = mockRouter.create();
89+
const router = httpServiceMock.createRouter();
9090

9191
(verifyApiAccess as jest.Mock).mockImplementation(() => {
9292
throw new Error('OMG');

0 commit comments

Comments
 (0)