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 rule: prefer-import-tag #1314

Open
boneskull opened this issue Sep 16, 2024 · 1 comment
Open

new rule: prefer-import-tag #1314

boneskull opened this issue Sep 16, 2024 · 1 comment

Comments

@boneskull
Copy link

boneskull commented Sep 16, 2024

Motivation

I have a codebase littered with inline type-level imports (e.g., @type {import('foo').bar}). I'd like to be able to put a halt to this madness and use import tags instead. I would especially like this to be a fixable rule.

Current behavior

It's the wild west out there

Desired behavior

A rule which could raise an error if an inline type import was detected. Default behavior of this rule, when enabled, would be to always prefer import tags over inline imports.

Highly desired, but not required for a first pass:

  • automatically fixable (though this may be difficult or dangerous, because it will introduce symbols into the module scope [insofar as TS is concerned] and symbols can conflict)

Optionally:

  • inline imports could be preferred over import tags
  • exceptions based on some criteria (e.g., imports from foo are an exception to the configured behavior)

Alternatives considered

  • While TS itself supports JSDoc, typescript-eslint does not and will not support JSDoc.
  • I thought about writing a codemod but that seems too complicated.

I am unsure how feasible this is or how difficult an implementation would be, so I apologize in advance if it's ridiculous.

@brettz9
Copy link
Collaborator

brettz9 commented Sep 17, 2024

You can currently get this without a fixer by using:

'jsdoc/no-restricted-syntax': ['warn', {
  contexts: [
    {
      comment: 'JsdocBlock:has(JsdocTag:has(JsdocTypeImport))',
      context: 'any',
      message: 'Use @import tag over import() statements',
    },
  ],
}]

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

No branches or pull requests

2 participants