diff --git a/cxx-parser/__tests__/unit_test/cxx_parser_configs.test.ts b/cxx-parser/__tests__/unit_test/cxx_parser_configs.test.ts index ee3dd8a..2cf36fa 100644 --- a/cxx-parser/__tests__/unit_test/cxx_parser_configs.test.ts +++ b/cxx-parser/__tests__/unit_test/cxx_parser_configs.test.ts @@ -42,3 +42,39 @@ describe('CXXParserConfigs', () => { expect(config.parseFiles.include[1]).toEqual(expectedFiles[1]); // file2.h }); }); + +describe('ParseFilesConfig', () => { + let tmpDir: string = ''; + + beforeEach(() => { + tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), 'terra-ut-')); + }); + + afterEach(() => { + fs.rmSync(tmpDir, { recursive: true, force: true }); + }); + + it('resolveParseFiles with exclude', () => { + let path1 = path.join(tmpDir, 'file1.h'); + let path2 = path.join(tmpDir, 'file2.h'); + fs.writeFileSync(path1, '// file1.h'); + fs.writeFileSync(path2, '// file2.h'); + + let args = { + includeHeaderDirs: [], + definesMacros: [], + parseFiles: { include: [path1, path2], exclude: [path2] }, + customHeaders: [], + }; + + let config = CXXParserConfigs.resolve( + tmpDir, + args as unknown as CXXParserConfigs + ); + + let finalParseFiles = config.parseFiles.resolveParseFiles(); + + expect(finalParseFiles.length).toBe(1); + expect(finalParseFiles[0]).toEqual(path1); // file1.h + }); +}); diff --git a/cxx-parser/src/cxx_parser.ts b/cxx-parser/src/cxx_parser.ts index 7d9a429..090985a 100644 --- a/cxx-parser/src/cxx_parser.ts +++ b/cxx-parser/src/cxx_parser.ts @@ -117,9 +117,7 @@ export function CXXParser( ): ParseResult | undefined { let cxxParserConfigs = CXXParserConfigs.resolve(terraContext.configDir, args); - let parseFiles = cxxParserConfigs.parseFiles.include.filter((it) => { - return !cxxParserConfigs.parseFiles.exclude.includes(it); - }); + let parseFiles = cxxParserConfigs.parseFiles.resolveParseFiles(); let jsonContent = dumpCXXAstJson( terraContext, cxxParserConfigs.includeHeaderDirs, diff --git a/cxx-parser/src/cxx_parser_configs.ts b/cxx-parser/src/cxx_parser_configs.ts index 9ccb8a6..c3e5f42 100644 --- a/cxx-parser/src/cxx_parser_configs.ts +++ b/cxx-parser/src/cxx_parser_configs.ts @@ -11,6 +11,27 @@ export interface ParseFilesConfig { exclude: string[]; } +export class ParseFilesConfig { + /** + * Resolves the files to be parsed based on the include and exclude configurations. + * + * The include and exclude configurations determine which files should be included or excluded from parsing. + * + * The logic for resolving the parse files is as follows: + * - If include is specified, only files matching the include patterns will be included. + * - If exclude is specified, files matching the exclude patterns will be excluded. + * - If both include and exclude are specified, files matching the include patterns but not matching the exclude patterns will be included. + * - If neither include nor exclude is specified, all files will be included. + * + * @returns An array of strings representing the files to be parsed. + */ + resolveParseFiles(): string[] { + return this.include.filter((it) => { + return !this.exclude.includes(it); + }); + } +} + export interface CXXParserConfigs { includeHeaderDirs: string[]; definesMacros: string[]; @@ -37,7 +58,7 @@ export class CXXParserConfigs { return _resolvePaths(it, configDir); }) .flat(1), - }, + } as ParseFilesConfig, }; } }