IF Commons GitHub

scan

The reader of the structure of a TypeScript or JavaScript source (commons 0036-distribution FR-022, FR-025): which of its characters are code, which are comments and which are the text of strings, so that what a packer reads of a source (the specifiers it imports, whether it does something when it is loaded) is read from code and never from the text of a comment, a string or a template literal that only looks like it.

It is a scanner and not a parser: it tracks comments, the quotes, template literals with their expressions and regular expressions, which is all that the readers below need.

import { importSpecifiers } from "./scan.ts";
importSpecifiers('import a from "./a.ts"; const s = "import b from \\"./b.ts\\"";'); // ["./a.ts"]

Spec: 0001-libraries FR-029

Source: commons/libraries/src/scan.ts

1 of 4 exports are imported by a test of the run. A test that imports an export may not call it or check what it returns.

Scannedtypeno test imports it
type Scanned = {
  readonly code: string;
  readonly strings: readonly { readonly start: number; readonly end: number }[];
  readonly comments: readonly { readonly start: number; readonly end: number }[];
};

What a source is made of, for the readers below: its code with the comments and the insides of strings, templates and regular expressions blanked (each character a space, the quotes and the lines kept), and where the strings and the comments are.

scanSourcefunctionno test imports it
function scanSource(source: string): Scanned

Reads a source's structure: see Scanned. A template literal is blanked whole, its expressions included, and is no string.

specifierRangesfunctionno test imports it
function specifierRanges(source: string): readonly { readonly start: number; readonly end: number; }[]

The strings of a source that are specifiers: those that follow from, import or import(, with where each is. A name in a string that is no specifier (a list of packages, a field of a manifest, the text of generated code) is not one.

importSpecifiersfunction
function importSpecifiers(source: string): readonly string[]

The specifiers a source imports: no comment, no string that is no specifier.

Imported by 15 tests of the run (15 passed, 0 failed): the library's results

Titles, headings, exports, tables, columns and requirements. Esc closes.