ecmarkup
Version:
Custom element definitions and core utilities for markup that specifies ECMAScript and related technologies.
70 lines (69 loc) • 2.67 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.collectAlgorithmDiagnostics = void 0;
const ecmarkdown_1 = require("ecmarkdown");
const utils_1 = require("../utils");
const algorithm_line_style_1 = require("./rules/algorithm-line-style");
const algorithm_step_numbering_1 = require("./rules/algorithm-step-numbering");
const algorithm_step_labels_1 = require("./rules/algorithm-step-labels");
const for_each_element_1 = require("./rules/for-each-element");
const algorithmRules = [
algorithm_line_style_1.default,
algorithm_step_numbering_1.default,
algorithm_step_labels_1.default,
for_each_element_1.default,
];
function composeObservers(...observers) {
return {
enter(node) {
var _a;
for (const observer of observers) {
(_a = observer.enter) === null || _a === void 0 ? void 0 : _a.call(observer, node);
}
},
exit(node) {
var _a;
for (const observer of observers) {
(_a = observer.exit) === null || _a === void 0 ? void 0 : _a.call(observer, node);
}
},
};
}
function collectAlgorithmDiagnostics(report, spec, mainSource, algorithms) {
for (const algorithm of algorithms) {
const element = algorithm.element;
const location = spec.locate(element);
if (!location)
continue;
const { source: importSource } = location;
if (location.endTag == null) {
// we'll warn for this in collect-tag-diagnostics; no need to do so here
continue;
}
// TODO this wrapper is maybe not necessary
const reporter = ({ ruleId, message, line, column }) => {
report({
type: 'contents',
ruleId,
message,
node: element,
nodeRelativeLine: line,
nodeRelativeColumn: column,
});
};
const algorithmSource = (importSource !== null && importSource !== void 0 ? importSource : mainSource).slice(location.startTag.endOffset, location.endTag.startOffset);
const observer = composeObservers(...algorithmRules.map(f => f(reporter, element, algorithmSource)));
let tree;
try {
tree = (0, ecmarkdown_1.parseAlgorithm)(algorithmSource);
}
catch (e) {
(0, utils_1.warnEmdFailure)(report, element, e);
}
if (tree != null) {
(0, ecmarkdown_1.visit)(tree, observer);
}
algorithm.tree = tree;
}
}
exports.collectAlgorithmDiagnostics = collectAlgorithmDiagnostics;