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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ and adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
- In PHP, a method called on a freshly built object, like `(new SettingClient())->getSet(1)`, now links to the class the file's namespace and `use` imports name, or to a method it inherits, so these calls show up in `callers` and impact. Thanks @holysor for the report. (#3244)
- In Laravel, a route written like `[UserController::class, 'show']` now links to the controller the routes file imports, through a plain or aliased `use`, a group `use`, an imported namespace or a fully qualified name, instead of a same-named controller elsewhere or nothing. Thanks @inth3shadows for the reports and @johnhldiver-beep. (#2975, #3292)
- In Java, when two packages declare a class of the same name, a static call, a call on a new object or a call through a variable now reaches the one the calling file can see, including through a wildcard import, and a call written with the package, like `com.x.b.Util.log(…)`, reaches that package's class. Thanks @inth3shadows for the reports and @johnhldiver-beep. (#2976, #3290, #3291)
- In Kotlin, an extension declared on an import alias of a library type, like `fun DeclAlias.pad()` after `import androidx.compose.ui.Modifier as DeclAlias`, now links from calls on that type, whatever name the calling file imports it under. Thanks @mixxer for the report and the pull request. (#3084)

## [1.6.3] - 2026-11-17

Expand Down
66 changes: 66 additions & 0 deletions __tests__/kotlin-extension-receiver-alias.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
/**
* A Kotlin extension on a type from outside the project, declared on an
* import alias of it — `import androidx.compose.ui.Modifier as DeclAlias` then
* `fun DeclAlias.pad()` — is the extension a call on that type reaches, under
* whatever name the calling file imports the type: the type's own, another
* alias, or the same alias (#3084). The extension is indexed under the
* receiver as written (`DeclAlias::pad`), and the call was looked up under the
* type's own name only, so it linked nothing. An extension declared on an
* alias of another type, or one the calling file can't see, still isn't it.
*/
import { describe, it, expect, afterEach } from 'vitest';
import * as fs from 'fs';
import * as os from 'os';
import * as path from 'path';
import { CodeGraph } from '../src';

let root = '';
let cg: CodeGraph | undefined;

afterEach(() => {
cg?.close();
cg = undefined;
if (root) fs.rmSync(root, { recursive: true, force: true });
root = '';
});

async function index(files: Record<string, string>): Promise<CodeGraph> {
root = fs.mkdtempSync(path.join(os.tmpdir(), 'cg-kotlin-ext-alias-'));
for (const [rel, content] of Object.entries(files)) {
fs.mkdirSync(path.dirname(path.join(root, rel)), { recursive: true });
fs.writeFileSync(path.join(root, rel), content);
}
cg = await CodeGraph.init(root, { index: true });
return cg;
}

/** The files of what `calls` edges from the function `fn` reach. */
function calleeFiles(graph: CodeGraph, fn: string): string[] {
const db = (graph as unknown as { db: { getDb(): { prepare(sql: string): { all(...a: unknown[]): Array<{ f: string }> } } } }).db;
return db.getDb().prepare(`
SELECT t.file_path f FROM edges e
JOIN nodes s ON s.id = e.source JOIN nodes t ON t.id = e.target
WHERE e.kind = 'calls' AND s.kind = 'function' AND s.name = ?`).all(fn).map((r) => r.f.replace(/\\/g, '/')).sort();
}

const typeImport = (alias: string) => `import androidx.compose.ui.Modifier${alias === 'Modifier' ? '' : ` as ${alias}`}`;

describe('a Kotlin extension declared on an import alias of an outside type (#3084)', () => {
it.each([
['DeclAlias', 'CallAlias'],
['SameAlias', 'SameAlias'],
['DeclAlias', 'Modifier'],
['Modifier', 'CallAlias'],
])('a receiver declared as %s links a call written on %s', async (declared, called) => {
const graph = await index({
'extensions/Extensions.kt': `package extensions\n${typeImport(declared)}\nfun ${declared}.pad() {}\n`,
// An extension of the same name on another type behind the same alias.
'decoy/Extensions.kt': `package decoy\nimport javax.lang.model.element.Modifier as ${declared}\nfun ${declared}.pad() {}\n`,
'caller/Calls.kt': `package caller\n${typeImport(called)}\nimport extensions.pad\nfun caller() { ${called}.pad() }\n`,
// A file that doesn't import the extension can't call it.
'unimported/Calls.kt': `package unimported\n${typeImport(called)}\nfun hidden() { ${called}.pad() }\n`,
});
expect(calleeFiles(graph, 'caller')).toEqual(['extensions/Extensions.kt']);
expect(calleeFiles(graph, 'hidden')).toEqual([]);
});
});
12 changes: 10 additions & 2 deletions src/resolution/name-matcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2480,15 +2480,23 @@ export function isJavaOutsideImport(name: string, ref: UnresolvedRef, context: R
* `Modifier::pad` — that the call site can see (its file, its package, an
* import of it by name or `.*`) and whose own file imports the same type.
* Compose's own `Modifier.fillMaxSize()` matches none and stays unlinked.
* Either file may name the type through an import alias (#3084): `fun
* DeclAlias.pad()`, indexed as `DeclAlias::pad`, extends the type its file
* imports as `DeclAlias`, whatever the calling file calls it.
*/
function kotlinExtensionOnImportedType(ref: UnresolvedRef, context: ResolutionContext): ResolvedRef | null {
const call = /^(\w+)\.(\w+)$/.exec(ref.referenceName);
if (!call || ref.referenceKind !== 'calls') return null;
const binding = context.getImportMappings(ref.filePath, ref.language).find((m) => m.localName === call[1]);
if (!binding) return null;
const pkg = binding.source.slice(0, binding.source.lastIndexOf('.'));
const extensions = context.getNodesByQualifiedName(`${binding.exportedName}::${call[2]}`).filter((n) => {
if (n.kind !== 'method' || n.language !== 'kotlin' || !isKotlinTopLevelVisible(n, ref, context)) return false;
const extensions = context.getNodesByName(call[2]!).filter((n) => {
if (n.kind !== 'method' || n.language !== 'kotlin') return false;
const receiver = n.qualifiedName.slice(0, Math.max(0, n.qualifiedName.lastIndexOf('::')));
const alias = context.getImportMappings(n.filePath, 'kotlin').find((m) => m.localName === receiver && m.localName !== m.exportedName);
if (alias ? alias.source !== binding.source : receiver !== binding.exportedName) return false;
if (!isKotlinTopLevelVisible(n, ref, context)) return false;
if (alias) return true;
const scope = kotlinFileScope(n.filePath, context);
return scope.imports.has(binding.source) || scope.stars.has(pkg);
});
Expand Down