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

@babel/plugin-transform-typescript removes regular imports #9723

Closed
hulkish opened this issue Mar 20, 2019 · 5 comments
Closed

@babel/plugin-transform-typescript removes regular imports #9723

hulkish opened this issue Mar 20, 2019 · 5 comments
Labels
i: bug i: needs triage outdated A closed issue/PR that is archived due to age. Recommended to make a new issue

Comments

@hulkish
Copy link
Contributor

hulkish commented Mar 20, 2019

Bug Report

Current Behavior
this logic seems to be introducing a bug for non-typescript code.

Specifically, it is incorrectly removing a regular module import.

Input Code

import foo from 'foo';
export bar from 'bar';
export * as baz from 'baz';

export default class Foobar {}

Expected output

import foo from 'foo'; // why is this line is removed?
import _bar from 'bar';
export { _bar as bar };
import * as _baz from 'baz';
export { _baz as baz };
var Foobar = function Foobar() {};
export { Foobar as default };

Actual output

import _bar from 'bar';
export { _bar as bar };
import * as _baz from 'baz';
export { _baz as baz };
export default class Foobar {}

Babel Configuration (.babelrc, package.json, cli command)

{
  "presets": ["@babel/preset-typescript", { isTSX: true, allExtensions: true }]
}

Environment

  • Babel version(s): @babel/core@7.3.4, @babel/preset-typescript@7.3.3
  • Node/npm version: npm@, node@10.15.3
  • OS: macOS@10.14.1
  • Monorepo: yes, but doesn't matter - happens anywhere.
  • How you are using Babel: directly using transformFileSync() from @babel/core

Possible Solution
Fix issue in @babel/plugin-transform-typescript

Additional context/Screenshots
N/A

@babel-bot
Copy link
Collaborator

Hey @hulkish! We really appreciate you taking the time to report an issue. The collaborators
on this project attempt to help as many people as possible, but we're a limited number of volunteers,
so it's possible this won't be addressed swiftly.

If you need any help, or just have general Babel or JavaScript questions, we have a vibrant Slack
community
that typically always has someone willing to help. You can sign-up here
for an invite.

@nicolo-ribaudo
Copy link
Member

That is the expected behavior: https://www.typescriptlang.org/play/index.html#src=import%20foo%20from%20'foo'%3B%0A%0Aexport%20default%20class%20Foobar%20%7B%7D

In TypeScript, unused imports as considered as type imports and thus removed.

@hulkish
Copy link
Contributor Author

hulkish commented Mar 20, 2019

@nicolo-ribaudo How is this considered safe? What if foo has side effects? This seems very wrong to me.

@nicolo-ribaudo
Copy link
Member

I agree that it is unexpected, but it is a bug with the TypeScript language. We won't diverge from their implementation, so it needs to be fixed upstream first.

If you need foo for its side effects, you can use import "foo" and it shouldn't be removed.

@hulkish
Copy link
Contributor Author

hulkish commented Mar 20, 2019

Understood, thanks for the explanation!

@hulkish hulkish closed this as completed Mar 20, 2019
@lock lock bot added the outdated A closed issue/PR that is archived due to age. Recommended to make a new issue label Jun 19, 2019
@lock lock bot locked as resolved and limited conversation to collaborators Jun 19, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
i: bug i: needs triage outdated A closed issue/PR that is archived due to age. Recommended to make a new issue
Projects
None yet
Development

No branches or pull requests

3 participants