checksync
Version:
A tool that allows code to be annotated across different files to ensure they remain in sync.
113 lines (112 loc) • 3.55 kB
TypeScript
import { Targets, normalizeTargetFn, ErrorDetails, TargetType } from "./types";
type TrackedTarget = {
/**
* The type of the target.
*
* Local targets are files we can parse for the return markers.
* Remote targets are URLs.
*
* @type {TargetType}
*/
type: TargetType;
/**
* The checksum for the target as recorded where the target is
* referenced.
*
* @type {string}
*/
checksum: string;
/**
* 0-based index of line where this target was described.
*
* @type {number}
*/
line: number;
/**
* The full declaration for this target's sync-tag.
*
* @type {string}
*/
declaration: string;
};
type TrackedTargets = {
[target: string]: Array<TrackedTarget>;
};
type TrackedMarker = {
/**
* The content for which a checksum will be generated.
*
* @type {Array<string>}
*/
content: Array<string>;
/**
* The targets recorded for this marker.
*
* @type {TrackedTargets}
*/
targets: TrackedTargets;
/**
* The comment start style we detected.
*
* @type {string}
*/
commentStart: string;
/**
* The comment end style we detected.
*
* @type {string}
*/
commentEnd: string;
};
type TrackedMarkers = {
[id: string]: TrackedMarker;
};
type addMarkerFn = (id: string, content: ReadonlyArray<string> | undefined, targets: Targets, commentStart: string, commentEnd: string) => void;
type recordErrorFn = (error: ErrorDetails) => void;
/**
* Parser to extract sync markers from lines of text.
*
* This expects to be given lines in the order they appear in a given text
* and builds marker information as it goes. When a complete marker is parsed,
* a callback is invoked, passing the details of that marker to the calling
* code.
*
* @export
* @class MarkerParser
*/
export default class MarkerParser {
_openMarkers: TrackedMarkers;
_addMarker: addMarkerFn;
_recordError: recordErrorFn;
_normalizePath: normalizeTargetFn;
_startTagRegExp: string;
_startTagDecodeRegExp: string;
_endTagRegExp: string;
_endTagDecodeRegExp: string;
_lineNumber: number;
/**
* Construct a `MarkerParser` instance.
*
* @param {normalizeTargetFn} normalizePath - Callback that will normalize a given path.
* @param {addMarkerFn} addMarker - Callback invoked when a complete marker has been parsed.
* @param {recordErrorFn} recordError - Callback invoked to record an error.
* @param {$ReadOnlyArray<string>} comments - An array of strings that are used to detect the start of single-line comments.
*/
constructor(normalizePath: normalizeTargetFn, addMarker: addMarkerFn, recordError: recordErrorFn, comments: ReadonlyArray<string>);
_recordMarkerStart: (id: string, targetPath: string, line: number, checksum: string, commentStart: string, commentEnd: string, declaration: string) => void;
_recordMarkerEnd: (id: string, line: number) => void;
_recordUnterminatedMarkerEnd: (id: string) => void;
_addContentToOpenMarkers: (line: string) => void;
recordUnterminatedMarkers: () => void;
/**
* Parse a line of content and build into markers as appropriate.
*
* This assumes it is being called for each line of a block of text, one
* line at a time.
*
* @memberof MarkerParser
* @param {string} content The line content to be parsed.
*/
parseLine: (content: string) => void;
}
export {};