All files / utils IncludeDirectiveText.ts

100% Statements 6/6
100% Branches 6/6
100% Functions 2/2
100% Lines 5/5

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                                              1987x 1987x 1984x 1984x               1778x              
/**
 * The text of an `#include` directive: split into the path it names and its
 * form, and joined back.
 *
 * #1672 made this the one split of a directive's text: 1.1 reads the lexer's
 * token with it, and 2.1 and 2.3 read the parser's, which is the same token.
 * Each held regexes of its own, which agreed with it on every fixture.
 *
 * #1444: it lived on `IncludeDiscovery`, so every pass that read include text
 * reached a discovery primitive to do it. Splitting text decides nothing about
 * which files exist, and three passes need it, which is `utils/`' admission
 * test. Whether a spelling names C-Next source is a decision, and stays with
 * 1.1 Discover, which records its answer per directive.
 */
class IncludeDirectiveText {
  /**
   * One `INCLUDE_DIRECTIVE` token's path and delimiter, or null when the text
   * names nothing: `#include <>` is a token too, and text with no delimiter
   * is not a directive.
   *
   * @param text - An `INCLUDE_DIRECTIVE` token's text, exactly
   */
  static split(text: string): { path: string; isLocal: boolean } | null {
    const open = text.search(/[<"]/);
    if (open === -1) return null;
    const path = text.slice(open + 1, -1);
    return path === "" ? null : { path, isLocal: text[open] === '"' };
  }
 
  /**
   * The directive that names `include`: `#include "path"` or `#include <path>`.
   * The key 1.1 records its answers under and later passes read them by.
   */
  static join(include: { path: string; isLocal: boolean }): string {
    return include.isLocal
      ? `#include "${include.path}"`
      : `#include <${include.path}>`;
  }
}
 
export default IncludeDirectiveText;