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
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,8 @@ and adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
- `codegraph sync` and the file watcher now do the same for a Python import under a name of its own, like `from .models import User as UserModel`, `from . import helpers as h` or `from app.pkg import sub as subpkg`: when the module or package is added after the importing file was indexed, comes back after being deleted, or gets the imported name in a later edit, the import and every use of the alias, such as a call, a base class or the class passed as a value, now link to it. Before, they stayed unlinked until the importing file changed or the project was re-indexed. Re-index Python projects after upgrading to pick up links an earlier version missed.
- `codegraph sync` and the file watcher now also link a Python import of a name a package re-exports, like `from fastapi import FastAPI` through `fastapi/__init__.py`, when that `__init__.py` is added after the importing files were indexed or gains the re-export in a later edit. Before, calls through the name stayed unlinked until each importing file changed or the project was re-indexed. Large projects gain the most: sync also used to skip, in any language, a module or package that more than 500 references were waiting for, and now restoring Django's `django/urls/__init__.py` relinks every file that imports `path` from it. Re-index Python projects after upgrading to pick up links an earlier version missed.
- `codegraph sync` and the file watcher now update the files that import through a module that re-exports what it imports, like a package's `__init__.py` or an `index.ts` barrel, when that module is deleted or added, and in Python also the files that reach it through another package re-exporting it. Before, deleting `shop/__init__.py` left every `from shop import Order` linked to the class it re-exported, and deleting `components/index.ts` left every `import { Button } from './components'` linked, until each importing file changed or the project was re-indexed. When such a module came back, the imports stayed linked the way they were while it was missing, and in a large Python project an import of a module whose name is as common as `models` or `test`, like `from django.test import TestCase`, stayed unlinked. Python, JavaScript, TypeScript, Vue, Svelte and Astro.
- In JavaScript and TypeScript, a file that re-exports a module, like an `index.ts` with `export * from './button'` or `export { Button } from './button'`, now counts as importing that module, the way an `import` statement does. `codegraph affected` and a module's dependents now include the barrels that re-export it; before, a barrel made only of `export *` lines showed no dependency at all. Re-index JavaScript and TypeScript projects after upgrading.
- `codegraph sync` and the file watcher now also update the files that import through an `index.ts` re-exporting another barrel, like `export * from './dropdown-menu'`, when that inner barrel is deleted or added. Before, their imports stayed linked through the deleted barrel, or stayed linked the way they were while it was missing, until each importing file changed or the project was re-indexed.
- In C, C++ and Objective-C, a struct or class member declared as a pointer, reference, array or function, like `SharedState* shared;`, `jv elements[];` or `virtual Status Put(…) = 0;`, no longer makes the struct look like it inherits from the member's type. Before, `codegraph_explore` and impact listed those types as base classes, every struct holding a pointer to a type as one of its subclasses, and a method of that struct as an override of the type's method with the same name. Real base classes like `class Derived : public Base`, Objective-C superclasses and Go's embedded fields are unchanged. Re-index C, C++ and Objective-C projects after upgrading.
- In C++, a call on a variable, parameter or member declared through a `typedef` or `using` alias now links to the method of the type the alias names, with the alias looked up the way C++ does it: in the calling function, its class and the classes that class inherits from, then the namespaces around it. In google/leveldb, `table_.Insert(buf)` on a `Table table_;` declared next to `typedef SkipList<const char*, KeyComparator> Table;` now reaches `SkipList::Insert` instead of the unrelated `HandleTable::Insert`, and `Table::Iterator iter(&table_);` reaches `SkipList::Iterator`'s constructor. A class that only shares the alias's name is no longer taken for it, and when the aliased type has no such method, as with an alias of `std::vector` or of a template parameter, no method is guessed from the receiver's name. Re-index C++ projects after upgrading.
- In C++, a call on a variable, parameter or member declared with template arguments nested inside its template arguments, like `autovector<std::pair<int, FileMetaData*>>` or `Striped<CacheAlignedWrapper<port::Mutex>>`, now links to the method of the class it is declared as. Before, the nested arguments hid that class: in RocksDB, `files_marked_for_compaction_.clear()` on such an `autovector` linked to an unrelated `CompactionInputFiles::clear` that only shares words with the variable's name, and `mutex_.Get(key)` on a `Striped` linked to nothing. Re-index C++ projects after upgrading.
Expand Down
28 changes: 28 additions & 0 deletions __tests__/extraction.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10555,6 +10555,34 @@ export const registry = [widget];
expect(names).toContain('beta');
});

it('a re-export imports its source module, as an import statement does', () => {
const result = extractFromSource(
'barrel.ts',
`export * from './star';\nexport { alpha } from './named';\nexport * as ns from './space';\n`
);
const modules = ['./star', './named', './space'];
expect(result.nodes.filter((n) => n.kind === 'import').map((n) => n.name)).toEqual(modules);
const refs = result.unresolvedReferences.filter((r) => r.referenceKind === 'imports').map((r) => r.referenceName);
for (const module of modules) expect(refs).toContain(module);
expect(refs).toContain('alpha');
});

it('a barrel that only re-exports everything is a dependent of the module', async () => {
const dir = createTempDir();
try {
fs.mkdirSync(path.join(dir, 'src'), { recursive: true });
fs.writeFileSync(path.join(dir, 'src', 'foo.ts'), `export function helper(): void {}\n`);
fs.writeFileSync(path.join(dir, 'src', 'index.ts'), `export * from './foo';\n`);
const cg = CodeGraph.initSync(dir, { config: { include: ['src/**/*.ts'], exclude: [] } });
await cg.indexAll();
cg.resolveReferences();
expect(cg.getFileDependents('src/foo.ts')).toContain('src/index.ts');
cg.destroy();
} finally {
cleanupTempDir(dir);
}
});

it('a value imported/re-exported but never called still makes the importer a dependent', async () => {
const dir = createTempDir();
try {
Expand Down
11 changes: 10 additions & 1 deletion __tests__/sync-module-importers.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,13 +53,22 @@ const PYTHON: Files = {
};
const PYTHON_BARRELS = ['app/shop/__init__.py', 'lib/api.py', 'app/orm/__init__.py'];

/** ES modules: an `index.ts` re-exporting under the same name and under another. */
/**
* ES modules: an `index.ts` re-exporting under the same name and under
* another, and modules re-exporting it in turn — an outer `index.ts`, all of
* it and one name renamed, and a module passing on one name, read through a
* namespace import — imported in its stead.
*/
const TS: Files = {
'package.json': '{"name":"app"}',
'src/components/Button.ts': 'export function Button() { return 1; }\n',
'src/components/Icon.ts': 'export function BaseIcon() { return 2; }\n',
'src/components/index.ts': "export { Button } from './Button';\nexport { BaseIcon as Icon } from './Icon';\n",
'src/app.ts': "import { Button, Icon } from './components';\nexport function render() { return [Button(), Icon()]; }\n",
'src/ui/index.ts': "export * from '../components';\nexport { Icon as UiIcon } from '../components';\n",
'src/page.ts': "import { Button, UiIcon } from './ui';\nexport function page() { return [Button(), UiIcon()]; }\n",
'src/kit.ts': "export { Button as KitButton } from './components';\n",
'src/screen.ts': "import * as kit from './kit';\nexport function screen() { return kit.KitButton(); }\n",
};
const TS_BARRELS = ['src/components/index.ts'];

Expand Down
5 changes: 4 additions & 1 deletion codegraph-kernel/src/tsjs/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -721,7 +721,10 @@ impl<'t> Walker<'t> {
} else if kind == "import_statement" {
self.extract_import(node);
} else if kind == "export_statement" && node.child_by_field_name("source").is_some() {
// Re-export: `export { X } from './y'`.
// Re-export: `export { X } from './y'`, `export * from './y'`. It
// imports the module as `import … from` does (an import node and a
// reference to the module), then links each re-exported name.
self.extract_import(node);
self.emit_re_export_refs(node);
} else if kind == "export_statement" && self.looks_like_vue_store_file() {
// Vuex MODULE default export (`export default { actions: {…} }`).
Expand Down
26 changes: 18 additions & 8 deletions src/extraction/tree-sitter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -564,6 +564,8 @@ export class TreeSitterExtractor {
private fileScopeValueCounts = new Map<string, number>(); // file-scope nodes per name (conditional-def detection)
private valueRefScopes: Array<{ id: string; node: SyntaxNode; name: string }> = [];
private errors: ExtractionError[] = [];
// Import nodes made for `export … from` statements: no symbols of the file's own.
private reExportImports = 0;
private extractor: LanguageExtractor | null = null;
private nodeStack: string[] = []; // Stack of parent node IDs
// C/C++ enclosing `namespace ns { … }` names, prepended to every contained
Expand Down Expand Up @@ -733,7 +735,9 @@ export class TreeSitterExtractor {
this.unresolvedReferences.push(...commonJsRequireRefs(this.filePath, this.source, this.language));

// hasError is routine for several grammars; warn only when no symbols survived.
const symbolCount = this.nodes.filter((n) => n.kind !== 'file').length;
// (A re-export's import of its module is none: a barrel whose other lines
// failed to parse has lost what it passes on.)
const symbolCount = this.nodes.filter((n) => n.kind !== 'file').length - this.reExportImports;
if (this.tree?.rootNode.hasError && symbolCount === 0) {
this.errors.push({
message:
Expand Down Expand Up @@ -1577,20 +1581,26 @@ export class TreeSitterExtractor {
else if (this.extractor.importTypes.includes(nodeType)) {
this.extractImport(node);
}
// Re-export from another module — `export { X } from './y'` (TS/JS). A
// re-export is a dependency on the source module just like an import, but
// the export_statement is otherwise only descended into (no declaration to
// extract), so a barrel that ONLY re-exports produced zero edges and showed
// 0 dependents. Link each re-exported name to its definition. Children are
// still visited (a non-re-export `export const X = …` has no `source` and
// falls through to its normal declaration extraction).
// Re-export from another module — `export { X } from './y'`, `export * from
// './y'` (TS/JS). A re-export is a dependency on the source module just like
// an import, but the export_statement is otherwise only descended into (no
// declaration to extract), so a barrel that ONLY re-exports produced zero
// edges and showed 0 dependents. It imports the module as `import … from`
// does — an import node and a reference to the module, which links the
// barrel to the file it names (so a barrel of barrels can be followed) —
// and links each re-exported name to its definition. Children are still
// visited (a non-re-export `export const X = …` has no `source` and falls
// through to its normal declaration extraction).
else if (
nodeType === 'export_statement' &&
(this.language === 'typescript' || this.language === 'tsx' ||
this.language === 'javascript' || this.language === 'jsx' ||
this.language === 'arkts') &&
getChildByField(node, 'source')
) {
const before = this.nodes.length;
this.extractImport(node);
this.reExportImports += this.nodes.length - before;
const parentId = this.nodeStack[this.nodeStack.length - 1];
if (parentId) this.emitReExportRefs(node, parentId);
}
Expand Down
2 changes: 1 addition & 1 deletion src/resolution/import-resolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2732,7 +2732,7 @@ function findGoPackageMember(
/** Recursive depth cap for re-export chain following. Real codebases
* rarely chain barrels more than 2–3 deep; 8 is a generous safety
* net that still bounds worst-case work. */
const REEXPORT_MAX_DEPTH = 8;
export const REEXPORT_MAX_DEPTH = 8;

/**
* Find an exported symbol in `filePath`, following `export { x } from
Expand Down
Loading