myst-cli
Version:
Command line tools for MyST
60 lines (59 loc) • 3.1 kB
JavaScript
import chalk from 'chalk';
import { warnings } from '../store/reducers.js';
import { selectCurrentProjectConfig } from '../store/selectors.js';
export function addWarningForFile(session, file, message, severity = 'warn', opts) {
var _a, _b, _c, _d, _e;
const line = ((_a = opts === null || opts === void 0 ? void 0 : opts.position) === null || _a === void 0 ? void 0 : _a.start.line) ? `:${opts === null || opts === void 0 ? void 0 : opts.position.start.line}` : '';
const column = ((_b = opts === null || opts === void 0 ? void 0 : opts.position) === null || _b === void 0 ? void 0 : _b.start.column) && ((_c = opts === null || opts === void 0 ? void 0 : opts.position) === null || _c === void 0 ? void 0 : _c.start.column) > 1
? `:${opts === null || opts === void 0 ? void 0 : opts.position.start.column}`
: '';
const note = (opts === null || opts === void 0 ? void 0 : opts.note) ? `\n ${chalk.reset.dim(opts.note)}` : '';
const url = (opts === null || opts === void 0 ? void 0 : opts.url) ? chalk.reset.dim(`\n See also: ${opts.url}\n`) : '';
const prefix = file ? `${file}${line}${column} ` : '';
const formatted = `${message}${note}${url}`;
if (opts === null || opts === void 0 ? void 0 : opts.ruleId) {
const config = selectCurrentProjectConfig(session.store.getState());
const handler = (_d = config === null || config === void 0 ? void 0 : config.error_rules) === null || _d === void 0 ? void 0 : _d.find((rule) => {
if (rule.key) {
return rule.id === opts.ruleId && rule.key === opts.key;
}
return rule.id === opts.ruleId;
});
if (handler) {
if (handler.severity === 'ignore') {
session.log.debug(`${prefix}${formatted}`);
return;
}
severity = (_e = handler.severity) !== null && _e !== void 0 ? _e : severity;
}
}
switch (severity) {
case 'info':
session.log.info(`ℹ️ ${prefix}${formatted}`);
break;
case 'error':
session.log.error(`⛔️ ${prefix}${formatted}`);
break;
case 'warn':
session.log.warn(`⚠️ ${prefix}${formatted}`);
break;
case 'debug':
default:
session.log.debug(`${prefix}${formatted}`);
break;
}
if (opts === null || opts === void 0 ? void 0 : opts.ruleId) {
session.log.debug(`To suppress this message, add rule: "${opts.ruleId}"${opts.key ? ` with key: "${opts.key}"` : ''} to "error_rules" in your project config`);
}
if (file) {
session.store.dispatch(warnings.actions.addWarning({
file,
message,
kind: severity,
url: opts === null || opts === void 0 ? void 0 : opts.url,
note: opts === null || opts === void 0 ? void 0 : opts.note,
position: opts === null || opts === void 0 ? void 0 : opts.position,
ruleId: opts === null || opts === void 0 ? void 0 : opts.ruleId,
}));
}
}