Hone logo
Hone
Problems

Implementing Triple-Slash Directives for Enhanced Type Safety in TypeScript

Triple-slash directives (/// <reference ...>) are a powerful feature in TypeScript that allows you to reference external files, type definitions, and other modules, enabling better type checking and code organization. This challenge asks you to create a function that parses a TypeScript file and extracts all triple-slash directives, validating their syntax and resolving their paths relative to the original file. This is crucial for ensuring that your project's type definitions are correctly linked and that the TypeScript compiler can accurately infer types across multiple files.

Problem Description

You are tasked with creating a TypeScript function called extractTripleSlashDirectives. This function will take a string representing the content of a TypeScript file as input and return an array of strings, where each string represents a valid triple-slash directive found in the file. The function should:

  1. Identify Triple-Slash Directives: Locate all occurrences of /// <reference ...> within the input string.
  2. Validate Syntax: Ensure that each identified directive follows the correct syntax: /// <reference path="..." /> or /// <reference types="..." />. The path or types attribute must be enclosed in double quotes.
  3. Extract Directive Text: Extract the complete directive text (e.g., /// <reference path="some/path.d.ts" />).
  4. Handle Invalid Directives: Ignore any directives that do not conform to the expected syntax.
  5. Return Valid Directives: Return an array containing only the valid triple-slash directive strings.

Examples

Example 1:

Input: `/// <reference path="utils.d.ts" />\nconst x = 1;`
Output: `["/// <reference path=\"utils.d.ts\" />"]`
Explanation: The input contains one valid triple-slash directive referencing a path. The function should extract this directive and return it in an array.

**Example 2:**

Input: /// <reference path="utils.d.ts" />\n/// <reference types="jquery" />\nconst x = 1; Output: ["/// <reference path=\"utils.d.ts\" />", "/// <reference types=\"jquery\" />"] Explanation: The input contains two valid directives, one referencing a path and one referencing types. Both should be extracted.

Example 3:

Input: `/// <reference path=utils.d.ts />\nconst x = 1;`
Output: `[]`
Explanation: The path attribute is not enclosed in double quotes, making the directive invalid. The function should ignore this directive.

**Example 4:**

Input: /// <reference path="utils.d.ts" />\n/// <reference path="another/file.d.ts" />\nconst x = 1; Output: ["/// <reference path=\"utils.d.ts\" />", "/// <reference path=\"another/file.d.ts\" />"] Explanation: Multiple valid directives are present.

Example 5:

Input: `const x = 1;`
Output: `[]`
Explanation: No triple-slash directives are present.

Constraints

  • The input string will always be a valid TypeScript file content.
  • The path or types attribute within the directive will contain only alphanumeric characters, underscores, periods, and forward slashes.
  • The function must return an array of strings.
  • The function should be efficient enough to handle files up to 10,000 lines of code.

Notes

  • Regular expressions can be a useful tool for identifying and validating the directives.
  • Consider edge cases such as comments containing similar patterns and directives spanning multiple lines (though multi-line directives are not standard and should be ignored).
  • Focus on accurately identifying and extracting valid directives according to the specified syntax. Don't attempt to resolve the paths; simply extract the directive string as is.
  • The order of the directives in the output array should match the order they appear in the input string.
Loading editor...
typescript