ecmarkup
Version:
Custom element definitions and core utilities for markup that specifies ECMAScript and related technologies.
74 lines (73 loc) • 3.22 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.collectHeaderDiagnostics = collectHeaderDiagnostics;
const Clause_1 = require("../Clause");
const utils_1 = require("../utils");
const ruleId = 'header-format';
function collectHeaderDiagnostics(report, headers) {
for (const { element, contents } of headers) {
if ((0, Clause_1.extractStructuredHeader)(element) !== null) {
// these will be handled by header-parser.ts
continue;
}
if (element.getAttribute('type') === 'built-in function') {
// these will be handled by Clause.ts
continue;
}
if (!/\(.*\)$/.test(contents) || / Operator \( `[^`]+` \)$/.test(contents)) {
continue;
}
const name = contents.substring(0, contents.indexOf('('));
const params = contents.substring(contents.indexOf('(') + 1, contents.length - 1);
if (!/[\S] $/.test(name)) {
const { line, column } = (0, utils_1.offsetToLineAndColumn)(contents, name.length - 1);
report({
type: 'contents',
ruleId,
message: 'expected header to have a single space before the argument list',
node: element,
nodeRelativeLine: line,
nodeRelativeColumn: column,
});
}
const nameMatches = [
// Runtime Semantics: Foo
/^(Runtime|Static) Semantics: [A-Z][A-Za-z0-9/]*\s*$/,
// Number::foo
/^[A-Z][A-Za-z0-9]*::[a-z][A-Za-z0-9]*\s*$/,
// [[GetOwnProperty]]
/^\[\[[A-Z][A-Za-z0-9]*\]\]\s*$/,
// ForIn/OfHeadEvaluation
/^[A-Za-z][A-Za-z0-9/]*\s*$/,
Clause_1.VALID_BUILTIN_NAME_REGEX,
].some(r => r.test(name));
if (!nameMatches) {
const { line, column } = (0, utils_1.offsetToLineAndColumn)(contents, 0);
let message = `expected operation to have a name like 'Example', 'Runtime Semantics: Foo', 'Example.prop', etc, but found ${JSON.stringify(name)}`;
const oldSymbolMatch = name.match(/@@([a-z][a-zA-Z]+)/);
if (oldSymbolMatch != null) {
message = `found use of unsupported legacy well-known Symbol notation ${oldSymbolMatch[0]}; use %Symbol.${oldSymbolMatch[1]}% instead`;
}
report({
type: 'contents',
ruleId,
message,
node: element,
nodeRelativeLine: line,
nodeRelativeColumn: column,
});
}
const paramsMatches = (0, Clause_1.parseParams)(params);
if (!paramsMatches) {
const { line, column } = (0, utils_1.offsetToLineAndColumn)(contents, name.length);
report({
type: 'contents',
ruleId,
message: `expected parameter list to look like '( _a_ [ , _b_ ] )', '( _foo_, _bar_, ..._baz_ )', '( _foo_, … , _bar_ )', or '( . . . )'`,
node: element,
nodeRelativeLine: line,
nodeRelativeColumn: column,
});
}
}
}