UNPKG

2.65 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3exports.collectAlgorithmDiagnostics = void 0;
4const ecmarkdown_1 = require("ecmarkdown");
5const utils_1 = require("../utils");
6const algorithm_line_style_1 = require("./rules/algorithm-line-style");
7const algorithm_step_numbering_1 = require("./rules/algorithm-step-numbering");
8const algorithm_step_labels_1 = require("./rules/algorithm-step-labels");
9const for_each_element_1 = require("./rules/for-each-element");
10const algorithmRules = [
11 algorithm_line_style_1.default,
12 algorithm_step_numbering_1.default,
13 algorithm_step_labels_1.default,
14 for_each_element_1.default,
15];
16function composeObservers(...observers) {
17 return {
18 enter(node) {
19 var _a;
20 for (const observer of observers) {
21 (_a = observer.enter) === null || _a === void 0 ? void 0 : _a.call(observer, node);
22 }
23 },
24 exit(node) {
25 var _a;
26 for (const observer of observers) {
27 (_a = observer.exit) === null || _a === void 0 ? void 0 : _a.call(observer, node);
28 }
29 },
30 };
31}
32function collectAlgorithmDiagnostics(report, spec, mainSource, algorithms) {
33 for (const algorithm of algorithms) {
34 const element = algorithm.element;
35 const location = spec.locate(element);
36 if (!location)
37 continue;
38 const { source: importSource } = location;
39 if (location.endTag == null) {
40 // we'll warn for this in collect-tag-diagnostics; no need to do so here
41 continue;
42 }
43 // TODO this wrapper is maybe not necessary
44 const reporter = ({ ruleId, message, line, column }) => {
45 report({
46 type: 'contents',
47 ruleId,
48 message,
49 node: element,
50 nodeRelativeLine: line,
51 nodeRelativeColumn: column,
52 });
53 };
54 const algorithmSource = (importSource !== null && importSource !== void 0 ? importSource : mainSource).slice(location.startTag.endOffset, location.endTag.startOffset);
55 const observer = composeObservers(...algorithmRules.map(f => f(reporter, element, algorithmSource)));
56 let tree;
57 try {
58 tree = ecmarkdown_1.parseAlgorithm(algorithmSource);
59 }
60 catch (e) {
61 utils_1.warnEmdFailure(report, element, e);
62 }
63 if (tree != null) {
64 ecmarkdown_1.visit(tree, observer);
65 }
66 algorithm.tree = tree;
67 }
68}
69exports.collectAlgorithmDiagnostics = collectAlgorithmDiagnostics;