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

[New platform] Restrict import from core&plugin internals for js files #33697

Merged
merged 8 commits into from
Mar 29, 2019
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
51 changes: 51 additions & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@ const ELASTIC_LICENSE_HEADER = `
*/
`;

const corePublicPattern = './src/core/public/(?!utils|index).*';
spalger marked this conversation as resolved.
Show resolved Hide resolved
const coreServerPattern = './src/core/server/(?!index).*';
const pluginsPublicPattern = './src/plugins/.*/public/(?!index).*';
const pluginsServerPattern = './src/plugins/.*/server/(?!index).*';
module.exports = {
extends: ['@elastic/eslint-config-kibana', '@elastic/eslint-config-kibana/jest'],
plugins: ['@kbn/eslint-plugin-eslint'],
Expand All @@ -47,6 +51,53 @@ module.exports = {
rules: {
'no-restricted-imports': [2, restrictedModules],
'no-restricted-modules': [2, restrictedModules],
'@kbn/eslint/no-restricted-paths': [
'error',
{
zones: [
{
// when tslint is removed we will check *.ts files as well
target: './src/legacy/.*js$',
Copy link
Contributor

Choose a reason for hiding this comment

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

I'm not opposed to limiting to .js files, but do you think it's necessary? ESLint will only process the files .js files, and this will automatically apply to .ts files as soon as we get them in here without this limitation.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

didn't want to force #33826 to comment/fix all problem places in *.ts files.

Copy link
Contributor

Choose a reason for hiding this comment

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

We're going to limit all existing rules to .js files in that PR, so I wouldn't worry about it.

from: [
corePublicPattern,
coreServerPattern,
pluginsPublicPattern,
pluginsServerPattern,
],
},
{
target: './x-pack/(?!.*test).*js$',
from: [
corePublicPattern,
coreServerPattern,
pluginsPublicPattern,
pluginsServerPattern,
],
},
{
target: './src/core/public/.*js$',
from: [coreServerPattern, pluginsPublicPattern, pluginsServerPattern],
},
{
target: './src/core/server/.*js$',
from: [corePublicPattern, pluginsPublicPattern, pluginsServerPattern],
},
{
target: './src/plugins/.*/public/.*js$',
from: [corePublicPattern, coreServerPattern, pluginsServerPattern],
},
{
target: './src/plugins/.*/server/.*js$',
from: [corePublicPattern, coreServerPattern, pluginsPublicPattern],
},
{
target: './src/plugins/.*/(public|server)/.*js$',
from: [pluginsPublicPattern, pluginsServerPattern],
allowSameFolder: true,
},
],
},
],
},

overrides: [
Expand Down
27 changes: 27 additions & 0 deletions NOTICE.txt
Original file line number Diff line number Diff line change
Expand Up @@ -184,3 +184,30 @@ 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.

---
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.

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,5 +22,6 @@ 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'),
},
};
7 changes: 6 additions & 1 deletion packages/kbn-eslint-plugin-eslint/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,12 @@
"eslint": "^5.6.0",
"babel-eslint": "^9.0.0"
},
"devDependencies": {
"@types/eslint": "^4.16.6"
spalger marked this conversation as resolved.
Show resolved Hide resolved
},
"dependencies": {
"dedent": "^0.7.0"
"contains-path": "^0.1.0",
"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,228 @@
/* eslint-disable @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,
},
});

function resolve(relativePath) {
return path.join(__dirname, 'files', relativePath);
}

ruleTester.run('@kbn/eslint/no-restricted-paths', rule, {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

@spalger I cannot see "test" script in https://github.com/elastic/kibana/blob/master/packages/kbn-eslint-plugin-eslint/package.json
how we run tests for the package? I did use `node path/to/test' to run tests manually

Copy link
Contributor

@spalger spalger Mar 25, 2019

Choose a reason for hiding this comment

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

node scripts/mocha packages/kbn-eslint-plugin-eslint/rules/__tests__/no_restricted_paths.js

valid: [
{
code: 'import a from "../client/a.js"',
filename: resolve('./no_restricted_paths/server/b.js'),
options: [
{
zones: [
{
target: resolve('./no_restricted_paths/server'),
from: resolve('./no_restricted_paths/other'),
},
],
},
],
},
{
code: 'const a = require("../client/a.js")',
filename: resolve('./no_restricted_paths/server/b.js'),
options: [
{
zones: [
{
target: './no_restricted_paths/server',
from: './no_restricted_paths/other',
},
],
},
],
},
{
code: 'import b from "../server/b.js"',
filename: resolve('./no_restricted_paths/client/a.js'),
options: [
{
zones: [
{
target: './no_restricted_paths/client',
from: './no_restricted_paths/other',
},
],
},
],
},

// irrelevant function calls
{ code: 'notrequire("../server/b.js")' },
{
code: 'notrequire("../server/b.js")',
filename: resolve('./no_restricted_paths/client/a.js'),
options: [
{
zones: [
{
target: './no_restricted_paths/client',
from: './no_restricted_paths/server',
},
],
},
],
},

// no config
{ code: 'require("../server/b.js")' },
{ code: 'import b from "../server/b.js"' },

// builtin (ignore)
{ code: 'require("os")' },

{
code: 'const d = require("./deep/d.js")',
filename: resolve('./no_restricted_paths/server/b.js'),
options: [
{
zones: [
{
allowSameFolder: true,
target: resolve('./no_restricted_paths/'),
from: resolve('./no_restricted_paths/'),
},
],
},
],
},
],

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

{
code: 'const d = require("./deep/d.js")',
filename: resolve('./no_restricted_paths/server/b.js'),
options: [
{
zones: [
{
target: resolve('./no_restricted_paths/'),
from: resolve('./no_restricted_paths/'),
},
],
},
],
errors: [
{
message: 'Unexpected path "./deep/d.js" imported in restricted zone.',
line: 1,
column: 19,
},
],
},
],
});