All files / transpiler/logic detectCppSyntax.ts

100% Statements 11/11
100% Branches 10/10
100% Functions 1/1
100% Lines 7/7

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43                                            40x     30x 4x     26x     24x     21x     18x        
/**
 * Issue #208: Detect if header content contains C++ syntax requiring C++14 parser
 *
 * This heuristic determines whether to parse a .h file with the C or C++ parser.
 * Previously, the pipeline would parse ALL .h files with BOTH parsers and merge,
 * which was wasteful and led to issues with C++-specific features like typed enums.
 *
 * C++ indicators:
 * - Typed enums: enum Name : type { ... }
 * - Class/struct inheritance: class Foo : public Bar
 * - Namespaces: namespace Foo { ... }
 * - Templates: template<...>
 * - Access specifiers: public:, private:, protected:
 */
 
/**
 * Detect if header content contains C++ syntax requiring C++14 parser
 * @param content Raw header file content
 * @returns true if C++ parser should be used, false for C parser
 */
function detectCppSyntax(content: string): boolean {
  // Typed enums: enum Name : type { (C++14 feature, key for Issue #208)
  if (/enum\s+\w+\s*:\s*\w+\s*\{/.test(content)) return true;
 
  // Class/struct with inheritance: class Foo : public/private/protected Bar
  if (/\b(class|struct)\s+\w+\s*:\s*(public|private|protected)/.test(content))
    return true;
 
  // namespace keyword: namespace Foo {
  if (/\bnamespace\s+\w+/.test(content)) return true;
 
  // template declarations: template<...>
  if (/\btemplate\s*</.test(content)) return true;
 
  // Access specifiers at line start (class members)
  if (/^\s*(public|private|protected)\s*:/m.test(content)) return true;
 
  // Default to C parser for pure C headers
  return false;
}
 
export default detectCppSyntax;