Skip to content

Organize imports: don’t delete import declarations used for module augmentation#31482

Merged
andrewbranch merged 3 commits into
microsoft:masterfrom
andrewbranch:bug/31338
May 30, 2019
Merged

Organize imports: don’t delete import declarations used for module augmentation#31482
andrewbranch merged 3 commits into
microsoft:masterfrom
andrewbranch:bug/31338

Conversation

@andrewbranch
Copy link
Copy Markdown
Member

Fixes #31338

Import clauses are removed, e.g.

import { Caseless } from 'caseless';

declare module 'caseless' {
  interface Caseless {
    test(name: KeyType): boolean;
  }
}

now becomes

import 'caseless';

declare module 'caseless' {
  interface Caseless {
    test(name: KeyType): boolean;
  }
}

(Previously, the whole import declaration was deleted.)

Comment thread src/services/organizeImports.ts Outdated

function hasModuleDeclarationMatchingSpecifier(sourceFile: SourceFile, moduleSpecifier: Expression) {
const moduleSpecifierText = isStringLiteral(moduleSpecifier) && moduleSpecifier.text;
return isString(moduleSpecifierText) && some(sourceFile.statements, statement =>
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Do you need to handle the new patternAmbientModule as well apart from the m,odule specifier text

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Hmm, I don't think so, but I'm not sure exactly what rule says why. Can you come up with an example where that would be necessary? E.g., this doesn't work:

import 'typescript';
declare module 'type*' {
  interface Node {
    mySpecialNodeAugmentation: string;
  }
}

Comment thread src/services/organizeImports.ts Outdated
}
// ==ORGANIZED==

import 'caseless';
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Changing this to a side-effect import affects the emitted JS. Previously the import was elided due to being unused. With this slightly different syntax it will always be emitted.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Ah, interesting. We can do this in declaration files (which should be the norm), but either do import {} from 'caseless' or just don’t touch it in TypeScript files?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Addressed by not messing with imports for module augmentation outside of declaration files

Comment thread src/services/organizeImports.ts
@andrewbranch andrewbranch requested a review from sheetalkamat May 22, 2019 18:40
importDecl.modifiers,
/*importClause*/ undefined,
moduleSpecifier));
}
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Does this need to be instead updateImportDeclarationAndClause

Copy link
Copy Markdown
Member Author

@andrewbranch andrewbranch May 22, 2019

Choose a reason for hiding this comment

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

No, that adds an empty import clause instead of leaving it off.

@andrewbranch andrewbranch requested review from rbuckton and sandersn May 23, 2019 17:33
Copy link
Copy Markdown
Member

@amcasey amcasey left a comment

Choose a reason for hiding this comment

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

LGTM, modulo Sheetal's comments.

@andrewbranch andrewbranch merged commit b8dcf27 into microsoft:master May 30, 2019
@andrewbranch andrewbranch deleted the bug/31338 branch May 30, 2019 21:02
@microsoft microsoft locked as resolved and limited conversation to collaborators Oct 21, 2025
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Organize imports removes augmented module

4 participants