Skip to content

Commit 2144415

Browse files
committed
fix bug: if two files has same symbol name, should give a mangled name
1 parent 97b2ba6 commit 2144415

3 files changed

Lines changed: 60 additions & 18 deletions

File tree

ts-parser/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "abcoder-ts-parser",
3-
"version": "0.0.3",
3+
"version": "0.0.6",
44
"description": "TypeScript AST parser for UNIAST v0.1.3 specification",
55
"main": "dist/index.js",
66
"types": "dist/index.d.ts",

ts-parser/src/parser/test/FunctionParser.test.ts

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -513,5 +513,43 @@ describe('FunctionParser', () => {
513513

514514
cleanup();
515515
});
516+
517+
it('should give mangled names for 2 symbol located in different files', () => {
518+
const { project, sourceFile, cleanup } = createTestProjectWithMultipleFiles({
519+
'file1.ts': `
520+
export function sharedName() {
521+
return 'from file1';
522+
}
523+
`,
524+
'file2.ts': `
525+
export function sharedName() {
526+
return 'from file2';
527+
}
528+
`,
529+
'test.ts': `
530+
import { sharedName as sharedName1 } from './file1';
531+
import { sharedName as sharedName2 } from './file2';
532+
533+
function testFunction() {
534+
sharedName1();
535+
sharedName2();
536+
}
537+
`
538+
});
539+
540+
const parser = new FunctionParser(project, process.cwd());
541+
let pkgPathAbsFile: string = sourceFile.getFilePath();
542+
pkgPathAbsFile = pkgPathAbsFile.split('/').slice(0, -1).join('/');
543+
const pkgPath = path.relative(process.cwd(), pkgPathAbsFile);
544+
545+
const functions = parser.parseFunctions(sourceFile, 'parser-tests', pkgPath);
546+
547+
const testFunction = expectToBeDefined(functions['testFunction']);
548+
expect(testFunction.FunctionCalls).toBeDefined();
549+
550+
const callNames = expectToBeDefined(testFunction.FunctionCalls).map(call => call.Name);
551+
expect(callNames).toHaveLength(2);
552+
cleanup();
553+
});
516554
});
517555
});

ts-parser/src/utils/symbol-resolver.ts

Lines changed: 21 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -323,19 +323,22 @@ const symbolNameCache = new Map<string, Symbol>();
323323

324324
export function assignSymbolName(symbol: Symbol): string {
325325

326-
const decls = symbol.getDeclarations()
327-
if(decls.length === 0) {
328-
return symbol.getName()
326+
const decls = symbol.getDeclarations();
327+
if (decls.length === 0) {
328+
return symbol.getName();
329329
}
330330

331-
const declFile = decls[0].getSourceFile()
332-
const declFilePath = declFile.getFilePath()
331+
const declFile = decls[0].getSourceFile();
332+
const declFilePath = declFile.getFilePath();
333+
const declDirPath = path.dirname(declFilePath);
333334

334-
if(declFile.getDefaultExportSymbol() === symbol) {
335-
return declFile.getBaseName() + '_default_export_symbol'
336-
}
335+
let rawName = symbol.getName(); // Initialize rawName here
336+
337+
const id = declDirPath + "#" + rawName;
337338

338-
let rawName = symbol.getName()
339+
if (declFile.getDefaultExportSymbol() === symbol) {
340+
return declFile.getBaseName() + '_default_export_symbol';
341+
}
339342

340343
// Handle methods, properties, constructors, and functions with proper naming
341344
const firstDecl = decls[0];
@@ -381,17 +384,12 @@ export function assignSymbolName(symbol: Symbol): string {
381384
}
382385
}
383386

384-
const id = declFilePath + "#" + rawName
385387
if(!symbolNameCache.has(id)) {
386388
symbolNameCache.set(id, symbol)
387389
return rawName
388390
}
389391

390-
const symbolExists = symbolNameCache.get(id)
391-
// make ts happy
392-
if(!symbolExists) {
393-
return rawName
394-
}
392+
const symbolExists = symbolNameCache.get(id)!
395393

396394
const getDeclsPos = (symbol: Symbol) => {
397395
const declsPos = []
@@ -404,10 +402,16 @@ export function assignSymbolName(symbol: Symbol): string {
404402

405403
const arr1 = getDeclsPos(symbol)
406404
const arr2 = getDeclsPos(symbolExists)
407-
if(arr1.join(',') === arr2.join(',')) {
405+
406+
const symbolExistsDecls = symbolExists.getDeclarations()
407+
if(symbolExistsDecls.length === 0) {
408+
return rawName
409+
}
410+
const symbolExistsDeclFile = symbolExistsDecls[0].getSourceFile()
411+
if(arr1.join(',') === arr2.join(',') && symbolExistsDeclFile.getFilePath() === declFilePath) {
408412
return rawName
409413
}
410414

411415
// mangled name
412-
return rawName + "_" + getDeclsPos(symbol).join(".")
416+
return rawName + "_" + path.basename(declFilePath) + "_" + getDeclsPos(symbol).join(".")
413417
}

0 commit comments

Comments
 (0)