regex-explain 0.0.0-stage → 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,123 @@
1
+ /** One explained piece of the pattern. */
2
+ export interface ExplanationNode {
3
+ /** The pattern source this node explains. */
4
+ readonly raw: string;
5
+ /** Offset of `raw` in the pattern, inclusive. */
6
+ readonly start: number;
7
+ /** Offset of `raw` in the pattern, exclusive. */
8
+ readonly end: number;
9
+ /** A sentence or noun phrase describing `raw`. May contain Markdown code spans. */
10
+ readonly description: string;
11
+ /** Explanations of the parts of `raw`, in source order. */
12
+ readonly children: readonly ExplanationNode[];
13
+ }
14
+ /** The meaning of one flag. */
15
+ export interface FlagExplanation {
16
+ /** The flag letter, such as `g`. */
17
+ readonly flag: string;
18
+ /** The `RegExp` property the flag sets, such as `global`. */
19
+ readonly name: string;
20
+ /** What the flag does. May contain Markdown code spans. */
21
+ readonly description: string;
22
+ }
23
+ /** A regex as `new RegExp(pattern, flags)` takes it: the pattern without slashes, and the flags. */
24
+ export interface RegexSource {
25
+ /** The pattern, without slashes. */
26
+ readonly pattern: string;
27
+ /** The flags. */
28
+ readonly flags: string;
29
+ }
30
+ /**
31
+ * Checks whether a value is a {@link RegexSource}, such as an untyped command argument.
32
+ *
33
+ * @param value The value.
34
+ *
35
+ * @returns Whether the value has string `pattern` and `flags` properties.
36
+ */
37
+ export declare function isRegexSource(value: unknown): value is RegexSource;
38
+ /** A pattern that can take super-linear time to reject some inputs (ReDoS). */
39
+ export interface BacktrackingWarning {
40
+ /** The quantifier that backtracks, as pattern source. */
41
+ readonly raw: string;
42
+ /** Offset of `raw` in the pattern, inclusive. */
43
+ readonly start: number;
44
+ /** Offset of `raw` in the pattern, exclusive. */
45
+ readonly end: number;
46
+ /** Whether the backtracking is exponential rather than polynomial. */
47
+ readonly exponential: boolean;
48
+ /** A character that, repeated many times, triggers the backtracking. */
49
+ readonly attackCharacter: string;
50
+ /** What the problem is. May contain Markdown code spans. */
51
+ readonly description: string;
52
+ /** An equivalent regex without this problem, when one could be derived. */
53
+ readonly fix?: RegexSource;
54
+ }
55
+ /** How long a match can be. */
56
+ export interface LengthSummary {
57
+ /** The fewest characters a match consumes. */
58
+ readonly min: number;
59
+ /** The most characters a match consumes; `Infinity` when unbounded. */
60
+ readonly max: number;
61
+ }
62
+ /** A successfully explained regex. */
63
+ export interface RegexExplanation extends RegexSource {
64
+ /** Discriminates success from {@link RegexExplanationError}. */
65
+ readonly ok: true;
66
+ /** Top-level explanation steps, in match order. */
67
+ readonly nodes: readonly ExplanationNode[];
68
+ /** The meaning of each flag, in the order given. */
69
+ readonly flagExplanations: readonly FlagExplanation[];
70
+ /** How long a match can be. */
71
+ readonly length: LengthSummary;
72
+ /** Catastrophic backtracking problems; empty when none were found or the check was disabled. */
73
+ readonly warnings: readonly BacktrackingWarning[];
74
+ /**
75
+ * Whether `^` and `$` match at line breaks anywhere in the pattern, through the `m` flag or a
76
+ * group such as `(?m:...)`, so that text is matched as separate lines.
77
+ */
78
+ readonly multiline: boolean;
79
+ }
80
+ /** A regex that could not be parsed. */
81
+ export interface RegexExplanationError extends RegexSource {
82
+ /** Discriminates failure from {@link RegexExplanation}. */
83
+ readonly ok: false;
84
+ /** The parser's error message. */
85
+ readonly message: string;
86
+ /** Offset into the pattern where the error was detected, if known. */
87
+ readonly index?: number;
88
+ }
89
+ /** The result of {@link explainRegex}. */
90
+ export type ExplainResult = RegexExplanation | RegexExplanationError;
91
+ /** Options for {@link explainRegex}. */
92
+ export interface ExplainOptions {
93
+ /** Check for catastrophic backtracking. Defaults to `true`. */
94
+ readonly checkBacktracking?: boolean;
95
+ }
96
+ /**
97
+ * Explains a regex pattern compiled with the given flags. Syntax errors are returned, not thrown.
98
+ *
99
+ * @param pattern The pattern without surrounding slashes, as passed to `new RegExp()`.
100
+ * @param flags The flags, such as `"gi"`.
101
+ * @param options Analysis options.
102
+ *
103
+ * @returns The explanation, or the reason the pattern is invalid.
104
+ */
105
+ export declare function explainRegex(pattern: string, flags?: string, options?: ExplainOptions): ExplainResult;
106
+ /**
107
+ * Reads typed or selected text as a regex.
108
+ *
109
+ * @param text Either a literal such as `/ab+c/gi`, optionally surrounded by whitespace, or a bare
110
+ * pattern.
111
+ *
112
+ * @returns The regex; bare patterns have no flags. The pattern is not validated.
113
+ */
114
+ export declare function parseRegexInput(text: string): RegexSource;
115
+ /**
116
+ * Words a repetition count.
117
+ *
118
+ * @param min The minimum count.
119
+ * @param max The maximum count; `Infinity` when unbounded.
120
+ *
121
+ * @returns A phrase such as "one or more times".
122
+ */
123
+ export declare function describeCount(min: number, max: number): string;