astwerk 0.0.0-stage → 0.1.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,374 @@
1
+ type OfType<Union extends {
2
+ type: string;
3
+ }, Type extends Union['type']> = Union extends unknown ? Type extends Union['type'] ? Union : never : never;
4
+ interface Span {
5
+ from: number;
6
+ to: number;
7
+ }
8
+ declare const COMPARE_OPERATORS: readonly ['>=', '<=', '>', '<'];
9
+ type CompareOperator = (typeof COMPARE_OPERATORS)[number];
10
+ /** A value as written. `quoted` is true when any part of it was in quotes, which makes `@` and `*` plain text. */
11
+ interface EqualValue {
12
+ kind: 'equal';
13
+ raw: string;
14
+ quoted: boolean;
15
+ span: Span;
16
+ }
17
+ interface CompareValue {
18
+ kind: 'compare';
19
+ operator: CompareOperator;
20
+ raw: string;
21
+ quoted: boolean;
22
+ span: Span;
23
+ }
24
+ /** An open end, written as `*` or left empty, has no bound. */
25
+ interface RangeValue {
26
+ kind: 'range';
27
+ min?: RangeBound;
28
+ max?: RangeBound;
29
+ separator: Span;
30
+ span: Span;
31
+ }
32
+ interface RangeBound {
33
+ raw: string;
34
+ quoted: boolean;
35
+ }
36
+ type RawValue = EqualValue | CompareValue | RangeValue;
37
+ interface SyntaxGroup {
38
+ type: 'and' | 'or';
39
+ children: Array<SyntaxNode>;
40
+ span: Span;
41
+ }
42
+ interface SyntaxNot {
43
+ type: 'not';
44
+ child: SyntaxNode;
45
+ span: Span;
46
+ }
47
+ interface SyntaxTerm {
48
+ type: 'term';
49
+ key: string;
50
+ keySpan: Span;
51
+ values: Array<RawValue>;
52
+ span: Span;
53
+ }
54
+ interface SyntaxText {
55
+ type: 'text';
56
+ value: string;
57
+ quoted: boolean;
58
+ span: Span;
59
+ }
60
+ type SyntaxNode = SyntaxGroup | SyntaxNot | SyntaxTerm | SyntaxText;
61
+ /** The cursor is on a word that may become a field name. Replacing `from` to `to` replaces the key and its colon. */
62
+ interface KeyCompletion {
63
+ kind: 'key';
64
+ prefix: string;
65
+ from: number;
66
+ to: number;
67
+ }
68
+ /** The cursor is on a value of `key`. `taken` holds the term's other values, and `operator` a written comparison. */
69
+ interface ValueCompletion {
70
+ kind: 'value';
71
+ key: string;
72
+ prefix: string;
73
+ from: number;
74
+ to: number;
75
+ taken: Array<string>;
76
+ atEnd: boolean;
77
+ operator?: CompareOperator;
78
+ }
79
+ type CompletionContext = KeyCompletion | ValueCompletion;
80
+ /**
81
+ * What the cursor is on, for autocompletion. Returns `undefined` inside quoted free text, where nothing is suggested.
82
+ */
83
+ export declare function getCompletionContext(text: string, cursor: number): CompletionContext | undefined;
84
+ interface QueryGroup {
85
+ type: 'and' | 'or';
86
+ children: Array<Query>;
87
+ }
88
+ interface QueryNot {
89
+ type: 'not';
90
+ child: Query;
91
+ }
92
+ /** Values are the field type's values, or a `TokenValue` for a dynamic value such as `@me`. */
93
+ interface InCondition {
94
+ type: 'in';
95
+ field: string;
96
+ values: Array<unknown>;
97
+ }
98
+ interface Bound {
99
+ value: unknown;
100
+ inclusive: boolean;
101
+ }
102
+ interface RangeCondition {
103
+ type: 'range';
104
+ field: string;
105
+ min?: Bound;
106
+ max?: Bound;
107
+ }
108
+ interface TextCondition {
109
+ type: 'text';
110
+ value: string;
111
+ }
112
+ /** A field with no value, written `no:field`. `has:field` is its negation. */
113
+ interface EmptyCondition {
114
+ type: 'empty';
115
+ field: string;
116
+ }
117
+ /** A value with wildcards, where each `*` in `pattern` stands for any text. */
118
+ interface MatchCondition {
119
+ type: 'match';
120
+ field: string;
121
+ pattern: string;
122
+ }
123
+ /**
124
+ * Text that could not be bound, kept as written so that it stays in the text. `fold` and `toPredicate` skip it, so it
125
+ * filters nothing. A single invalid value of a known field is kept as a term of that field.
126
+ */
127
+ interface InvalidQuery {
128
+ type: 'invalid';
129
+ syntax: SyntaxNode;
130
+ }
131
+ type Condition = InCondition | RangeCondition | TextCondition | EmptyCondition | MatchCondition;
132
+ type FieldCondition = Exclude<Condition, TextCondition>;
133
+ type Query = QueryGroup | QueryNot | Condition | InvalidQuery;
134
+ type QueryOf<Type extends Query['type']> = OfType<Query, Type>;
135
+ /**
136
+ * What tokens are resolved with. Apps add members for their own tokens, such as the signed-in user, by declaration
137
+ * merging.
138
+ */
139
+ interface ResolveContext {
140
+ /** @defaultValue the current time */
141
+ now?: Date;
142
+ }
143
+ interface TokenOffset {
144
+ amount: number;
145
+ unit?: string;
146
+ }
147
+ /** A dynamic value such as `@today-7d`. The query keeps it unresolved, so the text keeps its meaning. */
148
+ interface TokenValue {
149
+ kind: 'token';
150
+ name: string;
151
+ offset?: TokenOffset;
152
+ raw: string;
153
+ }
154
+ export declare function isTokenValue(value: unknown): value is TokenValue;
155
+ interface FieldSuggestion<V> {
156
+ value: V;
157
+ label?: string;
158
+ description?: string;
159
+ }
160
+ interface TokenDefinition<V> {
161
+ description?: string;
162
+ /** Allows an offset such as `+3` or `-7d`. A unit, when written, is one of these. */
163
+ offsetUnits?: ReadonlyArray<string>;
164
+ resolve(offset: TokenOffset | undefined, context: ResolveContext & {
165
+ now: Date;
166
+ }): V;
167
+ }
168
+ /** How a field reads, writes and compares its values. Built-in types and app types implement the same interface. */
169
+ interface FieldType<V> {
170
+ /** Returns `undefined` for text that is not a value of the type, which reports an invalid value. */
171
+ parse(raw: string): V | undefined;
172
+ format(value: V): string;
173
+ /** Decides `field:value`. Without it, `compare` decides, and without both, `==`. */
174
+ equals?(a: V, b: V): boolean;
175
+ /** Orders values. Only a type with `compare` takes comparisons and ranges. */
176
+ compare?(a: V, b: V): number;
177
+ /** Lets an unquoted `*` stand for any text, as in `label:*bug*`. */
178
+ wildcards?: boolean;
179
+ suggest?(prefix: string): Array<FieldSuggestion<V>> | Promise<Array<FieldSuggestion<V>>>;
180
+ /** Dynamic values the type accepts, by name without the `@`. */
181
+ tokens?: Readonly<Record<string, TokenDefinition<V>>>;
182
+ }
183
+ type FieldTypeOptions<V> = Pick<FieldType<V>, 'suggest' | 'tokens'>;
184
+ interface FieldDefinition<Item, V> {
185
+ type: FieldType<V>;
186
+ label?: string;
187
+ description?: string;
188
+ /** Other names the field answers to, such as the names in older links. Text is written with the field's own name. */
189
+ aliases?: ReadonlyArray<string>;
190
+ /**
191
+ * Reads the field from an item for `toPredicate`.
192
+ *
193
+ * @defaultValue the item's own property of the field's name
194
+ */
195
+ get?(item: Item): NoInfer<V> | ReadonlyArray<NoInfer<V>> | null | undefined;
196
+ /**
197
+ * Makes the field stand for other conditions when a query is evaluated, as `involves:@me` stands for
198
+ * `author:@me OR assignee:@me`. The text keeps the field as written. Returning `undefined` makes the field filter
199
+ * nothing, for an option such as `sort:` that the app reads with `valuesOf`.
200
+ */
201
+ expand?: (condition: FieldCondition) => Query | undefined;
202
+ }
203
+ /** `has` and `no` take field names as values, so no field can use either name. */
204
+ interface ReservedFieldNames {
205
+ has?: never;
206
+ no?: never;
207
+ }
208
+ interface SchemaDefinition<Item, Values extends Record<string, unknown>> {
209
+ fields: { [Name in keyof Values]: FieldDefinition<Item, Values[Name]>; } & Record<string, FieldDefinition<Item, unknown>>;
210
+ /** The text that free words search in `toPredicate`. Without it, free words filter nothing there. */
211
+ text?(item: Item): string;
212
+ }
213
+ interface Schema<Item = unknown> {
214
+ readonly fields: Readonly<Record<string, FieldDefinition<Item, unknown>>>;
215
+ text?(item: Item): string;
216
+ readonly '~names': ReadonlyMap<string, string>;
217
+ }
218
+ /** Field names are matched exactly, so `Label:bug` is an unknown field when the field is `label`. */
219
+ export declare function defineSchema<Item = unknown, Values extends Record<string, unknown> & ReservedFieldNames = {}>(definition: SchemaDefinition<Item, Values>): Schema<Item>;
220
+ /** `insertText` is ready to replace the context's `from` to `to`, quoted where the value needs it. */
221
+ interface Suggestion {
222
+ label: string;
223
+ insertText: string;
224
+ description?: string;
225
+ }
226
+ type Suggestions = Array<Suggestion> | Promise<Array<Suggestion>>;
227
+ /**
228
+ * Suggests field names for a key, and for a value the field type's suggestions followed by its tokens. A field type
229
+ * whose `suggest` is asynchronous makes the result a promise.
230
+ */
231
+ export declare function getSuggestions(context: CompletionContext, schema: Schema): Suggestions;
232
+ export declare const DIAGNOSTIC_CODE: {
233
+ readonly unknownField: 'unknownField';
234
+ readonly invalidValue: 'invalidValue';
235
+ readonly unknownToken: 'unknownToken';
236
+ readonly unsupportedOperator: 'unsupportedOperator';
237
+ readonly negatedGroup: 'negatedGroup';
238
+ readonly unclosedQuote: 'unclosedQuote';
239
+ readonly unbalancedParenthesis: 'unbalancedParenthesis';
240
+ readonly nestingTooDeep: 'nestingTooDeep';
241
+ readonly emptyRange: 'emptyRange';
242
+ };
243
+ type DiagnosticCode = (typeof DIAGNOSTIC_CODE)[keyof typeof DIAGNOSTIC_CODE];
244
+ /**
245
+ * An `error` marks a part that filters nothing. A `warning` marks text that is read and filters, but maybe not as
246
+ * meant.
247
+ */
248
+ export declare const DIAGNOSTIC_SEVERITY: {
249
+ readonly error: 'error';
250
+ readonly warning: 'warning';
251
+ };
252
+ type DiagnosticSeverity = (typeof DIAGNOSTIC_SEVERITY)[keyof typeof DIAGNOSTIC_SEVERITY];
253
+ /** A problem in the query text. It carries no message, so the app words it from `code`. */
254
+ interface Diagnostic extends Span {
255
+ code: DiagnosticCode;
256
+ severity: DiagnosticSeverity;
257
+ }
258
+ /** Whether a set also takes items with no value (`no:field`) or leaves them out (`has:field`). */
259
+ export declare const FLAT_EMPTY: {
260
+ readonly include: 'include';
261
+ readonly exclude: 'exclude';
262
+ };
263
+ type FlatEmpty = (typeof FLAT_EMPTY)[keyof typeof FLAT_EMPTY];
264
+ /** Items with any of `include` (all items when it is empty) and none of `exclude`. */
265
+ interface FlatSet {
266
+ kind: 'set';
267
+ include: Array<unknown>;
268
+ exclude: Array<unknown>;
269
+ empty?: FlatEmpty;
270
+ }
271
+ interface FlatRange {
272
+ kind: 'range';
273
+ min?: Bound;
274
+ max?: Bound;
275
+ }
276
+ type FlatField = FlatSet | FlatRange;
277
+ /** A query as the state of a filter panel, field by field. Parts that could not be bound are carried along. */
278
+ interface Flat {
279
+ fields: Record<string, FlatField>;
280
+ text: Array<string>;
281
+ invalid: Array<InvalidQuery>;
282
+ }
283
+ /**
284
+ * Reads a query as panel state. Returns `undefined` for a query a panel cannot show, such as an `OR` across fields,
285
+ * two lists of values for one field, a negated range, a wildcard or negated free text.
286
+ */
287
+ export declare function toFlat(query: Query): Flat | undefined;
288
+ /** Builds a query from panel state, field by field in the order of `fields`, then free text and unbound parts. */
289
+ export declare function fromFlat(flat: Flat): Query;
290
+ interface BindResult {
291
+ query: Query;
292
+ diagnostics: Array<Diagnostic>;
293
+ }
294
+ /**
295
+ * Applies a schema to a syntax tree. Unknown fields, invalid values and unknown tokens become diagnostics and stay in
296
+ * the query as written, so they stay in the text and filter nothing.
297
+ */
298
+ export declare function bind(tree: SyntaxNode, schema: Schema): BindResult;
299
+ /** One function per part of a query, for translating it into a backend's query. */
300
+ interface QueryVisitor<Result> {
301
+ and(children: Array<Result>): Result;
302
+ or(children: Array<Result>): Result;
303
+ not(child: Result): Result;
304
+ in(condition: QueryOf<'in'>): Result;
305
+ range(condition: QueryOf<'range'>): Result;
306
+ text(condition: QueryOf<'text'>): Result;
307
+ empty(condition: QueryOf<'empty'>): Result;
308
+ match(condition: QueryOf<'match'>): Result;
309
+ }
310
+ /**
311
+ * Folds a query from its conditions up. Parts that could not be bound are left out first, so they filter nothing,
312
+ * fields with `expand` are replaced by what they stand for, and a query with nothing left folds as an empty `and`.
313
+ */
314
+ export declare function fold<Result>(query: Query, schema: Schema, visitor: QueryVisitor<Result>): Result;
315
+ type Test<Item> = (item: Item) => boolean;
316
+ /**
317
+ * Builds a filter for items in memory. Values match without regard to case, a free word matches the start of a word in
318
+ * the schema's `text`, parts that could not be bound filter nothing, and fields with `expand` filter by what they
319
+ * stand for.
320
+ */
321
+ export declare function toPredicate<Item>(query: Query, schema: Schema<Item>, context?: ResolveContext): Test<Item>;
322
+ /** Replaces dynamic values such as `@today` with the values they stand for now, for a backend that needs them. */
323
+ export declare function resolveTokens(query: Query, schema: Schema, context?: ResolveContext): Query;
324
+ /**
325
+ * Writes a query as canonical text. A negated group is written by negating its parts, and an `OR` of one field's
326
+ * values as one comma list.
327
+ */
328
+ export declare function stringify(query: Query, schema: Schema): string;
329
+ /** The canonical form of a query text. Writing a canonical text again changes nothing. */
330
+ export declare function canonical(text: string, schema: Schema): string;
331
+ /** The values a field is set to for the whole query, such as the value of `sort:`. Values inside `OR` or `NOT` do not count. */
332
+ export declare function valuesOf(query: Query, field: string): Array<unknown>;
333
+ /** Removes every condition on a field, `has:` and `no:` included, and keeps the rest of the query as it was. */
334
+ export declare function withoutField(query: Query, field: string): Query;
335
+ /** `true` or `false`, in any case. */
336
+ export declare function boolean(): FieldType<boolean>;
337
+ /** An ISO 8601 day (`2026-01-31`) or a UTC instant (`2026-01-31T11:30:00Z`). An item's value may also be a `Date`. */
338
+ type DateValue = string | Date;
339
+ /**
340
+ * A day or an instant in UTC. A day covers all of it, so `created:2026-01-31` is any time that day and
341
+ * `created:>2026-01-31` starts the day after. `@today` is the current day in UTC.
342
+ */
343
+ export declare function date(options?: FieldTypeOptions<DateValue>): FieldType<DateValue>;
344
+ /** One of a fixed list. A value written in another case reads as the listed one, so `is:OPEN` is `is:open`. */
345
+ export declare function enumOf<const Value extends string>(values: ReadonlyArray<Value>, options?: Pick<FieldTypeOptions<Value>, 'tokens'>): FieldType<Value>;
346
+ /** A number with a dot for decimals. It takes comparisons and ranges. */
347
+ export declare function number(options?: FieldTypeOptions<number>): FieldType<number>;
348
+ /** Any text. Values match without regard to case, and `*` works as a wildcard. */
349
+ export declare function string(options?: FieldTypeOptions<string>): FieldType<string>;
350
+ export declare const HIGHLIGHT_KIND: {
351
+ readonly key: 'key';
352
+ readonly value: 'value';
353
+ readonly dynamic: 'dynamic';
354
+ readonly text: 'text';
355
+ readonly keyword: 'keyword';
356
+ readonly negation: 'negation';
357
+ readonly comparison: 'comparison';
358
+ readonly range: 'range';
359
+ readonly separator: 'separator';
360
+ readonly paren: 'paren';
361
+ };
362
+ type HighlightKind = (typeof HIGHLIGHT_KIND)[keyof typeof HIGHLIGHT_KIND];
363
+ interface HighlightSpan extends Span {
364
+ kind: HighlightKind;
365
+ }
366
+ /** Spans to color, in order. Whitespace and nothing else is left out. */
367
+ export declare function highlight(text: string): Array<HighlightSpan>;
368
+ interface ParseResult {
369
+ tree: SyntaxNode;
370
+ diagnostics: Array<Diagnostic>;
371
+ }
372
+ /** Reads any text into a tree. Broken text gives diagnostics, never an exception. */
373
+ export declare function parse(text: string): ParseResult;
374
+ export type { BindResult, Bound, CompareOperator, CompareValue, CompletionContext, Condition, DateValue, Diagnostic, DiagnosticCode, DiagnosticSeverity, EmptyCondition, EqualValue, FieldCondition, FieldDefinition, FieldSuggestion, FieldType, FieldTypeOptions, Flat, FlatEmpty, FlatField, FlatRange, FlatSet, HighlightKind, HighlightSpan, InCondition, InvalidQuery, KeyCompletion, MatchCondition, ParseResult, Query, QueryGroup, QueryNot, QueryVisitor, RangeBound, RangeCondition, RangeValue, RawValue, ReservedFieldNames, ResolveContext, Schema, SchemaDefinition, Span, Suggestion, SyntaxGroup, SyntaxNode, SyntaxNot, SyntaxTerm, SyntaxText, TextCondition, TokenDefinition, TokenOffset, TokenValue, ValueCompletion };