Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Auto-detect ModuleKind for JS/TS files based on import/export presence #8526

@coderabbitai

Description

@coderabbitai

Summary

Currently, JsFileSource::is_script() only determines the module kind based on the file extension (e.g., .cjs). This can lead to incorrect assumptions about whether a file is a script or module, as a .js or .ts file without imports/exports is treated as a module even though it behaves as a script.

Proposal

Auto-detect a JS or TS file's ModuleKind based on the presence of import or export statements in the file content. Files without top-level imports or exports would be treated as scripts, whilst those with imports/exports would be treated as modules.

Benefits

  • Provides lint rules with more accurate information about the file's module context
  • Reduces false positives in rules that need to distinguish between script and module scope
  • Better aligns with the actual runtime behaviour of the code

Context

This issue was identified whilst fixing #8331 in #8506. The noUnusedVariables rule needed to handle top-level interfaces/namespaces in script files differently, but couldn't rely on is_script() because it doesn't accurately detect scripts based on content.

Considerations

This change has broader implications across the codebase, as it would affect how all lint rules and analysis tools interpret file types.


Backlinks:

Metadata

Metadata

Assignees

No one assigned

    Labels

    S-Needs discussionStatus: needs a discussion to understand criteria

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions