Skip to content

Commit

Permalink
[New platform] Restrict import from core&plugin internals for js files (
Browse files Browse the repository at this point in the history
elastic#33697)

* restrict import from core&plugin internals

* Fork import/no-restricted-paths and add allowSameFolder option

Our use case requires to restrict imports from plugin folders, which names are unknown for us yet. We cannot use 'import/no-restricted-paths' in the current state, because if we define 'from: plugins/*/server/' the rule will report all relative imports in the same folder as well. To fix this problem we added another option 'allowSameFolder' that makes the rule to ignore imports in the same folder.

* update notices

* add basePath option

* support glob pattern instead of reagexp

* remove @notice, make basePath required
  • Loading branch information
mshustov committed Mar 29, 2019
1 parent 09ec26f commit 31b1ad0
Show file tree
Hide file tree
Showing 13 changed files with 488 additions and 20 deletions.
32 changes: 32 additions & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,38 @@ module.exports = {
rules: {
'no-restricted-imports': [2, restrictedModules],
'no-restricted-modules': [2, restrictedModules],
'@kbn/eslint/no-restricted-paths': [
'error',
{
basePath: __dirname,
zones: [
{
target: [
'src/legacy/**/*',
'x-pack/**/*',
'!x-pack/**/*.test.*',
'src/plugins/**/(public|server)/**/*',
'src/core/(public|server)/**/*',
],
from: [
'src/core/public/**/*',
'!src/core/public/index*',
'!src/core/public/utils/**/*',

'src/core/server/**/*',
'!src/core/server/index*',

'src/plugins/**/public/**/*',
'!src/plugins/**/public/index*',

'src/plugins/**/server/**/*',
'!src/plugins/**/server/index*',
],
allowSameFolder: true,
},
],
},
],
'@kbn/eslint/module_migration': [
'error',
[
Expand Down
1 change: 1 addition & 0 deletions packages/kbn-eslint-plugin-eslint/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ module.exports = {
'require-license-header': require('./rules/require_license_header'),
'disallow-license-headers': require('./rules/disallow_license_headers'),
'no-default-export': require('./rules/no_default_export'),
'no-restricted-paths': require('./rules/no_restricted_paths'),
module_migration: require('./rules/module_migration'),
},
};
4 changes: 3 additions & 1 deletion packages/kbn-eslint-plugin-eslint/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
"babel-eslint": "^10.0.1"
},
"dependencies": {
"dedent": "^0.7.0"
"micromatch": "3.1.10",
"dedent": "^0.7.0",
"eslint-module-utils": "^2.3.0"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/* eslint-disable */
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/* eslint-disable */
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/* eslint-disable */
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/* eslint-disable */
Original file line number Diff line number Diff line change
@@ -0,0 +1,283 @@
/* 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');
const rule = require('../no_restricted_paths');

const ruleTester = new RuleTester({
parser: 'babel-eslint',
parserOptions: {
sourceType: 'module',
ecmaVersion: 2015,
},
});

ruleTester.run('@kbn/eslint/no-restricted-paths', rule, {
valid: [
{
code: 'import a from "../client/a.js"',
filename: path.join(__dirname, './files/no_restricted_paths/server/b.js'),
options: [
{
basePath: __dirname,
zones: [
{
target: 'files/no_restricted_paths/server/**/*',
from: 'files/no_restricted_paths/other/**/*',
},
],
},
],
},
{
code: 'const a = require("../client/a.js")',
filename: path.join(__dirname, './files/no_restricted_paths/server/b.js'),
options: [
{
basePath: __dirname,
zones: [
{
target: 'files/no_restricted_paths/server/**/*',
from: 'files/no_restricted_paths/other/**/*',
},
],
},
],
},
{
code: 'import b from "../server/b.js"',
filename: path.join(__dirname, './files/no_restricted_paths/client/a.js'),
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")',
filename: path.join(__dirname, './files/no_restricted_paths/client/a.js'),
options: [
{
basePath: __dirname,
zones: [
{
target: 'files/no_restricted_paths/client/**/*',
from: 'files/no_restricted_paths/server/**/*',
},
],
},
],
},

// 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")',
filename: path.join(__dirname, './files/no_restricted_paths/server/b.js'),
options: [
{
basePath: __dirname,
zones: [
{
allowSameFolder: true,
target: 'files/no_restricted_paths/**/*',
from: 'files/no_restricted_paths/**/*',
},
],
},
],
},
],

invalid: [
{
code: 'import b from "../server/b.js"',
filename: path.join(__dirname, './files/no_restricted_paths/client/a.js'),
options: [
{
basePath: __dirname,
zones: [
{
target: 'files/no_restricted_paths/client/**/*',
from: 'files/no_restricted_paths/server/**/*',
},
],
},
],
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"',
filename: path.join(__dirname, './files/no_restricted_paths/server/b.js'),
options: [
{
basePath: __dirname,
zones: [
{
target: 'files/no_restricted_paths/server/**/*',
from: 'files/no_restricted_paths/client/**/*',
},
{
target: 'files/no_restricted_paths/server/**/*',
from: 'files/no_restricted_paths/server/c.js',
},
],
},
],
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")',
filename: path.join(__dirname, './files/no_restricted_paths/client/a.js'),
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")',
filename: path.join(__dirname, './files/no_restricted_paths/client/a.js'),
options: [
{
basePath: path.join(__dirname, 'files', 'no_restricted_paths'),
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")',
filename: path.join(__dirname, './files/no_restricted_paths/server/b.js'),
options: [
{
basePath: __dirname,
zones: [
{
target: 'files/no_restricted_paths/**/*',
from: 'files/no_restricted_paths/**/*',
},
],
},
],
errors: [
{
message: 'Unexpected path "./deep/d.js" imported in restricted zone.',
line: 1,
column: 19,
},
],
},
],
});

0 comments on commit 31b1ad0

Please sign in to comment.