ucn 3.1.3 → 3.1.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Potentially problematic release.
This version of ucn might be problematic. Click here for more details.
- package/cli/index.js +36 -4
- package/core/discovery.js +37 -17
- package/core/project.js +35 -16
- package/languages/javascript.js +48 -0
- package/package.json +1 -1
- package/test/parser.test.js +82 -0
package/cli/index.js
CHANGED
|
@@ -960,7 +960,8 @@ function runProjectCommand(rootDir, command, arg) {
|
|
|
960
960
|
|
|
961
961
|
case 'graph': {
|
|
962
962
|
requireArg(arg, 'Usage: ucn . graph <file>');
|
|
963
|
-
const
|
|
963
|
+
const graphDepth = flags.depth ?? 2; // Default to 2 for cleaner output
|
|
964
|
+
const graphResult = index.graph(arg, { direction: 'both', maxDepth: graphDepth });
|
|
964
965
|
if (graphResult.nodes.length === 0) {
|
|
965
966
|
console.log(`File not found: ${arg}`);
|
|
966
967
|
} else {
|
|
@@ -970,7 +971,7 @@ function runProjectCommand(rootDir, command, arg) {
|
|
|
970
971
|
nodes: r.nodes.map(n => ({ file: n.relativePath, depth: n.depth })),
|
|
971
972
|
edges: r.edges.map(e => ({ from: path.relative(index.root, e.from), to: path.relative(index.root, e.to) }))
|
|
972
973
|
}, null, 2),
|
|
973
|
-
r => { printGraph(r, index.root); }
|
|
974
|
+
r => { printGraph(r, index.root, graphDepth); }
|
|
974
975
|
);
|
|
975
976
|
}
|
|
976
977
|
break;
|
|
@@ -1801,12 +1802,15 @@ function printStats(stats) {
|
|
|
1801
1802
|
}
|
|
1802
1803
|
}
|
|
1803
1804
|
|
|
1804
|
-
function printGraph(graph, root) {
|
|
1805
|
+
function printGraph(graph, root, maxDepth = 2) {
|
|
1805
1806
|
const rootRelPath = path.relative(root, graph.root);
|
|
1806
1807
|
console.log(`Dependency graph for ${rootRelPath}`);
|
|
1807
1808
|
console.log('═'.repeat(60));
|
|
1808
1809
|
|
|
1809
1810
|
const printed = new Set();
|
|
1811
|
+
const maxChildren = 8; // Limit children per node
|
|
1812
|
+
let truncatedNodes = 0;
|
|
1813
|
+
let depthLimited = false;
|
|
1810
1814
|
|
|
1811
1815
|
function printNode(file, indent = 0) {
|
|
1812
1816
|
const fileEntry = graph.nodes.find(n => n.file === file);
|
|
@@ -1819,15 +1823,43 @@ function printGraph(graph, root) {
|
|
|
1819
1823
|
}
|
|
1820
1824
|
printed.add(file);
|
|
1821
1825
|
|
|
1826
|
+
// Depth limiting
|
|
1827
|
+
if (indent > maxDepth) {
|
|
1828
|
+
depthLimited = true;
|
|
1829
|
+
console.log(`${prefix}${relPath} ...`);
|
|
1830
|
+
return;
|
|
1831
|
+
}
|
|
1832
|
+
|
|
1822
1833
|
console.log(`${prefix}${relPath}`);
|
|
1823
1834
|
|
|
1824
1835
|
const edges = graph.edges.filter(e => e.from === file);
|
|
1825
|
-
|
|
1836
|
+
|
|
1837
|
+
// Limit children
|
|
1838
|
+
const displayEdges = edges.slice(0, maxChildren);
|
|
1839
|
+
const hiddenCount = edges.length - displayEdges.length;
|
|
1840
|
+
|
|
1841
|
+
for (const edge of displayEdges) {
|
|
1826
1842
|
printNode(edge.to, indent + 1);
|
|
1827
1843
|
}
|
|
1844
|
+
|
|
1845
|
+
if (hiddenCount > 0) {
|
|
1846
|
+
truncatedNodes += hiddenCount;
|
|
1847
|
+
console.log(`${' '.repeat(indent)}└── ... and ${hiddenCount} more`);
|
|
1848
|
+
}
|
|
1828
1849
|
}
|
|
1829
1850
|
|
|
1830
1851
|
printNode(graph.root);
|
|
1852
|
+
|
|
1853
|
+
// Print helpful note about expanding
|
|
1854
|
+
if (depthLimited || truncatedNodes > 0) {
|
|
1855
|
+
console.log('\n' + '─'.repeat(60));
|
|
1856
|
+
if (depthLimited) {
|
|
1857
|
+
console.log(`Depth limited to ${maxDepth}. Use --depth=N for deeper graph.`);
|
|
1858
|
+
}
|
|
1859
|
+
if (truncatedNodes > 0) {
|
|
1860
|
+
console.log(`${truncatedNodes} nodes hidden. Graph has ${graph.nodes.length} total files.`);
|
|
1861
|
+
}
|
|
1862
|
+
}
|
|
1831
1863
|
}
|
|
1832
1864
|
|
|
1833
1865
|
function printSearchResults(results, term) {
|
package/core/discovery.js
CHANGED
|
@@ -332,31 +332,51 @@ function findProjectRoot(startDir) {
|
|
|
332
332
|
|
|
333
333
|
/**
|
|
334
334
|
* Auto-detect the glob pattern for a project based on its type
|
|
335
|
+
* Checks both project root and immediate subdirectories for config files
|
|
335
336
|
*/
|
|
336
337
|
function detectProjectPattern(projectRoot) {
|
|
337
338
|
const extensions = [];
|
|
338
339
|
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
340
|
+
// Helper to check for config files in a directory
|
|
341
|
+
const checkDir = (dir) => {
|
|
342
|
+
if (fs.existsSync(path.join(dir, 'package.json'))) {
|
|
343
|
+
extensions.push('js', 'jsx', 'ts', 'tsx', 'mjs', 'cjs');
|
|
344
|
+
}
|
|
342
345
|
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
346
|
+
if (fs.existsSync(path.join(dir, 'pyproject.toml')) ||
|
|
347
|
+
fs.existsSync(path.join(dir, 'setup.py')) ||
|
|
348
|
+
fs.existsSync(path.join(dir, 'requirements.txt'))) {
|
|
349
|
+
extensions.push('py');
|
|
350
|
+
}
|
|
348
351
|
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
+
if (fs.existsSync(path.join(dir, 'go.mod'))) {
|
|
353
|
+
extensions.push('go');
|
|
354
|
+
}
|
|
352
355
|
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
+
if (fs.existsSync(path.join(dir, 'Cargo.toml'))) {
|
|
357
|
+
extensions.push('rs');
|
|
358
|
+
}
|
|
356
359
|
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
+
if (fs.existsSync(path.join(dir, 'pom.xml')) ||
|
|
361
|
+
fs.existsSync(path.join(dir, 'build.gradle'))) {
|
|
362
|
+
extensions.push('java', 'kt');
|
|
363
|
+
}
|
|
364
|
+
};
|
|
365
|
+
|
|
366
|
+
// Check project root
|
|
367
|
+
checkDir(projectRoot);
|
|
368
|
+
|
|
369
|
+
// Also check immediate subdirectories for multi-language projects (e.g., web/, frontend/, server/)
|
|
370
|
+
try {
|
|
371
|
+
const entries = fs.readdirSync(projectRoot, { withFileTypes: true });
|
|
372
|
+
for (const entry of entries) {
|
|
373
|
+
if (entry.isDirectory() && !entry.name.startsWith('.') &&
|
|
374
|
+
!EXCLUDED_DIRS.has(entry.name)) {
|
|
375
|
+
checkDir(path.join(projectRoot, entry.name));
|
|
376
|
+
}
|
|
377
|
+
}
|
|
378
|
+
} catch (e) {
|
|
379
|
+
// Ignore errors reading directory
|
|
360
380
|
}
|
|
361
381
|
|
|
362
382
|
if (extensions.length > 0) {
|
package/core/project.js
CHANGED
|
@@ -1010,11 +1010,37 @@ class ProjectIndex {
|
|
|
1010
1010
|
}
|
|
1011
1011
|
|
|
1012
1012
|
// Look up each callee in the symbol table
|
|
1013
|
+
// For methods, prefer callees from: 1) same file, 2) same package, 3) same receiver type
|
|
1013
1014
|
const result = [];
|
|
1015
|
+
const defDir = path.dirname(def.file);
|
|
1016
|
+
const defReceiver = def.receiver;
|
|
1017
|
+
|
|
1014
1018
|
for (const [calleeName, count] of callees) {
|
|
1015
1019
|
const symbols = this.symbols.get(calleeName);
|
|
1016
1020
|
if (symbols && symbols.length > 0) {
|
|
1017
|
-
|
|
1021
|
+
let callee = symbols[0];
|
|
1022
|
+
|
|
1023
|
+
// If multiple definitions, try to find the best match
|
|
1024
|
+
if (symbols.length > 1) {
|
|
1025
|
+
// Priority 1: Same file
|
|
1026
|
+
const sameFile = symbols.find(s => s.file === def.file);
|
|
1027
|
+
if (sameFile) {
|
|
1028
|
+
callee = sameFile;
|
|
1029
|
+
} else {
|
|
1030
|
+
// Priority 2: Same directory (package)
|
|
1031
|
+
const sameDir = symbols.find(s => path.dirname(s.file) === defDir);
|
|
1032
|
+
if (sameDir) {
|
|
1033
|
+
callee = sameDir;
|
|
1034
|
+
} else if (defReceiver) {
|
|
1035
|
+
// Priority 3: Same receiver type (for methods)
|
|
1036
|
+
const sameReceiver = symbols.find(s => s.receiver === defReceiver);
|
|
1037
|
+
if (sameReceiver) {
|
|
1038
|
+
callee = sameReceiver;
|
|
1039
|
+
}
|
|
1040
|
+
}
|
|
1041
|
+
}
|
|
1042
|
+
}
|
|
1043
|
+
|
|
1018
1044
|
result.push({
|
|
1019
1045
|
...callee,
|
|
1020
1046
|
callCount: count,
|
|
@@ -2137,23 +2163,15 @@ class ProjectIndex {
|
|
|
2137
2163
|
|
|
2138
2164
|
const def = definitions[0];
|
|
2139
2165
|
const visited = new Set();
|
|
2166
|
+
const defDir = path.dirname(def.file);
|
|
2140
2167
|
|
|
2141
|
-
const buildTree = (
|
|
2142
|
-
|
|
2168
|
+
const buildTree = (funcDef, currentDepth, dir) => {
|
|
2169
|
+
const funcName = funcDef.name;
|
|
2170
|
+
if (currentDepth > maxDepth || visited.has(`${funcDef.file}:${funcDef.startLine}`)) {
|
|
2143
2171
|
return null;
|
|
2144
2172
|
}
|
|
2145
|
-
visited.add(
|
|
2146
|
-
|
|
2147
|
-
const funcDefs = this.symbols.get(funcName);
|
|
2148
|
-
if (!funcDefs || funcDefs.length === 0) {
|
|
2149
|
-
return {
|
|
2150
|
-
name: funcName,
|
|
2151
|
-
external: true,
|
|
2152
|
-
children: []
|
|
2153
|
-
};
|
|
2154
|
-
}
|
|
2173
|
+
visited.add(`${funcDef.file}:${funcDef.startLine}`);
|
|
2155
2174
|
|
|
2156
|
-
const funcDef = funcDefs[0];
|
|
2157
2175
|
const node = {
|
|
2158
2176
|
name: funcName,
|
|
2159
2177
|
file: funcDef.relativePath,
|
|
@@ -2165,7 +2183,8 @@ class ProjectIndex {
|
|
|
2165
2183
|
if (dir === 'down' || dir === 'both') {
|
|
2166
2184
|
const callees = this.findCallees(funcDef);
|
|
2167
2185
|
for (const callee of callees.slice(0, 10)) { // Limit children
|
|
2168
|
-
|
|
2186
|
+
// callee already has the best-matched definition from findCallees
|
|
2187
|
+
const childTree = buildTree(callee, currentDepth + 1, 'down');
|
|
2169
2188
|
if (childTree) {
|
|
2170
2189
|
node.children.push({
|
|
2171
2190
|
...childTree,
|
|
@@ -2179,7 +2198,7 @@ class ProjectIndex {
|
|
|
2179
2198
|
return node;
|
|
2180
2199
|
};
|
|
2181
2200
|
|
|
2182
|
-
const tree = buildTree(
|
|
2201
|
+
const tree = buildTree(def, 0, direction);
|
|
2183
2202
|
|
|
2184
2203
|
// Also get callers if direction is 'up' or 'both'
|
|
2185
2204
|
let callers = [];
|
package/languages/javascript.js
CHANGED
|
@@ -812,6 +812,50 @@ function findCallsInCode(code, parser) {
|
|
|
812
812
|
return true;
|
|
813
813
|
}
|
|
814
814
|
|
|
815
|
+
// Handle JSX component usage: <Component /> or <Component>...</Component>
|
|
816
|
+
// Only track PascalCase names (React components), not lowercase (HTML elements)
|
|
817
|
+
if (node.type === 'jsx_self_closing_element' || node.type === 'jsx_opening_element') {
|
|
818
|
+
// First named child is the element name
|
|
819
|
+
for (let i = 0; i < node.namedChildCount; i++) {
|
|
820
|
+
const child = node.namedChild(i);
|
|
821
|
+
if (child.type === 'identifier') {
|
|
822
|
+
const name = child.text;
|
|
823
|
+
// React components start with uppercase
|
|
824
|
+
if (name && /^[A-Z]/.test(name)) {
|
|
825
|
+
const enclosingFunction = getCurrentEnclosingFunction();
|
|
826
|
+
calls.push({
|
|
827
|
+
name: name,
|
|
828
|
+
line: node.startPosition.row + 1,
|
|
829
|
+
isMethod: false,
|
|
830
|
+
isJsxComponent: true,
|
|
831
|
+
enclosingFunction
|
|
832
|
+
});
|
|
833
|
+
}
|
|
834
|
+
break;
|
|
835
|
+
}
|
|
836
|
+
// Handle namespaced components: <Foo.Bar />
|
|
837
|
+
if (child.type === 'member_expression' || child.type === 'nested_identifier') {
|
|
838
|
+
const text = child.text;
|
|
839
|
+
// Get the last part after the dot
|
|
840
|
+
const parts = text.split('.');
|
|
841
|
+
const componentName = parts[parts.length - 1];
|
|
842
|
+
if (componentName && /^[A-Z]/.test(componentName)) {
|
|
843
|
+
const enclosingFunction = getCurrentEnclosingFunction();
|
|
844
|
+
calls.push({
|
|
845
|
+
name: componentName,
|
|
846
|
+
line: node.startPosition.row + 1,
|
|
847
|
+
isMethod: true,
|
|
848
|
+
receiver: parts.slice(0, -1).join('.'),
|
|
849
|
+
isJsxComponent: true,
|
|
850
|
+
enclosingFunction
|
|
851
|
+
});
|
|
852
|
+
}
|
|
853
|
+
break;
|
|
854
|
+
}
|
|
855
|
+
}
|
|
856
|
+
return true;
|
|
857
|
+
}
|
|
858
|
+
|
|
815
859
|
return true;
|
|
816
860
|
}, {
|
|
817
861
|
onLeave: (node) => {
|
|
@@ -1338,6 +1382,10 @@ function findUsagesInCode(code, name, parser) {
|
|
|
1338
1382
|
usageType = 'reference';
|
|
1339
1383
|
}
|
|
1340
1384
|
}
|
|
1385
|
+
// JSX component usage: <Component /> or <Component>...</Component>
|
|
1386
|
+
else if (parent.type === 'jsx_self_closing_element' || parent.type === 'jsx_opening_element') {
|
|
1387
|
+
usageType = 'call'; // Treat JSX component usage as a "call"
|
|
1388
|
+
}
|
|
1341
1389
|
}
|
|
1342
1390
|
|
|
1343
1391
|
usages.push({ line, column, usageType });
|
package/package.json
CHANGED
package/test/parser.test.js
CHANGED
|
@@ -4497,6 +4497,88 @@ function main() {
|
|
|
4497
4497
|
}
|
|
4498
4498
|
});
|
|
4499
4499
|
|
|
4500
|
+
it('should prefer same-file callees for Go methods', () => {
|
|
4501
|
+
const tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), 'ucn-go-callee-disambig-'));
|
|
4502
|
+
try {
|
|
4503
|
+
fs.writeFileSync(path.join(tmpDir, 'go.mod'), `module example.com/test
|
|
4504
|
+
go 1.21
|
|
4505
|
+
`);
|
|
4506
|
+
|
|
4507
|
+
// Two files with same method name 'helper'
|
|
4508
|
+
fs.writeFileSync(path.join(tmpDir, 'service_a.go'), `package main
|
|
4509
|
+
|
|
4510
|
+
type ServiceA struct{}
|
|
4511
|
+
|
|
4512
|
+
func (s *ServiceA) Process() {
|
|
4513
|
+
s.helper()
|
|
4514
|
+
}
|
|
4515
|
+
|
|
4516
|
+
func (s *ServiceA) helper() {}
|
|
4517
|
+
`);
|
|
4518
|
+
|
|
4519
|
+
fs.writeFileSync(path.join(tmpDir, 'service_b.go'), `package main
|
|
4520
|
+
|
|
4521
|
+
type ServiceB struct{}
|
|
4522
|
+
|
|
4523
|
+
func (s *ServiceB) Process() {
|
|
4524
|
+
s.helper()
|
|
4525
|
+
}
|
|
4526
|
+
|
|
4527
|
+
func (s *ServiceB) helper() {}
|
|
4528
|
+
`);
|
|
4529
|
+
|
|
4530
|
+
const index = new ProjectIndex(tmpDir);
|
|
4531
|
+
index.build('**/*.go', { quiet: true });
|
|
4532
|
+
|
|
4533
|
+
// Get callees for ServiceA.Process - should find ServiceA.helper, not ServiceB.helper
|
|
4534
|
+
const defs = index.symbols.get('Process') || [];
|
|
4535
|
+
const serviceAProcess = defs.find(d => d.relativePath.includes('service_a.go'));
|
|
4536
|
+
assert.ok(serviceAProcess, 'Should find ServiceA.Process');
|
|
4537
|
+
|
|
4538
|
+
const callees = index.findCallees(serviceAProcess);
|
|
4539
|
+
assert.ok(callees.length >= 1, 'Should find at least 1 callee');
|
|
4540
|
+
|
|
4541
|
+
const helperCallee = callees.find(c => c.name === 'helper');
|
|
4542
|
+
assert.ok(helperCallee, 'Should find helper callee');
|
|
4543
|
+
assert.ok(helperCallee.relativePath.includes('service_a.go'),
|
|
4544
|
+
'helper callee should be from service_a.go, not service_b.go');
|
|
4545
|
+
} finally {
|
|
4546
|
+
fs.rmSync(tmpDir, { recursive: true, force: true });
|
|
4547
|
+
}
|
|
4548
|
+
});
|
|
4549
|
+
|
|
4550
|
+
it('should detect JSX component usage as calls', () => {
|
|
4551
|
+
const tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), 'ucn-jsx-'));
|
|
4552
|
+
try {
|
|
4553
|
+
fs.writeFileSync(path.join(tmpDir, 'package.json'), `{"name": "test"}`);
|
|
4554
|
+
|
|
4555
|
+
fs.writeFileSync(path.join(tmpDir, 'Page.tsx'), `
|
|
4556
|
+
function EnvironmentsPage() {
|
|
4557
|
+
return <div>Hello</div>;
|
|
4558
|
+
}
|
|
4559
|
+
|
|
4560
|
+
function App() {
|
|
4561
|
+
return <EnvironmentsPage />;
|
|
4562
|
+
}
|
|
4563
|
+
|
|
4564
|
+
export { App, EnvironmentsPage };
|
|
4565
|
+
`);
|
|
4566
|
+
|
|
4567
|
+
const index = new ProjectIndex(tmpDir);
|
|
4568
|
+
index.build('**/*.tsx', { quiet: true });
|
|
4569
|
+
|
|
4570
|
+
const usages = index.usages('EnvironmentsPage', { codeOnly: true });
|
|
4571
|
+
const calls = usages.filter(u => u.usageType === 'call' && !u.isDefinition);
|
|
4572
|
+
|
|
4573
|
+
// Should find JSX usage as a call
|
|
4574
|
+
assert.ok(calls.length >= 1, 'Should find at least 1 JSX component usage');
|
|
4575
|
+
assert.ok(calls.some(c => c.content && c.content.includes('<EnvironmentsPage')),
|
|
4576
|
+
'Should detect <EnvironmentsPage /> as a call');
|
|
4577
|
+
} finally {
|
|
4578
|
+
fs.rmSync(tmpDir, { recursive: true, force: true });
|
|
4579
|
+
}
|
|
4580
|
+
});
|
|
4581
|
+
|
|
4500
4582
|
it('should detect Rust method calls in usages', () => {
|
|
4501
4583
|
const tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), 'ucn-rust-method-'));
|
|
4502
4584
|
try {
|