Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions packages/bundler-plugins/src/rewriter/ast.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,25 @@ export function declarationOf(node: BaseNode): BaseNode | null | undefined {
return node.type === 'ExportNamedDeclaration' ? (node as ExportNamedNode).declaration : node;
}

/**
* The expression a class extends, looking through parentheses and through
* an assignment that caches it. Decorator transforms emit that assignment,
* as in `class A extends (_Base = Base)`.
*/
export function superClassTarget(superClass: BaseNode | null | undefined): BaseNode | null | undefined {
let node = superClass;
while (node) {
if (node.type === 'ParenthesizedExpression') {
node = (node as BaseNode & { expression?: BaseNode }).expression;
} else if (node.type === 'AssignmentExpression' && (node as BaseNode & { operator?: string }).operator === '=') {
node = (node as BaseNode & { right?: BaseNode }).right;
} else {
break;
}
}
return node;
}

/** Whether `node` calls `methodName`, either bare (`fn()`) or as a member (`ns.fn()`). */
export function isCallToMethod(node: BaseNode, methodName: string): boolean {
if (node.type !== 'CallExpression') return false;
Expand Down
5 changes: 3 additions & 2 deletions packages/bundler-plugins/src/rewriter/class-extends.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import type {
MemberExpressionNode,
ProgramBody,
} from './ast';
import { declarationOf } from './ast';
import { declarationOf, superClassTarget } from './ast';

/** A base class, named by the module it is imported from and its export name. */
export interface BaseClassRef {
Expand Down Expand Up @@ -81,11 +81,12 @@ function collectBaseImports(ast: ProgramBody, base: BaseClassRef): BaseBindings
}

function extendsBase(
superClass: BaseNode | null | undefined,
node: BaseNode | null | undefined,
base: BaseClassRef,
bases: BaseBindings,
detected: Set<string>,
): boolean {
const superClass = superClassTarget(node);
if (!superClass) return false;
if (superClass.type === 'Identifier') {
const name = (superClass as IdentifierNode).name;
Expand Down
5 changes: 3 additions & 2 deletions packages/bundler-plugins/src/rewriter/module-shape.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import type {
MemberExpressionNode,
ProgramBody,
} from './ast';
import { exportedName, isTypeOnlyExport } from './ast';
import { exportedName, isTypeOnlyExport, superClassTarget } from './ast';
import { langFromId, parseModule } from './parse';

/** Binding name used for a module's default export. */
Expand Down Expand Up @@ -109,7 +109,8 @@ export function shapeFromSource(code: string, id: string): ModuleShape | undefin
return undefined;
}

function superRefFromNode(superClass: BaseNode | null | undefined): SuperRef | undefined {
function superRefFromNode(node: BaseNode | null | undefined): SuperRef | undefined {
const superClass = superClassTarget(node);
if (!superClass) return undefined;

if (superClass.type === 'Identifier') {
Expand Down
12 changes: 12 additions & 0 deletions packages/bundler-plugins/test/rewriter/class-extends.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,18 @@ describe('findClassesExtending', () => {
expect(findClassesExtending(parseModule(code.join('\n'), 'module.ts'), BASE)).toEqual(new Set());
});

it('finds a class whose base is cached in an assignment', () => {
const code = [
"import { WorkerEntrypoint } from 'cloudflare:workers';",
"import * as cf from 'cloudflare:workers';",
'var _WE, _cf;',
'class A extends (_WE = WorkerEntrypoint) {}',
'class B extends (_cf = cf.WorkerEntrypoint) {}',
'class C extends (_WE = A) {}',
];
expect(find(code.join('\n'))).toEqual(new Set(['A', 'B', 'C']));
});

it('ignores classes without the base import', () => {
expect(find('class A extends B {}')).toEqual(new Set());
});
Expand Down
16 changes: 16 additions & 0 deletions packages/bundler-plugins/test/rewriter/module-shape.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,22 @@ describe('shapeFromAst', () => {
);
});

// Decorator transforms emit this form, e.g. Babel's for `@callable()`.
it('reads the superclass through an assignment that caches it', () => {
const shape = shapeFromAst(
parseModule(
['var _B, _D;', 'class A extends (_B = B) {}', 'class C extends (_D = ns.D) {}'].join('\n'),
'module.js',
),
);
expect(shape.classes).toEqual(
new Map([
['A', { kind: 'identifier', name: 'B' }],
['C', { kind: 'member', object: 'ns', property: 'D' }],
]),
);
});

it('collects named, default and namespace imports', () => {
const shape = shapeFromAst(
parseModule("import Def, { a as b, c } from 'm';\nimport * as ns from 'n';", 'module.js'),
Expand Down
Loading