@khanacademy/perseus-linter 2.0.0 → 3.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,2232 @@
1
+ import _extends from '@babel/runtime/helpers/extends';
2
+ import { PerseusError, Errors } from '@khanacademy/perseus-core';
3
+ import { addLibraryVersionToPerseusDebug } from '@khanacademy/perseus-utils';
4
+ import PropTypes from 'prop-types';
5
+
6
+ /* eslint-disable no-useless-escape */
7
+ /**
8
+ * The Selector class implements a CSS-like system for matching nodes in a
9
+ * parse tree based on the structure of the tree. Create a Selector object by
10
+ * calling the static Selector.parse() method on a string that describes the
11
+ * tree structure you want to match. For example, if you want to find text
12
+ * nodes that are direct children of paragraph nodes that immediately follow
13
+ * heading nodes, you could create an appropriate selector like this:
14
+ *
15
+ * selector = Selector.parse("heading + paragraph > text");
16
+ *
17
+ * Recall from the TreeTransformer class, that we consider any object with a
18
+ * string-valued `type` property to be a tree node. The words "heading",
19
+ * "paragraph" and "text" in the selector string above specify node types and
20
+ * will match nodes in a parse tree that have `type` properties with those
21
+ * values.
22
+ *
23
+ * Selectors are designed for use during tree traversals done with the
24
+ * TreeTransformer traverse() method. To test whether the node currently being
25
+ * traversed matches a selector, simply pass the TraversalState object to the
26
+ * match() method of the Selector object. If the node does not match the
27
+ * selector, match() returns null. If it does match, then match() returns an
28
+ * array of nodes that match the selector. In the example above the first
29
+ * element of the array would be the node the heading node, the second would
30
+ * be the paragraph node that follows it, and the third would be the text node
31
+ * that is a child of the paragraph. The last element of a returned array of
32
+ * nodes is always equal to the current node of the tree traversal.
33
+ *
34
+ * Code that uses a selector might look like this:
35
+ *
36
+ * matchingNodes = selector.match(state);
37
+ * if (matchingNodes) {
38
+ * let heading = matchingNodes[0];
39
+ * let text = matchingNodes[2];
40
+ * // do something with those nodes
41
+ * }
42
+ *
43
+ * The Selector.parse() method recognizes a grammar that is similar to CSS
44
+ * selectors:
45
+ *
46
+ * selector := treeSelector (, treeSelector)*
47
+ *
48
+ * A selector is one or more comma-separated treeSelectors. A node matches
49
+ * the selector if it matches any of the treeSelectors.
50
+ *
51
+ * treeSelector := (treeSelector combinator)? nodeSelector
52
+ *
53
+ * A treeSelector is a nodeSelector optionally preceeded by a combinator
54
+ * and another tree selector. The tree selector matches if the current node
55
+ * matches the node selector and a sibling or ancestor (depending on the
56
+ * combinator) of the current node matches the optional treeSelector.
57
+ *
58
+ * combinator := ' ' | '>' | '+' | '~' // standard CSS3 combinators
59
+ *
60
+ * A combinator is a space or punctuation character that specifies the
61
+ * relationship between two nodeSelectors. A space between two
62
+ * nodeSelectors means that the first selector much match an ancestor of
63
+ * the node that matches the second selector. A '>' character means that
64
+ * the first selector must match the parent of the node matched by the
65
+ * second. The '~' combinator means that the first selector must match a
66
+ * previous sibling of the node matched by the second. And the '+' selector
67
+ * means that first selector must match the immediate previous sibling of
68
+ * the node that matched the second.
69
+ *
70
+ * nodeSelector := <IDENTIFIER> | '*'
71
+ *
72
+ * A nodeSelector is simply an identifier (a letter followed by any number
73
+ * of letters, digits, hypens, and underscores) or the wildcard asterisk
74
+ * character. A wildcard node selector matches any node. An identifier
75
+ * selector matches any node that has a `type` property whose value matches
76
+ * the identifier.
77
+ *
78
+ * If you call Selector.parse() on a string that does not match this grammar,
79
+ * it will throw an exception
80
+ *
81
+ * TODO(davidflanagan): it might be useful to allow more sophsticated node
82
+ * selector matching with attribute matches and pseudo-classes, like
83
+ * "heading[level=2]" or "paragraph:first-child"
84
+ *
85
+ * Implementation Note: this file exports a very simple Selector class but all
86
+ * the actual work is done in various internal classes. The Parser class
87
+ * parses the string representation of a selector into a parse tree that
88
+ * consists of instances of various subclasses of the Selector class. It is
89
+ * these subclasses that implement the selector matching logic, often
90
+ * depending on features of the TraversalState object from the TreeTransformer
91
+ * traversal.
92
+ */
93
+
94
+ /**
95
+ * This is the base class for all Selector types. The key method that all
96
+ * selector subclasses must implement is match(). It takes a TraversalState
97
+ * object (from a TreeTransformer traversal) and tests whether the selector
98
+ * matches at the current node. See the comment at the start of this file for
99
+ * more details on the match() method.
100
+ */
101
+ class Selector {
102
+ static parse(selectorText) {
103
+ return new Parser(selectorText).parse();
104
+ }
105
+
106
+ /**
107
+ * Return an array of the nodes that matched or null if no match.
108
+ * This is the base class so we just throw an exception. All Selector
109
+ * subclasses must provide an implementation of this method.
110
+ */
111
+ match(state) {
112
+ throw new PerseusError("Selector subclasses must implement match()", Errors.NotAllowed);
113
+ }
114
+
115
+ /**
116
+ * Selector subclasses all define a toString() method primarily
117
+ * because it makes it easy to write parser tests.
118
+ */
119
+ toString() {
120
+ return "Unknown selector class";
121
+ }
122
+ }
123
+
124
+ /**
125
+ * This class implements a parser for the selector grammar. Pass the source
126
+ * text to the Parser() constructor, and then call the parse() method to
127
+ * obtain a corresponding Selector object. parse() throws an exception
128
+ * if there are syntax errors in the selector.
129
+ *
130
+ * This class is not exported, and you don't need to use it directly.
131
+ * Instead call the static Selector.parse() method.
132
+ */
133
+ class Parser {
134
+ // Which token in the array we're looking at now
135
+
136
+ constructor(s) {
137
+ // We do lexing with a simple regular expression
138
+ this.tokens = void 0;
139
+ // The array of tokens
140
+ this.tokenIndex = void 0;
141
+ // Normalize whitespace:
142
+ // - remove leading and trailing whitespace
143
+ // - replace runs of whitespace with single space characters
144
+ s = s.trim().replace(/\s+/g, " ");
145
+ // Convert the string to an array of tokens. Note that the TOKENS
146
+ // pattern ignores spaces that do not appear before identifiers
147
+ // or the * wildcard.
148
+ this.tokens = s.match(Parser.TOKENS) || [];
149
+ this.tokenIndex = 0;
150
+ }
151
+
152
+ // Return the next token or the empty string if there are no more
153
+ nextToken() {
154
+ return this.tokens[this.tokenIndex] || "";
155
+ }
156
+
157
+ // Increment the token index to "consume" the token we were looking at
158
+ // and move on to the next one.
159
+ consume() {
160
+ this.tokenIndex++;
161
+ }
162
+
163
+ // Return true if the current token is an identifier or false otherwise
164
+ isIdentifier() {
165
+ // The Parser.TOKENS regexp ensures that we only have to check
166
+ // the first character of a token to know what kind of token it is.
167
+ const c = this.tokens[this.tokenIndex][0];
168
+ return c >= "a" && c <= "z" || c >= "A" && c <= "Z";
169
+ }
170
+
171
+ // Consume space tokens until the next token is not a space.
172
+ skipSpace() {
173
+ while (this.nextToken() === " ") {
174
+ this.consume();
175
+ }
176
+ }
177
+
178
+ // Parse a comma-separated sequence of tree selectors. This is the
179
+ // entry point for the Parser class and the only method that clients
180
+ // ever need to call.
181
+ parse() {
182
+ // We expect at least one tree selector
183
+ const ts = this.parseTreeSelector();
184
+
185
+ // Now see what's next
186
+ let token = this.nextToken();
187
+
188
+ // If there is no next token then we're done parsing and can return
189
+ // the tree selector object we got above
190
+ if (!token) {
191
+ return ts;
192
+ }
193
+
194
+ // Otherwise, there is more go come and we're going to need a
195
+ // list of tree selectors
196
+ const treeSelectors = [ts];
197
+ while (token) {
198
+ // The only character we allow after a tree selector is a comma
199
+ if (token === ",") {
200
+ this.consume();
201
+ } else {
202
+ throw new ParseError("Expected comma");
203
+ }
204
+
205
+ // And if we saw a comma, then it must be followed by another
206
+ // tree selector
207
+ treeSelectors.push(this.parseTreeSelector());
208
+ token = this.nextToken();
209
+ }
210
+
211
+ // If we parsed more than one tree selector, return them in a
212
+ // SelectorList object.
213
+ return new SelectorList(treeSelectors);
214
+ }
215
+
216
+ // Parse a sequence of node selectors linked together with
217
+ // hierarchy combinators: space, >, + and ~.
218
+ parseTreeSelector() {
219
+ this.skipSpace(); // Ignore space after a comma, for example
220
+
221
+ // A tree selector must begin with a node selector
222
+ let ns = this.parseNodeSelector();
223
+ for (;;) {
224
+ // Now check the next token. If there is none, or if it is a
225
+ // comma, then we're done with the treeSelector. Otherwise
226
+ // we expect a combinator followed by another node selector.
227
+ // If we don't see a combinator, we throw an error. If we
228
+ // do see a combinator and another node selector then we
229
+ // combine the current node selector with the new node selector
230
+ // using a Selector subclass that depends on the combinator.
231
+ const token = this.nextToken();
232
+ if (!token || token === ",") {
233
+ break;
234
+ } else if (token === " ") {
235
+ this.consume();
236
+ ns = new AncestorCombinator(ns, this.parseNodeSelector());
237
+ } else if (token === ">") {
238
+ this.consume();
239
+ ns = new ParentCombinator(ns, this.parseNodeSelector());
240
+ } else if (token === "+") {
241
+ this.consume();
242
+ ns = new PreviousCombinator(ns, this.parseNodeSelector());
243
+ } else if (token === "~") {
244
+ this.consume();
245
+ ns = new SiblingCombinator(ns, this.parseNodeSelector());
246
+ } else {
247
+ throw new ParseError("Unexpected token: " + token);
248
+ }
249
+ }
250
+ return ns;
251
+ }
252
+
253
+ // Parse a single node selector.
254
+ // For now, this is just a node type or a wildcard.
255
+ //
256
+ // TODO(davidflanagan): we may need to extend this with attribute
257
+ // selectors like 'heading[level=3]', or with pseudo-classes like
258
+ // paragraph:first-child
259
+ parseNodeSelector() {
260
+ // First, skip any whitespace
261
+ this.skipSpace();
262
+ const t = this.nextToken();
263
+ if (t === "*") {
264
+ this.consume();
265
+ return new AnyNode();
266
+ }
267
+ if (this.isIdentifier()) {
268
+ this.consume();
269
+ return new TypeSelector(t);
270
+ }
271
+ throw new ParseError("Expected node type");
272
+ }
273
+ }
274
+
275
+ // We break the input string into tokens with this regexp. Token types
276
+ // are identifiers, integers, punctuation and spaces. Note that spaces
277
+ // tokens are only returned when they appear before an identifier or
278
+ // wildcard token and are otherwise omitted.
279
+ Parser.TOKENS = void 0;
280
+ Parser.TOKENS = /([a-zA-Z][\w-]*)|(\d+)|[^\s]|(\s(?=[a-zA-Z\*]))/g;
281
+
282
+ /**
283
+ * This is a trivial Error subclass that the Parser uses to signal parse errors
284
+ */
285
+ class ParseError extends Error {
286
+ constructor(message) {
287
+ super(message);
288
+ }
289
+ }
290
+
291
+ /**
292
+ * This Selector subclass is a list of selectors. It matches a node if any of
293
+ * the selectors on the list matches the node. It considers the selectors in
294
+ * order, and returns the array of nodes returned by whichever one matches
295
+ * first.
296
+ */
297
+ class SelectorList extends Selector {
298
+ constructor(selectors) {
299
+ super();
300
+ this.selectors = void 0;
301
+ this.selectors = selectors;
302
+ }
303
+ match(state) {
304
+ for (let i = 0; i < this.selectors.length; i++) {
305
+ const s = this.selectors[i];
306
+ const result = s.match(state);
307
+ if (result) {
308
+ return result;
309
+ }
310
+ }
311
+ return null;
312
+ }
313
+ toString() {
314
+ let result = "";
315
+ for (let i = 0; i < this.selectors.length; i++) {
316
+ result += i > 0 ? ", " : "";
317
+ result += this.selectors[i].toString();
318
+ }
319
+ return result;
320
+ }
321
+ }
322
+
323
+ /**
324
+ * This trivial Selector subclass implements the '*' wildcard and
325
+ * matches any node.
326
+ */
327
+ class AnyNode extends Selector {
328
+ match(state) {
329
+ return [state.currentNode()];
330
+ }
331
+ toString() {
332
+ return "*";
333
+ }
334
+ }
335
+
336
+ /**
337
+ * This selector subclass implements the <IDENTIFIER> part of the grammar.
338
+ * it matches any node whose `type` property is a specified string
339
+ */
340
+ class TypeSelector extends Selector {
341
+ constructor(type) {
342
+ super();
343
+ this.type = void 0;
344
+ this.type = type;
345
+ }
346
+ match(state) {
347
+ const node = state.currentNode();
348
+ if (node.type === this.type) {
349
+ return [node];
350
+ }
351
+ return null;
352
+ }
353
+ toString() {
354
+ return this.type;
355
+ }
356
+ }
357
+
358
+ /**
359
+ * This selector subclass is the superclass of the classes that implement
360
+ * matching for the four combinators. It defines left and right properties for
361
+ * the two selectors that are to be combined, but does not define a match
362
+ * method.
363
+ */
364
+ class SelectorCombinator extends Selector {
365
+ constructor(left, right) {
366
+ super();
367
+ this.left = void 0;
368
+ this.right = void 0;
369
+ this.left = left;
370
+ this.right = right;
371
+ }
372
+ }
373
+
374
+ /**
375
+ * This Selector subclass implements the space combinator. It matches if the
376
+ * right selector matches the current node and the left selector matches some
377
+ * ancestor of the current node.
378
+ */
379
+ class AncestorCombinator extends SelectorCombinator {
380
+ constructor(left, right) {
381
+ super(left, right);
382
+ }
383
+ match(state) {
384
+ const rightResult = this.right.match(state);
385
+ if (rightResult) {
386
+ state = state.clone();
387
+ while (state.hasParent()) {
388
+ state.goToParent();
389
+ const leftResult = this.left.match(state);
390
+ if (leftResult) {
391
+ return leftResult.concat(rightResult);
392
+ }
393
+ }
394
+ }
395
+ return null;
396
+ }
397
+ toString() {
398
+ return this.left.toString() + " " + this.right.toString();
399
+ }
400
+ }
401
+
402
+ /**
403
+ * This Selector subclass implements the > combinator. It matches if the
404
+ * right selector matches the current node and the left selector matches
405
+ * the parent of the current node.
406
+ */
407
+ class ParentCombinator extends SelectorCombinator {
408
+ constructor(left, right) {
409
+ super(left, right);
410
+ }
411
+ match(state) {
412
+ const rightResult = this.right.match(state);
413
+ if (rightResult) {
414
+ if (state.hasParent()) {
415
+ state = state.clone();
416
+ state.goToParent();
417
+ const leftResult = this.left.match(state);
418
+ if (leftResult) {
419
+ return leftResult.concat(rightResult);
420
+ }
421
+ }
422
+ }
423
+ return null;
424
+ }
425
+ toString() {
426
+ return this.left.toString() + " > " + this.right.toString();
427
+ }
428
+ }
429
+
430
+ /**
431
+ * This Selector subclass implements the + combinator. It matches if the
432
+ * right selector matches the current node and the left selector matches
433
+ * the immediate previous sibling of the current node.
434
+ */
435
+ class PreviousCombinator extends SelectorCombinator {
436
+ constructor(left, right) {
437
+ super(left, right);
438
+ }
439
+ match(state) {
440
+ const rightResult = this.right.match(state);
441
+ if (rightResult) {
442
+ if (state.hasPreviousSibling()) {
443
+ state = state.clone();
444
+ state.goToPreviousSibling();
445
+ const leftResult = this.left.match(state);
446
+ if (leftResult) {
447
+ return leftResult.concat(rightResult);
448
+ }
449
+ }
450
+ }
451
+ return null;
452
+ }
453
+ toString() {
454
+ return this.left.toString() + " + " + this.right.toString();
455
+ }
456
+ }
457
+
458
+ /**
459
+ * This Selector subclass implements the ~ combinator. It matches if the
460
+ * right selector matches the current node and the left selector matches
461
+ * any previous sibling of the current node.
462
+ */
463
+ class SiblingCombinator extends SelectorCombinator {
464
+ constructor(left, right) {
465
+ super(left, right);
466
+ }
467
+ match(state) {
468
+ const rightResult = this.right.match(state);
469
+ if (rightResult) {
470
+ state = state.clone();
471
+ while (state.hasPreviousSibling()) {
472
+ state.goToPreviousSibling();
473
+ const leftResult = this.left.match(state);
474
+ if (leftResult) {
475
+ return leftResult.concat(rightResult);
476
+ }
477
+ }
478
+ }
479
+ return null;
480
+ }
481
+ toString() {
482
+ return this.left.toString() + " ~ " + this.right.toString();
483
+ }
484
+ }
485
+
486
+ /**
487
+ * The Rule class represents a Perseus lint rule. A Rule instance has a check()
488
+ * method that takes the same (node, state, content) arguments that a
489
+ * TreeTransformer traversal callback function does. Call the check() method
490
+ * during a tree traversal to determine whether the current node of the tree
491
+ * violates the rule. If there is no violation, then check() returns
492
+ * null. Otherwise, it returns an object that includes the name of the rule,
493
+ * an error message, and the start and end positions within the node's content
494
+ * string of the lint.
495
+ *
496
+ * A Perseus lint rule consists of a name, a severity, a selector, a pattern
497
+ * (RegExp) and two functions. The check() method uses the selector, pattern,
498
+ * and functions as follows:
499
+ *
500
+ * - First, when determining which rules to apply to a particular piece of
501
+ * content, each rule can specify an optional function provided in the fifth
502
+ * parameter to evaluate whether or not we should be applying this rule.
503
+ * If the function returns false, we don't use the rule on this content.
504
+ *
505
+ * - Next, check() tests whether the node currently being traversed matches
506
+ * the selector. If it does not, then the rule does not apply at this node
507
+ * and there is no lint and check() returns null.
508
+ *
509
+ * - If the selector matched, then check() tests the text content of the node
510
+ * (and its children) against the pattern. If the pattern does not match,
511
+ * then there is no lint, and check() returns null.
512
+ *
513
+ * - If both the selector and pattern match, then check() calls the function
514
+ * passing the TraversalState object, the content string for the node, the
515
+ * array of nodes returned by the selector match, and the array of strings
516
+ * returned by the pattern match. This function can use these arguments to
517
+ * implement any kind of lint detection logic it wants. If it determines
518
+ * that there is no lint, then it should return null. Otherwise, it should
519
+ * return an error message as a string, or an object with `message`, `start`
520
+ * and `end` properties. The start and end properties are numbers that mark
521
+ * the beginning and end of the problematic content. Note that these numbers
522
+ * are relative to the content string passed to the traversal callback, not
523
+ * to the entire string that was used to generate the parse tree in the
524
+ * first place. TODO(davidflanagan): modify the simple-markdown library to
525
+ * have an option to add the text offset of each node to the parse
526
+ * tree. This will allows us to pinpoint lint errors within a long string
527
+ * of markdown text.
528
+ *
529
+ * - If the function returns null, then check() returns null. Otherwise,
530
+ * check() returns an object with `rule`, `message`, `start` and `end`
531
+ * properties. The value of the `rule` property is the name of the rule,
532
+ * which is useful for error reporting purposes.
533
+ *
534
+ * The name, severity, selector, pattern and function arguments to the Rule()
535
+ * constructor are optional, but you may not omit both the selector and the
536
+ * pattern. If you do not specify a selector, a default selector that matches
537
+ * any node of type "text" will be used. If you do not specify a pattern, then
538
+ * any node that matches the selector will be assumed to match the pattern as
539
+ * well. If you don't pass a function as the fourth argument to the Rule()
540
+ * constructor, then you must pass an error message string instead. If you do
541
+ * this, you'll get a default function that unconditionally returns an object
542
+ * that includes the error message and the start and end indexes of the
543
+ * portion of the content string that matched the pattern. If you don't pass a
544
+ * function in the fifth parameter, the rule will be applied in any context.
545
+ *
546
+ * One of the design goals of this Rule class is to allow simple lint rules to
547
+ * be described in JSON files without any JavaScript code. So in addition to
548
+ * the Rule() constructor, the class also defines a Rule.makeRule() factory
549
+ * method. This method takes a single object as its argument and expects the
550
+ * object to have four string properties. The `name` property is passed as the
551
+ * first argument to the Rule() construtctor. The optional `selector`
552
+ * property, if specified, is passed to Selector.parse() and the resulting
553
+ * Selector object is used as the second argument to Rule(). The optional
554
+ * `pattern` property is converted to a RegExp before being passed as the
555
+ * third argument to Rule(). (See Rule.makePattern() for details on the string
556
+ * to RegExp conversion). Finally, the `message` property specifies an error
557
+ * message that is passed as the final argument to Rule(). You can also use a
558
+ * real RegExp as the value of the `pattern` property or define a custom lint
559
+ * function on the `lint` property instead of setting the `message`
560
+ * property. Doing either of these things means that your rule description can
561
+ * no longer be saved in a JSON file, however.
562
+ *
563
+ * For example, here are two lint rules defined with Rule.makeRule():
564
+ *
565
+ * let nestedLists = Rule.makeRule({
566
+ * name: "nested-lists",
567
+ * selector: "list list",
568
+ * message: `Nested lists:
569
+ * nested lists are hard to read on mobile devices;
570
+ * do not use additional indentation.`,
571
+ * });
572
+ *
573
+ * let longParagraph = Rule.makeRule({
574
+ * name: "long-paragraph",
575
+ * selector: "paragraph",
576
+ * pattern: /^.{501,}/,
577
+ * lint: function(state, content, nodes, match) {
578
+ * return `Paragraph too long:
579
+ * This paragraph is ${content.length} characters long.
580
+ * Shorten it to 500 characters or fewer.`;
581
+ * },
582
+ * });
583
+ *
584
+ * Certain advanced lint rules need additional information about the content
585
+ * being linted in order to detect lint. For example, a rule to check for
586
+ * whitespace at the start and end of the URL for an image can't use the
587
+ * information in the node or content arguments because the markdown parser
588
+ * strips leading and trailing whitespace when parsing. (Nevertheless, these
589
+ * spaces have been a practical problem for our content translation process so
590
+ * in order to check for them, a lint rule needs access to the original
591
+ * unparsed source text. Similarly, there are various lint rules that check
592
+ * widget usage. For example, it is easy to write a lint rule to ensure that
593
+ * images have alt text for images encoded in markdown. But when images are
594
+ * added to our content via an image widget we also want to be able to check
595
+ * for alt text. In order to do this, the lint rule needs to be able to look
596
+ * widgets up by name in the widgets object associated with the parse tree.
597
+ *
598
+ * In order to support advanced linting rules like these, the check() method
599
+ * takes a context object as its optional fourth argument, and passes this
600
+ * object on to the lint function of each rule. Rules that require extra
601
+ * context should not assume that they will always get it, and should verify
602
+ * that the necessary context has been supplied before using it. Currently the
603
+ * "content" property of the context object is the unparsed source text if
604
+ * available, and the "widgets" property of the context object is the widget
605
+ * object associated with that content string in the JSON object that defines
606
+ * the Perseus article or exercise that is being linted.
607
+ */
608
+
609
+
610
+ // This represents the type returned by String.match(). It is an
611
+ // array of strings, but also has index:number and input:string properties.
612
+ // TypeScript doesn't handle it well, so we punt and just use any.
613
+
614
+ // This is the return type of the check() method of a Rule object
615
+
616
+ // This is the return type of the lint detection function passed as the 4th
617
+ // argument to the Rule() constructor. It can return null or a string or an
618
+ // object containing a string and two numbers.
619
+ // prettier-ignore
620
+ // (prettier formats this in a way that ka-lint does not like)
621
+
622
+ // This is the type of the lint detection function that the Rule() constructor
623
+ // expects as its fourth argument. It is passed the TraversalState object and
624
+ // content string that were passed to check(), and is also passed the array of
625
+ // nodes returned by the selector match and the array of strings returned by
626
+ // the pattern match. It should return null if no lint is detected or an
627
+ // error message or an object contining an error message.
628
+
629
+ // An optional check to verify whether or not a particular rule should
630
+ // be checked by context. For example, some rules only apply in exercises,
631
+ // and should never be applied to articles. Defaults to true, so if we
632
+ // omit the applies function in a rule, it'll be tested everywhere.
633
+
634
+ /**
635
+ * A Rule object describes a Perseus lint rule. See the comment at the top of
636
+ * this file for detailed description.
637
+ */
638
+ class Rule {
639
+ // The comment at the top of this file has detailed docs for
640
+ // this constructor and its arguments
641
+ constructor(name, severity, selector, pattern, lint, applies) {
642
+ this.name = void 0;
643
+ // The name of the rule
644
+ this.severity = void 0;
645
+ // The severity of the rule
646
+ this.selector = void 0;
647
+ // The specified selector or the DEFAULT_SELECTOR
648
+ this.pattern = void 0;
649
+ // A regular expression if one was specified
650
+ this.lint = void 0;
651
+ // The lint-testing function or a default
652
+ this.applies = void 0;
653
+ // Checks to see if we should apply a rule or not
654
+ this.message = void 0;
655
+ if (!selector && !pattern) {
656
+ throw new PerseusError("Lint rules must have a selector or pattern", Errors.InvalidInput, {
657
+ metadata: {
658
+ name
659
+ }
660
+ });
661
+ }
662
+ this.name = name || "unnamed rule";
663
+ this.severity = severity || Rule.Severity.BULK_WARNING;
664
+ this.selector = selector || Rule.DEFAULT_SELECTOR;
665
+ this.pattern = pattern || null;
666
+
667
+ // If we're called with an error message instead of a function then
668
+ // use a default function that will return the message.
669
+ if (typeof lint === "function") {
670
+ this.lint = lint;
671
+ this.message = null;
672
+ } else {
673
+ this.lint = (...args) => this._defaultLintFunction(...args);
674
+ this.message = lint;
675
+ }
676
+ this.applies = applies || function () {
677
+ return true;
678
+ };
679
+ }
680
+
681
+ // A factory method for use with rules described in JSON files
682
+ // See the documentation at the start of this file for details.
683
+ static makeRule(options) {
684
+ return new Rule(options.name, options.severity, options.selector ? Selector.parse(options.selector) : null, Rule.makePattern(options.pattern), options.lint || options.message, options.applies);
685
+ }
686
+
687
+ // Check the node n to see if it violates this lint rule. A return value
688
+ // of false means there is no lint. A returned object indicates a lint
689
+ // error. See the documentation at the top of this file for details.
690
+ check(node, traversalState, content, context) {
691
+ // First, see if we match the selector.
692
+ // If no selector was passed to the constructor, we use a
693
+ // default selector that matches text nodes.
694
+ const selectorMatch = this.selector.match(traversalState);
695
+
696
+ // If the selector did not match, then we're done
697
+ if (!selectorMatch) {
698
+ return null;
699
+ }
700
+
701
+ // If the selector matched, then see if the pattern matches
702
+ let patternMatch;
703
+ if (this.pattern) {
704
+ patternMatch = content.match(this.pattern);
705
+ } else {
706
+ // If there is no pattern, then just match all of the content.
707
+ // Use a fake RegExp match object to represent this default match.
708
+ patternMatch = Rule.FakePatternMatch(content, content, 0);
709
+ }
710
+
711
+ // If there was a pattern and it didn't match, then we're done
712
+ if (!patternMatch) {
713
+ return null;
714
+ }
715
+ try {
716
+ // If we get here, then the selector and pattern have matched
717
+ // so now we call the lint function to see if there is lint.
718
+ const error = this.lint(traversalState, content, selectorMatch, patternMatch, context);
719
+
720
+ // eslint-disable-next-line @typescript-eslint/strict-boolean-expressions
721
+ if (!error) {
722
+ return null; // No lint; we're done
723
+ }
724
+ if (typeof error === "string") {
725
+ // If the lint function returned a string we assume it
726
+ // applies to the entire content of the node and return it.
727
+ return {
728
+ rule: this.name,
729
+ severity: this.severity,
730
+ message: error,
731
+ start: 0,
732
+ end: content.length
733
+ };
734
+ }
735
+ // If the lint function returned an object, then we just
736
+ // add the rule name to the message, start and end.
737
+ return {
738
+ rule: this.name,
739
+ severity: this.severity,
740
+ message: error.message,
741
+ start: error.start,
742
+ end: error.end
743
+ };
744
+ } catch (e) {
745
+ // If the lint function threw an exception we handle that as
746
+ // a special type of lint. We want the user to see the lint
747
+ // warning in this case (even though it is out of their control)
748
+ // so that the bug gets reported. Otherwise we'd never know that
749
+ // a rule was failing.
750
+ return {
751
+ rule: "lint-rule-failure",
752
+ message: `Exception in rule ${this.name}: ${e.message}
753
+ Stack trace:
754
+ ${e.stack}`,
755
+ start: 0,
756
+ end: content.length
757
+ };
758
+ }
759
+ }
760
+
761
+ // This internal method is the default lint function that we use when a
762
+ // rule is defined without a function. This is useful for rules where the
763
+ // selector and/or pattern match are enough to indicate lint. This
764
+ // function unconditionally returns the error message that was passed in
765
+ // place of a function, but also adds start and end properties that
766
+ // specify which particular portion of the node content matched the
767
+ // pattern.
768
+ _defaultLintFunction(state, content, selectorMatch, patternMatch, context) {
769
+ return {
770
+ message: this.message || "",
771
+ start: patternMatch.index,
772
+ end: patternMatch.index + patternMatch[0].length
773
+ };
774
+ }
775
+
776
+ // The makeRule() factory function uses this static method to turn its
777
+ // argument into a RegExp. If the argument is already a RegExp, we just
778
+ // return it. Otherwise, we compile it into a RegExp and return that.
779
+ // The reason this is necessary is that Rule.makeRule() is designed for
780
+ // use with data from JSON files and JSON files can't include RegExp
781
+ // literals. Strings passed to this function do not need to be delimited
782
+ // with / characters unless you want to include flags for the RegExp.
783
+ //
784
+ // Examples:
785
+ //
786
+ // input "" ==> output null
787
+ // input /foo/ ==> output /foo/
788
+ // input "foo" ==> output /foo/
789
+ // input "/foo/i" ==> output /foo/i
790
+ //
791
+ static makePattern(pattern) {
792
+ // eslint-disable-next-line @typescript-eslint/strict-boolean-expressions
793
+ if (!pattern) {
794
+ return null;
795
+ }
796
+ if (pattern instanceof RegExp) {
797
+ return pattern;
798
+ }
799
+ if (pattern[0] === "/") {
800
+ const lastSlash = pattern.lastIndexOf("/");
801
+ const expression = pattern.substring(1, lastSlash);
802
+ const flags = pattern.substring(lastSlash + 1);
803
+ // @ts-expect-error - TS2713 - Cannot access 'RegExp.flags' because 'RegExp' is a type, but not a namespace. Did you mean to retrieve the type of the property 'flags' in 'RegExp' with 'RegExp["flags"]'?
804
+ return new RegExp(expression, flags);
805
+ }
806
+ return new RegExp(pattern);
807
+ }
808
+
809
+ // This static method returns an string array with index and input
810
+ // properties added, in order to simulate the return value of the
811
+ // String.match() method. We use it when a Rule has no pattern and we
812
+ // want to simulate a match on the entire content string.
813
+ static FakePatternMatch(input, match, index) {
814
+ const result = [match];
815
+ result.index = index;
816
+ result.input = input;
817
+ return result;
818
+ }
819
+ }
820
+ // The error message for use with the default function
821
+ Rule.DEFAULT_SELECTOR = void 0;
822
+ Rule.Severity = {
823
+ ERROR: 1,
824
+ WARNING: 2,
825
+ GUIDELINE: 3,
826
+ BULK_WARNING: 4
827
+ };
828
+ Rule.DEFAULT_SELECTOR = Selector.parse("text");
829
+
830
+ /* eslint-disable no-useless-escape */
831
+ // Return the portion of a URL between // and /. This is the authority
832
+ // portion which is usually just the hostname, but may also include
833
+ // a username, password or port. We don't strip those things out because
834
+ // we typically want to reject any URL that includes them
835
+ const HOSTNAME = /\/\/([^\/]+)/;
836
+
837
+ // Return the hostname of the URL, with any "www." prefix removed.
838
+ // If this is a relative URL with no hostname, return an empty string.
839
+ function getHostname(url) {
840
+ if (!url) {
841
+ return "";
842
+ }
843
+ const match = url.match(HOSTNAME);
844
+ return match ? match[1] : "";
845
+ }
846
+
847
+ var AbsoluteUrl = Rule.makeRule({
848
+ name: "absolute-url",
849
+ severity: Rule.Severity.GUIDELINE,
850
+ selector: "link, image",
851
+ lint: function (state, content, nodes, match) {
852
+ const url = nodes[0].target;
853
+ const hostname = getHostname(url);
854
+ if (hostname === "khanacademy.org" || hostname.endsWith(".khanacademy.org")) {
855
+ return `Don't use absolute URLs:
856
+ When linking to KA content or images, omit the
857
+ https://www.khanacademy.org URL prefix.
858
+ Use a relative URL beginning with / instead.`;
859
+ }
860
+ }
861
+ });
862
+
863
+ var BlockquotedMath = Rule.makeRule({
864
+ name: "blockquoted-math",
865
+ severity: Rule.Severity.WARNING,
866
+ selector: "blockQuote math, blockQuote blockMath",
867
+ message: `Blockquoted math:
868
+ math should not be indented.`
869
+ });
870
+
871
+ var BlockquotedWidget = Rule.makeRule({
872
+ name: "blockquoted-widget",
873
+ severity: Rule.Severity.WARNING,
874
+ selector: "blockQuote widget",
875
+ message: `Blockquoted widget:
876
+ widgets should not be indented.`
877
+ });
878
+
879
+ /* eslint-disable no-useless-escape */
880
+ var DoubleSpacingAfterTerminal = Rule.makeRule({
881
+ name: "double-spacing-after-terminal",
882
+ severity: Rule.Severity.BULK_WARNING,
883
+ selector: "paragraph",
884
+ pattern: /[.!\?] {2}/i,
885
+ message: `Use a single space after a sentence-ending period, or
886
+ any other kind of terminal punctuation.`
887
+ });
888
+
889
+ function buttonNotInButtonSet(name, set) {
890
+ return `Answer requires a button not found in the button sets: ${name} (in ${set})`;
891
+ }
892
+ const stringToButtonSet = {
893
+ "\\sqrt": "prealgebra",
894
+ "\\sin": "trig",
895
+ "\\cos": "trig",
896
+ "\\tan": "trig",
897
+ "\\log": "logarithms",
898
+ "\\ln": "logarithms"
899
+ };
900
+
901
+ /**
902
+ * Rule to make sure that Expression questions that require
903
+ * a specific math symbol to answer have that math symbol
904
+ * available in the keypad (desktop learners can use a keyboard,
905
+ * but mobile learners must use the MathInput keypad)
906
+ */
907
+ var ExpressionWidget = Rule.makeRule({
908
+ name: "expression-widget",
909
+ severity: Rule.Severity.WARNING,
910
+ selector: "widget",
911
+ lint: function (state, content, nodes, match, context) {
912
+ var _context$widgets;
913
+ // This rule only looks at image widgets
914
+ if (state.currentNode().widgetType !== "expression") {
915
+ return;
916
+ }
917
+ const nodeId = state.currentNode().id;
918
+ if (!nodeId) {
919
+ return;
920
+ }
921
+
922
+ // If it can't find a definition for the widget it does nothing
923
+ const widget = context == null || (_context$widgets = context.widgets) == null ? void 0 : _context$widgets[nodeId];
924
+ if (!widget) {
925
+ return;
926
+ }
927
+ const answers = widget.options.answerForms;
928
+ const buttons = widget.options.buttonSets;
929
+ for (const answer of answers) {
930
+ for (const [str, set] of Object.entries(stringToButtonSet)) {
931
+ if (answer.value.includes(str) && !buttons.includes(set)) {
932
+ return buttonNotInButtonSet(str, set);
933
+ }
934
+ }
935
+ }
936
+ }
937
+ });
938
+
939
+ var ExtraContentSpacing = Rule.makeRule({
940
+ name: "extra-content-spacing",
941
+ selector: "paragraph",
942
+ pattern: /\s+$/,
943
+ applies: function (context) {
944
+ return (context == null ? void 0 : context.contentType) === "article";
945
+ },
946
+ message: `No extra whitespace at the end of content blocks.`
947
+ });
948
+
949
+ var HeadingLevel1 = Rule.makeRule({
950
+ name: "heading-level-1",
951
+ severity: Rule.Severity.WARNING,
952
+ selector: "heading",
953
+ lint: function (state, content, nodes, match) {
954
+ if (nodes[0].level === 1) {
955
+ return `Don't use level-1 headings:
956
+ Begin headings with two or more # characters.`;
957
+ }
958
+ }
959
+ });
960
+
961
+ var HeadingLevelSkip = Rule.makeRule({
962
+ name: "heading-level-skip",
963
+ severity: Rule.Severity.WARNING,
964
+ selector: "heading ~ heading",
965
+ lint: function (state, content, nodes, match) {
966
+ const currentHeading = nodes[1];
967
+ const previousHeading = nodes[0];
968
+ // A heading can have a level less than, the same as
969
+ // or one more than the previous heading. But going up
970
+ // by 2 or more levels is not right
971
+ if (currentHeading.level > previousHeading.level + 1) {
972
+ return `Skipped heading level:
973
+ this heading is level ${currentHeading.level} but
974
+ the previous heading was level ${previousHeading.level}`;
975
+ }
976
+ }
977
+ });
978
+
979
+ var HeadingSentenceCase = Rule.makeRule({
980
+ name: "heading-sentence-case",
981
+ severity: Rule.Severity.GUIDELINE,
982
+ selector: "heading",
983
+ pattern: /^\W*[a-z]/,
984
+ // first letter is lowercase
985
+ message: `First letter is lowercase:
986
+ the first letter of a heading should be capitalized.`
987
+ });
988
+
989
+ // These are 3-letter and longer words that we would not expect to be
990
+ // capitalized even in a title-case heading. See
991
+ // http://blog.apastyle.org/apastyle/2012/03/title-case-and-sentence-case-capitalization-in-apa-style.html
992
+ const littleWords = {
993
+ and: true,
994
+ nor: true,
995
+ but: true,
996
+ the: true,
997
+ for: true
998
+ };
999
+ function isCapitalized(word) {
1000
+ const c = word[0];
1001
+ return c === c.toUpperCase();
1002
+ }
1003
+ var HeadingTitleCase = Rule.makeRule({
1004
+ name: "heading-title-case",
1005
+ severity: Rule.Severity.GUIDELINE,
1006
+ selector: "heading",
1007
+ pattern: /[^\s:]\s+[A-Z]+[a-z]/,
1008
+ locale: "en",
1009
+ lint: function (state, content, nodes, match) {
1010
+ // We want to assert that heading text is in sentence case, not
1011
+ // title case. The pattern above requires a capital letter at the
1012
+ // start of the heading and allows them after a colon, or in
1013
+ // acronyms that are all capitalized.
1014
+ //
1015
+ // But we can't warn just because the pattern matched because
1016
+ // proper nouns are also allowed bo be capitalized. We're not
1017
+ // going to do dictionary lookup to check for proper nouns, so
1018
+ // we try a heuristic: if the title is more than 3 words long
1019
+ // and if all the words are capitalized or are on the list of
1020
+ // words that don't get capitalized, then we'll assume that
1021
+ // the heading is incorrectly in title case and will warn.
1022
+ // But if there is at least one non-capitalized long word then
1023
+ // we're not in title case and we should not warn.
1024
+ //
1025
+ // TODO(davidflanagan): if this rule causes a lot of false
1026
+ // positives, we should tweak it or remove it. Note that it will
1027
+ // fail for headings like "World War II in Russia"
1028
+ //
1029
+ // TODO(davidflanagan): This rule is specific to English.
1030
+ // It is marked with a locale property above, but that is NYI
1031
+ //
1032
+ // for APA style rules for title case
1033
+
1034
+ const heading = content.trim();
1035
+ let words = heading.split(/\s+/);
1036
+
1037
+ // Remove the first word and the little words
1038
+ words.shift();
1039
+ words = words.filter(
1040
+ // eslint-disable-next-line no-prototype-builtins
1041
+ w => w.length > 2 && !littleWords.hasOwnProperty(w));
1042
+
1043
+ // If there are at least 3 remaining words and all
1044
+ // are capitalized, then the heading is in title case.
1045
+ if (words.length >= 3 && words.every(w => isCapitalized(w))) {
1046
+ return `Title-case heading:
1047
+ This heading appears to be in title-case, but should be sentence-case.
1048
+ Only capitalize the first letter and proper nouns.`;
1049
+ }
1050
+ }
1051
+ });
1052
+
1053
+ var ImageAltText = Rule.makeRule({
1054
+ name: "image-alt-text",
1055
+ severity: Rule.Severity.WARNING,
1056
+ selector: "image",
1057
+ lint: function (state, content, nodes, match) {
1058
+ const image = nodes[0];
1059
+ if (!image.alt || !image.alt.trim()) {
1060
+ return `Images should have alt text:
1061
+ for accessibility, all images should have alt text.
1062
+ Specify alt text inside square brackets after the !.`;
1063
+ }
1064
+ if (image.alt.length < 8) {
1065
+ return `Images should have alt text:
1066
+ for accessibility, all images should have descriptive alt text.
1067
+ This image's alt text is only ${image.alt.length} characters long.`;
1068
+ }
1069
+ }
1070
+ });
1071
+
1072
+ var ImageInTable = Rule.makeRule({
1073
+ name: "image-in-table",
1074
+ severity: Rule.Severity.BULK_WARNING,
1075
+ selector: "table image",
1076
+ message: `Image in table:
1077
+ do not put images inside of tables.`
1078
+ });
1079
+
1080
+ var ImageSpacesAroundUrls = Rule.makeRule({
1081
+ name: "image-spaces-around-urls",
1082
+ severity: Rule.Severity.ERROR,
1083
+ selector: "image",
1084
+ lint: function (state, content, nodes, match, context) {
1085
+ const image = nodes[0];
1086
+ const url = image.target;
1087
+
1088
+ // The markdown parser strips leading and trailing spaces for us,
1089
+ // but they're still a problem for our translation process, so
1090
+ // we need to go check for them in the unparsed source string
1091
+ // if we have it.
1092
+ if (context && context.content) {
1093
+ // Find the url in the original content and make sure that the
1094
+ // character before is '(' and the character after is ')'
1095
+ const index = context.content.indexOf(url);
1096
+ if (index === -1) {
1097
+ // It is not an error if we didn't find it.
1098
+ return;
1099
+ }
1100
+ if (context.content[index - 1] !== "(" || context.content[index + url.length] !== ")") {
1101
+ return `Whitespace before or after image url:
1102
+ For images, don't include any space or newlines after '(' or before ')'.
1103
+ Whitespace in image URLs causes translation difficulties.`;
1104
+ }
1105
+ }
1106
+ }
1107
+ });
1108
+
1109
+ var ImageUrlEmpty = Rule.makeRule({
1110
+ name: "image-url-empty",
1111
+ severity: Rule.Severity.ERROR,
1112
+ selector: "image",
1113
+ lint: function (state, content, nodes) {
1114
+ const image = nodes[0];
1115
+ const url = image.target;
1116
+
1117
+ // If no URL is provided, an infinite spinner will be shown in articles
1118
+ // overlaying the page where the image should be. This prevents the page
1119
+ // from fully loading. As a result, we check for URLS with all images.
1120
+ if (!url || !url.trim()) {
1121
+ return "Images should have a URL";
1122
+ }
1123
+
1124
+ // NOTE(TB): Ideally there would be a check to confirm the URL works
1125
+ // and leads to a valid resource, but fetching the URL would require
1126
+ // linting to be able to handle async functions, which it currently
1127
+ // cannot do.
1128
+ }
1129
+ });
1130
+
1131
+ // Normally we have one rule per file. But since our selector class
1132
+ // can't match specific widget types directly, this rule implements
1133
+ // a number of image widget related rules in one place. This should
1134
+ // slightly increase efficiency, but it means that if there is more
1135
+ // than one problem with an image widget, the user will only see one
1136
+ // problem at a time.
1137
+ var ImageWidget = Rule.makeRule({
1138
+ name: "image-widget",
1139
+ severity: Rule.Severity.WARNING,
1140
+ selector: "widget",
1141
+ lint: function (state, content, nodes, match, context) {
1142
+ // This rule only looks at image widgets
1143
+ if (state.currentNode().widgetType !== "image") {
1144
+ return;
1145
+ }
1146
+ const nodeId = state.currentNode().id;
1147
+ if (!nodeId) {
1148
+ return;
1149
+ }
1150
+
1151
+ // If it can't find a definition for the widget it does nothing
1152
+ // eslint-disable-next-line @typescript-eslint/strict-boolean-expressions
1153
+ const widget = context && context.widgets && context.widgets[nodeId];
1154
+ if (!widget) {
1155
+ return;
1156
+ }
1157
+
1158
+ // Make sure there is alt text
1159
+ const alt = widget.options.alt;
1160
+ if (!alt) {
1161
+ return `Images should have alt text:
1162
+ for accessibility, all images should have a text description.
1163
+ Add a description in the "Alt Text" box of the image widget.`;
1164
+ }
1165
+
1166
+ // Make sure the alt text it is not trivial
1167
+ if (alt.trim().length < 8) {
1168
+ return `Images should have alt text:
1169
+ for accessibility, all images should have descriptive alt text.
1170
+ This image's alt text is only ${alt.trim().length} characters long.`;
1171
+ }
1172
+
1173
+ // Make sure there is no math in the caption
1174
+ if (widget.options.caption && widget.options.caption.match(/[^\\]\$/)) {
1175
+ return `No math in image captions:
1176
+ Don't include math expressions in image captions.`;
1177
+ }
1178
+ }
1179
+ });
1180
+
1181
+ var LinkClickHere = Rule.makeRule({
1182
+ name: "link-click-here",
1183
+ severity: Rule.Severity.WARNING,
1184
+ selector: "link",
1185
+ pattern: /click here/i,
1186
+ message: `Inappropriate link text:
1187
+ Do not use the words "click here" in links.`
1188
+ });
1189
+
1190
+ var LongParagraph = Rule.makeRule({
1191
+ name: "long-paragraph",
1192
+ severity: Rule.Severity.GUIDELINE,
1193
+ selector: "paragraph",
1194
+ pattern: /^.{501,}/,
1195
+ lint: function (state, content, nodes, match) {
1196
+ return `Paragraph too long:
1197
+ This paragraph is ${content.length} characters long.
1198
+ Shorten it to 500 characters or fewer.`;
1199
+ }
1200
+ });
1201
+
1202
+ var MathAdjacent = Rule.makeRule({
1203
+ name: "math-adjacent",
1204
+ severity: Rule.Severity.WARNING,
1205
+ selector: "blockMath+blockMath",
1206
+ message: `Adjacent math blocks:
1207
+ combine the blocks between \\begin{align} and \\end{align}`
1208
+ });
1209
+
1210
+ var MathAlignExtraBreak = Rule.makeRule({
1211
+ name: "math-align-extra-break",
1212
+ severity: Rule.Severity.WARNING,
1213
+ selector: "blockMath",
1214
+ pattern: /(\\{2,})\s*\\end{align}/,
1215
+ message: `Extra space at end of block:
1216
+ Don't end an align block with backslashes`
1217
+ });
1218
+
1219
+ var MathAlignLinebreaks = Rule.makeRule({
1220
+ name: "math-align-linebreaks",
1221
+ severity: Rule.Severity.WARNING,
1222
+ selector: "blockMath",
1223
+ // Match any align block with double backslashes in it
1224
+ // Use [\s\S]* instead of .* so we match newlines as well.
1225
+ pattern: /\\begin{align}[\s\S]*\\\\[\s\S]+\\end{align}/,
1226
+ // Look for double backslashes and ensure that they are
1227
+ // followed by optional space and another pair of backslashes.
1228
+ // Note that this rule can't know where line breaks belong so
1229
+ // it can't tell whether backslashes are completely missing. It just
1230
+ // enforces that you don't have the wrong number of pairs of backslashes.
1231
+ lint: function (state, content, nodes, match) {
1232
+ let text = match[0];
1233
+ while (text.length) {
1234
+ const index = text.indexOf("\\\\");
1235
+ if (index === -1) {
1236
+ // No more backslash pairs, so we found no lint
1237
+ return;
1238
+ }
1239
+ text = text.substring(index + 2);
1240
+
1241
+ // Now we expect to find optional spaces, another pair of
1242
+ // backslashes, and more optional spaces not followed immediately
1243
+ // by another pair of backslashes.
1244
+ const nextpair = text.match(/^\s*\\\\\s*(?!\\\\)/);
1245
+
1246
+ // If that does not match then we either have too few or too
1247
+ // many pairs of backslashes.
1248
+ if (!nextpair) {
1249
+ return "Use four backslashes between lines of an align block";
1250
+ }
1251
+
1252
+ // If it did match, then, shorten the string and continue looping
1253
+ // (because a single align block may have multiple lines that
1254
+ // all must be separated by two sets of double backslashes).
1255
+ text = text.substring(nextpair[0].length);
1256
+ }
1257
+ }
1258
+ });
1259
+
1260
+ var MathEmpty = Rule.makeRule({
1261
+ name: "math-empty",
1262
+ severity: Rule.Severity.WARNING,
1263
+ selector: "math, blockMath",
1264
+ pattern: /^$/,
1265
+ message: "Empty math: don't use $$ in your markdown."
1266
+ });
1267
+
1268
+ var MathFrac = Rule.makeRule({
1269
+ name: "math-frac",
1270
+ severity: Rule.Severity.GUIDELINE,
1271
+ selector: "math, blockMath",
1272
+ pattern: /\\frac[ {]/,
1273
+ message: "Use \\dfrac instead of \\frac in your math expressions."
1274
+ });
1275
+
1276
+ var MathNested = Rule.makeRule({
1277
+ name: "math-nested",
1278
+ severity: Rule.Severity.ERROR,
1279
+ selector: "math, blockMath",
1280
+ pattern: /\\text{[^$}]*\$[^$}]*\$[^}]*}/,
1281
+ message: `Nested math:
1282
+ Don't nest math expressions inside \\text{} blocks`
1283
+ });
1284
+
1285
+ var MathStartsWithSpace = Rule.makeRule({
1286
+ name: "math-starts-with-space",
1287
+ severity: Rule.Severity.GUIDELINE,
1288
+ selector: "math, blockMath",
1289
+ pattern: /^\s*(~|\\qquad|\\quad|\\,|\\;|\\:|\\ |\\!|\\enspace|\\phantom)/,
1290
+ message: `Math starts with space:
1291
+ math should not be indented. Do not begin math expressions with
1292
+ LaTeX space commands like ~, \\;, \\quad, or \\phantom`
1293
+ });
1294
+
1295
+ var MathTextEmpty = Rule.makeRule({
1296
+ name: "math-text-empty",
1297
+ severity: Rule.Severity.WARNING,
1298
+ selector: "math, blockMath",
1299
+ pattern: /\\text{\s*}/,
1300
+ message: "Empty \\text{} block in math expression"
1301
+ });
1302
+
1303
+ // Because no selector is specified, this rule only applies to text nodes.
1304
+ // Math and code hold their content directly and do not have text nodes
1305
+ // beneath them (unlike the HTML DOM) so this rule automatically does not
1306
+ // apply inside $$ or ``.
1307
+ var MathWithoutDollars = Rule.makeRule({
1308
+ name: "math-without-dollars",
1309
+ severity: Rule.Severity.GUIDELINE,
1310
+ pattern: /\\\w+{[^}]*}|{|}/,
1311
+ message: `This looks like LaTeX:
1312
+ did you mean to put it inside dollar signs?`
1313
+ });
1314
+
1315
+ var NestedLists = Rule.makeRule({
1316
+ name: "nested-lists",
1317
+ severity: Rule.Severity.WARNING,
1318
+ selector: "list list",
1319
+ message: `Nested lists:
1320
+ nested lists are hard to read on mobile devices;
1321
+ do not use additional indentation.`
1322
+ });
1323
+
1324
+ var StaticWidgetInQuestionStem = Rule.makeRule({
1325
+ name: "static-widget-in-question-stem",
1326
+ severity: Rule.Severity.WARNING,
1327
+ selector: "widget",
1328
+ lint: (state, content, nodes, match, context) => {
1329
+ var _context$widgets;
1330
+ if ((context == null ? void 0 : context.contentType) !== "exercise") {
1331
+ return;
1332
+ }
1333
+ if (context.stack.includes("hint")) {
1334
+ return;
1335
+ }
1336
+ const nodeId = state.currentNode().id;
1337
+ if (!nodeId) {
1338
+ return;
1339
+ }
1340
+ const widget = context == null || (_context$widgets = context.widgets) == null ? void 0 : _context$widgets[nodeId];
1341
+ if (!widget) {
1342
+ return;
1343
+ }
1344
+ if (widget.static) {
1345
+ return `Widget in question stem is static (non-interactive).`;
1346
+ }
1347
+ }
1348
+ });
1349
+
1350
+ var TableMissingCells = Rule.makeRule({
1351
+ name: "table-missing-cells",
1352
+ severity: Rule.Severity.WARNING,
1353
+ selector: "table",
1354
+ lint: function (state, content, nodes, match) {
1355
+ const table = nodes[0];
1356
+ const headerLength = table.header.length;
1357
+ const rowLengths = table.cells.map(r => r.length);
1358
+ for (let r = 0; r < rowLengths.length; r++) {
1359
+ if (rowLengths[r] !== headerLength) {
1360
+ return `Table rows don't match header:
1361
+ The table header has ${headerLength} cells, but
1362
+ Row ${r + 1} has ${rowLengths[r]} cells.`;
1363
+ }
1364
+ }
1365
+ }
1366
+ });
1367
+
1368
+ // Because no selector is specified, this rule only applies to text nodes.
1369
+ // Math and code hold their content directly and do not have text nodes
1370
+ // beneath them (unlike the HTML DOM) so this rule automatically does not
1371
+ // apply inside $$ or ``.
1372
+ var UnbalancedCodeDelimiters = Rule.makeRule({
1373
+ name: "unbalanced-code-delimiters",
1374
+ severity: Rule.Severity.ERROR,
1375
+ pattern: /[`~]+/,
1376
+ message: `Unbalanced code delimiters:
1377
+ code blocks should begin and end with the same type and number of delimiters`
1378
+ });
1379
+
1380
+ var UnescapedDollar = Rule.makeRule({
1381
+ name: "unescaped-dollar",
1382
+ severity: Rule.Severity.ERROR,
1383
+ selector: "unescapedDollar",
1384
+ message: `Unescaped dollar sign:
1385
+ Dollar signs must appear in pairs or be escaped as \\$`
1386
+ });
1387
+
1388
+ var WidgetInTable = Rule.makeRule({
1389
+ name: "widget-in-table",
1390
+ severity: Rule.Severity.BULK_WARNING,
1391
+ selector: "table widget",
1392
+ message: `Widget in table:
1393
+ do not put widgets inside of tables.`
1394
+ });
1395
+
1396
+ // TODO(davidflanagan):
1397
+ // This should probably be converted to use import and to export
1398
+ // and object that maps rule names to rules. Also, maybe this should
1399
+ // be an auto-generated file with a script that updates it any time
1400
+ // we add a new rule?
1401
+
1402
+ var AllRules = [AbsoluteUrl, BlockquotedMath, BlockquotedWidget, DoubleSpacingAfterTerminal, ImageUrlEmpty, ExpressionWidget, ExtraContentSpacing, HeadingLevel1, HeadingLevelSkip, HeadingSentenceCase, HeadingTitleCase, ImageAltText, ImageInTable, LinkClickHere, LongParagraph, MathAdjacent, MathAlignExtraBreak, MathAlignLinebreaks, MathEmpty, MathFrac, MathNested, MathStartsWithSpace, MathTextEmpty, NestedLists, StaticWidgetInQuestionStem, TableMissingCells, UnescapedDollar, WidgetInTable, MathWithoutDollars, UnbalancedCodeDelimiters, ImageSpacesAroundUrls, ImageWidget];
1403
+
1404
+ /**
1405
+ * TreeTransformer is a class for traversing and transforming trees. Create a
1406
+ * TreeTransformer by passing the root node of the tree to the
1407
+ * constructor. Then traverse that tree by calling the traverse() method. The
1408
+ * argument to traverse() is a callback function that will be called once for
1409
+ * each node in the tree. This is a post-order depth-first traversal: the
1410
+ * callback is not called on the a way down, but on the way back up. That is,
1411
+ * the children of a node are traversed before the node itself is.
1412
+ *
1413
+ * The traversal callback function is passed three arguments, the node being
1414
+ * traversed, a TraversalState object, and the concatentated text content of
1415
+ * the node and all of its descendants. The TraversalState object is the most
1416
+ * most interesting argument: it has methods for querying the ancestors and
1417
+ * siblings of the node, and for deleting or replacing the node. These
1418
+ * transformation methods are why this class is a tree transformer and not
1419
+ * just a tree traverser.
1420
+ *
1421
+ * A typical tree traversal looks like this:
1422
+ *
1423
+ * new TreeTransformer(root).traverse((node, state, content) => {
1424
+ * let parent = state.parent();
1425
+ * let previous = state.previousSibling();
1426
+ * // etc.
1427
+ * });
1428
+ *
1429
+ * The traverse() method descends through nodes and arrays of nodes and calls
1430
+ * the traverse callback on each node on the way back up to the root of the
1431
+ * tree. (Note that it only calls the callback on the nodes themselves, not
1432
+ * any arrays that contain nodes.) A node is loosely defined as any object
1433
+ * with a string-valued `type` property. Objects that do not have a type
1434
+ * property are assumed to not be part of the tree and are not traversed. When
1435
+ * traversing an array, all elements of the array are examined, and any that
1436
+ * are nodes or arrays are recursively traversed. When traversing a node, all
1437
+ * properties of the object are examined and any node or array values are
1438
+ * recursively traversed. In typical parse trees, the children of a node are
1439
+ * in a `children` or `content` array, but this class is designed to handle
1440
+ * more general trees. The Perseus markdown parser, for example, produces
1441
+ * nodes of type "table" that have children in the `header` and `cells`
1442
+ * properties.
1443
+ *
1444
+ * CAUTION: the traverse() method does not make any attempt to detect
1445
+ * cycles. If you call it on a cyclic graph instead of a tree, it will cause
1446
+ * infinite recursion (or, more likely, a stack overflow).
1447
+ *
1448
+ * TODO(davidflanagan): it probably wouldn't be hard to detect cycles: when
1449
+ * pushing a new node onto the containers stack we could just check that it
1450
+ * isn't already there.
1451
+ *
1452
+ * If a node has a text-valued `content` property, it is taken to be the
1453
+ * plain-text content of the node. The traverse() method concatenates these
1454
+ * content strings and passes them to the traversal callback for each
1455
+ * node. This means that the callback has access the full text content of its
1456
+ * node and all of the nodes descendants.
1457
+ *
1458
+ * See the TraversalState class for more information on what information and
1459
+ * methods are available to the traversal callback.
1460
+ **/
1461
+
1462
+
1463
+ // TreeNode is the type of a node in a parse tree. The only real requirement is
1464
+ // that every node has a string-valued `type` property
1465
+
1466
+ // TraversalCallback is the type of the callback function passed to the
1467
+ // traverse() method. It is invoked with node, state, and content arguments
1468
+ // and is expected to return nothing.
1469
+
1470
+ // This is the TreeTransformer class described in detail at the
1471
+ // top of this file.
1472
+ class TreeTransformer {
1473
+ // To create a tree transformer, just pass the root node of the tree
1474
+ constructor(root) {
1475
+ this.root = void 0;
1476
+ this.root = root;
1477
+ }
1478
+
1479
+ // A utility function for determing whether an arbitrary value is a node
1480
+ static isNode(n) {
1481
+ return n && typeof n === "object" && typeof n.type === "string";
1482
+ }
1483
+
1484
+ // Determines whether a value is a node with type "text" and has
1485
+ // a text-valued `content` property.
1486
+ static isTextNode(n) {
1487
+ return TreeTransformer.isNode(n) && n.type === "text" && typeof n.content === "string";
1488
+ }
1489
+
1490
+ // This is the main entry point for the traverse() method. See the comment
1491
+ // at the top of this file for a detailed description. Note that this
1492
+ // method just creates a new TraversalState object to use for this
1493
+ // traversal and then invokes the internal _traverse() method to begin the
1494
+ // recursion.
1495
+ traverse(f) {
1496
+ this._traverse(this.root, new TraversalState(this.root), f);
1497
+ }
1498
+
1499
+ // Do a post-order traversal of node and its descendants, invoking the
1500
+ // callback function f() once for each node and returning the concatenated
1501
+ // text content of the node and its descendants. f() is passed three
1502
+ // arguments: the current node, a TraversalState object representing the
1503
+ // current state of the traversal, and a string that holds the
1504
+ // concatenated text of the node and its descendants.
1505
+ //
1506
+ // This private method holds all the traversal logic and implementation
1507
+ // details. Note that this method uses the TraversalState object to store
1508
+ // information about the structure of the tree.
1509
+ _traverse(n, state, f) {
1510
+ let content = "";
1511
+ if (TreeTransformer.isNode(n)) {
1512
+ // If we were called on a node object, then we handle it
1513
+ // this way.
1514
+ const node = n; // safe cast; we just tested
1515
+
1516
+ // Put the node on the stack before recursing on its children
1517
+ state._containers.push(node);
1518
+ state._ancestors.push(node);
1519
+
1520
+ // Record the node's text content if it has any.
1521
+ // Usually this is for nodes with a type property of "text",
1522
+ // but other nodes types like "math" may also have content.
1523
+ // @ts-expect-error - TS2339 - Property 'content' does not exist on type 'TreeNode'.
1524
+ if (typeof node.content === "string") {
1525
+ // @ts-expect-error - TS2339 - Property 'content' does not exist on type 'TreeNode'.
1526
+ content = node.content;
1527
+ }
1528
+
1529
+ // Recurse on the node. If there was content above, then there
1530
+ // probably won't be any children to recurse on, but we check
1531
+ // anyway.
1532
+ //
1533
+ // If we wanted to make the traversal completely specific to the
1534
+ // actual Perseus parse trees that we'll be dealing with we could
1535
+ // put a switch statement here to dispatch on the node type
1536
+ // property with specific recursion steps for each known type of
1537
+ // node.
1538
+ const keys = Object.keys(node);
1539
+ keys.forEach(key => {
1540
+ // Never recurse on the type property
1541
+ if (key === "type") {
1542
+ return;
1543
+ }
1544
+ // Ignore properties that are null or primitive and only
1545
+ // recurse on objects and arrays. Note that we don't do a
1546
+ // isNode() check here. That is done in the recursive call to
1547
+ // _traverse(). Note that the recursive call on each child
1548
+ // returns the text content of the child and we add that
1549
+ // content to the content for this node. Also note that we
1550
+ // push the name of the property we're recursing over onto a
1551
+ // TraversalState stack.
1552
+ const value = node[key];
1553
+ if (value && typeof value === "object") {
1554
+ state._indexes.push(key);
1555
+ content += this._traverse(value, state, f);
1556
+ state._indexes.pop();
1557
+ }
1558
+ });
1559
+
1560
+ // Restore the stacks after recursing on the children
1561
+ state._currentNode = state._ancestors.pop();
1562
+ state._containers.pop();
1563
+
1564
+ // And finally call the traversal callback for this node. Note
1565
+ // that this is post-order traversal. We call the callback on the
1566
+ // way back up the tree, not on the way down. That way we already
1567
+ // know all the content contained within the node.
1568
+ f(node, state, content);
1569
+ } else if (Array.isArray(n)) {
1570
+ // If we were called on an array instead of a node, then
1571
+ // this is the code we use to recurse.
1572
+ const nodes = n;
1573
+
1574
+ // Push the array onto the stack. This will allow the
1575
+ // TraversalState object to locate siblings of this node.
1576
+ state._containers.push(nodes);
1577
+
1578
+ // Now loop through this array and recurse on each element in it.
1579
+ // Before recursing on an element, we push its array index on a
1580
+ // TraversalState stack so that the TraversalState sibling methods
1581
+ // can work. Note that TraversalState methods can alter the length
1582
+ // of the array, and change the index of the current node, so we
1583
+ // are careful here to test the array length on each iteration and
1584
+ // to reset the index when we pop the stack. Also note that we
1585
+ // concatentate the text content of the children.
1586
+ let index = 0;
1587
+ while (index < nodes.length) {
1588
+ state._indexes.push(index);
1589
+ content += this._traverse(nodes[index], state, f);
1590
+ // Casting to convince TypeScript that this is a number
1591
+ index = state._indexes.pop() + 1;
1592
+ }
1593
+
1594
+ // Pop the array off the stack. Note, however, that we do not call
1595
+ // the traversal callback on the array. That function is only
1596
+ // called for nodes, not arrays of nodes.
1597
+ state._containers.pop();
1598
+ }
1599
+
1600
+ // The _traverse() method always returns the text content of
1601
+ // this node and its children. This is the one piece of state that
1602
+ // is not tracked in the TraversalState object.
1603
+ return content;
1604
+ }
1605
+ }
1606
+
1607
+ // An instance of this class is passed to the callback function for
1608
+ // each node traversed. The class itself is not exported, but its
1609
+ // methods define the API available to the traversal callback.
1610
+
1611
+ /**
1612
+ * This class represents the state of a tree traversal. An instance is created
1613
+ * by the traverse() method of the TreeTransformer class to maintain the state
1614
+ * for that traversal, and the instance is passed to the traversal callback
1615
+ * function for each node that is traversed. This class is not intended to be
1616
+ * instantiated directly, but is exported so that its type can be used for
1617
+ * type annotaions.
1618
+ **/
1619
+ class TraversalState {
1620
+ // The constructor just stores the root node and creates empty stacks.
1621
+ constructor(root) {
1622
+ // The root node of the tree being traversed
1623
+ this.root = void 0;
1624
+ // These are internal state properties. Use the accessor methods defined
1625
+ // below instead of using these properties directly. Note that the
1626
+ // _containers and _indexes stacks can have two different types of
1627
+ // elements, depending on whether we just recursed on an array or on a
1628
+ // node. This is hard for TypeScript to deal with, so you'll see a number of
1629
+ // type casts through the any type when working with these two properties.
1630
+ this._currentNode = void 0;
1631
+ this._containers = void 0;
1632
+ this._indexes = void 0;
1633
+ this._ancestors = void 0;
1634
+ this.root = root;
1635
+
1636
+ // When the callback is called, this property will hold the
1637
+ // node that is currently being traversed.
1638
+ this._currentNode = null;
1639
+
1640
+ // This is a stack of the objects and arrays that we've
1641
+ // traversed through before reaching the currentNode.
1642
+ // It is different than the ancestors array.
1643
+ this._containers = new Stack();
1644
+
1645
+ // This stack has the same number of elements as the _containers
1646
+ // stack. The last element of this._indexes[] is the index of
1647
+ // the current node in the object or array that is the last element
1648
+ // of this._containers[]. If the last element of this._containers[] is
1649
+ // an array, then the last element of this stack will be a number.
1650
+ // Otherwise if the last container is an object, then the last index
1651
+ // will be a string property name.
1652
+ this._indexes = new Stack();
1653
+
1654
+ // This is a stack of the ancestor nodes of the current one.
1655
+ // It is different than the containers[] stack because it only
1656
+ // includes nodes, not arrays.
1657
+ this._ancestors = new Stack();
1658
+ }
1659
+
1660
+ /**
1661
+ * Return the current node in the traversal. Any time the traversal
1662
+ * callback is called, this method will return the name value as the
1663
+ * first argument to the callback.
1664
+ */
1665
+ currentNode() {
1666
+ return this._currentNode || this.root;
1667
+ }
1668
+
1669
+ /**
1670
+ * Return the parent of the current node, if there is one, or null.
1671
+ */
1672
+ parent() {
1673
+ return this._ancestors.top();
1674
+ }
1675
+
1676
+ /**
1677
+ * Return an array of ancestor nodes. The first element of this array is
1678
+ * the same as this.parent() and the last element is the root node. If we
1679
+ * are currently at the root node, the the returned array will be empty.
1680
+ * This method makes a copy of the internal state, so modifications to the
1681
+ * returned array have no effect on the traversal.
1682
+ */
1683
+ ancestors() {
1684
+ return this._ancestors.values();
1685
+ }
1686
+
1687
+ /**
1688
+ * Return the next sibling of this node, if it has one, or null otherwise.
1689
+ */
1690
+ nextSibling() {
1691
+ const siblings = this._containers.top();
1692
+
1693
+ // If we're at the root of the tree or if the parent is an
1694
+ // object instead of an array, then there are no siblings.
1695
+ // eslint-disable-next-line @typescript-eslint/strict-boolean-expressions
1696
+ if (!siblings || !Array.isArray(siblings)) {
1697
+ return null;
1698
+ }
1699
+
1700
+ // The top index is a number because the top container is an array
1701
+ const index = this._indexes.top();
1702
+ if (siblings.length > index + 1) {
1703
+ return siblings[index + 1];
1704
+ }
1705
+ return null; // There is no next sibling
1706
+ }
1707
+
1708
+ /**
1709
+ * Return the previous sibling of this node, if it has one, or null
1710
+ * otherwise.
1711
+ */
1712
+ previousSibling() {
1713
+ const siblings = this._containers.top();
1714
+
1715
+ // If we're at the root of the tree or if the parent is an
1716
+ // object instead of an array, then there are no siblings.
1717
+ // eslint-disable-next-line @typescript-eslint/strict-boolean-expressions
1718
+ if (!siblings || !Array.isArray(siblings)) {
1719
+ return null;
1720
+ }
1721
+
1722
+ // The top index is a number because the top container is an array
1723
+ const index = this._indexes.top();
1724
+ if (index > 0) {
1725
+ return siblings[index - 1];
1726
+ }
1727
+ return null; // There is no previous sibling
1728
+ }
1729
+
1730
+ /**
1731
+ * Remove the next sibling node (if there is one) from the tree. Returns
1732
+ * the removed sibling or null. This method makes it easy to traverse a
1733
+ * tree and concatenate adjacent text nodes into a single node.
1734
+ */
1735
+ removeNextSibling() {
1736
+ const siblings = this._containers.top();
1737
+ // eslint-disable-next-line @typescript-eslint/strict-boolean-expressions
1738
+ if (siblings && Array.isArray(siblings)) {
1739
+ // top index is a number because top container is an array
1740
+ const index = this._indexes.top();
1741
+ if (siblings.length > index + 1) {
1742
+ return siblings.splice(index + 1, 1)[0];
1743
+ }
1744
+ }
1745
+ return null;
1746
+ }
1747
+
1748
+ /**
1749
+ * Replace the current node in the tree with the specified nodes. If no
1750
+ * nodes are passed, this is a node deletion. If one node (or array) is
1751
+ * passed, this is a 1-for-1 replacement. If more than one node is passed
1752
+ * then this is a combination of deletion and insertion. The new node or
1753
+ * nodes will not be traversed, so this method can safely be used to
1754
+ * reparent the current node node beneath a new parent.
1755
+ *
1756
+ * This method throws an error if you attempt to replace the root node of
1757
+ * the tree.
1758
+ */
1759
+ replace(...replacements) {
1760
+ const parent = this._containers.top();
1761
+ // eslint-disable-next-line @typescript-eslint/strict-boolean-expressions
1762
+ if (!parent) {
1763
+ throw new PerseusError("Can't replace the root of the tree", Errors.Internal);
1764
+ }
1765
+
1766
+ // The top of the container stack is either an array or an object
1767
+ // and the top of the indexes stack is a corresponding array index
1768
+ // or object property. This is hard for TypeScript, so we have to do some
1769
+ // unsafe casting and be careful when we use which cast version
1770
+ if (Array.isArray(parent)) {
1771
+ const index = this._indexes.top();
1772
+ // For an array parent we just splice the new nodes in
1773
+ parent.splice(index, 1, ...replacements);
1774
+ // Adjust the index to account for the changed array length.
1775
+ // We don't want to traverse any of the newly inserted nodes.
1776
+ this._indexes.pop();
1777
+ this._indexes.push(index + replacements.length - 1);
1778
+ } else {
1779
+ const property = this._indexes.top();
1780
+ // For an object parent we care how many new nodes there are
1781
+ if (replacements.length === 0) {
1782
+ // Deletion
1783
+ delete parent[property];
1784
+ } else if (replacements.length === 1) {
1785
+ // Replacement
1786
+ parent[property] = replacements[0];
1787
+ } else {
1788
+ // Replace one node with an array of nodes
1789
+ parent[property] = replacements;
1790
+ }
1791
+ }
1792
+ }
1793
+
1794
+ /**
1795
+ * Returns true if the current node has a previous sibling and false
1796
+ * otherwise. If this method returns false, then previousSibling() will
1797
+ * return null, and goToPreviousSibling() will throw an error.
1798
+ */
1799
+ hasPreviousSibling() {
1800
+ return Array.isArray(this._containers.top()) && this._indexes.top() > 0;
1801
+ }
1802
+
1803
+ /**
1804
+ * Modify this traversal state object to have the state it would have had
1805
+ * when visiting the previous sibling. Note that you may want to use
1806
+ * clone() to make a copy before modifying the state object like this.
1807
+ * This mutator method is not typically used during ordinary tree
1808
+ * traversals, but is used by the Selector class for matching multi-node
1809
+ * selectors.
1810
+ */
1811
+ goToPreviousSibling() {
1812
+ if (!this.hasPreviousSibling()) {
1813
+ throw new PerseusError("goToPreviousSibling(): node has no previous sibling", Errors.Internal);
1814
+ }
1815
+ this._currentNode = this.previousSibling();
1816
+ // Since we know that we have a previous sibling, we know that
1817
+ // the value on top of the stack is a number, but we have to do
1818
+ // this unsafe cast because TypeScript doesn't know that.
1819
+ const index = this._indexes.pop();
1820
+ this._indexes.push(index - 1);
1821
+ }
1822
+
1823
+ /**
1824
+ * Returns true if the current node has an ancestor and false otherwise.
1825
+ * If this method returns false, then the parent() method will return
1826
+ * null and goToParent() will throw an error
1827
+ */
1828
+ hasParent() {
1829
+ return this._ancestors.size() !== 0;
1830
+ }
1831
+
1832
+ /**
1833
+ * Modify this object to look like it will look when we (later) visit the
1834
+ * parent node of this node. You should not modify the instance passed to
1835
+ * the tree traversal callback. Instead, make a copy with the clone()
1836
+ * method and modify that. This mutator method is not typically used
1837
+ * during ordinary tree traversals, but is used by the Selector class for
1838
+ * matching multi-node selectors that involve parent and ancestor
1839
+ * selectors.
1840
+ */
1841
+ goToParent() {
1842
+ if (!this.hasParent()) {
1843
+ throw new PerseusError("goToParent(): node has no ancestor", Errors.NotAllowed);
1844
+ }
1845
+ this._currentNode = this._ancestors.pop();
1846
+
1847
+ // We need to pop the containers and indexes stacks at least once
1848
+ // and more as needed until we restore the invariant that
1849
+ // this._containers.top()[this.indexes.top()] === this._currentNode
1850
+ //
1851
+ while (
1852
+ // eslint-disable-next-line @typescript-eslint/strict-boolean-expressions
1853
+ this._containers.size() && this._containers.top()[this._indexes.top()] !== this._currentNode) {
1854
+ this._containers.pop();
1855
+ this._indexes.pop();
1856
+ }
1857
+ }
1858
+
1859
+ /**
1860
+ * Return a new TraversalState object that is a copy of this one.
1861
+ * This method is useful in conjunction with the mutating methods
1862
+ * goToParent() and goToPreviousSibling().
1863
+ */
1864
+ clone() {
1865
+ const clone = new TraversalState(this.root);
1866
+ clone._currentNode = this._currentNode;
1867
+ clone._containers = this._containers.clone();
1868
+ clone._indexes = this._indexes.clone();
1869
+ clone._ancestors = this._ancestors.clone();
1870
+ return clone;
1871
+ }
1872
+
1873
+ /**
1874
+ * Returns true if this TraversalState object is equal to that
1875
+ * TraversalState object, or false otherwise. This method exists
1876
+ * primarily for use by our unit tests.
1877
+ */
1878
+ equals(that) {
1879
+ return this.root === that.root && this._currentNode === that._currentNode && this._containers.equals(that._containers) && this._indexes.equals(that._indexes) && this._ancestors.equals(that._ancestors);
1880
+ }
1881
+ }
1882
+
1883
+ /**
1884
+ * This class is an internal utility that just treats an array as a stack
1885
+ * and gives us a top() method so we don't have to write expressions like
1886
+ * `ancestors[ancestors.length-1]`. The values() method automatically
1887
+ * copies the internal array so we don't have to worry about client code
1888
+ * modifying our internal stacks. The use of this Stack abstraction makes
1889
+ * the TraversalState class simpler in a number of places.
1890
+ */
1891
+ class Stack {
1892
+ constructor(array) {
1893
+ this.stack = void 0;
1894
+ this.stack = array ? array.slice(0) : [];
1895
+ }
1896
+
1897
+ /** Push a value onto the stack. */
1898
+ push(v) {
1899
+ this.stack.push(v);
1900
+ }
1901
+
1902
+ /** Pop a value off of the stack. */
1903
+ pop() {
1904
+ // @ts-expect-error - TS2322 - Type 'T | undefined' is not assignable to type 'T'.
1905
+ return this.stack.pop();
1906
+ }
1907
+
1908
+ /** Return the top value of the stack without popping it. */
1909
+ top() {
1910
+ return this.stack[this.stack.length - 1];
1911
+ }
1912
+
1913
+ /** Return a copy of the stack as an array */
1914
+ values() {
1915
+ return this.stack.slice(0);
1916
+ }
1917
+
1918
+ /** Return the number of elements in the stack */
1919
+ size() {
1920
+ return this.stack.length;
1921
+ }
1922
+
1923
+ /** Return a string representation of the stack */
1924
+ toString() {
1925
+ return this.stack.toString();
1926
+ }
1927
+
1928
+ /** Return a shallow copy of the stack */
1929
+ clone() {
1930
+ return new Stack(this.stack);
1931
+ }
1932
+
1933
+ /**
1934
+ * Compare this stack to another and return true if the contents of
1935
+ * the two arrays are the same.
1936
+ */
1937
+ equals(that) {
1938
+ // eslint-disable-next-line @typescript-eslint/strict-boolean-expressions
1939
+ if (!that || !that.stack || that.stack.length !== this.stack.length) {
1940
+ return false;
1941
+ }
1942
+ for (let i = 0; i < this.stack.length; i++) {
1943
+ if (this.stack[i] !== that.stack[i]) {
1944
+ return false;
1945
+ }
1946
+ }
1947
+ return true;
1948
+ }
1949
+ }
1950
+
1951
+ // This file is processed by a Rollup plugin (replace) to inject the production
1952
+ // version number during the release build.
1953
+ // In dev, you'll never see the version number.
1954
+
1955
+ const libName = "@khanacademy/perseus-linter";
1956
+ const libVersion = "3.0.0";
1957
+ addLibraryVersionToPerseusDebug(libName, libVersion);
1958
+
1959
+ // Define the shape of the linter context object that is passed through the
1960
+ // tree with additional information about what we are checking.
1961
+ const linterContextProps = PropTypes.shape({
1962
+ contentType: PropTypes.string,
1963
+ highlightLint: PropTypes.bool,
1964
+ paths: PropTypes.arrayOf(PropTypes.string),
1965
+ stack: PropTypes.arrayOf(PropTypes.string)
1966
+ });
1967
+ const linterContextDefault = {
1968
+ contentType: "",
1969
+ highlightLint: false,
1970
+ paths: [],
1971
+ stack: []
1972
+ };
1973
+
1974
+ const allLintRules = AllRules.filter(r => r.severity < Rule.Severity.BULK_WARNING);
1975
+
1976
+ /**
1977
+ * Run the Perseus linter over the specified markdown parse tree,
1978
+ * with the specified context object, and
1979
+ * return a (possibly empty) array of lint warning objects. If the
1980
+ * highlight argument is true, this function also modifies the parse
1981
+ * tree to add "lint" nodes that can be visually rendered,
1982
+ * highlighting the problems for the user. The optional rules argument
1983
+ * is an array of Rule objects specifying which lint rules should be
1984
+ * applied to this parse tree. When omitted, a default set of rules is used.
1985
+ *
1986
+ * The context object may have additional properties that some lint
1987
+ * rules require:
1988
+ *
1989
+ * context.content is the source content string that was parsed to create
1990
+ * the parse tree.
1991
+ *
1992
+ * context.widgets is the widgets object associated
1993
+ * with the content string
1994
+ *
1995
+ * TODO: to make this even more general, allow the first argument to be
1996
+ * a string and run the parser over it in that case? (but ignore highlight
1997
+ * in that case). This would allow the one function to be used for both
1998
+ * online linting and batch linting.
1999
+ */
2000
+ function runLinter(tree, context, highlight, rules = allLintRules) {
2001
+ const warnings = [];
2002
+ const tt = new TreeTransformer(tree);
2003
+
2004
+ // The markdown parser often outputs adjacent text nodes. We
2005
+ // coalesce them before linting for efficiency and accuracy.
2006
+ tt.traverse((node, state, content) => {
2007
+ if (TreeTransformer.isTextNode(node)) {
2008
+ let next = state.nextSibling();
2009
+ while (TreeTransformer.isTextNode(next)) {
2010
+ // @ts-expect-error - TS2339 - Property 'content' does not exist on type 'TreeNode'. | TS2533 - Object is possibly 'null' or 'undefined'. | TS2339 - Property 'content' does not exist on type 'TreeNode'.
2011
+ node.content += next.content;
2012
+ state.removeNextSibling();
2013
+ next = state.nextSibling();
2014
+ }
2015
+ }
2016
+ });
2017
+
2018
+ // HTML tables are complicated, and the CSS we use in
2019
+ // ../components/lint.jsx to display lint does not work to
2020
+ // correctly position the lint indicators in the margin when the
2021
+ // lint is inside a table. So as a workaround we keep track of all
2022
+ // the lint that appears within a table and move it up to the
2023
+ // table element itself.
2024
+ //
2025
+ // It is not ideal to have to do this here,
2026
+ // but it is cleaner here than fixing up the lint during rendering
2027
+ // in perseus-markdown.jsx. If our lint display was simpler and
2028
+ // did not require indicators in the margin, this wouldn't be a
2029
+ // problem. Or, if we modified the lint display stuff so that
2030
+ // indicator positioning and tooltip display were both handled
2031
+ // with JavaScript (instead of pure CSS), then we could avoid this
2032
+ // issue too. But using JavaScript has its own downsides: there is
2033
+ // risk that the linter JavaScript would interfere with
2034
+ // widget-related Javascript.
2035
+ let tableWarnings = [];
2036
+ let insideTable = false;
2037
+
2038
+ // Traverse through the nodes of the parse tree. At each node, loop
2039
+ // through the array of lint rules and check whether there is a
2040
+ // lint violation at that node.
2041
+ tt.traverse((node, state, content) => {
2042
+ const nodeWarnings = [];
2043
+
2044
+ // If our rule is only designed to be tested against a particular
2045
+ // content type and we're not in that content type, we don't need to
2046
+ // consider that rule.
2047
+ const applicableRules = rules.filter(r => r.applies(context));
2048
+
2049
+ // Generate a stack so we can identify our position in the tree in
2050
+ // lint rules
2051
+ const stack = [...context.stack];
2052
+ stack.push(node.type);
2053
+ const nodeContext = _extends({}, context, {
2054
+ stack: stack.join(".")
2055
+ });
2056
+ applicableRules.forEach(rule => {
2057
+ const warning = rule.check(node, state, content, nodeContext);
2058
+ if (warning) {
2059
+ // The start and end locations are relative to this
2060
+ // particular node, and so are not generally very useful.
2061
+ // TODO: When the markdown parser saves the node
2062
+ // locations in the source string then we can add
2063
+ // these numbers to that one and get and absolute
2064
+ // character range that will be useful
2065
+ if (warning.start || warning.end) {
2066
+ warning.target = content.substring(warning.start, warning.end);
2067
+ }
2068
+
2069
+ // Add the warning to the list of all lint we've found
2070
+ warnings.push(warning);
2071
+
2072
+ // If we're going to be highlighting lint, then we also
2073
+ // need to keep track of warnings specific to this node.
2074
+ if (highlight) {
2075
+ nodeWarnings.push(warning);
2076
+ }
2077
+ }
2078
+ });
2079
+
2080
+ // If we're not highlighting lint in the tree, then we're done
2081
+ // traversing this node.
2082
+ if (!highlight) {
2083
+ return;
2084
+ }
2085
+
2086
+ // If the node we are currently at is a table, and there was lint
2087
+ // inside the table, then we want to add that lint here
2088
+ if (node.type === "table") {
2089
+ // eslint-disable-next-line @typescript-eslint/strict-boolean-expressions
2090
+ if (tableWarnings.length) {
2091
+ nodeWarnings.push(...tableWarnings);
2092
+ }
2093
+
2094
+ // We're not in a table anymore, and don't have to remember
2095
+ // the warnings for the table
2096
+ insideTable = false;
2097
+ tableWarnings = [];
2098
+ } else if (!insideTable) {
2099
+ // Otherwise, if we are not already inside a table, check
2100
+ // to see if we've entered one. Because this is a post-order
2101
+ // traversal we'll see the table contents before the table itself.
2102
+ // Note that once we're inside the table, we don't have to
2103
+ // do this check each time... We can just wait until we ascend
2104
+ // up to the table, then we'll know we're out of it.
2105
+ insideTable = state.ancestors().some(n => n.type === "table");
2106
+ }
2107
+
2108
+ // If we are inside a table and there were any warnings on
2109
+ // this node, then we need to save the warnings for display
2110
+ // on the table itself
2111
+ // eslint-disable-next-line @typescript-eslint/strict-boolean-expressions
2112
+ if (insideTable && nodeWarnings.length) {
2113
+ // @ts-expect-error - TS2345 - Argument of type 'any' is not assignable to parameter of type 'never'.
2114
+ tableWarnings.push(...nodeWarnings);
2115
+ }
2116
+
2117
+ // If there were any warnings on this node, and if we're highlighting
2118
+ // lint, then reparent the node so we can highlight it. Note that
2119
+ // a single node can have multiple warnings. If this happends we
2120
+ // concatenate the warnings and newline separate them. (The lint.jsx
2121
+ // component that displays the warnings may want to convert the
2122
+ // newlines into <br> tags.) We also provide a lint rule name
2123
+ // so that lint.jsx can link to a document that provides more details
2124
+ // on that particular lint rule. If there is more than one warning
2125
+ // we only link to the first rule, however.
2126
+ //
2127
+ // Note that even if we're inside a table, we still reparent the
2128
+ // linty node so that it can be highlighted. We just make a note
2129
+ // of whether this lint is inside a table or not.
2130
+ // eslint-disable-next-line @typescript-eslint/strict-boolean-expressions
2131
+ if (nodeWarnings.length) {
2132
+ nodeWarnings.sort((a, b) => {
2133
+ return a.severity - b.severity;
2134
+ });
2135
+ if (node.type !== "text" || nodeWarnings.length > 1) {
2136
+ // If the linty node is not a text node, or if there is more
2137
+ // than one warning on a text node, then reparent the entire
2138
+ // node under a new lint node and put the warnings there.
2139
+ state.replace({
2140
+ type: "lint",
2141
+ // @ts-expect-error - TS2345 - Argument of type '{ type: string; content: TreeNode; message: string; ruleName: any; blockHighlight: any; insideTable: boolean; severity: any; }' is not assignable to parameter of type 'TreeNode'.
2142
+ content: node,
2143
+ message: nodeWarnings.map(w => w.message).join("\n\n"),
2144
+ ruleName: nodeWarnings[0].rule,
2145
+ blockHighlight: nodeContext.blockHighlight,
2146
+ insideTable: insideTable,
2147
+ severity: nodeWarnings[0].severity
2148
+ });
2149
+ } else {
2150
+ //
2151
+ // Otherwise, it is a single warning on a text node, and we
2152
+ // only want to highlight the actual linty part of that string
2153
+ // of text. So we want to replace the text node with (in the
2154
+ // general case) three nodes:
2155
+ //
2156
+ // 1) A new text node that holds the non-linty prefix
2157
+ //
2158
+ // 2) A lint node that is the parent of a new text node
2159
+ // that holds the linty part
2160
+ //
2161
+ // 3) A new text node that holds the non-linty suffix
2162
+ //
2163
+ // If the lint begins and/or ends at the boundaries of the
2164
+ // original text node, then nodes 1 and/or 3 won't exist, of
2165
+ // course.
2166
+ //
2167
+ // Note that we could generalize this to work with multple
2168
+ // warnings on a text node as long as the warnings are
2169
+ // non-overlapping. Hopefully, though, multiple warnings in a
2170
+ // single text node will be rare in practice. Also, we don't
2171
+ // have a good way to display multiple lint indicators on a
2172
+ // single line, so keeping them combined in that case might
2173
+ // be the best thing, anyway.
2174
+ //
2175
+ // @ts-expect-error - TS2339 - Property 'content' does not exist on type 'TreeNode'.
2176
+ const _content = node.content; // Text nodes have content
2177
+ const warning = nodeWarnings[0]; // There is only one warning.
2178
+ // These are the lint boundaries within the content
2179
+ const start = warning.start || 0;
2180
+ const end = warning.end || _content.length;
2181
+ const prefix = _content.substring(0, start);
2182
+ const lint = _content.substring(start, end);
2183
+ const suffix = _content.substring(end);
2184
+ // TODO(FEI-5003): Give this a real type.
2185
+ const replacements = []; // What we'll replace the node with
2186
+
2187
+ // The prefix text node, if there is one
2188
+ if (prefix) {
2189
+ replacements.push({
2190
+ type: "text",
2191
+ content: prefix
2192
+ });
2193
+ }
2194
+
2195
+ // The lint node wrapped around the linty text
2196
+ replacements.push({
2197
+ type: "lint",
2198
+ content: {
2199
+ type: "text",
2200
+ content: lint
2201
+ },
2202
+ message: warning.message,
2203
+ ruleName: warning.rule,
2204
+ insideTable: insideTable,
2205
+ severity: warning.severity
2206
+ });
2207
+
2208
+ // The suffix node, if there is one
2209
+ if (suffix) {
2210
+ replacements.push({
2211
+ type: "text",
2212
+ content: suffix
2213
+ });
2214
+ }
2215
+
2216
+ // Now replace the lint text node with the one to three
2217
+ // nodes in the replacement array
2218
+ state.replace(...replacements);
2219
+ }
2220
+ }
2221
+ });
2222
+ return warnings;
2223
+ }
2224
+ function pushContextStack(context, name) {
2225
+ const stack = context.stack || [];
2226
+ return _extends({}, context, {
2227
+ stack: stack.concat(name)
2228
+ });
2229
+ }
2230
+
2231
+ export { Rule, libVersion, linterContextDefault, linterContextProps, pushContextStack, allLintRules as rules, runLinter };
2232
+ //# sourceMappingURL=index.js.map