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

importHelpers + module: es2015/esnext emits unused helper imports #41515

Open
d-fischer opened this issue Nov 12, 2020 · 2 comments
Open

importHelpers + module: es2015/esnext emits unused helper imports #41515

d-fischer opened this issue Nov 12, 2020 · 2 comments
Labels
Bug A bug in TypeScript
Milestone

Comments

@d-fischer
Copy link

TypeScript Version: 4.2.0-dev.20201112

Search Terms: esm, importHelpers, unused

Code

// @importHelpers: true
// @module: es2015
// @target: es5
// @downlevelIteration: true
const a = [1, 2];
const b = [3, 4]
export const c = [...a, ...b];

Expected behavior:
Imports only the used helpers:

import { __spread } from "tslib";
var a = [1, 2];
var b = [3, 4];
export var c = __spread(a, b);

Actual behavior:
Imports the used helpers and also the helpers they depend on:

import { __read, __spread } from "tslib";
var a = [1, 2];
var b = [3, 4];
export var c = __spread(a, b);

Note the extra unused import in the first line.

Playground Link: https://www.typescriptlang.org/play?downlevelIteration=true&importHelpers=true&target=1&module=5&ts=4.2.0-dev.20201112#code/MYewdgzgLgBAhjAvDA2gRgDQwEwF0DcAUKJLAEZKoDMWALLoQKYAeADiAE6wnQzCUoAdMLhZhgsgSA

Additional info: I already have a fix for this ready; basically I'm just following protocol, waiting for approval.

@ExE-Boss
Copy link
Contributor

I believe it’s because __spread uses __read under the hood: https://github.com/microsoft/tslib/blob/76bedfddc2cad38aa11060a32f56e9484c279c98/tslib.es6.js#L147-L151

And the way the helper emit code is currently written doesn’t distinguish between helpers that are only needed by other helpers, and helpers that are actually needed by transpiled code:

function createSpreadHelper(argumentList: readonly Expression[]) {
context.requestEmitHelper(readHelper);
context.requestEmitHelper(spreadHelper);
return factory.createCallExpression(
getUnscopedHelperName("__spread"),
/*typeArguments*/ undefined,
argumentList
);
}

@d-fischer
Copy link
Author

As I already noted in the original post, a fix is ready to be PRd on my machine. I'm just waiting for the approval to match the process outlined in the guidelines and the PR template.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug A bug in TypeScript
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants