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

import/order false positives for TypeScript module declarations #2217

Closed
remcohaszing opened this issue Sep 3, 2021 · 1 comment · Fixed by #2226
Closed

import/order false positives for TypeScript module declarations #2217

remcohaszing opened this issue Sep 3, 2021 · 1 comment · Fixed by #2226

Comments

@remcohaszing
Copy link
Contributor

The import/order rule gives false positives for imports inside TypeScript module declarations.

Practical example:

declare module 'monaco-editor/esm/vs/base/common/cancellation' {
  export enum CancellationToken {
    None,
  }
}

declare module 'monaco-editor/esm/vs/editor/contrib/documentSymbols/documentSymbols' {
  import { ITextModel, languages } from 'monaco-editor';
  import { CancellationToken } from 'monaco-editor/esm/vs/base/common/cancellation';

  export function getDocumentSymbols(
    model: ITextModel,
    flat: boolean,
    token: CancellationToken,
  ): Promise<languages.DocumentSymbol[]>;
}

declare module 'monaco-editor/esm/vs/editor/editor.worker' {
  import { worker } from 'monaco-editor/esm/vs/editor/editor.api';

  export function initialize(
    fn: (ctx: worker.IWorkerContext, createData: unknown) => unknown,
  ): void;
}

This is happening, because import/order collects all import statements and assumes they are in the program body.

I would expect the import/order rule to be applied to each body separately. Currently these are the Program body and the TSModuleBlock body, but who knows what the future holds.

Alternatively the rule could be applied to imports which are direct children Program body only.

@ljharb
Copy link
Member

ljharb commented Sep 3, 2021

It seems reasonable to special-case imports/exports that are inside declare blocks.

remcohaszing added a commit to remcohaszing/eslint-plugin-import that referenced this issue Sep 12, 2021
Without this, `import/order` checks if all imports in a file are sorted. The
autofix would then move all imports to the type of the file, breaking TypeScript
module declarations.

Closes import-js#2217
@ljharb ljharb closed this as completed in 47ea669 Sep 18, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Development

Successfully merging a pull request may close this issue.

2 participants