Skip to content

Organize imports removes react imports in js projects that don't configure "jsx" #23287

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 Apr 9, 2018 · 10 comments
Closed
Assignees
Labels
Bug A bug in TypeScript Domain: JSX/TSX Relates to the JSX parser and emitter Domain: Organize Imports Issues with the organize imports feature Domain: Quick Fixes Editor-provided fixes, often called code actions. Fixed A PR has been merged for this issue

Comments

@mjbvz
Copy link
Contributor

mjbvz commented Apr 9, 2018

From microsoft/vscode#47287

TypeScript Version: 2.9.0-dev.20180409

Search Terms:

  • organize imports
  • jsx

Code
For a simple js project that does not configure "jsx:

jsconfig.json:

{
    "compilerOptions": {
        "module": "commonjs",
        "target": "es2016",
        "experimentalDecorators": true
    },
    "exclude": [
        "node_modules",
        "**/node_modules/*"
    ]
}

index.js:

import * as React from 'react'

const Button = () => <button />
  1. Run organize imports

Problem:
Typescript (correctly from its perspective) removes the React import as React is not referenced anywhere. However this is very confusing for users.

Proposal:
Some ideas:

  • Special case React imports for files that use jsx even when you don't have "jsx" configured

  • Default "jsx" on for jsconfig projects (the jsconfigs that vscode generates for users already do this). Allow the config to turn if off it a user really needs to

  • Show an editor suggestion on jsx in js files that are part of a jsconfig project that does not configure "jsx"

@DanielRosenwasser DanielRosenwasser added Bug A bug in TypeScript Domain: JSX/TSX Relates to the JSX parser and emitter Domain: Quick Fixes Editor-provided fixes, often called code actions. Domain: Organize Imports Issues with the organize imports feature Salsa labels Apr 9, 2018
@mhegazy mhegazy added this to the TypeScript 2.9 milestone Apr 10, 2018
@mhegazy mhegazy modified the milestones: TypeScript 2.9, TypeScript 3.0 May 8, 2018
@amcasey
Copy link
Member

amcasey commented May 18, 2018

I believe we already have heuristics for this. Sounds like you're just asking us to drop the check for the compiler option?

https://github.com/Microsoft/TypeScript/blob/1df79970144489924daa2fa1ef056d847488a8a5/src/services/organizeImports.ts#L95

@amcasey
Copy link
Member

amcasey commented May 18, 2018

Confirmed that the import is correctly retained if jsx is set in the compiler options.

@amcasey
Copy link
Member

amcasey commented May 18, 2018

@mhegazy The perf cost of removing the check looks negligible but I don't know enough about JSX to understand the correctness implications.

@amcasey
Copy link
Member

amcasey commented May 18, 2018

I assume the worst case is that we might never remove such imports, which seems acceptable.

@mhegazy
Copy link
Contributor

mhegazy commented May 19, 2018

I think removing the check for the compiler option is the safer choice. we have a problem that it will be tagged as unused and now shown as grey.. but i guess that is a different bug.

@amcasey
Copy link
Member

amcasey commented May 22, 2018

It is rather annoying that we can gray it out accurately but not organize/remove it accurately.

@amcasey
Copy link
Member

amcasey commented May 22, 2018

@mhegazy As I recall, we did it that way because we didn't want to trigger a full bind for the refactoring?

amcasey added a commit to amcasey/TypeScript that referenced this issue May 22, 2018
@amcasey
Copy link
Member

amcasey commented May 22, 2018

@mhegazy I've created a PR to have organizeImports stop deleting it. What do you want to do about not removing grayed out imports? File a Future bug?

@mhegazy
Copy link
Contributor

mhegazy commented May 22, 2018

Yes please.

@amcasey
Copy link
Member

amcasey commented May 22, 2018

As we discussed offline, we can improve the heuristic now so there's no reason to file a separate bug.

@mhegazy mhegazy added the Fixed A PR has been merged for this issue label May 22, 2018
@microsoft microsoft locked and limited conversation to collaborators Jul 31, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Bug A bug in TypeScript Domain: JSX/TSX Relates to the JSX parser and emitter Domain: Organize Imports Issues with the organize imports feature Domain: Quick Fixes Editor-provided fixes, often called code actions. Fixed A PR has been merged for this issue
Projects
None yet
Development

No branches or pull requests

4 participants