Files
codegraph/src/resolution/js-class-expressions.ts
T
Colby MchenryandClaude Opus 5.5 013b91c1be fix(js,ts): a named class expression is a class with its own members (#2825)
A named class expression, like register(class Widget extends Base {})
or module.exports = class Router {}, was not indexed as a class, as
issue #2499 found. Its methods became free functions, its fields and
supertypes were dropped, and calls in it had no class to start from,
so its this-calls, super calls and own static calls went unresolved or
to a by-name guess, and calls elsewhere guessed into its methods.

Both engines now index a named class expression as a class, with its
methods, properties, extends and implements. An anonymous one is still
passed over. Its head (class Widget extends Base) is its signature,
which marks it as an expression, and it is never exported by itself.

JavaScript binds the name only inside the expression, and the resolver
now scopes it the same way. A reference written outside every class
expression resolves in a view of the graph that hides class
expressions and their members, so new Widget() elsewhere links
nothing, and a same-named class, function or binding keeps its links.
A name only class expressions carry is unknown there. A reference
inside one sees the whole graph, unless it names another class
expression. A class a CommonJS module exports, module.exports = class
Router {} or exports.Store = class Store {}, is the module's value,
which require hands to every importer, so it is not scoped: it reads
as a declaration does, and a require of it reaches its static methods
and its instances' methods. The reader of CommonJS named exports takes
exports.X = class Y {} too. Super calls in a class expression now
resolve through the super-call matcher.

Validation: the new test fails 11 of 13 on main d5fc1fbb and passes 13
of 13 on the fix, and a mutation of each of 7 parts (the view, the
inside check, the name rule, the expression marker, the class node,
the CommonJS export rule, the named-export reader) fails a test.
A/B against main d5fc1fbb on 22 repos (20 TS/JS, flask and gin): 193
classes are added in 10 repos with their members, and 801 edge sites
change in 7 repos, each bucketed and sampled by hand. 563 are jest's
calls to its global test(), 0.4 guesses that move from one wrong
namesake to another: the old one, a class expression's test() in an
e2e fixture, is now a method. Of the rest, 85 are new extends,
implements and type references of class expressions; 74 calls, news
and references move to the member they are written in; 65 calls and
news gain or change a target, inside class expressions (this-calls,
own static calls) and through require (static calls, typed instance
calls, new); and 14 guesses are removed, among them 3 super calls
whose parent the index can't name. 12 repos' dumps, the controls among
them, are byte-identical. Kernel parity holds on 22 repos. Three sync
scenarios match a fresh index. Index time, median of 5 interleaved
runs: lit +0.8%, webpack +2.7%, jest +0.6%; insomnia +12.8%, though it
indexes byte-identically, holds no class expression, and its resolver
time is the same on both arms, so that gap is other jobs' load.

Fixes #2499

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-23 03:16:07 +00:00

67 lines
3.5 KiB
TypeScript

/**
* A named class expression's name is bound inside the expression alone
* (#2499): `register(class Gadget {…})` declares no `Gadget` for the code
* around it, where `new Gadget()` throws a ReferenceError. The extractor gives
* such a class a node, so its members are its own and its `extends` is
* known, and marks it with its head as its signature (`class Gadget`), which
* a class declaration doesn't carry.
*
* The class's name is bound nowhere outside the expression, and its value
* travels in ways resolution doesn't follow, except an import of what a
* module exports. So the resolver reads a reference outside the class
* expressions without their classes and members (ReferenceResolver's
* classExpressionFreeContextFor): there the reference means what it meant
* before those classes were indexed. A binding of the same name around an
* expression, like the `Panel` of `const Panel = class Panel {…}`, keeps the
* name, at the confidence it had. A class a CommonJS module exports,
* `module.exports = class Router {…}`, is the module's value, which every
* importer binds, so it isn't scoped: it is read as a declaration is.
*/
import type { Node } from '../types';
import type { UnresolvedRef } from './types';
/** Languages a class expression's class and the references to its name can be written in: TS/JS, and the scripts of Vue, Svelte and Astro files. */
export const CLASS_EXPRESSION_REF_LANGUAGES: ReadonlySet<string> = new Set(['typescript', 'tsx', 'javascript', 'jsx', 'arkts', 'vue', 'svelte', 'astro']);
/** What the resolver knows of the classes named class expressions declare (ReferenceResolver.classExpressions). */
export interface ClassExpressionIndex {
/** The classes, by name. */
byName: Map<string, Node[]>;
/** The classes, by the file that holds them. */
byFile: Map<string, Node[]>;
/** The files that hold them. */
files: Set<string>;
/** The ids of the classes and of their members. */
hiddenIds: Set<string>;
/** The class a member belongs to, when it is one of them. */
ownerOf: (member: Node) => Node | undefined;
}
/** Whether `node` is a class a named class expression declares. */
export function isJsClassExpression(node: Node): boolean {
return node.kind === 'class' && !!node.signature && CLASS_EXPRESSION_REF_LANGUAGES.has(node.language);
}
/** The end of the text before a class a CommonJS module exports: `module.exports = `, `exports.Router = `, `module.exports.Router = `. */
const COMMONJS_EXPORT_BEFORE = /(?:^|[^\w$.])(?:module\.)?exports(?:\.[A-Za-z_$][\w$]*)?\s*=\s*$/;
/**
* Whether a class expression is what its CommonJS module exports, as in
* `module.exports = class Router {…}` or `exports.Router = class Router {…}`
* (`lines`: its file's lines). An importer reaches that class, its statics
* and, through an instance, its methods, as it reaches a declaration's.
*/
export function isCommonJsExportedClass(node: Node, lines: readonly string[]): boolean {
const line = lines[node.startLine - 1];
if (line === undefined) return false;
return COMMONJS_EXPORT_BEFORE.test(`${lines[node.startLine - 2] ?? ''}\n${line.slice(0, node.startColumn)}`);
}
/** Whether the reference is written inside `node`'s source range. */
export function writtenIn(node: Node, ref: UnresolvedRef): boolean {
if (node.filePath !== ref.filePath) return false;
const afterStart = ref.line > node.startLine || (ref.line === node.startLine && ref.column >= node.startColumn);
const beforeEnd = ref.line < node.endLine || (ref.line === node.endLine && ref.column <= node.endColumn);
return afterStart && beforeEnd;
}