Skip to content
Merged
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
105 changes: 59 additions & 46 deletions ts-parser/src/utils/typescript-structure.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,60 +114,68 @@ export class TypeScriptStructureAnalyzer {
}

const allFiles = new Set<string>();
const isNodeModules = (filePath: string) => {
const relativePath = path.relative(module.path, filePath);
return relativePath.split(path.sep).includes('node_modules');
};

const addFileIfAllowed = (filePath: string) => {
if (!isNodeModules(filePath)) {
allFiles.add(filePath);
}
};

// 1. Handle srcPatterns if provided
if (options.srcPatterns && options.srcPatterns.length > 0) {
// For now, if patterns are provided, we search the entire module for matching files
// In a more complex implementation, we might want to support actual glob matching
// Here we reuse findTypeScriptFiles which already finds .ts/.js files
const files = this.findTypeScriptFiles(module.path, options);
files.forEach(f => allFiles.add(f));
return Array.from(allFiles);
}

// 2. Original behavior fallback
// Get tsconfig.json configuration
const config = this.tsConfigCache.getTsConfig(module.path);

// Default: all files in tsconfig.json
if(config.fileNames && config.fileNames.length > 0) {
config.fileNames.forEach(file => {
if (fs.existsSync(file)) {
allFiles.add(file);
}
});
if (allFiles.size > 0) {
return Array.from(allFiles);
files.forEach(f => addFileIfAllowed(f));
} else {
// 2. Original behavior fallback
// Get tsconfig.json configuration
const config = this.tsConfigCache.getTsConfig(module.path);

// Default: all files in tsconfig.json
if(config.fileNames && config.fileNames.length > 0) {
config.fileNames.forEach(file => {
if (fs.existsSync(file)) {
addFileIfAllowed(file);
}
});
}
}

// Fallback to rootDir and outDir
const searchDirs: string[] = [];
if (config.rootDir) {
searchDirs.push(path.join(module.path, config.rootDir));
}
if (config.outDir && !(options.noDist && config.outDir === 'dist')) {
searchDirs.push(path.join(module.path, config.outDir));
}
if (allFiles.size === 0) {
// Fallback to rootDir and outDir
const searchDirs: string[] = [];
if (config.rootDir) {
searchDirs.push(path.join(module.path, config.rootDir));
}
if (config.outDir && !(options.noDist && config.outDir === 'dist')) {
searchDirs.push(path.join(module.path, config.outDir));
}

// Default source directories
const defaultDirs = ['src', 'lib'];
if (!options.noDist) {
defaultDirs.push('dist');
}

for (const dir of defaultDirs) {
const dirPath = path.join(module.path, dir);
if (fs.existsSync(dirPath) && fs.statSync(dirPath).isDirectory()) {
searchDirs.push(dirPath);
}
}
// Default source directories
const defaultDirs = ['src', 'lib'];
if (!options.noDist) {
defaultDirs.push('dist');
}
for (const dir of defaultDirs) {
const dirPath = path.join(module.path, dir);
if (fs.existsSync(dirPath) && fs.statSync(dirPath).isDirectory()) {
searchDirs.push(dirPath);
}
}

// Find all files in the collected directories
for (const dir of [...new Set(searchDirs)]) {
if (fs.existsSync(dir)) {
const files = this.findTypeScriptFiles(dir, options);
files.forEach(f => allFiles.add(f));
// Find all files in the collected directories
for (const dir of [...new Set(searchDirs)]) {
if (fs.existsSync(dir)) {
const files = this.findTypeScriptFiles(dir, options);
files.forEach(f => addFileIfAllowed(f));
}
}
}
}

Expand Down Expand Up @@ -202,7 +210,12 @@ export class TypeScriptStructureAnalyzer {
private findTypeScriptFiles(dir: string, options: { noDist?: boolean, srcPatterns?: string[] } = {}): string[] {
const files: string[] = [];

function traverse(currentDir: string) {
// Safety check: if the starting directory itself is node_modules, skip it
if (path.basename(dir) === 'node_modules') {
return [];
}

const traverse = (currentDir: string) => {
if (!fs.existsSync(currentDir)) return;

const entries = fs.readdirSync(currentDir, { withFileTypes: true });
Expand All @@ -228,7 +241,7 @@ export class TypeScriptStructureAnalyzer {
}
}
}
}
};

traverse(dir);
return files;
Expand Down
Loading