Skip to content

Commit

Permalink
fix: Try harder to construct a use chain in synthetic AST
Browse files Browse the repository at this point in the history
This should work around a more strict DCE in rollup > 2.75
  • Loading branch information
Swatinem committed May 29, 2022
1 parent cfdf55a commit cbdb276
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 13 deletions.
6 changes: 5 additions & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 9 additions & 4 deletions src/transform/DeclarationScope.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
createIdentifier,
createIIFE,
createReference,
createReturn,
Range,
withStartEnd,
} from "./astHelpers.js";
Expand Down Expand Up @@ -37,6 +38,7 @@ interface DeclarationScopeOptions {
export class DeclarationScope {
declaration: ESTree.FunctionDeclaration;
iife?: ESTree.ExpressionStatement;
private returnExpr: ESTree.ArrayExpression;

constructor({ id, range }: DeclarationScopeOptions) {
if (id) {
Expand All @@ -46,6 +48,9 @@ export class DeclarationScope {
this.iife = iife;
this.declaration = fn as any;
}
const ret = createReturn();
this.declaration.body.body.push(ret.stmt);
this.returnExpr = ret.expr;
}

/**
Expand All @@ -67,9 +72,6 @@ export class DeclarationScope {
this.scopes[this.scopes.length - 1]?.add(name);
}

pushRaw(expr: ESTree.AssignmentPattern) {
this.declaration.params.push(expr);
}
pushReference(id: ESTree.Expression) {
let name: string | undefined;
// We convert references from TS AST to ESTree
Expand All @@ -90,7 +92,10 @@ export class DeclarationScope {
}
}
}
this.pushRaw(createReference(id));
const { ident, expr } = createReference(id);

this.declaration.params.push(expr);
this.returnExpr.elements.push(ident);
}
pushIdentifierReference(id: ts.Identifier) {
this.pushReference(createIdentifier(id));
Expand Down
36 changes: 29 additions & 7 deletions src/transform/astHelpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,18 @@ export function createProgram(node: ts.SourceFile): ESTree.Program {
* Creates a reference to `id`:
* `_ = ${id}`
*/
export function createReference(id: ESTree.Expression): ESTree.AssignmentPattern {
export function createReference(id: ESTree.Expression): { ident: ESTree.Identifier; expr: ESTree.AssignmentPattern } {
const ident: ESTree.Identifier = {
type: "Identifier",
name: String(IDs++),
};
return {
type: "AssignmentPattern",
left: {
type: "Identifier",
name: String(IDs++),
ident,
expr: {
type: "AssignmentPattern",
left: ident,
right: id,
},
right: id,
};
}

Expand All @@ -47,7 +51,7 @@ export function createIdentifier(node: ts.Identifier): ESTree.Identifier {
* Create a new Scope which is always included
* `(function (_ = MARKER) {})()`
*/
export function createIIFE(range: Range) {
export function createIIFE(range: Range): { fn: ESTree.FunctionExpression; iife: ESTree.ExpressionStatement } {
const fn = withStartEnd<ESTree.FunctionExpression>(
{
type: "FunctionExpression",
Expand All @@ -72,6 +76,24 @@ export function createIIFE(range: Range) {
return { fn, iife };
}

/**
* Create a dummy ReturnStatement with an ArrayExpression:
* `return [];`
*/
export function createReturn(): { stmt: ESTree.Statement; expr: ESTree.ArrayExpression } {
const expr: ESTree.ArrayExpression = {
type: "ArrayExpression",
elements: [],
};
return {
expr,
stmt: {
type: "ReturnStatement",
argument: expr,
},
};
}

/**
* Create a new Declaration and Scope for `id`:
* `function ${id}(_ = MARKER) {}`
Expand Down
1 change: 0 additions & 1 deletion tests/testcases/type-typeof/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
interface A {}
declare const a: A;
export declare function typeQuery(): typeof a;
export {};

0 comments on commit cbdb276

Please sign in to comment.