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

typescript_strip: option to remove completely unused imports #1060

Merged
merged 6 commits into from
Sep 15, 2020

Conversation

kdy1
Copy link
Member

@kdy1 kdy1 commented Sep 11, 2020

Current behavior is intended for multiple reason.

  1. At the time of implementing it, type-only imports without import type are only stripped out if it's used as type.

ts 3.8 introduced type-only imports, because the problem I wrote above.
(Note that typescript had a breaking change after it)

  1. swc works on file-by-file basis, so it cannot know if it's a type-only.

I'm considering two options.

  1. Implement emit-only tsc which also handles some special stuffs like const enum within swc_bundler, as it already works by loading multiple file.

  2. Add config to allow user select which behavior should be used.


See: denoland/deno#7413 (comment)

@kdy1 kdy1 marked this pull request as ready for review September 11, 2020 02:53
@Brooooooklyn
Copy link
Member

I feel this behavior is unsafe.

The import statement import { Type } from './types'; , we are not sure if ./types.ts file is side effect free.

For example:

// types.ts

windows.a = 1

And in other file:

const b = window.a + 1;

@kdy1 kdy1 marked this pull request as draft September 11, 2020 04:19
@kdy1
Copy link
Member Author

kdy1 commented Sep 11, 2020

I'm not sure about this.

I feel this behavior is unsafe.

The import statement import { Type } from './types'; , we are not sure if ./types.ts file is side effect free.

I agree with you. That's why I implemented it to preserve side effects. But I'm now confused because typescript compiler removes it.

@Brooooooklyn
Copy link
Member

But I'm now confused because typescript compiler removes it

I think because TypeScript know this is type only import.

@nayeemrmn
Copy link
Contributor

I agree with you. That's why I implemented it to preserve side effects. But I'm now confused because typescript compiler removes it.

@kdy1 The behaviour you implemented aligns with --importsNotUsedAsValue=preserve. The typescript compiler removes it when --importsNotUsedAsValue=remove. I suggest going with the first. Ref denoland/deno#7413 (comment).

@lucacasonato
Copy link
Contributor

The typescript default is --importsNotUsedAsValue=remove though.

@nayeemrmn
Copy link
Contributor

Yeah I guess it makes less sense for swc to be opinionated about that. Can it be a configuration option?

@nayeemrmn
Copy link
Contributor

@kdy1 As for this PR, it would be ideal to keep the behaviour of --importsNotUsedAsValues=remove for now and exclusively address #1065. The way forward should be more clear.

@kdy1
Copy link
Member Author

kdy1 commented Sep 11, 2020

@nayeemrmn I'll add a option to switch behavior, while preserving current semantic as default.

@kdy1 kdy1 modified the milestones: v1.2.27, v1.2.28 Sep 13, 2020
@kdy1 kdy1 changed the title typescript_strip: remove completely unused imports typescript_strip: option to remove completely unused imports Sep 15, 2020
@kdy1 kdy1 marked this pull request as ready for review September 15, 2020 05:24
@kdy1 kdy1 merged commit bc82b55 into swc-project:master Sep 15, 2020
@kdy1 kdy1 deleted the ts-strip branch September 15, 2020 05:25
#[derive(Debug, Serialize, Deserialize)]
#[non_exhaustive]
pub enum ImportNotUsedAsValues {
#[serde(rename = "rename")]
Copy link
Member

Choose a reason for hiding this comment

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

rename = "remove" ?

Copy link
Member Author

Choose a reason for hiding this comment

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

Oh, thanks!

Comment on lines +724 to +731
ImportNotUsedAsValues::Preserve => match entry {
Some(&DeclInfo {
has_type: true,
has_concrete: false,
..
}) => false,
_ => true,
},
Copy link
Contributor

Choose a reason for hiding this comment

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

@kdy1 This is not the behaviour of tsc under "importsNotUsedAsValues": "preserve". The correct behaviour is to never preserve completely unused import identifiers. The above branch should always be used.

I think ImportNotUsedAsValues should be removed since it's a misunderstanding of the equivalent tsc option.

Copy link
Member Author

Choose a reason for hiding this comment

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

I'll make a breaking change to fix it.

@swc-project swc-project locked as resolved and limited conversation to collaborators Nov 13, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Development

Successfully merging this pull request may close these issues.

4 participants