2019-03-29 08:48:32 +01:00
|
|
|
/* eslint-disable-line @kbn/eslint/require-license-header */
|
|
|
|
/*
|
|
|
|
* This product uses import/no-restricted-paths which is available under a
|
|
|
|
* "MIT" license.
|
|
|
|
*
|
|
|
|
* The MIT License (MIT)
|
|
|
|
*
|
|
|
|
* Copyright (c) 2015-present, Ben Mosher
|
|
|
|
* https://github.com/benmosher/eslint-plugin-import
|
|
|
|
*
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
* of this software and associated documentation files (the "Software"), to deal
|
|
|
|
* in the Software without restriction, including without limitation the rights
|
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
* copies of the Software, and to permit persons to whom the Software is
|
|
|
|
* furnished to do so, subject to the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice shall be included in
|
|
|
|
* all copies or substantial portions of the Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
* THE SOFTWARE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
const path = require('path');
|
|
|
|
const { RuleTester } = require('eslint');
|
2020-12-11 00:51:25 +01:00
|
|
|
const rule = require('./no_restricted_paths');
|
2019-03-29 08:48:32 +01:00
|
|
|
|
|
|
|
const ruleTester = new RuleTester({
|
2019-10-22 01:32:31 +02:00
|
|
|
parser: require.resolve('babel-eslint'),
|
2019-03-29 08:48:32 +01:00
|
|
|
parserOptions: {
|
|
|
|
sourceType: 'module',
|
2019-06-03 19:52:38 +02:00
|
|
|
ecmaVersion: 2018,
|
2019-03-29 08:48:32 +01:00
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
ruleTester.run('@kbn/eslint/no-restricted-paths', rule, {
|
|
|
|
valid: [
|
|
|
|
{
|
|
|
|
code: 'import a from "../client/a.js"',
|
2020-12-11 00:51:25 +01:00
|
|
|
filename: path.join(__dirname, './__fixtures__/no_restricted_paths/server/b.js'),
|
2019-03-29 08:48:32 +01:00
|
|
|
options: [
|
|
|
|
{
|
|
|
|
basePath: __dirname,
|
|
|
|
zones: [
|
|
|
|
{
|
2020-12-11 00:51:25 +01:00
|
|
|
target: '__fixtures__/no_restricted_paths/server/**/*',
|
|
|
|
from: '__fixtures__/no_restricted_paths/other/**/*',
|
2019-03-29 08:48:32 +01:00
|
|
|
},
|
|
|
|
],
|
|
|
|
},
|
|
|
|
],
|
|
|
|
},
|
|
|
|
{
|
|
|
|
code: 'const a = require("../client/a.js")',
|
2020-12-11 00:51:25 +01:00
|
|
|
filename: path.join(__dirname, './__fixtures__/no_restricted_paths/server/b.js'),
|
2019-03-29 08:48:32 +01:00
|
|
|
options: [
|
|
|
|
{
|
|
|
|
basePath: __dirname,
|
|
|
|
zones: [
|
|
|
|
{
|
2020-12-11 00:51:25 +01:00
|
|
|
target: '__fixtures__/no_restricted_paths/server/**/*',
|
|
|
|
from: '__fixtures__/no_restricted_paths/other/**/*',
|
2019-03-29 08:48:32 +01:00
|
|
|
},
|
|
|
|
],
|
|
|
|
},
|
|
|
|
],
|
|
|
|
},
|
|
|
|
{
|
|
|
|
code: 'import b from "../server/b.js"',
|
2020-12-11 00:51:25 +01:00
|
|
|
filename: path.join(__dirname, './__fixtures__/no_restricted_paths/client/a.js'),
|
2019-03-29 08:48:32 +01:00
|
|
|
options: [
|
|
|
|
{
|
|
|
|
basePath: __dirname,
|
|
|
|
zones: [
|
|
|
|
{
|
|
|
|
target: '**/no_restricted_paths/client/**/*',
|
|
|
|
from: '**/no_restricted_paths/other/**/*',
|
|
|
|
},
|
|
|
|
],
|
|
|
|
},
|
|
|
|
],
|
|
|
|
},
|
|
|
|
|
|
|
|
// irrelevant function calls
|
|
|
|
{
|
|
|
|
code: 'notrequire("../server/b.js")',
|
|
|
|
options: [
|
|
|
|
{
|
|
|
|
basePath: __dirname,
|
|
|
|
},
|
|
|
|
],
|
|
|
|
},
|
|
|
|
{
|
|
|
|
code: 'notrequire("../server/b.js")',
|
2020-12-11 00:51:25 +01:00
|
|
|
filename: path.join(__dirname, './__fixtures__/no_restricted_paths/client/a.js'),
|
2019-03-29 08:48:32 +01:00
|
|
|
options: [
|
|
|
|
{
|
|
|
|
basePath: __dirname,
|
|
|
|
zones: [
|
|
|
|
{
|
2020-12-11 00:51:25 +01:00
|
|
|
target: '__fixtures__/no_restricted_paths/client/**/*',
|
|
|
|
from: '__fixtures__/no_restricted_paths/server/**/*',
|
2019-03-29 08:48:32 +01:00
|
|
|
},
|
|
|
|
],
|
|
|
|
},
|
|
|
|
],
|
|
|
|
},
|
|
|
|
|
|
|
|
// no config
|
|
|
|
{
|
|
|
|
code: 'require("../server/b.js")',
|
|
|
|
options: [
|
|
|
|
{
|
|
|
|
basePath: __dirname,
|
|
|
|
},
|
|
|
|
],
|
|
|
|
},
|
|
|
|
{
|
|
|
|
code: 'import b from "../server/b.js"',
|
|
|
|
options: [
|
|
|
|
{
|
|
|
|
basePath: __dirname,
|
|
|
|
},
|
|
|
|
],
|
|
|
|
},
|
|
|
|
|
|
|
|
// builtin (ignore)
|
|
|
|
{
|
|
|
|
code: 'require("os")',
|
|
|
|
options: [
|
|
|
|
{
|
|
|
|
basePath: __dirname,
|
|
|
|
},
|
|
|
|
],
|
|
|
|
},
|
|
|
|
|
|
|
|
{
|
|
|
|
code: 'const d = require("./deep/d.js")',
|
2020-12-11 00:51:25 +01:00
|
|
|
filename: path.join(__dirname, './__fixtures__/no_restricted_paths/server/b.js'),
|
2019-03-29 08:48:32 +01:00
|
|
|
options: [
|
|
|
|
{
|
|
|
|
basePath: __dirname,
|
|
|
|
zones: [
|
|
|
|
{
|
|
|
|
allowSameFolder: true,
|
2020-12-11 00:51:25 +01:00
|
|
|
target: '__fixtures__/no_restricted_paths/**/*',
|
|
|
|
from: '__fixtures__/no_restricted_paths/**/*',
|
2019-03-29 08:48:32 +01:00
|
|
|
},
|
|
|
|
],
|
|
|
|
},
|
|
|
|
],
|
|
|
|
},
|
2019-04-10 14:56:09 +02:00
|
|
|
{
|
|
|
|
code: 'const d = require("./deep/d.js")',
|
2020-12-11 00:51:25 +01:00
|
|
|
filename: path.join(__dirname, './__fixtures__/no_restricted_paths/server/b.js'),
|
2019-04-10 14:56:09 +02:00
|
|
|
options: [
|
|
|
|
{
|
|
|
|
basePath: __dirname,
|
|
|
|
zones: [
|
|
|
|
{
|
|
|
|
allowSameFolder: true,
|
2020-12-11 00:51:25 +01:00
|
|
|
target: '__fixtures__/no_restricted_paths/**/*',
|
|
|
|
from: [
|
|
|
|
'__fixtures__/no_restricted_paths/**/*',
|
|
|
|
'!__fixtures__/no_restricted_paths/server/b*',
|
|
|
|
],
|
2019-04-10 14:56:09 +02:00
|
|
|
},
|
|
|
|
],
|
|
|
|
},
|
|
|
|
],
|
|
|
|
},
|
2019-10-31 11:40:53 +01:00
|
|
|
|
|
|
|
{
|
|
|
|
// Check if dirs that start with 'index' work correctly.
|
|
|
|
code: 'import { X } from "./index_patterns"',
|
2020-12-11 00:51:25 +01:00
|
|
|
filename: path.join(__dirname, './__fixtures__/no_restricted_paths/server/b.js'),
|
2019-10-31 11:40:53 +01:00
|
|
|
options: [
|
|
|
|
{
|
|
|
|
basePath: __dirname,
|
|
|
|
zones: [
|
|
|
|
{
|
2020-12-11 00:51:25 +01:00
|
|
|
target: ['__fixtures__/no_restricted_paths/(public|server)/**/*'],
|
2019-10-31 11:40:53 +01:00
|
|
|
from: [
|
2020-12-11 00:51:25 +01:00
|
|
|
'__fixtures__/no_restricted_paths/server/**/*',
|
|
|
|
'!__fixtures__/no_restricted_paths/server/index.{ts,tsx}',
|
2019-10-31 11:40:53 +01:00
|
|
|
],
|
|
|
|
allowSameFolder: true,
|
|
|
|
},
|
|
|
|
],
|
|
|
|
},
|
|
|
|
],
|
|
|
|
},
|
2019-03-29 08:48:32 +01:00
|
|
|
],
|
|
|
|
|
|
|
|
invalid: [
|
2020-05-15 17:21:42 +02:00
|
|
|
{
|
|
|
|
code: 'export { b } from "../server/b.js"',
|
2020-12-11 00:51:25 +01:00
|
|
|
filename: path.join(__dirname, './__fixtures__/no_restricted_paths/client/a.js'),
|
2020-05-15 17:21:42 +02:00
|
|
|
options: [
|
|
|
|
{
|
|
|
|
basePath: __dirname,
|
|
|
|
zones: [
|
|
|
|
{
|
2020-12-11 00:51:25 +01:00
|
|
|
target: '__fixtures__/no_restricted_paths/client/**/*',
|
|
|
|
from: '__fixtures__/no_restricted_paths/server/**/*',
|
2020-05-15 17:21:42 +02:00
|
|
|
},
|
|
|
|
],
|
|
|
|
},
|
|
|
|
],
|
|
|
|
errors: [
|
|
|
|
{
|
|
|
|
message: 'Unexpected path "../server/b.js" imported in restricted zone.',
|
|
|
|
line: 1,
|
|
|
|
column: 19,
|
|
|
|
},
|
|
|
|
],
|
|
|
|
},
|
2019-03-29 08:48:32 +01:00
|
|
|
{
|
|
|
|
code: 'import b from "../server/b.js"',
|
2020-12-11 00:51:25 +01:00
|
|
|
filename: path.join(__dirname, './__fixtures__/no_restricted_paths/client/a.js'),
|
2019-03-29 08:48:32 +01:00
|
|
|
options: [
|
|
|
|
{
|
|
|
|
basePath: __dirname,
|
|
|
|
zones: [
|
|
|
|
{
|
2020-12-11 00:51:25 +01:00
|
|
|
target: '__fixtures__/no_restricted_paths/client/**/*',
|
|
|
|
from: '__fixtures__/no_restricted_paths/server/**/*',
|
2019-03-29 08:48:32 +01:00
|
|
|
},
|
|
|
|
],
|
|
|
|
},
|
|
|
|
],
|
|
|
|
errors: [
|
|
|
|
{
|
|
|
|
message: 'Unexpected path "../server/b.js" imported in restricted zone.',
|
|
|
|
line: 1,
|
|
|
|
column: 15,
|
|
|
|
},
|
|
|
|
],
|
|
|
|
},
|
|
|
|
{
|
|
|
|
code: 'import a from "../client/a"\nimport c from "./c"',
|
2020-12-11 00:51:25 +01:00
|
|
|
filename: path.join(__dirname, './__fixtures__/no_restricted_paths/server/b.js'),
|
2019-03-29 08:48:32 +01:00
|
|
|
options: [
|
|
|
|
{
|
|
|
|
basePath: __dirname,
|
|
|
|
zones: [
|
|
|
|
{
|
2020-12-11 00:51:25 +01:00
|
|
|
target: '__fixtures__/no_restricted_paths/server/**/*',
|
|
|
|
from: '__fixtures__/no_restricted_paths/client/**/*',
|
2019-03-29 08:48:32 +01:00
|
|
|
},
|
|
|
|
{
|
2020-12-11 00:51:25 +01:00
|
|
|
target: '__fixtures__/no_restricted_paths/server/**/*',
|
|
|
|
from: '__fixtures__/no_restricted_paths/server/c.js',
|
2019-03-29 08:48:32 +01:00
|
|
|
},
|
|
|
|
],
|
|
|
|
},
|
|
|
|
],
|
|
|
|
errors: [
|
|
|
|
{
|
|
|
|
message: 'Unexpected path "../client/a" imported in restricted zone.',
|
|
|
|
line: 1,
|
|
|
|
column: 15,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
message: 'Unexpected path "./c" imported in restricted zone.',
|
|
|
|
line: 2,
|
|
|
|
column: 15,
|
|
|
|
},
|
|
|
|
],
|
|
|
|
},
|
|
|
|
{
|
|
|
|
code: 'const b = require("../server/b.js")',
|
2020-12-11 00:51:25 +01:00
|
|
|
filename: path.join(__dirname, './__fixtures__/no_restricted_paths/client/a.js'),
|
2019-03-29 08:48:32 +01:00
|
|
|
options: [
|
|
|
|
{
|
|
|
|
basePath: __dirname,
|
|
|
|
zones: [
|
|
|
|
{
|
|
|
|
target: '**/no_restricted_paths/client/**/*',
|
|
|
|
from: '**/no_restricted_paths/server/**/*',
|
|
|
|
},
|
|
|
|
],
|
|
|
|
},
|
|
|
|
],
|
|
|
|
errors: [
|
|
|
|
{
|
|
|
|
message: 'Unexpected path "../server/b.js" imported in restricted zone.',
|
|
|
|
line: 1,
|
|
|
|
column: 19,
|
|
|
|
},
|
|
|
|
],
|
|
|
|
},
|
|
|
|
{
|
|
|
|
code: 'const b = require("../server/b.js")',
|
2020-12-11 00:51:25 +01:00
|
|
|
filename: path.join(__dirname, './__fixtures__/no_restricted_paths/client/a.js'),
|
2019-03-29 08:48:32 +01:00
|
|
|
options: [
|
|
|
|
{
|
2020-12-11 00:51:25 +01:00
|
|
|
basePath: path.join(__dirname, '__fixtures__', 'no_restricted_paths'),
|
2019-03-29 08:48:32 +01:00
|
|
|
zones: [
|
|
|
|
{
|
|
|
|
target: 'client/**/*',
|
|
|
|
from: 'server/**/*',
|
|
|
|
},
|
|
|
|
],
|
|
|
|
},
|
|
|
|
],
|
|
|
|
errors: [
|
|
|
|
{
|
|
|
|
message: 'Unexpected path "../server/b.js" imported in restricted zone.',
|
|
|
|
line: 1,
|
|
|
|
column: 19,
|
|
|
|
},
|
|
|
|
],
|
|
|
|
},
|
|
|
|
|
|
|
|
{
|
|
|
|
code: 'const d = require("./deep/d.js")',
|
2020-12-11 00:51:25 +01:00
|
|
|
filename: path.join(__dirname, './__fixtures__/no_restricted_paths/server/b.js'),
|
2019-03-29 08:48:32 +01:00
|
|
|
options: [
|
|
|
|
{
|
|
|
|
basePath: __dirname,
|
|
|
|
zones: [
|
|
|
|
{
|
2020-12-11 00:51:25 +01:00
|
|
|
target: '__fixtures__/no_restricted_paths/**/*',
|
|
|
|
from: '__fixtures__/no_restricted_paths/**/*',
|
2019-03-29 08:48:32 +01:00
|
|
|
},
|
|
|
|
],
|
|
|
|
},
|
|
|
|
],
|
|
|
|
errors: [
|
|
|
|
{
|
|
|
|
message: 'Unexpected path "./deep/d.js" imported in restricted zone.',
|
|
|
|
line: 1,
|
|
|
|
column: 19,
|
|
|
|
},
|
|
|
|
],
|
|
|
|
},
|
2019-07-18 22:52:13 +02:00
|
|
|
|
|
|
|
{
|
|
|
|
// Does not allow to import deeply within Core, using "src/core/..." Webpack alias.
|
|
|
|
code: 'const d = require("src/core/server/saved_objects")',
|
2020-12-11 00:51:25 +01:00
|
|
|
filename: path.join(__dirname, './__fixtures__/no_restricted_paths/client/a.js'),
|
2019-07-18 22:52:13 +02:00
|
|
|
options: [
|
|
|
|
{
|
|
|
|
basePath: __dirname,
|
|
|
|
zones: [
|
|
|
|
{
|
2020-12-11 00:51:25 +01:00
|
|
|
target: '__fixtures__/no_restricted_paths/**/*',
|
2019-07-18 22:52:13 +02:00
|
|
|
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")',
|
2020-12-11 00:51:25 +01:00
|
|
|
filename: path.join(__dirname, './__fixtures__/no_restricted_paths/client/a.js'),
|
2019-07-18 22:52:13 +02:00
|
|
|
options: [
|
|
|
|
{
|
|
|
|
basePath: __dirname,
|
|
|
|
zones: [
|
|
|
|
{
|
|
|
|
from: ['src/legacy/ui/**/*', 'ui/**/*'],
|
2020-12-11 00:51:25 +01:00
|
|
|
target: '__fixtures__/no_restricted_paths/**/*',
|
2019-07-18 22:52:13 +02:00
|
|
|
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")',
|
2020-12-11 00:51:25 +01:00
|
|
|
filename: path.join(__dirname, './__fixtures__/no_restricted_paths/client/a.js'),
|
2019-07-18 22:52:13 +02:00
|
|
|
options: [
|
|
|
|
{
|
|
|
|
basePath: __dirname,
|
|
|
|
zones: [
|
|
|
|
{
|
|
|
|
from: ['src/legacy/ui/**/*', 'ui/**/*'],
|
2020-12-11 00:51:25 +01:00
|
|
|
target: '__fixtures__/no_restricted_paths/**/*',
|
2019-07-18 22:52:13 +02:00
|
|
|
allowSameFolder: true,
|
|
|
|
},
|
|
|
|
],
|
|
|
|
},
|
|
|
|
],
|
|
|
|
errors: [
|
|
|
|
{
|
|
|
|
message: 'Unexpected path "ui/kfetch/public/index" imported in restricted zone.',
|
|
|
|
line: 1,
|
|
|
|
column: 19,
|
|
|
|
},
|
|
|
|
],
|
|
|
|
},
|
2019-10-31 11:40:53 +01:00
|
|
|
|
|
|
|
{
|
|
|
|
// Don't use index*.
|
|
|
|
// It won't work with dirs that start with 'index'.
|
|
|
|
code: 'import { X } from "./index_patterns"',
|
2020-12-11 00:51:25 +01:00
|
|
|
filename: path.join(__dirname, './__fixtures__/no_restricted_paths/server/b.js'),
|
2019-10-31 11:40:53 +01:00
|
|
|
options: [
|
|
|
|
{
|
|
|
|
basePath: __dirname,
|
|
|
|
zones: [
|
|
|
|
{
|
2020-12-11 00:51:25 +01:00
|
|
|
target: ['__fixtures__/no_restricted_paths/(public|server)/**/*'],
|
2019-10-31 11:40:53 +01:00
|
|
|
from: [
|
2020-12-11 00:51:25 +01:00
|
|
|
'__fixtures__/no_restricted_paths/server/**/*',
|
|
|
|
'!__fixtures__/no_restricted_paths/server/index*',
|
2019-10-31 11:40:53 +01:00
|
|
|
],
|
|
|
|
allowSameFolder: true,
|
|
|
|
},
|
|
|
|
],
|
|
|
|
},
|
|
|
|
],
|
|
|
|
errors: [
|
|
|
|
{
|
|
|
|
message: 'Unexpected path "./index_patterns" imported in restricted zone.',
|
|
|
|
line: 1,
|
|
|
|
column: 19,
|
|
|
|
},
|
|
|
|
],
|
|
|
|
},
|
2019-03-29 08:48:32 +01:00
|
|
|
],
|
|
|
|
});
|