Skip to content

Refactoring suggtion: convert to namespace import #24044

Closed

Description

@mjbvz

Search Terms

  • Refactor
  • import

Suggestion

For the code:

import { a, b, c} from './xyz';

console.log(a, b, c);

Add a refactoring to convert the import to:

import * as xyz from './xyz';

console.log(xyz.a, xyz.b, xyz.c);

With a rename being triggered on xyz

Use Cases

I can see this being useful:

  • List of imports become too long and unmanageable.
  • Make it clear in code where a name comes from

Activity

  1. mjbvz commented on May 11, 2018

    @mjbvz
    ContributorAuthor

    Yes managing this automatically would also be nice. However a refactor that I can trigger would also be useful, as there are some module that I always want to use namespace imports for. I originally opened this issue because I noticed myself converting a lot of imports like:

    import {red, green blue} from 'vs/colors';

    to:

    import * as colors from 'vs/colors'

    Even when only a single value is used from vs/colors, I prefer using the namespace for this module

  2. DanielRosenwasser commented on May 17, 2018

    @DanielRosenwasser
    Member

    Looks like a duplicate of #19260

  3. cancerberoSgx commented on May 21, 2018

    @cancerberoSgx

    My humble opinion / question:

    • If you only provide this refactor and not the reverse one (* to named import) then people might think TypeScript is promoting this kind of imports. Make sure you want to do that or provide the reverse transformation.
    • In my experience developing typescript refactors, non named imports like the one you are suggesting could make it harder to navigate the AST for global refactors when you have to refactor imports / exports globally (think for example on refactors like move class to other file, move method to other class/interface, etc)

    My two cents

  4. ghost added
    FixedA PR has been merged for this issue
    on May 29, 2018
  5. ghost closed this as completedin #24469on May 30, 2018
  6. locked and limited conversation to collaborators on Jul 31, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

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: LS: Refactoringse.g. extract to constant or function, rename symbolFixedA PR has been merged for this issueSuggestionAn idea for TypeScript

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions