metal-soy-critic
Version:
A metal-soy code validation utility.
31 lines (30 loc) • 1.23 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
const soy_helpers_1 = require("./soy-helpers");
const util_1 = require("./util");
const chalk = require("chalk");
function formatMessage(node, name) {
return `$${name} - Line ${node.mark.start.line}`;
}
function validateTernaryElvis(soyContext) {
const references = [];
soyContext.visit({
Ternary(node) {
if (soy_helpers_1.isFunctionCall(node.condition) && node.condition.name === 'isNonnull') {
const firstArg = node.condition.body[0];
if (soy_helpers_1.isReference(firstArg)) {
const referenceName = firstArg.name;
if (soy_helpers_1.isReference(node.left) && node.left.name === referenceName) {
references.push(formatMessage(node, referenceName));
}
}
}
}
});
if (!references.length) {
return util_1.toResult(true);
}
return util_1.toResult(false, `Use the ${chalk.yellow('Elvis (?:)')} operator instead of a ternary for ${chalk.yellow('default')} values:\n\n` +
util_1.joinErrors(references));
}
exports.default = validateTernaryElvis;