/** @import * as AST from 'estree'; @import { CompileError } from '../types/index'; */ /** * Report an error at `node`: record it in `errors`, or throw it. `message` is * the error's entry, such as `TSRX_ERRORS.UNCLOSED_TAG('div')`, with its code, * or its text, with `code`. A warning is only ever recorded: without `errors` * it is dropped, so it never fails a compile that doesn't collect. * @param {string | { code: string | undefined, message: string }} message * @param {string | null} filename * @param {AST.Node | AST.NodeWithLocation} node * @param {CompileError[]} [errors] * @param {AST.CommentWithLocation[]} [comments] * @param {string} [code] TSRX's own code, or TypeScript's for a mistake TypeScript also reports * @param {CompileError['severity']} [severity] * @returns {void} */ export function error(message, filename, node, errors, comments, code, severity) { if (errors && comments && is_error_suppressed(node, comments)) { return; } if (!errors && severity === 'warning') { return; } if (typeof message !== 'string') { code ??= message.code; message = message.message; } const error = /** @type {CompileError} */ (new Error(message)); // same as the acorn compiler error error.pos = node.start ?? undefined; error.raisedAt = node.end ?? undefined; // custom properties error.fileName = filename; error.code = code; error.severity = severity; error.end = node.end ?? undefined; error.loc = !node.loc ? undefined : { start: { line: node.loc.start.line, column: node.loc.start.column, }, end: { line: node.loc.end.line, column: node.loc.end.column, }, }; if (errors) { error.type = 'usage'; errors.push(error); return; } error.type = 'fatal'; throw error; } /** * @param {AST.CommentWithLocation} comment * @return {boolean} */ function is_error_suppress_comment(comment) { const text = comment.value.trim(); return text.startsWith('@tsrx-ignore') || text.startsWith('@tsrx-expect-error'); } /** * Whether a `// @tsrx-ignore` or `// @tsrx-expect-error` comment on the line * before `node` suppresses the errors on it. * @param {AST.Node | AST.NodeWithLocation} node * @param {AST.CommentWithLocation[]} comments */ export function is_error_suppressed(node, comments) { if (node.loc) { const node_start_line = node.loc.start.line; for (const comment of comments) { if (comment.type === 'Line' && comment.loc.start.line === node_start_line - 1) { if (is_error_suppress_comment(comment)) { return true; } } } } return false; }