Skip to content

Commit

Permalink
feat: Add ParseFilesConfig.resolveParseFiles to handle the include/ex…
Browse files Browse the repository at this point in the history
…clude config
  • Loading branch information
littleGnAl committed Dec 13, 2023
1 parent 1767ab7 commit d84cff0
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 4 deletions.
36 changes: 36 additions & 0 deletions cxx-parser/__tests__/unit_test/cxx_parser_configs.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
});
});
4 changes: 1 addition & 3 deletions cxx-parser/src/cxx_parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
23 changes: 22 additions & 1 deletion cxx-parser/src/cxx_parser_configs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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[];
Expand All @@ -37,7 +58,7 @@ export class CXXParserConfigs {
return _resolvePaths(it, configDir);
})
.flat(1),
},
} as ParseFilesConfig,
};
}
}

0 comments on commit d84cff0

Please sign in to comment.