-
Notifications
You must be signed in to change notification settings - Fork 32
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
fix: Forbid self package import #1498
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
const noSelfPackageImportRule = require('./no-self-package-import'); | ||
|
||
const plugin = { | ||
rules: { 'no-self-package-import': noSelfPackageImportRule }, | ||
}; | ||
|
||
module.exports = plugin; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
/** | ||
* Derive the `@deephaven` package name from the given path. | ||
* Note that this assumes that the package folder name uses the same naming | ||
* convention as the package.json `name` field. | ||
*/ | ||
function deriveDeephavenPackageNameFromPath(path) { | ||
// In the off chance that there is a packages folder higher up in the path, | ||
// find the deepest one. | ||
const packageFolderNameI = path.lastIndexOf('packages'); | ||
|
||
if (packageFolderNameI === -1) { | ||
return null; | ||
} | ||
|
||
// Find the path token immediately after the `packages` folder. | ||
const [packageFolderName] = path | ||
.substring(packageFolderNameI + 'packages'.length + 1) | ||
.split('/'); | ||
|
||
if (packageFolderName === '') { | ||
return null; | ||
} | ||
|
||
return `@deephaven/${packageFolderName}`; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. One thing I'm not crazy about is this rule is not generic; even when we include this eslint config from Enterprise it won't technically be correct. We should also be using the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @mofojed This "shouldn't" ever actually be used in enterprise since it doesn't really apply. It is currently consumed via the top level Community package so doesn't get pulled in with the shared config. It really is scoped to @deephaven community packages. Does this alleviate any of your concern? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm actually second guessing the approach of using this plugin now. Since it's not actually configured in the shared package and not really useful elsewhere, maybe the top level community config should be fully responsible for it. Would be easy to just get the list of packages and build a rule array there |
||
} | ||
|
||
/** | ||
* Custom eslint rule that forbids importing from the same `@deephaven` package | ||
* that owns the module. Note that this rule is only useful within the `@deephaven` | ||
* web-client-ui monorepo. | ||
*/ | ||
module.exports = { | ||
meta: { | ||
type: 'problem', | ||
docs: { | ||
description: | ||
'Forbid importing from the same `@deephaven` package that owns this module.', | ||
}, | ||
}, | ||
create: function create(context) { | ||
const filePath = context.getFilename(); | ||
|
||
const packageName = deriveDeephavenPackageNameFromPath(filePath); | ||
if (packageName == null) { | ||
return {}; | ||
} | ||
|
||
return { | ||
ImportDeclaration(node) { | ||
if (node.source.value.includes(packageName)) { | ||
context.report({ | ||
node, | ||
message: | ||
'Forbid importing from the same `@deephaven` package that owns this module.', | ||
}); | ||
} | ||
}, | ||
}; | ||
}, | ||
}; |
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.
Looks like you don't need to do it this way. You can import the plugin directly
https://eslint.org/docs/latest/extend/custom-rule-tutorial#step-7-bundle-the-custom-rule-in-a-plugin
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.
This doesn't work if you try to add it to an array of plugins since the plugins array only supports string names. Granted the current config only has 1 plugin, this will be more future proof.
That said if you find a way to get it working alongside other plugins, I'd love to see how. I was not able to get it working.
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.
@mattrunyon @mofojed Thinking this PR may be a cleaner option
https://github.com/deephaven/web-client-ui/pull/1499/files