Skip to content

transformer: remove unused import foo = bar.baz or mark them as pure #16628

@sapphi-red

Description

@sapphi-red

When the value on the left side (foo) of import foo = bar.baz is not used, TypeScript removes the whole import ... = ... (TS playground). But Oxc does not remove them (playground).

While Babel does not implement this behavior (REPL), esbuild (esbuild try) and SWC (SWC playground) does.

The ones that implements this behavior removes import ... = ... completely, but there's an alternative way to achieve the same behavior. It is to add pure annotation to the member access.

// the behavior implemented by esbuild & SWC & TS
const a = foo.a;
const b = a.b;
const c = b.c;
export let bar = c;

// the alternative
var a = foo.a;
var b = a.b;
var c = b.c;
var x = /* #__PURE__ */ (() => foo.x)();
var y = /* #__PURE__ */ (() => x.y)();
export let bar = c;

Metadata

Metadata

Labels

A-transformerArea - Transformer / Transpiler

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions