Skip to content

Conversation

@hayemaxi
Copy link
Contributor

@hayemaxi hayemaxi commented Feb 6, 2025

I could not find a lint rule (or a configuration of a lint rule) to achieve this, so I just made one. I tried some things from eslint-plugin-import (namely no-cycles), but I was not happy with the results.

This will prevent you from importing from index.ts files while working in packages/core/, which has been a source of annoying and hard to understand circular dependency issues. These files should be reserved for exporting to the subprojects only.

It only affects imports in core, and only in src/ directories that do not start with "test"


  • Treat all work as PUBLIC. Private feature/x branches will not be squash-merged at release time.
  • Your code changes must meet the guidelines in CONTRIBUTING.md.
  • License: I confirm that my contribution is made under the terms of the Apache 2.0 license.

I could not find a lint rule (or a configuration of a lint rule) to achieve this, so I just made one. I tried some things from eslint-plugin-import (namely no-cycles), but I was not happy with the results.

This will prevent you from importing from index.ts files while working in packages/core/, which has been a source of annoying and hard to understand circular dependency issues.

It only affects imports in core, and only in src/ dirctories that do not start with test/
@github-actions
Copy link

github-actions bot commented Feb 6, 2025

  • This pull request modifies code in src/* but no tests were added/updated.
    • Confirm whether tests should be added or ensure the PR description explains why tests are not required.

@hayemaxi hayemaxi force-pushed the eslint-import-index branch from c8817f2 to abe6160 Compare February 6, 2025 19:26
@hayemaxi hayemaxi force-pushed the eslint-import-index branch from abe6160 to 042a307 Compare February 6, 2025 19:54
@hayemaxi hayemaxi marked this pull request as ready for review February 6, 2025 21:22
@hayemaxi hayemaxi requested review from a team as code owners February 6, 2025 21:22
defaultOptions: [],
create(context) {
const filePath = context.physicalFilename
if (!filePath.match(/packages\/core\/src\/(?!test)/)) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Is it possible to configure this at the top level in the .eslintrc.js file?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Not that I have found. If you are referring to no-restricted-imports specifically, no because it only matches on the import string literal.

messageId: 'default',
// TODO: We can add a fixer to resolve the import for us.
// fix: (fixer) => {
// // somehow parse the exports of the imported index.ts file to get the actual export path,
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 guessing this isn't straightforward? Does ESLint allow you to access this information or would it involve manually reading the file?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Eslint context doesn't seem to offer more than the file it is currently linting. The only thing I can think of is opening the file manually and load it into the ts lib to create an AST.

@hayemaxi hayemaxi merged commit 29c4048 into aws:master Feb 10, 2025
25 of 26 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants