Skip to content

Refactoring suggtion: convert to namespace import #24044

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

Closed
mjbvz opened this issue May 10, 2018 · 4 comments
Closed

Refactoring suggtion: convert to namespace import #24044

mjbvz opened this issue May 10, 2018 · 4 comments
Labels
Awaiting More Feedback This means we'd like to hear from more people who would be helped by this feature Domain: Refactorings e.g. extract to constant or function, rename symbol Fixed A PR has been merged for this issue Suggestion An idea for TypeScript

Comments

@mjbvz
Copy link
Contributor

mjbvz commented May 10, 2018

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
@mhegazy mhegazy added the Suggestion An idea for TypeScript label May 10, 2018
@ghost
Copy link

ghost commented May 10, 2018

Related to #22925 -- in my opinion that would be preferrable to have the user specify a # of named imports they'll accept and have them grouped/expanded every time they organize imports. With a refactoring they have to select each import that they want to group/ungroup individually.

@mhegazy mhegazy added Domain: Refactorings e.g. extract to constant or function, rename symbol Awaiting More Feedback This means we'd like to hear from more people who would be helped by this feature labels May 10, 2018
@mjbvz
Copy link
Contributor Author

mjbvz commented May 11, 2018

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

@DanielRosenwasser
Copy link
Member

Looks like a duplicate of #19260

@mhegazy mhegazy assigned ghost May 17, 2018
@mhegazy mhegazy added this to the TypeScript 3.0 milestone May 17, 2018
@cancerberoSgx
Copy link

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

@ghost ghost added the Fixed A PR has been merged for this issue label May 29, 2018
@ghost ghost closed this as completed in #24469 May 30, 2018
@microsoft microsoft locked and limited conversation to collaborators Jul 31, 2018
This issue was closed.
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Awaiting More Feedback This means we'd like to hear from more people who would be helped by this feature Domain: Refactorings e.g. extract to constant or function, rename symbol Fixed A PR has been merged for this issue Suggestion An idea for TypeScript
Projects
None yet
Development

No branches or pull requests

4 participants