squiffy-compiler 6.0.1 → 7.0.0-beta.11
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.
- package/README.md +10 -2
- package/dist/analyse.d.ts +96 -0
- package/dist/analyse.js +951 -0
- package/dist/compiler.d.ts +32 -40
- package/dist/compiler.js +26 -691
- package/dist/css-urls.d.ts +4 -0
- package/dist/css-urls.js +59 -0
- package/dist/emit.d.ts +71 -0
- package/dist/emit.js +192 -0
- package/dist/helpers.d.ts +1 -0
- package/dist/helpers.js +14 -0
- package/dist/ignore.d.ts +8 -0
- package/dist/ignore.js +71 -0
- package/dist/infix-expressions.d.ts +19 -0
- package/dist/infix-expressions.js +713 -0
- package/dist/language-service.d.ts +90 -0
- package/dist/language-service.js +883 -0
- package/dist/node.d.ts +23 -0
- package/dist/node.js +122 -0
- package/dist/parse.d.ts +22 -0
- package/dist/parse.js +316 -0
- package/dist/project.d.ts +129 -0
- package/dist/project.js +488 -0
- package/dist/quest/aslx.d.ts +51 -0
- package/dist/quest/aslx.js +184 -0
- package/dist/quest/convert.d.ts +20 -0
- package/dist/quest/convert.js +552 -0
- package/dist/quest/script.d.ts +92 -0
- package/dist/quest/script.js +453 -0
- package/dist/quest/scripts.d.ts +24 -0
- package/dist/quest/scripts.js +506 -0
- package/dist/quest/text.d.ts +18 -0
- package/dist/quest/text.js +323 -0
- package/dist/section-labels.d.ts +4 -0
- package/dist/section-labels.js +64 -0
- package/dist/setters.d.ts +49 -0
- package/dist/setters.js +477 -0
- package/dist/story-graph.d.ts +71 -0
- package/dist/story-graph.js +445 -0
- package/dist/syntax.d.ts +198 -0
- package/dist/syntax.js +8 -0
- package/dist/templates.d.ts +17 -0
- package/dist/templates.js +180 -0
- package/dist/text.d.ts +44 -0
- package/dist/text.js +348 -0
- package/dist/twine/audio.d.ts +21 -0
- package/dist/twine/audio.js +80 -0
- package/dist/twine/convert.d.ts +131 -0
- package/dist/twine/convert.js +1057 -0
- package/dist/twine/expression.d.ts +88 -0
- package/dist/twine/expression.js +318 -0
- package/dist/twine/harlowe.d.ts +27 -0
- package/dist/twine/harlowe.js +2341 -0
- package/dist/twine/settings.d.ts +9 -0
- package/dist/twine/settings.js +322 -0
- package/dist/twine/story.d.ts +24 -0
- package/dist/twine/story.js +231 -0
- package/dist/twine/sugarcube.d.ts +40 -0
- package/dist/twine/sugarcube.js +2172 -0
- package/dist/v5.js +2 -1
- package/package.json +35 -6
- package/syntaxes/squiffy.tmLanguage.json +1003 -0
package/dist/analyse.js
ADDED
|
@@ -0,0 +1,951 @@
|
|
|
1
|
+
import { parse } from "./parse.js";
|
|
2
|
+
import { positionMapper, transformText } from "./text.js";
|
|
3
|
+
import { isScriptPath, isSquiffyPath, isStylesheetPath, PROJECT_FILENAME } from "./project.js";
|
|
4
|
+
import { findV5Syntax } from "./v5.js";
|
|
5
|
+
import { findTemplateError, findTemplateNames } from "./templates.js";
|
|
6
|
+
import { BUILT_IN_HELPERS } from "./helpers.js";
|
|
7
|
+
import { ignoredDiagnostics } from "./ignore.js";
|
|
8
|
+
import { sectionLabels } from "./section-labels.js";
|
|
9
|
+
import { attributesSetIn, BUILT_IN_ATTRIBUTES, findAtAttributes, findBuiltInAttributeSets, findUnquotedText } from "./setters.js";
|
|
10
|
+
// Caches the syntax tree of each file by filename, reparsing a file only when its text changes.
|
|
11
|
+
export class ParseCache {
|
|
12
|
+
constructor() {
|
|
13
|
+
Object.defineProperty(this, "files", {
|
|
14
|
+
enumerable: true,
|
|
15
|
+
configurable: true,
|
|
16
|
+
writable: true,
|
|
17
|
+
value: new Map()
|
|
18
|
+
});
|
|
19
|
+
}
|
|
20
|
+
parse(text, filename) {
|
|
21
|
+
const key = filename ?? "";
|
|
22
|
+
const cached = this.files.get(key);
|
|
23
|
+
if (cached && cached.text === text) {
|
|
24
|
+
return cached.file;
|
|
25
|
+
}
|
|
26
|
+
const file = parse(text, filename);
|
|
27
|
+
this.files.set(key, { text, file });
|
|
28
|
+
return file;
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
// ---- Story model, which emit() turns into output ----
|
|
32
|
+
export class Story {
|
|
33
|
+
constructor() {
|
|
34
|
+
Object.defineProperty(this, "sections", {
|
|
35
|
+
enumerable: true,
|
|
36
|
+
configurable: true,
|
|
37
|
+
writable: true,
|
|
38
|
+
value: {}
|
|
39
|
+
});
|
|
40
|
+
Object.defineProperty(this, "title", {
|
|
41
|
+
enumerable: true,
|
|
42
|
+
configurable: true,
|
|
43
|
+
writable: true,
|
|
44
|
+
value: ""
|
|
45
|
+
});
|
|
46
|
+
Object.defineProperty(this, "scripts", {
|
|
47
|
+
enumerable: true,
|
|
48
|
+
configurable: true,
|
|
49
|
+
writable: true,
|
|
50
|
+
value: []
|
|
51
|
+
});
|
|
52
|
+
Object.defineProperty(this, "stylesheets", {
|
|
53
|
+
enumerable: true,
|
|
54
|
+
configurable: true,
|
|
55
|
+
writable: true,
|
|
56
|
+
value: []
|
|
57
|
+
});
|
|
58
|
+
Object.defineProperty(this, "start", {
|
|
59
|
+
enumerable: true,
|
|
60
|
+
configurable: true,
|
|
61
|
+
writable: true,
|
|
62
|
+
value: ""
|
|
63
|
+
});
|
|
64
|
+
Object.defineProperty(this, "id", {
|
|
65
|
+
enumerable: true,
|
|
66
|
+
configurable: true,
|
|
67
|
+
writable: true,
|
|
68
|
+
value: null
|
|
69
|
+
});
|
|
70
|
+
Object.defineProperty(this, "uiJs", {
|
|
71
|
+
enumerable: true,
|
|
72
|
+
configurable: true,
|
|
73
|
+
writable: true,
|
|
74
|
+
value: []
|
|
75
|
+
});
|
|
76
|
+
this.id = crypto.randomUUID();
|
|
77
|
+
}
|
|
78
|
+
addSection(name, filename, line, node) {
|
|
79
|
+
const section = new Section(name, filename, line, node);
|
|
80
|
+
this.sections[name] = section;
|
|
81
|
+
return section;
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
class TextContainer {
|
|
85
|
+
constructor() {
|
|
86
|
+
Object.defineProperty(this, "text", {
|
|
87
|
+
enumerable: true,
|
|
88
|
+
configurable: true,
|
|
89
|
+
writable: true,
|
|
90
|
+
value: []
|
|
91
|
+
});
|
|
92
|
+
Object.defineProperty(this, "textSources", {
|
|
93
|
+
enumerable: true,
|
|
94
|
+
configurable: true,
|
|
95
|
+
writable: true,
|
|
96
|
+
value: []
|
|
97
|
+
});
|
|
98
|
+
Object.defineProperty(this, "js", {
|
|
99
|
+
enumerable: true,
|
|
100
|
+
configurable: true,
|
|
101
|
+
writable: true,
|
|
102
|
+
value: []
|
|
103
|
+
});
|
|
104
|
+
Object.defineProperty(this, "clear", {
|
|
105
|
+
enumerable: true,
|
|
106
|
+
configurable: true,
|
|
107
|
+
writable: true,
|
|
108
|
+
value: false
|
|
109
|
+
});
|
|
110
|
+
Object.defineProperty(this, "attributes", {
|
|
111
|
+
enumerable: true,
|
|
112
|
+
configurable: true,
|
|
113
|
+
writable: true,
|
|
114
|
+
value: []
|
|
115
|
+
});
|
|
116
|
+
// @music (#444): the file to play, or null for @music stop. Undefined leaves it as it is.
|
|
117
|
+
Object.defineProperty(this, "music", {
|
|
118
|
+
enumerable: true,
|
|
119
|
+
configurable: true,
|
|
120
|
+
writable: true,
|
|
121
|
+
value: void 0
|
|
122
|
+
});
|
|
123
|
+
// @sound: the files to play once, in order, with "stop" for @sound stop
|
|
124
|
+
Object.defineProperty(this, "sounds", {
|
|
125
|
+
enumerable: true,
|
|
126
|
+
configurable: true,
|
|
127
|
+
writable: true,
|
|
128
|
+
value: []
|
|
129
|
+
});
|
|
130
|
+
// @class (#449): classes on the story's element while the section is the current one
|
|
131
|
+
Object.defineProperty(this, "classes", {
|
|
132
|
+
enumerable: true,
|
|
133
|
+
configurable: true,
|
|
134
|
+
writable: true,
|
|
135
|
+
value: []
|
|
136
|
+
});
|
|
137
|
+
// @tag (#527): tags the story can list sections and passages by
|
|
138
|
+
Object.defineProperty(this, "tags", {
|
|
139
|
+
enumerable: true,
|
|
140
|
+
configurable: true,
|
|
141
|
+
writable: true,
|
|
142
|
+
value: []
|
|
143
|
+
});
|
|
144
|
+
// @transition (#489): how it appears, and how long that takes in milliseconds
|
|
145
|
+
Object.defineProperty(this, "transition", {
|
|
146
|
+
enumerable: true,
|
|
147
|
+
configurable: true,
|
|
148
|
+
writable: true,
|
|
149
|
+
value: void 0
|
|
150
|
+
});
|
|
151
|
+
Object.defineProperty(this, "transitionTime", {
|
|
152
|
+
enumerable: true,
|
|
153
|
+
configurable: true,
|
|
154
|
+
writable: true,
|
|
155
|
+
value: void 0
|
|
156
|
+
});
|
|
157
|
+
// @leave: how the old text goes when this clears the screen, and how long that takes
|
|
158
|
+
Object.defineProperty(this, "leave", {
|
|
159
|
+
enumerable: true,
|
|
160
|
+
configurable: true,
|
|
161
|
+
writable: true,
|
|
162
|
+
value: void 0
|
|
163
|
+
});
|
|
164
|
+
Object.defineProperty(this, "leaveTime", {
|
|
165
|
+
enumerable: true,
|
|
166
|
+
configurable: true,
|
|
167
|
+
writable: true,
|
|
168
|
+
value: void 0
|
|
169
|
+
});
|
|
170
|
+
// Where @leave is, to say if there's nothing for it to do
|
|
171
|
+
Object.defineProperty(this, "leaveNode", {
|
|
172
|
+
enumerable: true,
|
|
173
|
+
configurable: true,
|
|
174
|
+
writable: true,
|
|
175
|
+
value: void 0
|
|
176
|
+
});
|
|
177
|
+
// The text after links have been converted
|
|
178
|
+
Object.defineProperty(this, "output", {
|
|
179
|
+
enumerable: true,
|
|
180
|
+
configurable: true,
|
|
181
|
+
writable: true,
|
|
182
|
+
value: ""
|
|
183
|
+
});
|
|
184
|
+
}
|
|
185
|
+
addText(text, source) {
|
|
186
|
+
this.text.push(text);
|
|
187
|
+
this.textSources.push(source);
|
|
188
|
+
}
|
|
189
|
+
}
|
|
190
|
+
export class Section extends TextContainer {
|
|
191
|
+
constructor(name, filename, line, node) {
|
|
192
|
+
super();
|
|
193
|
+
Object.defineProperty(this, "name", {
|
|
194
|
+
enumerable: true,
|
|
195
|
+
configurable: true,
|
|
196
|
+
writable: true,
|
|
197
|
+
value: name
|
|
198
|
+
});
|
|
199
|
+
Object.defineProperty(this, "filename", {
|
|
200
|
+
enumerable: true,
|
|
201
|
+
configurable: true,
|
|
202
|
+
writable: true,
|
|
203
|
+
value: filename
|
|
204
|
+
});
|
|
205
|
+
Object.defineProperty(this, "line", {
|
|
206
|
+
enumerable: true,
|
|
207
|
+
configurable: true,
|
|
208
|
+
writable: true,
|
|
209
|
+
value: line
|
|
210
|
+
});
|
|
211
|
+
Object.defineProperty(this, "node", {
|
|
212
|
+
enumerable: true,
|
|
213
|
+
configurable: true,
|
|
214
|
+
writable: true,
|
|
215
|
+
value: node
|
|
216
|
+
});
|
|
217
|
+
Object.defineProperty(this, "passages", {
|
|
218
|
+
enumerable: true,
|
|
219
|
+
configurable: true,
|
|
220
|
+
writable: true,
|
|
221
|
+
value: {}
|
|
222
|
+
});
|
|
223
|
+
}
|
|
224
|
+
addPassage(name, line, node) {
|
|
225
|
+
const passage = new Passage(name, line, node);
|
|
226
|
+
this.passages[name] = passage;
|
|
227
|
+
return passage;
|
|
228
|
+
}
|
|
229
|
+
}
|
|
230
|
+
export class Passage extends TextContainer {
|
|
231
|
+
constructor(name, line, node) {
|
|
232
|
+
super();
|
|
233
|
+
Object.defineProperty(this, "name", {
|
|
234
|
+
enumerable: true,
|
|
235
|
+
configurable: true,
|
|
236
|
+
writable: true,
|
|
237
|
+
value: name
|
|
238
|
+
});
|
|
239
|
+
Object.defineProperty(this, "line", {
|
|
240
|
+
enumerable: true,
|
|
241
|
+
configurable: true,
|
|
242
|
+
writable: true,
|
|
243
|
+
value: line
|
|
244
|
+
});
|
|
245
|
+
Object.defineProperty(this, "node", {
|
|
246
|
+
enumerable: true,
|
|
247
|
+
configurable: true,
|
|
248
|
+
writable: true,
|
|
249
|
+
value: node
|
|
250
|
+
});
|
|
251
|
+
// @repeat: its links stay clickable (#104)
|
|
252
|
+
Object.defineProperty(this, "repeat", {
|
|
253
|
+
enumerable: true,
|
|
254
|
+
configurable: true,
|
|
255
|
+
writable: true,
|
|
256
|
+
value: false
|
|
257
|
+
});
|
|
258
|
+
}
|
|
259
|
+
}
|
|
260
|
+
export const analysisInternals = new WeakMap();
|
|
261
|
+
const lineRange = (node) => node.range;
|
|
262
|
+
// A @transition time (#489), such as "1s", "0.5s" or "400ms", in milliseconds
|
|
263
|
+
function parseTransitionTime(time) {
|
|
264
|
+
const match = /^(\d+(?:\.\d+)?|\.\d+)(s|ms)$/.exec(time);
|
|
265
|
+
if (!match)
|
|
266
|
+
return undefined;
|
|
267
|
+
return Math.round(parseFloat(match[1]) * (match[2] == "s" ? 1000 : 1));
|
|
268
|
+
}
|
|
269
|
+
// Resolves @imports, builds the story's sections and passages, and reports diagnostics.
|
|
270
|
+
export async function analyse(settings) {
|
|
271
|
+
const story = new Story();
|
|
272
|
+
const cache = settings.parseCache ?? new ParseCache();
|
|
273
|
+
const files = [];
|
|
274
|
+
// Each file's text, in the same order as files
|
|
275
|
+
const texts = [];
|
|
276
|
+
const diagnostics = [];
|
|
277
|
+
const legacyErrors = [];
|
|
278
|
+
const legacyWarnings = [];
|
|
279
|
+
let autoSectionCount = 0;
|
|
280
|
+
// The story starts at the main file's first section by default, even if an @import
|
|
281
|
+
// above it has already added sections from other files.
|
|
282
|
+
let mainFirstSection;
|
|
283
|
+
// Each warning's line in legacyWarnings, so that a warning a squiffy-ignore comment turns off can be removed from both
|
|
284
|
+
const legacyWarningOf = new Map();
|
|
285
|
+
const report = (severity, code, filename, range, message, legacyLine, subject) => {
|
|
286
|
+
const diagnostic = { file: filename ?? "", range, severity, code, message, ...(subject === undefined ? {} : { subject }) };
|
|
287
|
+
diagnostics.push(diagnostic);
|
|
288
|
+
if (severity == "hint")
|
|
289
|
+
return;
|
|
290
|
+
const legacy = `${severity.toUpperCase()}: ${filename} line ${legacyLine}: ${message}`;
|
|
291
|
+
(severity == "error" ? legacyErrors : legacyWarnings).push(legacy);
|
|
292
|
+
if (severity == "warning")
|
|
293
|
+
legacyWarningOf.set(diagnostic, legacy);
|
|
294
|
+
};
|
|
295
|
+
const storyFiles = settings.storyFiles && new Set(settings.storyFiles);
|
|
296
|
+
// The .squiffy files processed so far, so a story file the main file has already @imported
|
|
297
|
+
// isn't processed again
|
|
298
|
+
const processed = new Set();
|
|
299
|
+
// Files read by @ui, which in folder mode aren't added to the page as scripts too
|
|
300
|
+
const uiFiles = new Set();
|
|
301
|
+
// The .css and .js files added to the page so far, named as getMatchingFilenames names them
|
|
302
|
+
const stylesheetFiles = [];
|
|
303
|
+
const scriptFiles = [];
|
|
304
|
+
// Adds a stylesheet or script to the page. An @import adds it every time, as in Squiffy 6, but
|
|
305
|
+
// folder mode only adds one that isn't there already.
|
|
306
|
+
const addPageFile = (filename, externalFiles, imported) => {
|
|
307
|
+
const [names, local] = isStylesheetPath(filename)
|
|
308
|
+
? [stylesheetFiles, story.stylesheets]
|
|
309
|
+
: [scriptFiles, story.scripts];
|
|
310
|
+
if (names.includes(filename) && !imported)
|
|
311
|
+
return;
|
|
312
|
+
if (!names.includes(filename))
|
|
313
|
+
names.push(filename);
|
|
314
|
+
local.push(externalFiles.getLocalFilename(filename));
|
|
315
|
+
};
|
|
316
|
+
async function processFile(text, filename, isFirst) {
|
|
317
|
+
const file = cache.parse(text, filename);
|
|
318
|
+
files.push(file);
|
|
319
|
+
texts.push(text);
|
|
320
|
+
processed.add(filename);
|
|
321
|
+
const lines = text.replace(/\r/g, "").split("\n");
|
|
322
|
+
warnAboutV5Syntax(text, lines, filename);
|
|
323
|
+
if (settings.expressions) {
|
|
324
|
+
for (const found of findAtAttributes(file)) {
|
|
325
|
+
const name = found.text.substring(1);
|
|
326
|
+
report("warning", "squiffy/at-attribute", filename, found.range, `Use ${name} rather than ${found.text}: in a story with a ${PROJECT_FILENAME}, setters name attributes without "@".`, found.range.start.line + 1);
|
|
327
|
+
}
|
|
328
|
+
}
|
|
329
|
+
let section = null;
|
|
330
|
+
let passage = null;
|
|
331
|
+
let inUiBlock = false;
|
|
332
|
+
// Where an earlier section or passage was defined, for a duplicate's warning
|
|
333
|
+
const earlierLocation = (earlierFilename, line) => earlierFilename == filename || !earlierFilename ? `line ${line}` : `${earlierFilename} line ${line}`;
|
|
334
|
+
const addSection = (name, line, node) => {
|
|
335
|
+
if (isFirst && !mainFirstSection)
|
|
336
|
+
mainFirstSection = name;
|
|
337
|
+
const earlier = hasOwn(story.sections, name) ? story.sections[name] : undefined;
|
|
338
|
+
// The compiler's own names (_default, _continue1, ...) only clash with a section the
|
|
339
|
+
// author named the same way. The main file's preamble can add "_default" more than
|
|
340
|
+
// once, each replacing the last, but that's not a clash the author can see.
|
|
341
|
+
if (earlier && node && !(node.kind == "preamble" && earlier.node?.kind == "preamble")) {
|
|
342
|
+
// A --- in the preamble starts a section that has no node of its own
|
|
343
|
+
const range = node.nameRange ?? node.header?.range
|
|
344
|
+
?? { start: { line: line - 1, character: 0 }, end: { line: line - 1, character: lines[line - 1]?.length ?? 0 } };
|
|
345
|
+
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);
|
|
346
|
+
}
|
|
347
|
+
return story.addSection(name, filename, line, node);
|
|
348
|
+
};
|
|
349
|
+
const ensureSectionExists = (line) => {
|
|
350
|
+
if (!section && isFirst) {
|
|
351
|
+
return addSection("_default", line, file.preamble);
|
|
352
|
+
}
|
|
353
|
+
return section;
|
|
354
|
+
};
|
|
355
|
+
const addAutoSection = (node, line) => {
|
|
356
|
+
autoSectionCount++;
|
|
357
|
+
const autoSectionName = `_continue${autoSectionCount}`;
|
|
358
|
+
section = addSection(autoSectionName, line, node);
|
|
359
|
+
passage = null;
|
|
360
|
+
return autoSectionName;
|
|
361
|
+
};
|
|
362
|
+
const sectionRequired = (node) => {
|
|
363
|
+
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);
|
|
364
|
+
return false;
|
|
365
|
+
};
|
|
366
|
+
const source = (node) => ({ line: node.range.start.line, length: lines[node.range.start.line].length });
|
|
367
|
+
const handleSectionHeader = (node) => {
|
|
368
|
+
const header = node.header;
|
|
369
|
+
const lineCount = header.range.start.line + 1;
|
|
370
|
+
switch (header.type) {
|
|
371
|
+
case "section-header":
|
|
372
|
+
section = addSection(header.name, lineCount, node);
|
|
373
|
+
passage = null;
|
|
374
|
+
return true;
|
|
375
|
+
case "continue": {
|
|
376
|
+
const previousSection = ensureSectionExists(lineCount);
|
|
377
|
+
if (!previousSection)
|
|
378
|
+
return sectionRequired(header);
|
|
379
|
+
const autoSectionName = addAutoSection(node, lineCount);
|
|
380
|
+
previousSection.addText(`[[${header.text || "Continue..."}]](${autoSectionName})`, source(header));
|
|
381
|
+
return true;
|
|
382
|
+
}
|
|
383
|
+
case "separator":
|
|
384
|
+
return handleSeparator(node, lineCount);
|
|
385
|
+
}
|
|
386
|
+
};
|
|
387
|
+
const handleSeparator = (node, lineCount) => {
|
|
388
|
+
inUiBlock = false;
|
|
389
|
+
if (!section) {
|
|
390
|
+
// Just add the _default section if we haven't started yet
|
|
391
|
+
ensureSectionExists(lineCount);
|
|
392
|
+
}
|
|
393
|
+
else {
|
|
394
|
+
addAutoSection(node ?? file.preamble, lineCount);
|
|
395
|
+
}
|
|
396
|
+
return true;
|
|
397
|
+
};
|
|
398
|
+
const handlePassageHeader = (node) => {
|
|
399
|
+
const lineCount = node.header.range.start.line + 1;
|
|
400
|
+
if (!section) {
|
|
401
|
+
report("error", "squiffy/passage-without-section", filename, node.nameRange, `Can't add passage "${node.name}" as no section has been created.`, lineCount);
|
|
402
|
+
return false;
|
|
403
|
+
}
|
|
404
|
+
if (hasOwn(section.passages, node.name)) {
|
|
405
|
+
const earlier = section.passages[node.name];
|
|
406
|
+
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);
|
|
407
|
+
}
|
|
408
|
+
passage = section.addPassage(node.name, lineCount, node);
|
|
409
|
+
return true;
|
|
410
|
+
};
|
|
411
|
+
const handleStatement = async (node) => {
|
|
412
|
+
const lineCount = node.range.start.line + 1;
|
|
413
|
+
switch (node.type) {
|
|
414
|
+
case "separator":
|
|
415
|
+
return handleSeparator(null, lineCount);
|
|
416
|
+
case "ui-block-start":
|
|
417
|
+
inUiBlock = true;
|
|
418
|
+
return true;
|
|
419
|
+
case "ui-file":
|
|
420
|
+
if (settings.externalFiles) {
|
|
421
|
+
// In folder mode, the file isn't also added to the page as a script, so it
|
|
422
|
+
// doesn't run twice. Its name is looked up before it's read, as reading it
|
|
423
|
+
// can leave it out of getMatchingFilenames.
|
|
424
|
+
if (storyFiles) {
|
|
425
|
+
for (const match of await settings.externalFiles.getMatchingFilenames(node.path))
|
|
426
|
+
uiFiles.add(match);
|
|
427
|
+
}
|
|
428
|
+
story.uiJs.push(await settings.externalFiles.getContent(node.path));
|
|
429
|
+
}
|
|
430
|
+
else {
|
|
431
|
+
warnExternalFilesUnavailable(`@ui ${node.path}`, node, filename);
|
|
432
|
+
}
|
|
433
|
+
return true;
|
|
434
|
+
case "clear":
|
|
435
|
+
if (!passage) {
|
|
436
|
+
section = ensureSectionExists(lineCount);
|
|
437
|
+
if (!section)
|
|
438
|
+
return sectionRequired(node);
|
|
439
|
+
section.clear = true;
|
|
440
|
+
}
|
|
441
|
+
else {
|
|
442
|
+
passage.clear = true;
|
|
443
|
+
}
|
|
444
|
+
return true;
|
|
445
|
+
case "repeat":
|
|
446
|
+
if (!passage) {
|
|
447
|
+
report("warning", "squiffy/repeat-not-in-passage", filename, lineRange(node), "@repeat only works in a passage, so this was ignored.", lineCount);
|
|
448
|
+
}
|
|
449
|
+
else {
|
|
450
|
+
passage.repeat = true;
|
|
451
|
+
}
|
|
452
|
+
return true;
|
|
453
|
+
case "end":
|
|
454
|
+
// Only for the language service's dead end check
|
|
455
|
+
if (!passage) {
|
|
456
|
+
section = ensureSectionExists(lineCount);
|
|
457
|
+
if (!section)
|
|
458
|
+
return sectionRequired(node);
|
|
459
|
+
}
|
|
460
|
+
return true;
|
|
461
|
+
case "title":
|
|
462
|
+
case "start":
|
|
463
|
+
// In folder mode, a file that isn't the main file could otherwise quietly
|
|
464
|
+
// change the story's title or where it starts
|
|
465
|
+
if (storyFiles && !isFirst) {
|
|
466
|
+
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);
|
|
467
|
+
}
|
|
468
|
+
else if (node.type == "title") {
|
|
469
|
+
story.title = node.value;
|
|
470
|
+
}
|
|
471
|
+
else {
|
|
472
|
+
story.start = node.value;
|
|
473
|
+
}
|
|
474
|
+
return true;
|
|
475
|
+
case "import": {
|
|
476
|
+
if (!settings.externalFiles) {
|
|
477
|
+
warnExternalFilesUnavailable(`@import ${node.pattern}`, node, filename);
|
|
478
|
+
return true;
|
|
479
|
+
}
|
|
480
|
+
const matches = await settings.externalFiles.getMatchingFilenames(node.pattern);
|
|
481
|
+
if (storyFiles && matches.length && matches.every(m => storyFiles.has(m))) {
|
|
482
|
+
report("hint", "squiffy/redundant-import", filename, lineRange(node), `${matches.length == 1 ? "This file is" : "These files are"} already part of the story, as ${PROJECT_FILENAME} includes every .squiffy, .css and .js file in the folder, so this @import isn't needed.`, lineCount);
|
|
483
|
+
}
|
|
484
|
+
for (const importFilename of matches) {
|
|
485
|
+
if (isSquiffyPath(importFilename)) {
|
|
486
|
+
const content = await settings.externalFiles.getContent(importFilename);
|
|
487
|
+
const success = await processFile(content, importFilename, false);
|
|
488
|
+
if (!success && !settings.recover)
|
|
489
|
+
return false;
|
|
490
|
+
}
|
|
491
|
+
else if (isStylesheetPath(importFilename) || isScriptPath(importFilename)) {
|
|
492
|
+
addPageFile(importFilename, settings.externalFiles, true);
|
|
493
|
+
}
|
|
494
|
+
}
|
|
495
|
+
return true;
|
|
496
|
+
}
|
|
497
|
+
case "sound": {
|
|
498
|
+
const target = passage ?? (section = ensureSectionExists(lineCount));
|
|
499
|
+
if (!target)
|
|
500
|
+
return sectionRequired(node);
|
|
501
|
+
const value = node.value.trim();
|
|
502
|
+
if (node.directive == "sound")
|
|
503
|
+
target.sounds.push(value);
|
|
504
|
+
else
|
|
505
|
+
target.music = value == "stop" ? null : value;
|
|
506
|
+
return true;
|
|
507
|
+
}
|
|
508
|
+
case "class": {
|
|
509
|
+
const target = passage ?? (section = ensureSectionExists(lineCount));
|
|
510
|
+
if (!target)
|
|
511
|
+
return sectionRequired(node);
|
|
512
|
+
for (const name of node.value.split(/\s+/)) {
|
|
513
|
+
if (name && !target.classes.includes(name))
|
|
514
|
+
target.classes.push(name);
|
|
515
|
+
}
|
|
516
|
+
return true;
|
|
517
|
+
}
|
|
518
|
+
case "tag": {
|
|
519
|
+
const target = passage ?? (section = ensureSectionExists(lineCount));
|
|
520
|
+
if (!target)
|
|
521
|
+
return sectionRequired(node);
|
|
522
|
+
for (const name of node.value.split(/\s+/)) {
|
|
523
|
+
if (name && !target.tags.includes(name))
|
|
524
|
+
target.tags.push(name);
|
|
525
|
+
}
|
|
526
|
+
return true;
|
|
527
|
+
}
|
|
528
|
+
case "transition": {
|
|
529
|
+
const target = passage ?? (section = ensureSectionExists(lineCount));
|
|
530
|
+
if (!target)
|
|
531
|
+
return sectionRequired(node);
|
|
532
|
+
if (!/^[A-Za-z][\w-]*$/.test(node.value)) {
|
|
533
|
+
report("warning", "squiffy/transition", filename, node.valueRange, `"${node.value}" can't be the name of a transition, so this was ignored. A name is letters, numbers, - and _, starting with a letter.`, lineCount);
|
|
534
|
+
return true;
|
|
535
|
+
}
|
|
536
|
+
let time;
|
|
537
|
+
if (node.time !== undefined) {
|
|
538
|
+
time = parseTransitionTime(node.time);
|
|
539
|
+
if (time === undefined) {
|
|
540
|
+
report("warning", "squiffy/transition", filename, lineRange(node), `"${node.time}" isn't a time, such as 1s or 400ms, so the transition takes its usual time.`, lineCount);
|
|
541
|
+
}
|
|
542
|
+
}
|
|
543
|
+
if (node.directive == "leave") {
|
|
544
|
+
target.leave = node.value;
|
|
545
|
+
target.leaveTime = time;
|
|
546
|
+
target.leaveNode = { filename, range: lineRange(node) };
|
|
547
|
+
}
|
|
548
|
+
else {
|
|
549
|
+
target.transition = node.value;
|
|
550
|
+
target.transitionTime = time;
|
|
551
|
+
}
|
|
552
|
+
return true;
|
|
553
|
+
}
|
|
554
|
+
case "attribute": {
|
|
555
|
+
section = ensureSectionExists(lineCount);
|
|
556
|
+
let attribute;
|
|
557
|
+
switch (node.directive) {
|
|
558
|
+
case "set":
|
|
559
|
+
attribute = node.value;
|
|
560
|
+
break;
|
|
561
|
+
case "unset":
|
|
562
|
+
attribute = "not " + node.value;
|
|
563
|
+
break;
|
|
564
|
+
case "inc":
|
|
565
|
+
attribute = node.value + "+=" + (node.amount ?? "1");
|
|
566
|
+
break;
|
|
567
|
+
case "dec":
|
|
568
|
+
attribute = node.value + "-=" + (node.amount ?? "1");
|
|
569
|
+
break;
|
|
570
|
+
}
|
|
571
|
+
if (passage) {
|
|
572
|
+
passage.attributes.push(attribute);
|
|
573
|
+
}
|
|
574
|
+
else {
|
|
575
|
+
if (!section)
|
|
576
|
+
return sectionRequired(node);
|
|
577
|
+
section.attributes.push(attribute);
|
|
578
|
+
}
|
|
579
|
+
return true;
|
|
580
|
+
}
|
|
581
|
+
case "js":
|
|
582
|
+
if (inUiBlock) {
|
|
583
|
+
story.uiJs.push(node.code);
|
|
584
|
+
}
|
|
585
|
+
else if (!passage) {
|
|
586
|
+
section = ensureSectionExists(lineCount);
|
|
587
|
+
if (!section)
|
|
588
|
+
return sectionRequired(node);
|
|
589
|
+
section.js.push(node.code);
|
|
590
|
+
}
|
|
591
|
+
else {
|
|
592
|
+
passage.js.push(node.code);
|
|
593
|
+
}
|
|
594
|
+
return true;
|
|
595
|
+
case "text":
|
|
596
|
+
if (inUiBlock) {
|
|
597
|
+
report("error", "squiffy/text-in-ui-block", filename, lineRange(node), "Unexpected text in @ui block.", lineCount);
|
|
598
|
+
return false;
|
|
599
|
+
}
|
|
600
|
+
else if (!passage) {
|
|
601
|
+
section = ensureSectionExists(lineCount);
|
|
602
|
+
// In an imported file, text before the first section is ignored
|
|
603
|
+
section?.addText(node.text, source(node));
|
|
604
|
+
}
|
|
605
|
+
else {
|
|
606
|
+
passage.addText(node.text, source(node));
|
|
607
|
+
}
|
|
608
|
+
return true;
|
|
609
|
+
}
|
|
610
|
+
};
|
|
611
|
+
for (const node of [file.preamble, ...file.sections]) {
|
|
612
|
+
if (node.type == "snippet") {
|
|
613
|
+
section = null;
|
|
614
|
+
passage = null;
|
|
615
|
+
continue;
|
|
616
|
+
}
|
|
617
|
+
if (node.header && !handleSectionHeader(node) && !settings.recover)
|
|
618
|
+
return false;
|
|
619
|
+
for (const statement of node.body) {
|
|
620
|
+
if (!await handleStatement(statement) && !settings.recover)
|
|
621
|
+
return false;
|
|
622
|
+
}
|
|
623
|
+
for (const passageNode of node.passages) {
|
|
624
|
+
if (!handlePassageHeader(passageNode)) {
|
|
625
|
+
if (!settings.recover)
|
|
626
|
+
return false;
|
|
627
|
+
// Skip the passage, since there's nowhere to put it
|
|
628
|
+
continue;
|
|
629
|
+
}
|
|
630
|
+
for (const statement of passageNode.body) {
|
|
631
|
+
if (!await handleStatement(statement) && !settings.recover)
|
|
632
|
+
return false;
|
|
633
|
+
}
|
|
634
|
+
}
|
|
635
|
+
}
|
|
636
|
+
return true;
|
|
637
|
+
}
|
|
638
|
+
function warnExternalFilesUnavailable(line, node, filename) {
|
|
639
|
+
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);
|
|
640
|
+
}
|
|
641
|
+
// Text in a setter without quotes, as in Squiffy 6 (#373). A single word is only reported if no
|
|
642
|
+
// attribute of that name is set anywhere in the story, so this needs every file first.
|
|
643
|
+
function warnAboutUnquotedText() {
|
|
644
|
+
const set = new Set();
|
|
645
|
+
files.forEach((file, i) => attributesSetIn(file, texts[i]).forEach(name => set.add(name)));
|
|
646
|
+
for (const file of files) {
|
|
647
|
+
for (const found of findUnquotedText(file, name => set.has(name))) {
|
|
648
|
+
const quoted = found.quoted ?? `"${found.text}"`;
|
|
649
|
+
const message = found.kind == "word"
|
|
650
|
+
? `${found.text} isn't set anywhere in the story, so this sets ${found.attribute} to nothing. For the text ${quoted}, use quotes.`
|
|
651
|
+
: `Text in a setter goes in quotes in a story with a ${PROJECT_FILENAME}: ${quoted}.`;
|
|
652
|
+
report("warning", "squiffy/unquoted-text", file.filename, found.range, message, found.range.start.line + 1);
|
|
653
|
+
}
|
|
654
|
+
}
|
|
655
|
+
}
|
|
656
|
+
// {{name}} on its own shows the attribute called name if there's no helper called that, and
|
|
657
|
+
// an attribute that hasn't been set shows nothing, so a mistyped or missing helper, or a
|
|
658
|
+
// mistyped attribute, would otherwise fail without a word (#526). An attribute counts as set
|
|
659
|
+
// if the story might set it anywhere: see attributesSetIn, which takes anything in quotes,
|
|
660
|
+
// and the story's JavaScript is read the same way, which covers squiffy.set("name", ...) and
|
|
661
|
+
// registerHelper("name", ...).
|
|
662
|
+
async function warnAboutUnknownNames() {
|
|
663
|
+
const known = new Set([...BUILT_IN_HELPERS, ...BUILT_IN_ATTRIBUTES.keys(), ...embedParameters]);
|
|
664
|
+
const addKnown = (name) => {
|
|
665
|
+
known.add(name);
|
|
666
|
+
known.add(name.split(".")[0]);
|
|
667
|
+
};
|
|
668
|
+
// Squiffy's own attributes, such as _section, start with "_"
|
|
669
|
+
const isUnknown = (bare) => !known.has(bare.root) && !bare.root.startsWith("_");
|
|
670
|
+
if (!bareNames.some(isUnknown))
|
|
671
|
+
return;
|
|
672
|
+
files.forEach((file, i) => attributesSetIn(file, texts[i]).forEach(addKnown));
|
|
673
|
+
if (!bareNames.some(isUnknown))
|
|
674
|
+
return;
|
|
675
|
+
const scripts = [...story.uiJs];
|
|
676
|
+
if (settings.externalFiles) {
|
|
677
|
+
for (const filename of scriptFiles) {
|
|
678
|
+
try {
|
|
679
|
+
scripts.push(await settings.externalFiles.getContent(filename));
|
|
680
|
+
}
|
|
681
|
+
catch {
|
|
682
|
+
// A script that can't be read can't be checked
|
|
683
|
+
}
|
|
684
|
+
}
|
|
685
|
+
}
|
|
686
|
+
for (const script of scripts) {
|
|
687
|
+
for (const match of script.matchAll(/"([^"\\\n]*)"|'([^'\\\n]*)'|`([^`\\]*)`/g)) {
|
|
688
|
+
addKnown((match[1] ?? match[2] ?? match[3]).trim());
|
|
689
|
+
}
|
|
690
|
+
}
|
|
691
|
+
const suggestions = [...known].filter(name => /^[A-Za-z_]\w*$/.test(name) && !name.startsWith("_"));
|
|
692
|
+
for (const bare of bareNames) {
|
|
693
|
+
if (!isUnknown(bare))
|
|
694
|
+
continue;
|
|
695
|
+
const close = closestName(bare.root, suggestions);
|
|
696
|
+
const suggestion = close ? ` Did you mean {{${bare.name.replace(bare.root, close)}}}?` : "";
|
|
697
|
+
report("warning", "squiffy/unknown-name", bare.filename, bare.range, `${bare.where}, {{${bare.name}}} isn't a helper, and nothing in the story sets an attribute called ${bare.root}, so it shows nothing.${suggestion}`, bare.range.start.line + 1, bare.root);
|
|
698
|
+
}
|
|
699
|
+
}
|
|
700
|
+
function warnAboutV5Syntax(text, lines, filename) {
|
|
701
|
+
const truncate = (s) => s.length > 60 ? s.substring(0, 57) + "..." : s;
|
|
702
|
+
for (const match of findV5Syntax(text)) {
|
|
703
|
+
let message = `${truncate(match.text)} looks like Squiffy 5 text processor syntax, which isn't supported in Squiffy 6.`;
|
|
704
|
+
if (match.suggestion) {
|
|
705
|
+
message += match.suggestion.length > 60
|
|
706
|
+
? " Convert it to Handlebars syntax."
|
|
707
|
+
: ` Use ${match.suggestion} instead.`;
|
|
708
|
+
}
|
|
709
|
+
else if (match.text.startsWith("{else:")) {
|
|
710
|
+
message += " Use {{else}} inside an {{#if}} block instead.";
|
|
711
|
+
}
|
|
712
|
+
const line = match.line - 1;
|
|
713
|
+
const lineText = lines[line] ?? "";
|
|
714
|
+
const found = lineText.indexOf(match.text.split("\n")[0]);
|
|
715
|
+
const start = Math.max(found, 0);
|
|
716
|
+
const end = found >= 0 ? Math.min(found + match.text.length, lineText.length) : lineText.length;
|
|
717
|
+
report("warning", "squiffy/v5-syntax", filename, { start: { line, character: start }, end: { line, character: end } }, message, match.line);
|
|
718
|
+
}
|
|
719
|
+
}
|
|
720
|
+
// Set once the sections are all known, before checking links
|
|
721
|
+
let sectionNames = [];
|
|
722
|
+
let sectionIndexes = new Map();
|
|
723
|
+
// What warnings call sections without names of their own, e.g. 'the "+++Continue..." after "cave"'
|
|
724
|
+
let labels = new Map();
|
|
725
|
+
// {{name}} on its own in the story's text, and the names of the values passed to embeds, for
|
|
726
|
+
// warnAboutUnknownNames, which needs every section's text first
|
|
727
|
+
const bareNames = [];
|
|
728
|
+
const embedParameters = new Set();
|
|
729
|
+
function checkText(container, section, passage) {
|
|
730
|
+
const getNextSectionName = () => {
|
|
731
|
+
return sectionNames[(sectionIndexes.get(section.name) ?? -1) + 1] || null;
|
|
732
|
+
};
|
|
733
|
+
const result = transformText(container.text.join("\n"), getNextSectionName, settings.expressions);
|
|
734
|
+
container.output = result.output;
|
|
735
|
+
const toPosition = positionMapper(container.textSources, container.text.map(t => t.length));
|
|
736
|
+
const toRange = (start, end) => ({ start: toPosition(start), end: toPosition(end) });
|
|
737
|
+
const label = labels.get(section.name);
|
|
738
|
+
const inSection = label && section.node?.kind != "named" ? `In ${label}` : `In section '${section.name}'`;
|
|
739
|
+
const where = passage ? `${inSection}, passage '${passage.name}'` : inSection;
|
|
740
|
+
const legacyLine = passage ? passage.line : section.line;
|
|
741
|
+
const warn = (code, start, end, message, line = legacyLine) => {
|
|
742
|
+
report("warning", code, section.filename, toRange(start, end), message, line);
|
|
743
|
+
};
|
|
744
|
+
// Handlebars syntax errors, which would stop the text being shown at all
|
|
745
|
+
const templateError = findTemplateError(result.output);
|
|
746
|
+
if (templateError) {
|
|
747
|
+
const range = toRange(result.mapStart(templateError.start), result.mapEnd(templateError.end));
|
|
748
|
+
report("error", "squiffy/template-syntax", section.filename, range, `${where}, ${templateError.message}`, range.start.line + 1);
|
|
749
|
+
}
|
|
750
|
+
else {
|
|
751
|
+
const names = findTemplateNames(result.output);
|
|
752
|
+
names.embedParameters.forEach(name => embedParameters.add(name));
|
|
753
|
+
for (const bare of names.bare) {
|
|
754
|
+
const range = toRange(result.mapStart(bare.start), result.mapEnd(bare.end));
|
|
755
|
+
bareNames.push({ ...bare, filename: section.filename, range, where });
|
|
756
|
+
}
|
|
757
|
+
}
|
|
758
|
+
for (const missing of result.noNextSection) {
|
|
759
|
+
warn("squiffy/no-next-section", missing.start, missing.end, `${inSection}, there is a ${missing.display} link but no following section exists`, section.line);
|
|
760
|
+
}
|
|
761
|
+
// Like the runtime, a passage link can go to one of the master section's passages
|
|
762
|
+
const masterPassages = story.sections[""]?.passages ?? {};
|
|
763
|
+
for (const link of result.links) {
|
|
764
|
+
if (link.form == "next")
|
|
765
|
+
continue;
|
|
766
|
+
if (link.form == "input") {
|
|
767
|
+
const exists = link.linkType == "section"
|
|
768
|
+
? hasOwn(story.sections, link.target)
|
|
769
|
+
: hasOwn(section.passages, link.target) || hasOwn(masterPassages, link.target);
|
|
770
|
+
if (!exists) {
|
|
771
|
+
warn(link.linkType == "section" ? "squiffy/unknown-section-link" : "squiffy/unknown-passage-link", link.start, link.end, `${where} there is a text box that goes to a ${link.linkType} called "${link.target}" when Enter is pressed, which doesn't exist`);
|
|
772
|
+
}
|
|
773
|
+
}
|
|
774
|
+
else if (link.linkType == "section") {
|
|
775
|
+
if (!linkDestinationExists(link.target, story.sections)) {
|
|
776
|
+
warn("squiffy/unknown-section-link", link.start, link.end, `${where} there is a link to a section called [[${link.target}]], which doesn't exist`);
|
|
777
|
+
}
|
|
778
|
+
}
|
|
779
|
+
else if (!linkDestinationExists(link.target, section.passages) && !linkDestinationExists(link.target, masterPassages)) {
|
|
780
|
+
warn("squiffy/unknown-passage-link", link.start, link.end, `${where} there is a link to a passage called [${link.target}], which doesn't exist`);
|
|
781
|
+
}
|
|
782
|
+
}
|
|
783
|
+
// Like the runtime's embed helper, a name can refer to a passage in the
|
|
784
|
+
// current section or the master section, or to any section.
|
|
785
|
+
for (const embed of result.embeds) {
|
|
786
|
+
if (!(embed.name in section.passages) && !(embed.name in story.sections) && !hasOwn(masterPassages, embed.name)) {
|
|
787
|
+
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`);
|
|
788
|
+
}
|
|
789
|
+
}
|
|
790
|
+
// {{goto}} only goes to sections
|
|
791
|
+
for (const goto of result.gotos) {
|
|
792
|
+
if (!goto.name || !(goto.name in story.sections)) {
|
|
793
|
+
const passage = goto.name in section.passages ? ` ({{goto}} can't go to a passage: use {{embed "${goto.name}"}} to show it here)` : "";
|
|
794
|
+
warn("squiffy/unknown-goto", goto.start, goto.end, `${where} there is a {{goto "${goto.name}"}}, but no section with that name exists${passage}`);
|
|
795
|
+
}
|
|
796
|
+
}
|
|
797
|
+
// {{deactivate}} disables links to a passage in this section, or in the master section
|
|
798
|
+
for (const deactivate of result.deactivates) {
|
|
799
|
+
if (!(deactivate.name in section.passages) && !(deactivate.name in masterPassages)) {
|
|
800
|
+
warn("squiffy/unknown-deactivate", deactivate.start, deactivate.end, `${where} there is a {{deactivate "${deactivate.name}"}}, but no passage with that name exists in this section`);
|
|
801
|
+
}
|
|
802
|
+
}
|
|
803
|
+
}
|
|
804
|
+
// With recover, processFile carries on after errors, so check for them too
|
|
805
|
+
let success = await processFile(settings.script, settings.scriptBaseFilename, true);
|
|
806
|
+
// In folder mode, the rest of the story's files, in order
|
|
807
|
+
for (const storyFile of settings.storyFiles ?? []) {
|
|
808
|
+
if (!success && !settings.recover)
|
|
809
|
+
break;
|
|
810
|
+
if (processed.has(storyFile))
|
|
811
|
+
continue;
|
|
812
|
+
if (!settings.externalFiles) {
|
|
813
|
+
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);
|
|
814
|
+
break;
|
|
815
|
+
}
|
|
816
|
+
if (!isSquiffyPath(storyFile)) {
|
|
817
|
+
if (!uiFiles.has(storyFile))
|
|
818
|
+
addPageFile(storyFile, settings.externalFiles, false);
|
|
819
|
+
continue;
|
|
820
|
+
}
|
|
821
|
+
const content = await settings.externalFiles.getContent(storyFile);
|
|
822
|
+
success = await processFile(content, storyFile, false) && success;
|
|
823
|
+
}
|
|
824
|
+
success && (success = !legacyErrors.length);
|
|
825
|
+
if (success || settings.recover) {
|
|
826
|
+
if (!Object.keys(story.sections).length) {
|
|
827
|
+
story.addSection("_default", settings.scriptBaseFilename, 0, undefined);
|
|
828
|
+
}
|
|
829
|
+
if (!story.start) {
|
|
830
|
+
story.start = mainFirstSection ?? Object.keys(story.sections)[0];
|
|
831
|
+
}
|
|
832
|
+
sectionNames = Object.keys(story.sections);
|
|
833
|
+
sectionIndexes = new Map(sectionNames.map((name, i) => [name, i]));
|
|
834
|
+
const nodeNames = new Map(Object.values(story.sections).flatMap(section => section.node ? [[section.node, section.name]] : []));
|
|
835
|
+
labels = sectionLabels(files, node => nodeNames.get(node));
|
|
836
|
+
for (const section of Object.values(story.sections)) {
|
|
837
|
+
checkText(section, section, null);
|
|
838
|
+
for (const passage of Object.values(section.passages)) {
|
|
839
|
+
checkText(passage, section, passage);
|
|
840
|
+
}
|
|
841
|
+
}
|
|
842
|
+
success && (success = !legacyErrors.length);
|
|
843
|
+
await warnAboutUnknownNames();
|
|
844
|
+
}
|
|
845
|
+
if (settings.expressions) {
|
|
846
|
+
warnAboutUnquotedText();
|
|
847
|
+
}
|
|
848
|
+
// A story setting an attribute that Squiffy sets itself, such as turns (#377)
|
|
849
|
+
files.forEach((file, i) => {
|
|
850
|
+
for (const found of findBuiltInAttributeSets(file, texts[i])) {
|
|
851
|
+
report("warning", "squiffy/built-in-attribute", file.filename, found.range, `Squiffy sets ${found.name} itself: it's ${BUILT_IN_ATTRIBUTES.get(found.name)}. Use a different name for your own attribute.`, found.range.start.line + 1);
|
|
852
|
+
}
|
|
853
|
+
});
|
|
854
|
+
// @leave only does something when the screen is cleared: by the section or passage it's in, or by
|
|
855
|
+
// one that it's for, as the master section's @leave is for every section
|
|
856
|
+
// With @clear, or {{clear}} in its text (#524), which may only clear it some of the time
|
|
857
|
+
const clearsItself = (t) => !!t && (t.clear || t.text.some(line => /\{\{~?\s*clear\s*~?\}\}/.test(line)));
|
|
858
|
+
const master = story.sections[""];
|
|
859
|
+
const clears = (s) => clearsItself(s) || Object.values(s.passages).some(clearsItself);
|
|
860
|
+
const anyClear = Object.values(story.sections).some(clears);
|
|
861
|
+
const checkLeave = (target, used) => {
|
|
862
|
+
if (!target.leaveNode || target.leave == "none" || used)
|
|
863
|
+
return;
|
|
864
|
+
report("warning", "squiffy/transition", target.leaveNode.filename, target.leaveNode.range, "@leave is for when the screen is cleared, but nothing it's for has @clear or {{clear}}, so it does nothing.", target.leaveNode.range.start.line + 1);
|
|
865
|
+
};
|
|
866
|
+
for (const section of Object.values(story.sections)) {
|
|
867
|
+
const masterPassageClears = clearsItself(section.passages[""]) || clearsItself(master?.passages[""]);
|
|
868
|
+
checkLeave(section, section === master ? anyClear : clears(section) || clearsItself(master) || (master ? Object.values(master.passages).some(clearsItself) : false));
|
|
869
|
+
for (const passage of Object.values(section.passages)) {
|
|
870
|
+
checkLeave(passage, clearsItself(passage) || masterPassageClears || (passage.name == "" && clears(section)));
|
|
871
|
+
}
|
|
872
|
+
}
|
|
873
|
+
// Warnings turned off by {{!-- squiffy-ignore --}} comments
|
|
874
|
+
const ignored = ignoredDiagnostics(files, diagnostics);
|
|
875
|
+
if (ignored.size) {
|
|
876
|
+
diagnostics.splice(0, diagnostics.length, ...diagnostics.filter(d => !ignored.has(d)));
|
|
877
|
+
const ignoredLines = new Set([...ignored].map(d => legacyWarningOf.get(d)));
|
|
878
|
+
legacyWarnings.splice(0, legacyWarnings.length, ...legacyWarnings.filter(w => !ignoredLines.has(w)));
|
|
879
|
+
}
|
|
880
|
+
const analysis = {
|
|
881
|
+
success,
|
|
882
|
+
files,
|
|
883
|
+
diagnostics,
|
|
884
|
+
sections: Object.values(story.sections).map(section => ({
|
|
885
|
+
name: section.name,
|
|
886
|
+
file: section.filename ?? "",
|
|
887
|
+
...(section.node ? { node: section.node } : {}),
|
|
888
|
+
passages: Object.values(section.passages).map(passage => ({ name: passage.name, node: passage.node })),
|
|
889
|
+
})),
|
|
890
|
+
start: story.start,
|
|
891
|
+
expressions: settings.expressions ?? false,
|
|
892
|
+
stylesheets: stylesheetFiles,
|
|
893
|
+
scripts: scriptFiles,
|
|
894
|
+
};
|
|
895
|
+
analysisInternals.set(analysis, { story, legacyErrors, legacyWarnings });
|
|
896
|
+
return analysis;
|
|
897
|
+
}
|
|
898
|
+
// The name most like name, for a typo: one letter out for a short name, or two for a longer one.
|
|
899
|
+
// A name of one or two letters is too short to guess at.
|
|
900
|
+
function closestName(name, names) {
|
|
901
|
+
if (name.length < 3)
|
|
902
|
+
return undefined;
|
|
903
|
+
const limit = name.length <= 5 ? 1 : 2;
|
|
904
|
+
let best;
|
|
905
|
+
let bestDistance = limit + 1;
|
|
906
|
+
for (const candidate of names) {
|
|
907
|
+
if (candidate == name || Math.abs(candidate.length - name.length) > limit)
|
|
908
|
+
continue;
|
|
909
|
+
const distance = editDistance(name.toLowerCase(), candidate.toLowerCase());
|
|
910
|
+
if (distance < bestDistance) {
|
|
911
|
+
best = candidate;
|
|
912
|
+
bestDistance = distance;
|
|
913
|
+
}
|
|
914
|
+
}
|
|
915
|
+
return best;
|
|
916
|
+
}
|
|
917
|
+
// Letters added, removed or changed, and two swapped, to turn a into b
|
|
918
|
+
function editDistance(a, b) {
|
|
919
|
+
const d = Array.from({ length: a.length + 1 }, (_, i) => [i, ...Array(b.length).fill(0)]);
|
|
920
|
+
for (let j = 1; j <= b.length; j++)
|
|
921
|
+
d[0][j] = j;
|
|
922
|
+
for (let i = 1; i <= a.length; i++) {
|
|
923
|
+
for (let j = 1; j <= b.length; j++) {
|
|
924
|
+
const cost = a[i - 1] == b[j - 1] ? 0 : 1;
|
|
925
|
+
d[i][j] = Math.min(d[i - 1][j] + 1, d[i][j - 1] + 1, d[i - 1][j - 1] + cost);
|
|
926
|
+
if (i > 1 && j > 1 && a[i - 1] == b[j - 2] && a[i - 2] == b[j - 1])
|
|
927
|
+
d[i][j] = Math.min(d[i][j], d[i - 2][j - 2] + 1);
|
|
928
|
+
}
|
|
929
|
+
}
|
|
930
|
+
return d[a.length][b.length];
|
|
931
|
+
}
|
|
932
|
+
function linkDestinationExists(link, keys) {
|
|
933
|
+
// Link destination data may look like:
|
|
934
|
+
// passageName
|
|
935
|
+
// passageName, my_attribute=2
|
|
936
|
+
// passage name with a comma, in the name
|
|
937
|
+
// We're only interested in checking if the named passage or section exists.
|
|
938
|
+
// First check if the full link is a valid destination (handles names containing commas).
|
|
939
|
+
const trimmedLink = link.trim();
|
|
940
|
+
if (hasOwn(keys, trimmedLink)) {
|
|
941
|
+
return true;
|
|
942
|
+
}
|
|
943
|
+
// Otherwise, assume a comma separates the destination from setters.
|
|
944
|
+
const linkDestination = trimmedLink.split(",")[0].trim();
|
|
945
|
+
return hasOwn(keys, linkDestination);
|
|
946
|
+
}
|
|
947
|
+
// The same as Object.keys(keys).includes(key) for the plain objects used here, without building
|
|
948
|
+
// an array for every link.
|
|
949
|
+
function hasOwn(keys, key) {
|
|
950
|
+
return Object.prototype.hasOwnProperty.call(keys, key);
|
|
951
|
+
}
|