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
mshustov
merged 8 commits into
elastic:master
from
mshustov:issue-33444-restrict-imports-eslint
Mar 29, 2019
Merged
Changes from 6 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
5e8cde7
restrict import from core&plugin internals
mshustov 5e1510a
Fork import/no-restricted-paths and add allowSameFolder option
mshustov f0a89b6
update notices
mshustov 8c0d2b5
add basePath option
mshustov cdaf22e
support glob pattern instead of reagexp
mshustov 6c9c965
Merge branch 'master' into issue-33444-restrict-imports-eslint
mshustov 253a96f
remove @notice, make basePath required
mshustov 328c122
Merge branch 'master' into issue-33444-restrict-imports-eslint
mshustov File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1 change: 1 addition & 0 deletions
1
packages/kbn-eslint-plugin-eslint/rules/__tests__/files/no_restricted_paths/client/a.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
/* eslint-disable */ |
1 change: 1 addition & 0 deletions
1
packages/kbn-eslint-plugin-eslint/rules/__tests__/files/no_restricted_paths/server/b.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
/* eslint-disable */ |
1 change: 1 addition & 0 deletions
1
packages/kbn-eslint-plugin-eslint/rules/__tests__/files/no_restricted_paths/server/c.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
/* eslint-disable */ |
1 change: 1 addition & 0 deletions
1
packages/kbn-eslint-plugin-eslint/rules/__tests__/files/no_restricted_paths/server/deep/d.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
/* eslint-disable */ |
254 changes: 254 additions & 0 deletions
254
packages/kbn-eslint-plugin-eslint/rules/__tests__/no_restricted_paths.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,254 @@ | ||
/* 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, | ||
}, | ||
}); | ||
|
||
function resolveAbsolute(relativePath) { | ||
return path.join(__dirname, 'files', relativePath); | ||
} | ||
|
||
function resolveRelative(relativePath) { | ||
return path.join(path.relative(process.cwd(), __dirname), 'files', relativePath); | ||
} | ||
|
||
ruleTester.run('@kbn/eslint/no-restricted-paths', rule, { | ||
valid: [ | ||
{ | ||
code: 'import a from "../client/a.js"', | ||
filename: resolveAbsolute('./no_restricted_paths/server/b.js'), | ||
options: [ | ||
{ | ||
zones: [ | ||
{ | ||
target: '**/no_restricted_paths/server/**/*', | ||
from: '**/no_restricted_paths/other/**/*', | ||
}, | ||
], | ||
}, | ||
], | ||
}, | ||
{ | ||
code: 'const a = require("../client/a.js")', | ||
filename: resolveAbsolute('./no_restricted_paths/server/b.js'), | ||
options: [ | ||
{ | ||
zones: [ | ||
{ | ||
target: resolveRelative('./no_restricted_paths/server/**/*'), | ||
from: resolveRelative('./no_restricted_paths/other/**/*'), | ||
}, | ||
], | ||
}, | ||
], | ||
}, | ||
{ | ||
code: 'import b from "../server/b.js"', | ||
filename: resolveAbsolute('./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: resolveAbsolute('./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: resolveAbsolute('./no_restricted_paths/server/b.js'), | ||
options: [ | ||
{ | ||
zones: [ | ||
{ | ||
allowSameFolder: true, | ||
target: '**/no_restricted_paths/**/*', | ||
from: '**/no_restricted_paths/**/*', | ||
}, | ||
], | ||
}, | ||
], | ||
}, | ||
], | ||
|
||
invalid: [ | ||
{ | ||
code: 'import b from "../server/b.js"', | ||
filename: resolveAbsolute('./no_restricted_paths/client/a.js'), | ||
options: [ | ||
{ | ||
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: 15, | ||
}, | ||
], | ||
}, | ||
{ | ||
code: 'import a from "../client/a"\nimport c from "./c"', | ||
filename: resolveAbsolute('./no_restricted_paths/server/b.js'), | ||
options: [ | ||
{ | ||
zones: [ | ||
{ | ||
target: '**/no_restricted_paths/server/**/*', | ||
from: '**/no_restricted_paths/client/**/*', | ||
}, | ||
{ | ||
target: '**/no_restricted_paths/server/**/*', | ||
from: '**/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: resolveAbsolute('./no_restricted_paths/client/a.js'), | ||
options: [ | ||
{ | ||
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: resolveAbsolute('./no_restricted_paths/client/a.js'), | ||
options: [ | ||
{ | ||
zones: [ | ||
{ | ||
target: 'client/**/*', | ||
from: 'server/**/*', | ||
}, | ||
], | ||
basePath: resolveRelative('./no_restricted_paths'), | ||
}, | ||
], | ||
errors: [ | ||
{ | ||
message: 'Unexpected path "../server/b.js" imported in restricted zone.', | ||
line: 1, | ||
column: 19, | ||
}, | ||
], | ||
}, | ||
|
||
{ | ||
code: 'const d = require("./deep/d.js")', | ||
filename: resolveAbsolute('./no_restricted_paths/server/b.js'), | ||
options: [ | ||
{ | ||
zones: [ | ||
{ | ||
target: '**/no_restricted_paths/**/*', | ||
from: '**/no_restricted_paths/**/*', | ||
}, | ||
], | ||
}, | ||
], | ||
errors: [ | ||
{ | ||
message: 'Unexpected path "./deep/d.js" imported in restricted zone.', | ||
line: 1, | ||
column: 19, | ||
}, | ||
], | ||
}, | ||
], | ||
}); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.