Skip to content

Commit 34f069f

Browse files
authored
fix(refs): parse .jsx files with the JavaScript grammar when counting references (#200)
* fix(refs): parse .jsx files with the JavaScript grammar when counting references * test(refs): build the JSX fixture with the indexer
1 parent e68a22c commit 34f069f

2 files changed

Lines changed: 44 additions & 0 deletions

File tree

‎src/utils/tree-sitter.ts‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -661,6 +661,9 @@ export async function findIdentifierOccurrences(
661661
return [];
662662
}
663663

664+
// The bundled JavaScript grammar also parses JSX.
665+
if (language === 'javascriptreact') language = 'javascript';
666+
664667
if (!supportsTreeSitter(language) || !content.trim()) {
665668
return null;
666669
}

‎tests/jsx-reference-counts.test.ts‎

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import { mkdtemp, rm, writeFile } from 'node:fs/promises';
2+
import os from 'node:os';
3+
import path from 'node:path';
4+
import { expect, it } from 'vitest';
5+
import { CodebaseIndexer } from '../src/core/indexer.js';
6+
import { findSymbolReferences } from '../src/core/symbol-references.js';
7+
8+
it('counts JSX identifiers without counting JSX text, comments, or quoted attributes', async () => {
9+
const root = await mkdtemp(path.join(os.tmpdir(), 'cbc-jsx-refs-'));
10+
try {
11+
const sourcePath = 'view.jsx';
12+
const source = [
13+
'export function Panel() { return null; }',
14+
'export const view = (',
15+
' <section title="Panel">',
16+
' {/* Panel is the component below */}',
17+
' <Panel />',
18+
' <p>Panel</p>',
19+
' </section>',
20+
');'
21+
].join('\n');
22+
await writeFile(path.join(root, sourcePath), source);
23+
24+
await new CodebaseIndexer({
25+
rootPath: root,
26+
config: { skipEmbedding: true },
27+
projectOptions: { preferredAnalyzer: 'generic' }
28+
}).index();
29+
30+
const result = await findSymbolReferences(root, 'Panel');
31+
expect(result).toMatchObject({ status: 'success', usageCount: 2, confidence: 'syntactic' });
32+
if (result.status === 'success') {
33+
expect(result.usages.map(({ file, line }) => [file, line])).toEqual([
34+
[sourcePath, 1],
35+
[sourcePath, 5]
36+
]);
37+
}
38+
} finally {
39+
await rm(root, { recursive: true, force: true });
40+
}
41+
});

0 commit comments

Comments
 (0)