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

Conversation

mshustov
Copy link
Contributor

@mshustov mshustov commented Mar 22, 2019

import/no-restricted-paths supports restrictions by zone mechanism.

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 I added another option 'allowSameFolder' that makes the rule to ignore imports in the same folder.

Checklist

Use strikethroughs to remove checklist items you don't feel are applicable to this PR.

For maintainers

@mshustov mshustov added Team:Core Core services & architecture: plugins, logging, config, saved objects, http, ES client, i18n, etc non-issue Indicates to automation that a pull request should not appear in the release notes Feature:New Platform v7.2.0 labels Mar 22, 2019
@elasticmachine
Copy link
Contributor

Pinging @elastic/kibana-platform

@elasticmachine
Copy link
Contributor

💔 Build Failed

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.
@mshustov mshustov force-pushed the issue-33444-restrict-imports-eslint branch from 50ea2d2 to 5e1510a Compare March 25, 2019 10:11
@mshustov mshustov added release_note:skip Skip the PR/issue when compiling release notes review and removed non-issue Indicates to automation that a pull request should not appear in the release notes labels Mar 25, 2019
@mshustov mshustov marked this pull request as ready for review March 25, 2019 10:14
@mshustov mshustov requested review from a team March 25, 2019 10:14
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

@elasticmachine
Copy link
Contributor

💔 Build Failed

@elasticmachine
Copy link
Contributor

💚 Build Succeeded

packages/kbn-eslint-plugin-eslint/package.json Outdated Show resolved Hide resolved
create(context) {
const options = context.options[0] || {};
const zones = options.zones || [];
const basePath = process.cwd();
Copy link
Contributor

Choose a reason for hiding this comment

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

Can we make this configurable? We use .js files for eslint config so we can provide the absolute root for all files, or even require that zone.target/zone.target are absolute.

.eslintrc.js Outdated Show resolved Hide resolved
.eslintrc.js Outdated
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.

@@ -0,0 +1,149 @@
/* eslint-disable @kbn/eslint/require-license-header */
/* @notice
Copy link
Contributor

Choose a reason for hiding this comment

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

Since we don't ship this code in the distributable I don't think we need to include the @notice, do you agree @epixa?

Copy link
Contributor

Choose a reason for hiding this comment

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

Hm, I'd have to reacquaint myself with how we handle that annotation. I don't think it makes sense to ship this package's license info in our distributions' notice file. I do still think we should be automatically checking it's license, albeit against our dev-acceptable list. In general, I'd rather ship a dependency's info in our notice file by accident than accidentally leave out a dependency we should have included.

Copy link
Contributor

Choose a reason for hiding this comment

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

This tag doesn't cause the licence to be checked, that's something we do "manually" by committing the code to the repo. This tag is only for generating the notice file in the build, so I think we can get rid of it.

@epixa
Copy link
Contributor

epixa commented Mar 25, 2019

I think I saw a reason for this, but I can't track it down: can we contribute the change upstream?

@mshustov
Copy link
Contributor Author

@epixa I was going but realized it could take some time to have it merged. At least there are several PRs are waiting for CR import-js/eslint-plugin-import#1238

@mshustov
Copy link
Contributor Author

mshustov commented Mar 26, 2019

@spalger switched to micromatch. it has multimatch out-of-the-box and faster than minimatch https://gist.github.com/jonschlinkert/96212e11d128fc659d12.
as you asked in #33697 (comment)
not sure if we should keep reference to import/no-restricted-paths, there is another implementation and comment can be misleading. should I remove it ?

@elasticmachine
Copy link
Contributor

💚 Build Succeeded

}

function resolveBasePath(basePath) {
return path.isAbsolute(basePath) ? basePath : path.relative(process.cwd(), basePath);
Copy link
Contributor

Choose a reason for hiding this comment

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

path.resolve(process.cwd(), basePath) is going to return a relative path... I don't think that's what you're trying to do here.

create(context) {
const options = context.options[0] || {};
const zones = options.zones || [];
const basePath = options.basePath ? resolveBasePath(options.basePath) : process.cwd();
Copy link
Contributor

Choose a reason for hiding this comment

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

Why not just require options.basePath? I think it would make the behavior of this rule more explicit and easier to reason about.

const basePath = options.basePath;
if (!basePath || !isAboslute(basePath)) {
  throw new Error('basePath option must be specified and must be absolute')
}

@elasticmachine
Copy link
Contributor

💚 Build Succeeded

@mshustov mshustov requested a review from spalger March 28, 2019 12:29
Copy link
Contributor

@spalger spalger left a comment

Choose a reason for hiding this comment

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

LGTM

@mshustov mshustov merged commit 2489180 into elastic:master Mar 29, 2019
@mshustov mshustov deleted the issue-33444-restrict-imports-eslint branch March 29, 2019 07:48
mshustov added a commit to mshustov/kibana that referenced this pull request Mar 29, 2019
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
mshustov added a commit that referenced this pull request Mar 29, 2019
#33697) (#34139)

* 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
@mshustov mshustov removed the review label Apr 8, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Feature:New Platform release_note:skip Skip the PR/issue when compiling release notes Team:Core Core services & architecture: plugins, logging, config, saved objects, http, ES client, i18n, etc v7.2.0
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants