squiffy-compiler 6.0.1 → 7.0.0-beta.2

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,79 @@
1
+ import type { Diagnostic, PassageNode, SectionNode, SquiffyFile } from "./syntax.js";
2
+ import { SourceLine } from "./text.js";
3
+ export interface ExternalFiles {
4
+ getMatchingFilenames(pattern: string): Promise<string[]>;
5
+ getContent(filename: string): Promise<string>;
6
+ getLocalFilename(filename: string): string;
7
+ }
8
+ export interface AnalyseSettings {
9
+ scriptBaseFilename?: string;
10
+ script: string;
11
+ externalFiles?: ExternalFiles;
12
+ parseCache?: ParseCache;
13
+ recover?: boolean;
14
+ storyFiles?: string[];
15
+ }
16
+ export declare class ParseCache {
17
+ private files;
18
+ parse(text: string, filename?: string): SquiffyFile;
19
+ }
20
+ export interface SectionSymbol {
21
+ name: string;
22
+ file: string;
23
+ node?: SectionNode;
24
+ passages: PassageSymbol[];
25
+ }
26
+ export interface PassageSymbol {
27
+ name: string;
28
+ node: PassageNode;
29
+ }
30
+ export interface Analysis {
31
+ success: boolean;
32
+ files: SquiffyFile[];
33
+ diagnostics: Diagnostic[];
34
+ sections: SectionSymbol[];
35
+ start: string;
36
+ }
37
+ export declare class Story {
38
+ sections: Record<string, Section>;
39
+ title: string;
40
+ scripts: string[];
41
+ stylesheets: string[];
42
+ start: string;
43
+ id: string | null;
44
+ uiJs: string[];
45
+ constructor();
46
+ addSection(name: string, filename: string | undefined, line: number, node: SectionNode | undefined): Section;
47
+ }
48
+ declare class TextContainer {
49
+ text: string[];
50
+ textSources: SourceLine[];
51
+ js: string[];
52
+ clear: boolean;
53
+ attributes: string[];
54
+ output: string;
55
+ addText(text: string, source: SourceLine): void;
56
+ }
57
+ export declare class Section extends TextContainer {
58
+ name: string;
59
+ filename: string | undefined;
60
+ line: number;
61
+ node: SectionNode | undefined;
62
+ passages: Record<string, Passage>;
63
+ constructor(name: string, filename: string | undefined, line: number, node: SectionNode | undefined);
64
+ addPassage(name: string, line: number, node: PassageNode): Passage;
65
+ }
66
+ export declare class Passage extends TextContainer {
67
+ name: string;
68
+ line: number;
69
+ node: PassageNode;
70
+ constructor(name: string, line: number, node: PassageNode);
71
+ }
72
+ export interface AnalysisInternals {
73
+ story: Story;
74
+ legacyErrors: string[];
75
+ legacyWarnings: string[];
76
+ }
77
+ export declare const analysisInternals: WeakMap<Analysis, AnalysisInternals>;
78
+ export declare function analyse(settings: AnalyseSettings): Promise<Analysis>;
79
+ export {};
@@ -0,0 +1,590 @@
1
+ import { parse } from "./parse.js";
2
+ import { positionMapper, transformText } from "./text.js";
3
+ import { PROJECT_FILENAME } from "./project.js";
4
+ import { findV5Syntax } from "./v5.js";
5
+ // Caches the syntax tree of each file by filename, reparsing a file only when its text changes.
6
+ export class ParseCache {
7
+ constructor() {
8
+ Object.defineProperty(this, "files", {
9
+ enumerable: true,
10
+ configurable: true,
11
+ writable: true,
12
+ value: new Map()
13
+ });
14
+ }
15
+ parse(text, filename) {
16
+ const key = filename ?? "";
17
+ const cached = this.files.get(key);
18
+ if (cached && cached.text === text) {
19
+ return cached.file;
20
+ }
21
+ const file = parse(text, filename);
22
+ this.files.set(key, { text, file });
23
+ return file;
24
+ }
25
+ }
26
+ // ---- Story model, which emit() turns into output ----
27
+ export class Story {
28
+ constructor() {
29
+ Object.defineProperty(this, "sections", {
30
+ enumerable: true,
31
+ configurable: true,
32
+ writable: true,
33
+ value: {}
34
+ });
35
+ Object.defineProperty(this, "title", {
36
+ enumerable: true,
37
+ configurable: true,
38
+ writable: true,
39
+ value: ""
40
+ });
41
+ Object.defineProperty(this, "scripts", {
42
+ enumerable: true,
43
+ configurable: true,
44
+ writable: true,
45
+ value: []
46
+ });
47
+ Object.defineProperty(this, "stylesheets", {
48
+ enumerable: true,
49
+ configurable: true,
50
+ writable: true,
51
+ value: []
52
+ });
53
+ Object.defineProperty(this, "start", {
54
+ enumerable: true,
55
+ configurable: true,
56
+ writable: true,
57
+ value: ""
58
+ });
59
+ Object.defineProperty(this, "id", {
60
+ enumerable: true,
61
+ configurable: true,
62
+ writable: true,
63
+ value: null
64
+ });
65
+ Object.defineProperty(this, "uiJs", {
66
+ enumerable: true,
67
+ configurable: true,
68
+ writable: true,
69
+ value: []
70
+ });
71
+ this.id = crypto.randomUUID();
72
+ }
73
+ addSection(name, filename, line, node) {
74
+ const section = new Section(name, filename, line, node);
75
+ this.sections[name] = section;
76
+ return section;
77
+ }
78
+ }
79
+ class TextContainer {
80
+ constructor() {
81
+ Object.defineProperty(this, "text", {
82
+ enumerable: true,
83
+ configurable: true,
84
+ writable: true,
85
+ value: []
86
+ });
87
+ Object.defineProperty(this, "textSources", {
88
+ enumerable: true,
89
+ configurable: true,
90
+ writable: true,
91
+ value: []
92
+ });
93
+ Object.defineProperty(this, "js", {
94
+ enumerable: true,
95
+ configurable: true,
96
+ writable: true,
97
+ value: []
98
+ });
99
+ Object.defineProperty(this, "clear", {
100
+ enumerable: true,
101
+ configurable: true,
102
+ writable: true,
103
+ value: false
104
+ });
105
+ Object.defineProperty(this, "attributes", {
106
+ enumerable: true,
107
+ configurable: true,
108
+ writable: true,
109
+ value: []
110
+ });
111
+ // The text after links have been converted
112
+ Object.defineProperty(this, "output", {
113
+ enumerable: true,
114
+ configurable: true,
115
+ writable: true,
116
+ value: ""
117
+ });
118
+ }
119
+ addText(text, source) {
120
+ this.text.push(text);
121
+ this.textSources.push(source);
122
+ }
123
+ }
124
+ export class Section extends TextContainer {
125
+ constructor(name, filename, line, node) {
126
+ super();
127
+ Object.defineProperty(this, "name", {
128
+ enumerable: true,
129
+ configurable: true,
130
+ writable: true,
131
+ value: name
132
+ });
133
+ Object.defineProperty(this, "filename", {
134
+ enumerable: true,
135
+ configurable: true,
136
+ writable: true,
137
+ value: filename
138
+ });
139
+ Object.defineProperty(this, "line", {
140
+ enumerable: true,
141
+ configurable: true,
142
+ writable: true,
143
+ value: line
144
+ });
145
+ Object.defineProperty(this, "node", {
146
+ enumerable: true,
147
+ configurable: true,
148
+ writable: true,
149
+ value: node
150
+ });
151
+ Object.defineProperty(this, "passages", {
152
+ enumerable: true,
153
+ configurable: true,
154
+ writable: true,
155
+ value: {}
156
+ });
157
+ }
158
+ addPassage(name, line, node) {
159
+ const passage = new Passage(name, line, node);
160
+ this.passages[name] = passage;
161
+ return passage;
162
+ }
163
+ }
164
+ export class Passage extends TextContainer {
165
+ constructor(name, line, node) {
166
+ super();
167
+ Object.defineProperty(this, "name", {
168
+ enumerable: true,
169
+ configurable: true,
170
+ writable: true,
171
+ value: name
172
+ });
173
+ Object.defineProperty(this, "line", {
174
+ enumerable: true,
175
+ configurable: true,
176
+ writable: true,
177
+ value: line
178
+ });
179
+ Object.defineProperty(this, "node", {
180
+ enumerable: true,
181
+ configurable: true,
182
+ writable: true,
183
+ value: node
184
+ });
185
+ }
186
+ }
187
+ export const analysisInternals = new WeakMap();
188
+ const lineRange = (node) => node.range;
189
+ // Resolves @imports, builds the story's sections and passages, and reports diagnostics.
190
+ export async function analyse(settings) {
191
+ const story = new Story();
192
+ const cache = settings.parseCache ?? new ParseCache();
193
+ const files = [];
194
+ const diagnostics = [];
195
+ const legacyErrors = [];
196
+ const legacyWarnings = [];
197
+ let autoSectionCount = 0;
198
+ // The story starts at the main file's first section by default, even if an @import
199
+ // above it has already added sections from other files.
200
+ let mainFirstSection;
201
+ const report = (severity, code, filename, range, message, legacyLine) => {
202
+ diagnostics.push({ file: filename ?? "", range, severity, code, message });
203
+ if (severity == "hint")
204
+ return;
205
+ const legacy = `${severity.toUpperCase()}: ${filename} line ${legacyLine}: ${message}`;
206
+ (severity == "error" ? legacyErrors : legacyWarnings).push(legacy);
207
+ };
208
+ const storyFiles = settings.storyFiles && new Set(settings.storyFiles);
209
+ // The .squiffy files processed so far, so a story file the main file has already @imported
210
+ // isn't processed again
211
+ const processed = new Set();
212
+ async function processFile(text, filename, isFirst) {
213
+ const file = cache.parse(text, filename);
214
+ files.push(file);
215
+ processed.add(filename);
216
+ const lines = text.replace(/\r/g, "").split("\n");
217
+ warnAboutV5Syntax(text, lines, filename);
218
+ let section = null;
219
+ let passage = null;
220
+ let inUiBlock = false;
221
+ // Where an earlier section or passage was defined, for a duplicate's warning
222
+ const earlierLocation = (earlierFilename, line) => earlierFilename == filename || !earlierFilename ? `line ${line}` : `${earlierFilename} line ${line}`;
223
+ const addSection = (name, line, node) => {
224
+ if (isFirst && !mainFirstSection)
225
+ mainFirstSection = name;
226
+ const earlier = hasOwn(story.sections, name) ? story.sections[name] : undefined;
227
+ // The compiler's own names (_default, _continue1, ...) only clash with a section the
228
+ // author named the same way. The main file's preamble can add "_default" more than
229
+ // once, each replacing the last, but that's not a clash the author can see.
230
+ if (earlier && node && !(node.kind == "preamble" && earlier.node?.kind == "preamble")) {
231
+ // A --- in the preamble starts a section that has no node of its own
232
+ const range = node.nameRange ?? node.header?.range
233
+ ?? { start: { line: line - 1, character: 0 }, end: { line: line - 1, character: lines[line - 1]?.length ?? 0 } };
234
+ report("warning", "squiffy/duplicate-section", filename, range, `There's already a section called [[${name}]], at ${earlierLocation(earlier.filename, earlier.line)}. This one replaces it.`, range.start.line + 1);
235
+ }
236
+ return story.addSection(name, filename, line, node);
237
+ };
238
+ const ensureSectionExists = (line) => {
239
+ if (!section && isFirst) {
240
+ return addSection("_default", line, file.preamble);
241
+ }
242
+ return section;
243
+ };
244
+ const addAutoSection = (node, line) => {
245
+ autoSectionCount++;
246
+ const autoSectionName = `_continue${autoSectionCount}`;
247
+ section = addSection(autoSectionName, line, node);
248
+ passage = null;
249
+ return autoSectionName;
250
+ };
251
+ const sectionRequired = (node) => {
252
+ report("error", "squiffy/content-before-section", filename, lineRange(node), "This needs to be in a section, but it comes before the first section in this file.", node.range.start.line + 1);
253
+ return false;
254
+ };
255
+ const source = (node) => ({ line: node.range.start.line, length: lines[node.range.start.line].length });
256
+ const handleSectionHeader = (node) => {
257
+ const header = node.header;
258
+ const lineCount = header.range.start.line + 1;
259
+ switch (header.type) {
260
+ case "section-header":
261
+ section = addSection(header.name, lineCount, node);
262
+ passage = null;
263
+ return true;
264
+ case "continue": {
265
+ const previousSection = ensureSectionExists(lineCount);
266
+ if (!previousSection)
267
+ return sectionRequired(header);
268
+ const autoSectionName = addAutoSection(node, lineCount);
269
+ previousSection.addText(`[[${header.text || "Continue..."}]](${autoSectionName})`, source(header));
270
+ return true;
271
+ }
272
+ case "separator":
273
+ return handleSeparator(node, lineCount);
274
+ }
275
+ };
276
+ const handleSeparator = (node, lineCount) => {
277
+ inUiBlock = false;
278
+ if (!section) {
279
+ // Just add the _default section if we haven't started yet
280
+ ensureSectionExists(lineCount);
281
+ }
282
+ else {
283
+ addAutoSection(node ?? file.preamble, lineCount);
284
+ }
285
+ return true;
286
+ };
287
+ const handlePassageHeader = (node) => {
288
+ const lineCount = node.header.range.start.line + 1;
289
+ if (!section) {
290
+ report("error", "squiffy/passage-without-section", filename, node.nameRange, `Can't add passage "${node.name}" as no section has been created.`, lineCount);
291
+ return false;
292
+ }
293
+ if (hasOwn(section.passages, node.name)) {
294
+ const earlier = section.passages[node.name];
295
+ report("warning", "squiffy/duplicate-passage", filename, node.nameRange, `There's already a passage called [${node.name}] in section '${section.name}', at ${earlierLocation(section.filename, earlier.line)}. This one replaces it.`, lineCount);
296
+ }
297
+ passage = section.addPassage(node.name, lineCount, node);
298
+ return true;
299
+ };
300
+ const handleStatement = async (node) => {
301
+ const lineCount = node.range.start.line + 1;
302
+ switch (node.type) {
303
+ case "separator":
304
+ return handleSeparator(null, lineCount);
305
+ case "ui-block-start":
306
+ inUiBlock = true;
307
+ return true;
308
+ case "ui-file":
309
+ if (settings.externalFiles) {
310
+ story.uiJs.push(await settings.externalFiles.getContent(node.path));
311
+ }
312
+ else {
313
+ warnExternalFilesUnavailable(`@ui ${node.path}`, node, filename);
314
+ }
315
+ return true;
316
+ case "clear":
317
+ if (!passage) {
318
+ section = ensureSectionExists(lineCount);
319
+ if (!section)
320
+ return sectionRequired(node);
321
+ section.clear = true;
322
+ }
323
+ else {
324
+ passage.clear = true;
325
+ }
326
+ return true;
327
+ case "end":
328
+ // Only for the language service's dead end check
329
+ if (!passage) {
330
+ section = ensureSectionExists(lineCount);
331
+ if (!section)
332
+ return sectionRequired(node);
333
+ }
334
+ return true;
335
+ case "title":
336
+ case "start":
337
+ // In folder mode, a file that isn't the main file could otherwise quietly
338
+ // change the story's title or where it starts
339
+ if (storyFiles && !isFirst) {
340
+ report("warning", "squiffy/not-in-main-file", filename, lineRange(node), `@${node.type} only works in the story's main file, so this was ignored.`, lineCount);
341
+ }
342
+ else if (node.type == "title") {
343
+ story.title = node.value;
344
+ }
345
+ else {
346
+ story.start = node.value;
347
+ }
348
+ return true;
349
+ case "import": {
350
+ if (!settings.externalFiles) {
351
+ warnExternalFilesUnavailable(`@import ${node.pattern}`, node, filename);
352
+ return true;
353
+ }
354
+ const matches = await settings.externalFiles.getMatchingFilenames(node.pattern);
355
+ const squiffyMatches = matches.filter(m => m.endsWith(".squiffy"));
356
+ if (storyFiles && squiffyMatches.length && squiffyMatches.length == matches.length
357
+ && squiffyMatches.every(m => storyFiles.has(m))) {
358
+ report("hint", "squiffy/redundant-import", filename, lineRange(node), `${squiffyMatches.length == 1 ? "This file is" : "These files are"} already part of the story, as ${PROJECT_FILENAME} includes every .squiffy file in the folder, so this @import isn't needed.`, lineCount);
359
+ }
360
+ for (const importFilename of matches) {
361
+ if (importFilename.endsWith(".squiffy")) {
362
+ const content = await settings.externalFiles.getContent(importFilename);
363
+ const success = await processFile(content, importFilename, false);
364
+ if (!success && !settings.recover)
365
+ return false;
366
+ }
367
+ else if (importFilename.endsWith(".js")) {
368
+ story.scripts.push(settings.externalFiles.getLocalFilename(importFilename));
369
+ }
370
+ else if (importFilename.endsWith(".css")) {
371
+ story.stylesheets.push(settings.externalFiles.getLocalFilename(importFilename));
372
+ }
373
+ }
374
+ return true;
375
+ }
376
+ case "attribute": {
377
+ section = ensureSectionExists(lineCount);
378
+ let attribute;
379
+ switch (node.directive) {
380
+ case "set":
381
+ attribute = node.value;
382
+ break;
383
+ case "unset":
384
+ attribute = "not " + node.value;
385
+ break;
386
+ case "inc":
387
+ attribute = node.value + "+=" + (node.amount ?? "1");
388
+ break;
389
+ case "dec":
390
+ attribute = node.value + "-=" + (node.amount ?? "1");
391
+ break;
392
+ }
393
+ if (passage) {
394
+ passage.attributes.push(attribute);
395
+ }
396
+ else {
397
+ if (!section)
398
+ return sectionRequired(node);
399
+ section.attributes.push(attribute);
400
+ }
401
+ return true;
402
+ }
403
+ case "js":
404
+ if (inUiBlock) {
405
+ story.uiJs.push(node.code);
406
+ }
407
+ else if (!passage) {
408
+ section = ensureSectionExists(lineCount);
409
+ if (!section)
410
+ return sectionRequired(node);
411
+ section.js.push(node.code);
412
+ }
413
+ else {
414
+ passage.js.push(node.code);
415
+ }
416
+ return true;
417
+ case "text":
418
+ if (inUiBlock) {
419
+ report("error", "squiffy/text-in-ui-block", filename, lineRange(node), "Unexpected text in @ui block.", lineCount);
420
+ return false;
421
+ }
422
+ else if (!passage) {
423
+ section = ensureSectionExists(lineCount);
424
+ // In an imported file, text before the first section is ignored
425
+ section?.addText(node.text, source(node));
426
+ }
427
+ else {
428
+ passage.addText(node.text, source(node));
429
+ }
430
+ return true;
431
+ }
432
+ };
433
+ for (const node of [file.preamble, ...file.sections]) {
434
+ if (node.type == "snippet") {
435
+ section = null;
436
+ passage = null;
437
+ continue;
438
+ }
439
+ if (node.header && !handleSectionHeader(node) && !settings.recover)
440
+ return false;
441
+ for (const statement of node.body) {
442
+ if (!await handleStatement(statement) && !settings.recover)
443
+ return false;
444
+ }
445
+ for (const passageNode of node.passages) {
446
+ if (!handlePassageHeader(passageNode)) {
447
+ if (!settings.recover)
448
+ return false;
449
+ // Skip the passage, since there's nowhere to put it
450
+ continue;
451
+ }
452
+ for (const statement of passageNode.body) {
453
+ if (!await handleStatement(statement) && !settings.recover)
454
+ return false;
455
+ }
456
+ }
457
+ }
458
+ return true;
459
+ }
460
+ function warnExternalFilesUnavailable(line, node, filename) {
461
+ report("warning", "squiffy/external-files-unavailable", filename, lineRange(node), `"${line}" was ignored, as other files can't be loaded here`, node.range.start.line + 1);
462
+ }
463
+ function warnAboutV5Syntax(text, lines, filename) {
464
+ const truncate = (s) => s.length > 60 ? s.substring(0, 57) + "..." : s;
465
+ for (const match of findV5Syntax(text)) {
466
+ let message = `${truncate(match.text)} looks like Squiffy 5 text processor syntax, which isn't supported in Squiffy 6.`;
467
+ if (match.suggestion) {
468
+ message += match.suggestion.length > 60
469
+ ? " Convert it to Handlebars syntax."
470
+ : ` Use ${match.suggestion} instead.`;
471
+ }
472
+ else if (match.text.startsWith("{else:")) {
473
+ message += " Use {{else}} inside an {{#if}} block instead.";
474
+ }
475
+ const line = match.line - 1;
476
+ const lineText = lines[line] ?? "";
477
+ const found = lineText.indexOf(match.text.split("\n")[0]);
478
+ const start = Math.max(found, 0);
479
+ const end = found >= 0 ? Math.min(found + match.text.length, lineText.length) : lineText.length;
480
+ report("warning", "squiffy/v5-syntax", filename, { start: { line, character: start }, end: { line, character: end } }, message, match.line);
481
+ }
482
+ }
483
+ // Set once the sections are all known, before checking links
484
+ let sectionNames = [];
485
+ let sectionIndexes = new Map();
486
+ function checkText(container, section, passage) {
487
+ const getNextSectionName = () => {
488
+ return sectionNames[(sectionIndexes.get(section.name) ?? -1) + 1] || null;
489
+ };
490
+ const result = transformText(container.text.join("\n"), getNextSectionName);
491
+ container.output = result.output;
492
+ const toPosition = positionMapper(container.textSources, container.text.map(t => t.length));
493
+ const toRange = (start, end) => ({ start: toPosition(start), end: toPosition(end) });
494
+ const where = passage
495
+ ? `In section '${section.name}', passage '${passage.name}'`
496
+ : `In section '${section.name}'`;
497
+ const legacyLine = passage ? passage.line : section.line;
498
+ const warn = (code, start, end, message, line = legacyLine) => {
499
+ report("warning", code, section.filename, toRange(start, end), message, line);
500
+ };
501
+ for (const missing of result.noNextSection) {
502
+ warn("squiffy/no-next-section", missing.start, missing.end, `In section '${section.name}', there is a ${missing.display} link but no following section exists`, section.line);
503
+ }
504
+ for (const link of result.links) {
505
+ if (link.form == "next")
506
+ continue;
507
+ if (link.linkType == "section") {
508
+ if (!linkDestinationExists(link.target, story.sections)) {
509
+ warn("squiffy/unknown-section-link", link.start, link.end, `${where} there is a link to a section called [[${link.target}]], which doesn't exist`);
510
+ }
511
+ }
512
+ else if (!linkDestinationExists(link.target, section.passages)) {
513
+ warn("squiffy/unknown-passage-link", link.start, link.end, `${where} there is a link to a passage called [${link.target}], which doesn't exist`);
514
+ }
515
+ }
516
+ // Like the runtime's embed helper, a name can refer to a passage in the
517
+ // current section or to any section.
518
+ for (const embed of result.embeds) {
519
+ if (!(embed.name in section.passages) && !(embed.name in story.sections)) {
520
+ warn("squiffy/unknown-embed", embed.start, embed.end, `${where} there is an embed of {{embed "${embed.name}"}}, but no section or passage with that name exists`);
521
+ }
522
+ }
523
+ }
524
+ // With recover, processFile carries on after errors, so check for them too
525
+ let success = await processFile(settings.script, settings.scriptBaseFilename, true);
526
+ // In folder mode, the rest of the story's files, in order
527
+ for (const storyFile of settings.storyFiles ?? []) {
528
+ if (!success && !settings.recover)
529
+ break;
530
+ if (processed.has(storyFile))
531
+ continue;
532
+ if (!settings.externalFiles) {
533
+ report("warning", "squiffy/external-files-unavailable", settings.scriptBaseFilename, { start: { line: 0, character: 0 }, end: { line: 0, character: 0 } }, "The story's other files were ignored, as other files can't be loaded here", 1);
534
+ break;
535
+ }
536
+ const content = await settings.externalFiles.getContent(storyFile);
537
+ success = await processFile(content, storyFile, false) && success;
538
+ }
539
+ success && (success = !legacyErrors.length);
540
+ if (success || settings.recover) {
541
+ if (!Object.keys(story.sections).length) {
542
+ story.addSection("_default", settings.scriptBaseFilename, 0, undefined);
543
+ }
544
+ if (!story.start) {
545
+ story.start = mainFirstSection ?? Object.keys(story.sections)[0];
546
+ }
547
+ sectionNames = Object.keys(story.sections);
548
+ sectionIndexes = new Map(sectionNames.map((name, i) => [name, i]));
549
+ for (const section of Object.values(story.sections)) {
550
+ checkText(section, section, null);
551
+ for (const passage of Object.values(section.passages)) {
552
+ checkText(passage, section, passage);
553
+ }
554
+ }
555
+ }
556
+ const analysis = {
557
+ success,
558
+ files,
559
+ diagnostics,
560
+ sections: Object.values(story.sections).map(section => ({
561
+ name: section.name,
562
+ file: section.filename ?? "",
563
+ ...(section.node ? { node: section.node } : {}),
564
+ passages: Object.values(section.passages).map(passage => ({ name: passage.name, node: passage.node })),
565
+ })),
566
+ start: story.start,
567
+ };
568
+ analysisInternals.set(analysis, { story, legacyErrors, legacyWarnings });
569
+ return analysis;
570
+ }
571
+ function linkDestinationExists(link, keys) {
572
+ // Link destination data may look like:
573
+ // passageName
574
+ // passageName, my_attribute=2
575
+ // passage name with a comma, in the name
576
+ // We're only interested in checking if the named passage or section exists.
577
+ // First check if the full link is a valid destination (handles names containing commas).
578
+ const trimmedLink = link.trim();
579
+ if (hasOwn(keys, trimmedLink)) {
580
+ return true;
581
+ }
582
+ // Otherwise, assume a comma separates the destination from setters.
583
+ const linkDestination = trimmedLink.split(",")[0].trim();
584
+ return hasOwn(keys, linkDestination);
585
+ }
586
+ // The same as Object.keys(keys).includes(key) for the plain objects used here, without building
587
+ // an array for every link.
588
+ function hasOwn(keys, key) {
589
+ return Object.prototype.hasOwnProperty.call(keys, key);
590
+ }