@rohal12/spindle 0.59.22 → 0.59.23

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.
@@ -156,11 +156,47 @@ export interface MarkupDiagnostic {
156
156
  /** 1-based column (UTF-16 code units). */
157
157
  column: number;
158
158
  message: string;
159
+ /** The kind of error, which tools tell apart without reading `message`. */
160
+ code: MarkupDiagnosticCode;
161
+ /**
162
+ * Where the offending text is in the passage's content: from `start` up to
163
+ * `end` (UTF-16 offsets). `line` and `column` are those of `start`.
164
+ */
165
+ start: number;
166
+ end: number;
167
+ /** What the diagnostic names, by `code`: the macro, the passage, what to try. */
168
+ data?: MarkupDiagnosticData;
159
169
  /** The source file and line, when the passage says where it came from. */
160
170
  file?: string;
161
171
  fileLine?: number;
162
172
  }
163
173
 
174
+ /**
175
+ * The kinds of diagnostic, which stay the same where the wording of the
176
+ * message changes (they are covered by semver, with the data they carry).
177
+ * The kinds of malformed markup are those of {@link MarkupErrorCode}.
178
+ *
179
+ * | code | `data` |
180
+ * | --- | --- |
181
+ * | `unknown-macro` | `name`, `suggestions` (the closest known macro, if close) |
182
+ * | `unknown-passage` | `name`, `macro` (`link` for `[[…]]`), `suggestions` |
183
+ * | `unquoted-passage-name` | `name`, `macro` |
184
+ * | `argument-error` | `macro` |
185
+ * | `code-syntax` | `macro`, for a macro argument |
186
+ */
187
+ export type MarkupDiagnosticCode =
188
+ | MarkupErrorCode
189
+ | 'unknown-macro'
190
+ | 'argument-error'
191
+ | 'code-syntax'
192
+ | 'unknown-passage'
193
+ | 'unquoted-passage-name';
194
+
195
+ /** What a diagnostic names, by code (see {@link MarkupDiagnosticCode}). */
196
+ export type MarkupDiagnosticData = Readonly<
197
+ Record<string, string | readonly string[]>
198
+ >;
199
+
164
200
  /**
165
201
  * Validate the markup of a story's passages as Spindle does when the story
166
202
  * starts: malformed markup (unclosed or mismatched macros, tags, links,
@@ -308,42 +344,90 @@ interface TokenSpan {
308
344
  end: number;
309
345
  }
310
346
 
347
+ /**
348
+ * Where the selectors of a token are written: from `selectorsStart` up to
349
+ * `selectorsEnd`, without the space that may follow them. Both are absent for
350
+ * a token without selectors.
351
+ */
352
+ export interface SelectorSpan {
353
+ selectorsStart?: number;
354
+ selectorsEnd?: number;
355
+ }
356
+
311
357
  export interface TextToken extends TokenSpan {
312
358
  type: 'text';
313
359
  value: string;
360
+ /** A closed HTML comment: markdown drops it, and so does raw rendering. */
361
+ comment?: true;
314
362
  }
315
363
 
316
- export interface LinkToken extends TokenSpan, Selectors {
364
+ export interface LinkToken extends TokenSpan, Selectors, SelectorSpan {
317
365
  type: 'link';
318
366
  display: string;
367
+ /** Where the label is written, from `displayStart` to `displayEnd`. */
368
+ displayStart: number;
369
+ displayEnd: number;
319
370
  target: string;
320
371
  /** Where the target is written, from `targetStart` to `targetEnd`. */
321
372
  targetStart: number;
322
373
  targetEnd: number;
323
374
  }
324
375
 
325
- export interface MacroToken extends TokenSpan, Selectors {
376
+ export interface MacroToken extends TokenSpan, Selectors, SelectorSpan {
326
377
  type: 'macro';
327
378
  name: string;
379
+ /** Where the name is written (after `{`, `/` and the selectors). */
380
+ nameStart: number;
381
+ nameEnd: number;
328
382
  rawArgs: string;
383
+ /**
384
+ * Where `rawArgs` is written, without the whitespace around it; an empty
385
+ * span at `nameEnd` when there are none.
386
+ */
387
+ argsStart: number;
388
+ argsEnd: number;
329
389
  isClose: boolean;
330
390
  }
331
391
 
332
- export interface VariableToken extends TokenSpan, Selectors {
392
+ export interface VariableToken extends TokenSpan, Selectors, SelectorSpan {
333
393
  type: 'variable';
334
394
  name: string;
395
+ /** Where the name is written, without the sigil. */
396
+ nameStart: number;
397
+ nameEnd: number;
335
398
  scope: VariableScope;
336
399
  }
337
400
 
338
- export interface ExpressionToken extends TokenSpan, Selectors {
401
+ export interface ExpressionToken extends TokenSpan, Selectors, SelectorSpan {
339
402
  type: 'expression';
340
403
  expression: string;
404
+ /** Where `expression` is written, after the `{` and the selectors. */
405
+ expressionStart: number;
406
+ expressionEnd: number;
407
+ }
408
+
409
+ /** An attribute of an HTML tag as it is written, in order. */
410
+ export interface AttributeSpan {
411
+ name: string;
412
+ nameStart: number;
413
+ nameEnd: number;
414
+ /** Where the value is written, without its quotes; absent with no `=`. */
415
+ valueStart?: number;
416
+ valueEnd?: number;
417
+ /** The quote around the value, if it has one. */
418
+ quote?: '"' | "'";
341
419
  }
342
420
 
343
421
  export interface HtmlToken extends TokenSpan {
344
422
  type: 'html';
345
423
  tag: string;
424
+ /** Where the tag name is written. */
425
+ tagNameStart: number;
426
+ tagNameEnd: number;
427
+ /** The attribute values by name; the first of equal names (any case). */
346
428
  attributes: Record<string, string>;
429
+ /** Every attribute as written, in order, duplicates included. */
430
+ attributeSpans: AttributeSpan[];
347
431
  isClose: boolean;
348
432
  isSelfClose: boolean;
349
433
  }
@@ -357,12 +441,49 @@ export type Token =
357
441
  | ExpressionToken
358
442
  | HtmlToken;
359
443
 
360
- /** Malformed markup, with where it starts (0-based offset, 1-based line and column). */
444
+ /**
445
+ * The kinds of malformed markup. A tag that is malformed: `unclosed-link`
446
+ * (`[[` with no `]]`), `unclosed-expression` (`{$…` with no `}`),
447
+ * `unclosed-macro` (`{name…` with no `}`), `invalid-closer` (`{/` and no
448
+ * name), `closer-with-selectors`, `closer-with-arguments`, `unclosed-tag`
449
+ * (`<div` with no `>`), `unexpected-character` (in a tag) and
450
+ * `unclosed-attribute` (a value with no closing quote). Tags that pair with
451
+ * nothing: `unclosed-block` (`{if}` with no `{/if}`, `<div>` with no
452
+ * `</div>`), `mismatched-closer`, `stray-closer` and `misplaced-branch`
453
+ * (`{else}` outside `{if}`). `syntax` is any other.
454
+ */
455
+ export type MarkupErrorCode =
456
+ | PairingErrorCode
457
+ | 'syntax'
458
+ | 'unclosed-link'
459
+ | 'unclosed-expression'
460
+ | 'unclosed-macro'
461
+ | 'invalid-closer'
462
+ | 'closer-with-selectors'
463
+ | 'closer-with-arguments'
464
+ | 'unclosed-tag'
465
+ | 'unexpected-character'
466
+ | 'unclosed-attribute';
467
+
468
+ /**
469
+ * Malformed markup, with where it starts (0-based offset, 1-based line and
470
+ * column).
471
+ */
361
472
  export declare class MarkupError extends Error {
473
+ /** What is wrong, without the position. */
362
474
  reason: string;
363
475
  offset: number;
364
476
  line: number;
365
477
  column: number;
478
+ /** The kind of error, which stays the same where the wording changes. */
479
+ code: MarkupErrorCode;
480
+ /** Where the offending text ends (0-based, exclusive). */
481
+ end: number;
482
+ /**
483
+ * The names involved, by `code`: `name` for an unclosed, mismatched or
484
+ * stray tag (and `closer`, `parent`, `inside` where there are several).
485
+ */
486
+ data: Readonly<Record<string, string>>;
366
487
  }
367
488
 
368
489
  export interface ParseMarkupOptions {
@@ -501,9 +622,330 @@ export interface PassageReference {
501
622
  * the passage of `{goto}`, `{include}` and `{link}` (and of macros that
502
623
  * declare a `passage` argument), the `goto` and `dialog` actions of
503
624
  * `{watch}` (and the `string` and `text` arguments of macros that declare
504
- * they hold a passage name) and the body of `{dialog}`. Malformed tags are
505
- * skipped, so half-typed markup reads.
625
+ * they hold a passage name) and the body of `{dialog}`, also in the labels
626
+ * and attribute values that hold markup. Malformed tags are skipped, so
627
+ * half-typed markup reads. It uses the macros registered here; see
628
+ * {@link collectStoryPassageReferences}.
506
629
  */
507
630
  export declare function collectPassageReferences(
508
631
  source: string,
509
632
  ): PassageReference[];
633
+
634
+ // ---------------------------------------------------------------------------
635
+ // Stateless checks
636
+ //
637
+ // `validateMarkup` and `collectPassageReferences` read the process-global
638
+ // registry (`defineMacro`). The functions below take the macros to check
639
+ // against and keep no state: use them to analyse several projects in one
640
+ // process.
641
+ // ---------------------------------------------------------------------------
642
+
643
+ /** What tooling knows about a macro (a `MacroMetadata` has all of it). */
644
+ export interface ToolingMacro {
645
+ name: string;
646
+ block: boolean;
647
+ subMacros: string[];
648
+ /**
649
+ * Its declared parameters: their types tell which arguments are code, and
650
+ * what a `string` or `text` argument holds (see `ParameterDef.holds`).
651
+ */
652
+ parameters?: readonly ParameterDef[];
653
+ /** Whether it resolves markup: its strings hold markup by default. */
654
+ interpolate?: boolean;
655
+ }
656
+
657
+ /**
658
+ * The built-in macros, as data: what `getMacroRegistry()` returns before any
659
+ * `defineMacro`. It is loaded without `fs`, so a bundler inlines it.
660
+ */
661
+ export declare const builtinMacros: readonly MacroMetadata[];
662
+
663
+ /**
664
+ * `validateMarkup` against the given `macros` (the built-in ones are in
665
+ * `builtinMacros`; add the user-defined ones): the same checks, with no
666
+ * registry involved.
667
+ */
668
+ export declare function validateStoryMarkup(
669
+ passages: Iterable<MarkupPassage>,
670
+ macros: Iterable<ToolingMacro>,
671
+ options?: ValidateMarkupOptions,
672
+ ): MarkupDiagnostic[];
673
+
674
+ /**
675
+ * `collectPassageReferences` against the given `macros`, with no registry
676
+ * involved.
677
+ */
678
+ export declare function collectStoryPassageReferences(
679
+ source: string,
680
+ macros: Iterable<ToolingMacro>,
681
+ ): PassageReference[];
682
+
683
+ // ---------------------------------------------------------------------------
684
+ // Pieces of a passage
685
+ // ---------------------------------------------------------------------------
686
+
687
+ /**
688
+ * What every piece says of where it is: it is at `offset` in the markup
689
+ * given (UTF-16 code units). A piece in the markup of a text (a label, an
690
+ * attribute value) is `nested`, with `where` the description of that text.
691
+ */
692
+ export interface PieceBase {
693
+ offset: number;
694
+ nested?: true;
695
+ where?: string;
696
+ /**
697
+ * For the code or text of a quoted string with escapes (a `\"`), whose
698
+ * characters are not where `offset` plus their index says: the offset of
699
+ * each character in the source, and of the end of the text, so that
700
+ * `sourceOffsets[i]` is where character `i` of `code` or `text` is. Absent
701
+ * when `offset + i` is (see {@link pieceOffset}).
702
+ */
703
+ sourceOffsets?: readonly number[];
704
+ }
705
+
706
+ /** Where character `index` of the code or text of `piece` is in the source. */
707
+ export declare function pieceOffset(piece: PieceBase, index: number): number;
708
+
709
+ /**
710
+ * A piece of code: a `{$expr}` display, a `{do}` body, a condition, a macro
711
+ * argument of an `expression`, `statements` or `passage` parameter, a string
712
+ * that holds code, or an attribute that holds code (`onclick`).
713
+ */
714
+ export interface CodePiece extends PieceBase {
715
+ kind: 'code';
716
+ code: string;
717
+ goal: JsGoal;
718
+ /** The markup it is in, for an error: `{print $a +}`. */
719
+ label: string;
720
+ /**
721
+ * Whether it names a passage: a `passage` argument that is an expression
722
+ * (one that is a string literal is a PassagePiece).
723
+ */
724
+ passage?: boolean;
725
+ /** Whether it is the code in a quoted string, as in `{watch}`. */
726
+ inString?: boolean;
727
+ /** The macro it is the argument of, for a `passage` argument. */
728
+ macro?: string;
729
+ }
730
+
731
+ /** A passage name written out: in a link, or a quoted `passage` argument. */
732
+ export interface PassagePiece extends PieceBase {
733
+ kind: 'passage';
734
+ name: string;
735
+ /** The markup it is in, for an error: `[[Go->Hall]]`. */
736
+ label: string;
737
+ /** How much of the markup is the name, as written (quotes included). */
738
+ length: number;
739
+ /** The macro it is the argument of; `link` for `[[…]]` links. */
740
+ macro: string;
741
+ }
742
+
743
+ /** Macro arguments that don't have their parameters' forms. */
744
+ export interface ArgumentErrorPiece extends PieceBase {
745
+ kind: 'argument-error';
746
+ message: string;
747
+ /** How much of the markup the arguments are. */
748
+ length: number;
749
+ /** The markup it is in, for an error: `{link Go}`. */
750
+ label: string;
751
+ /** The macro whose arguments they are. */
752
+ macro: string;
753
+ }
754
+
755
+ /**
756
+ * A text that may hold markup of its own: a label, an attribute value. The
757
+ * pieces of that markup follow it, `nested`.
758
+ */
759
+ export interface TextPiece extends PieceBase {
760
+ kind: 'text';
761
+ text: string;
762
+ /** Where it is, for an error: `In the label of {button}: `. */
763
+ where: string;
764
+ /**
765
+ * The tokens of the markup in it, with offsets in the source given (none
766
+ * for a text with no `{`, which has no markup in it).
767
+ */
768
+ tokens: Token[];
769
+ /**
770
+ * Its malformed markup, in the order a parser reading from left to right
771
+ * meets it, with offsets in the source given.
772
+ */
773
+ errors: MarkupError[];
774
+ }
775
+
776
+ /** What a passage runs and names, and where (see {@link passagePieces}). */
777
+ export type Piece = CodePiece | TextPiece | PassagePiece | ArgumentErrorPiece;
778
+
779
+ /**
780
+ * What a passage's markup runs and names, and where: the pieces of code
781
+ * (`{$expr}` displays, `{do}` bodies, conditions, macro arguments by their
782
+ * declared parameter types, strings that hold code), the passage names
783
+ * written out (links, `passage` arguments, `{dialog}` bodies), the texts that
784
+ * hold markup of their own (labels, attribute values) and the arguments that
785
+ * don't have their parameters' forms, in source order with offsets (UTF-16)
786
+ * into `source`. The markup in a text follows it, as pieces marked `nested`
787
+ * with offsets into `source` too. Malformed tags are skipped, so it reads
788
+ * half-typed markup. `macros` are those the markup may use (see
789
+ * `builtinMacros`); without them only the code that needs no declaration is
790
+ * found.
791
+ */
792
+ export declare function passagePieces(
793
+ source: string,
794
+ macros?: Iterable<ToolingMacro>,
795
+ ): Piece[];
796
+
797
+ // ---------------------------------------------------------------------------
798
+ // Pairing
799
+ // ---------------------------------------------------------------------------
800
+
801
+ /** What is wrong with the pairing of tags (a subset of {@link MarkupErrorCode}). */
802
+ export type PairingErrorCode =
803
+ | 'unclosed-block'
804
+ | 'mismatched-closer'
805
+ | 'stray-closer'
806
+ | 'misplaced-branch';
807
+
808
+ /** A problem in the pairing of the tokens. */
809
+ export interface PairingError {
810
+ code: PairingErrorCode;
811
+ /** What is wrong, without the position. */
812
+ message: string;
813
+ /** The token it is at: from `start` up to `end` (offsets in the source). */
814
+ start: number;
815
+ end: number;
816
+ /**
817
+ * The offset a parser reading left to right notices it at: where the
818
+ * closer ends, or past the end of the markup for what is left open there.
819
+ */
820
+ noticedAt: number;
821
+ /** The names involved, by code (see {@link MarkupError.data}). */
822
+ data: Record<string, string>;
823
+ }
824
+
825
+ /** A branch (`{else}`, `{case}`, …) of a macro, with what follows it. */
826
+ export interface PairedBranch {
827
+ tag: MacroToken;
828
+ children: PairedNode[];
829
+ }
830
+
831
+ /** What a macro or element holds, up to its closer. */
832
+ export interface PairedBody {
833
+ children: PairedNode[];
834
+ branches: PairedBranch[];
835
+ /** The closing tag; absent if it is never closed. */
836
+ close?: MacroToken | HtmlToken;
837
+ }
838
+
839
+ /**
840
+ * A node of the tree: a token that stands alone (text, a link, a variable, an
841
+ * expression, a macro without a body, a self-closing element), or the opening
842
+ * tag of a macro or element with its `body`.
843
+ */
844
+ export interface PairedNode {
845
+ /** The token, or the opening tag of the element. */
846
+ token: Token;
847
+ body?: PairedBody;
848
+ /** From the start of the token to the end of the closer (or of the body). */
849
+ start: number;
850
+ end: number;
851
+ }
852
+
853
+ export interface PairMarkupOptions {
854
+ /** Whether a macro takes a body (default: {@link isBlockMacro}). */
855
+ isBlock?(name: string): boolean;
856
+ /**
857
+ * Whether a macro's body is JavaScript, kept verbatim as one text token
858
+ * (default: `{do}`). One that is never closed is a problem at its opening
859
+ * tag, and the markup after it is not its body.
860
+ */
861
+ isRaw?(name: string): boolean;
862
+ /**
863
+ * The markup the tokens are of: lets a message say at which line and column
864
+ * an element was opened (else it says at which offset).
865
+ */
866
+ source?: string;
867
+ }
868
+
869
+ export interface PairedMarkup {
870
+ nodes: PairedNode[];
871
+ errors: PairingError[];
872
+ }
873
+
874
+ /**
875
+ * Pair the flat `tokens` of markup: which closer closes which opener, which
876
+ * branches belong to which macro, as the runtime reads them (it builds its
877
+ * AST from this). It never throws: a problem is an entry of `errors`, in the
878
+ * order a parser reading from left to right notices them, and the tree is
879
+ * recovered around it. A closer closes the innermost open element, or, when
880
+ * it names one further out, that one, leaving what is open inside it
881
+ * unclosed.
882
+ */
883
+ export declare function pairMarkup(
884
+ tokens: readonly Token[],
885
+ options?: PairMarkupOptions,
886
+ ): PairedMarkup;
887
+
888
+ /**
889
+ * Whether a macro takes a body closed by `{/name}`: the built-in block
890
+ * macros. Pass the block macros of your story to `pairMarkup` instead when it
891
+ * defines others (see `ToolingMacro.block`, and `{widget}`).
892
+ */
893
+ export declare function isBlockMacro(name: string): boolean;
894
+
895
+ // ---------------------------------------------------------------------------
896
+ // Declarations
897
+ // ---------------------------------------------------------------------------
898
+
899
+ /** What is wrong with a declaration (see {@link DeclarationError}). */
900
+ export type DeclarationErrorCode =
901
+ | 'invalid-declaration'
902
+ | 'invalid-name'
903
+ | 'duplicate-declaration'
904
+ | 'unsupported-value';
905
+
906
+ /** A declaration of a `StoryVariables` or `StoryTransients` passage. */
907
+ export interface Declaration {
908
+ name: string;
909
+ /** Where the name is written, without the sigil (UTF-16 offsets). */
910
+ nameStart: number;
911
+ nameEnd: number;
912
+ /** Where the initializer expression is written, without surrounding space. */
913
+ valueStart: number;
914
+ valueEnd: number;
915
+ /**
916
+ * The shape of the initializer when it is static: a literal, or an array or
917
+ * object literal of them. Absent when it is not (a call, a reference, an
918
+ * operator…).
919
+ */
920
+ schema?: FieldSchema;
921
+ }
922
+
923
+ /** An invalid line of a `StoryVariables` or `StoryTransients` passage. */
924
+ export interface DeclarationError {
925
+ code: DeclarationErrorCode;
926
+ /** From `offset` up to `end` (UTF-16 offsets into the content). */
927
+ offset: number;
928
+ end: number;
929
+ /** What is wrong, as `parseStoryVariables` throws it after the passage name. */
930
+ message: string;
931
+ }
932
+
933
+ /**
934
+ * Read the declarations of a `StoryVariables` (`$name = value`) or
935
+ * `StoryTransients` (`%name = value`, pass `sigil: '%'`) passage, without
936
+ * evaluating them and without throwing: every valid declaration, and every
937
+ * invalid line as an error. It is the grammar `parseStoryVariables` reads
938
+ * with.
939
+ *
940
+ * What is static: `number`, `string` (no template with `${}`), `boolean` and
941
+ * `null` literals (also signed numbers), arrays of them, and objects with
942
+ * identifier, string or number keys and static values. The `schema` of an
943
+ * array is `{ type: 'array' }`, as `parseStoryVariables` makes it. Anything
944
+ * else (`Math.PI`, `new Date()`, `1 + 2`, a reference, a spread, a computed
945
+ * key) has no `schema`, and is no error: only a value that no variable can
946
+ * hold is, a function (`function`, `=>`, `class`) or `undefined`.
947
+ */
948
+ export declare function parseDeclarations(
949
+ content: string,
950
+ sigil?: '$' | '%',
951
+ ): { declarations: Declaration[]; errors: DeclarationError[] };