Skip to content

Commit 07f6e8c

Browse files
committed
fix #4507: import assignment tree-shaking bug
1 parent 2500c0c commit 07f6e8c

4 files changed

Lines changed: 66 additions & 6 deletions

File tree

‎CHANGELOG.md‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,15 @@
22

33
## Unreleased
44

5+
* Fix tree shaking bug due to TypeScript import alias ([#4507](https://github.com/evanw/esbuild/issues/4507))
6+
7+
This release fixes a bug that could cause esbuild to incorrectly tree-shake imports that are used in a TypeScript type alias under certain circumstances. Affected code uses a TypeScript-specific `import` assignment and looks something like this:
8+
9+
```ts
10+
import Base from './dep.js';
11+
import Alias = Base.SomeType;
12+
```
13+
514
* Avoid overwriting input files without `--allow-overwrite` ([#4484](https://github.com/evanw/esbuild/issues/4484))
615

716
For example: `esbuild input.js --outfile=input.js` tells esbuild to overwrite `input.js` with the output of running esbuild on it. This was supposed to already be prevented by default, but it accidentally regressed in version 0.17.0 and apparently didn't have any test coverage. The error message was being printed but the input file was still being overwritten. Oops.

‎internal/bundler_tests/bundler_ts_test.go‎

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3064,3 +3064,29 @@ func TestParameterPropsUseDefineForClassFieldsTrueLowered(t *testing.T) {
30643064
},
30653065
})
30663066
}
3067+
3068+
// See: https://github.com/evanw/esbuild/issues/4507
3069+
func TestTreeShakingImportAliasIssue4507(t *testing.T) {
3070+
ts_suite.expectBundled(t, bundled{
3071+
files: map[string]string{
3072+
"/entry.ts": `
3073+
import Base from './dep.js';
3074+
import Alias = Base.SomeType;
3075+
3076+
export function make() {
3077+
return new Base();
3078+
}
3079+
`,
3080+
"/dep.js": `
3081+
export default class Base {
3082+
static hello() { return 'dep code is present'; }
3083+
}
3084+
`,
3085+
},
3086+
entryPaths: []string{"/entry.ts"},
3087+
options: config.Options{
3088+
Mode: config.ModeBundle,
3089+
AbsOutputFile: "/out.js",
3090+
},
3091+
})
3092+
}

‎internal/bundler_tests/snapshots/snapshots_ts.txt‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2251,3 +2251,21 @@ if (nested) {
22512251
new Bar(bar2(objBar));
22522252
}
22532253
var bar;
2254+
2255+
================================================================================
2256+
TestTreeShakingImportAliasIssue4507
2257+
---------- /out.js ----------
2258+
// dep.js
2259+
var Base = class {
2260+
static hello() {
2261+
return "dep code is present";
2262+
}
2263+
};
2264+
2265+
// entry.ts
2266+
function make() {
2267+
return new Base();
2268+
}
2269+
export {
2270+
make
2271+
};

‎internal/js_parser/js_parser.go‎

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1752,12 +1752,16 @@ func (p *parser) ignoreUsage(ref ast.Ref) {
17521752
// Roll back the use count increment in recordUsage()
17531753
if !p.isControlFlowDead {
17541754
p.symbols[ref.InnerIndex].UseCountEstimate--
1755-
use := p.symbolUses[ref]
1756-
use.CountEstimate--
1757-
if use.CountEstimate == 0 {
1758-
delete(p.symbolUses, ref)
1759-
} else {
1760-
p.symbolUses[ref] = use
1755+
1756+
// Only remove this usage if we're currently processing a part
1757+
if p.symbolUses != nil {
1758+
use := p.symbolUses[ref]
1759+
use.CountEstimate--
1760+
if use.CountEstimate == 0 {
1761+
delete(p.symbolUses, ref)
1762+
} else {
1763+
p.symbolUses[ref] = use
1764+
}
17611765
}
17621766
}
17631767

@@ -17729,6 +17733,9 @@ func (p *parser) appendPart(parts []js_ast.Part, stmts []js_ast.Stmt) []js_ast.P
1772917733
part.Scopes = p.scopesForCurrentPart
1773017734
parts = append(parts, part)
1773117735
}
17736+
17737+
// Reset the state for this part so we don't accidentally mutate it
17738+
p.symbolUses = nil
1773217739
return parts
1773317740
}
1773417741

0 commit comments

Comments
 (0)