Skip to content

autoImportFileExcludePatterns should have more nuance than just true/falseΒ #59776

Open
@axefrog

Description

πŸ” Search Terms

autoImportFileExcludePatterns
auto import

βœ… Viability Checklist

⭐ Suggestion

A big point of usefulness for me regarding the typescript.preferences.autoImportFileExcludePatterns setting is that I can have a subdirectory with internal implementation details and avoid inadvertently having other parts of the application import those internal details when they contain exports with the same or a similar name to one I'm trying to auto import.

Problems:

  • If I break apart my internal implementation into various internal files, the setting also prevents auto importing from working even within code inside the excluded directory. Essentially it means I miss out on the benefit of auto importing when working on the internal implementation itself.
  • The externally-facing export that utilises the internal functionality and is essentially the main facade exposing that functionality -- the one that I want the rest of the project to be able to auto import -- cannot live in the same directory as its internals. This is less of an issue than the first point, but it does reduce developer freedom with respect to choices regarding file and folder organisation.

My suggestion is that the current design, which looks like this:

{
  "typescript.preferences.autoImportFileExcludePatterns": [
    "**/*.internal.ts",
    "**/*.internal/**",
    "**/internal/**",
  ],
}

Could be improved to support object entries in addition to file path strings. I'd suggest something like this:

{
  "typescript.preferences.autoImportFileExcludePatterns": [
    "node_modules/**/*",
    // Use an object to define an auto import scope
    {
      // Exclusion patterns that are part of this scope:
      "match": [
        "**/*.internal.ts",
        "**/*.internal/**",
        "**/internal/**"
      ],
      // Among exclusion patterns in this scope, the following should be filtered out of the exclusion list:
      "exceptions": [
        "**/index.ts",
        "**/*.public.ts"
      ],
      // If false, the exclusion patterns defined in this scope will not be active for files excluded from auto-import
      // because of this scope. In other words, when editing the actual files that this scope excludes from
      // being auto imported elsewhere, the excluded files themselves will still be able to auto import each other.
      "propagateExclusionScopeInternally": false
    },
    "other/patterns/**/*",
    "etc/*"
  ]
}

πŸ“ƒ Motivating Example

Essentially the current design of the autoImportFileExcludePatterns feature is too much of a blunt instrument. Exclusions are not just useful for dead code and things that nobody should ever want to accidentally import, but also for preventing auto imports within a project from grabbing internal implementation details from subsystems implemented within that project's codebase. A project might have many internal subsystems, each with its own folder containing internal implementation details. While working on those implementation details, you still want auto imports to work among files in that scope -- it's just that we don't want files outside that scope to be able to auto import them. autoImportFileExcludePatterns gives us a half measure in terms of the second part of that equation, but it makes working on the internal implementation details more cumbersome as we have to manually type in any import statements for files within the scope.

πŸ’» Use Cases

  1. What do you want to use this for?
    Editing. This is a developer ergonomics issue.

  2. What shortcomings exist with current approaches?
    Already covered.

  3. What workarounds are you using in the meantime?
    Accepting increased editing friction when using autoImportFileExcludePatterns.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Assignees

No one assigned

    Labels

    Awaiting More FeedbackThis means we'd like to hear from more people who would be helped by this featureDomain: Auto-importSuggestionAn idea for TypeScript

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions