metal-soy-critic
Version:
A metal-soy code validation utility.
30 lines (29 loc) • 1.13 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
const util_1 = require("./util");
const chalk = require("chalk");
const paramDeclarationRegex = /\s+}/;
function matchTuple(node, raw) {
return [node, raw.match(paramDeclarationRegex)];
}
function formatMessage(node, match) {
const { input = "" } = match;
return `Line ${node.mark.end.line}: ${input.replace(match[0], chalk.inverse(match[0]))}`;
}
function paramDeclarations(soyContext) {
return soyContext.ast.body
.map(template => template.params)
.reduce((res, next) => [...res, ...next])
.map(node => matchTuple(node, soyContext.getRaw(node)))
.filter(([_, match]) => !!match)
.map(([node, match]) => formatMessage(node, match));
}
function validateWhitespace(soyContext) {
const badLines = paramDeclarations(soyContext);
if (badLines.length) {
return util_1.toResult(false, `There seems to be extra ${chalk.yellow('whitespace')} on these lines:\n\n` +
util_1.joinErrors(badLines));
}
return util_1.toResult(true);
}
exports.default = validateWhitespace;