UNPKG

eslint-plugin-o1js

Version:

o1js rules for ESLint

79 lines 3.37 kB
"use strict"; const typescript_estree_1 = require("@typescript-eslint/typescript-estree"); const ast_utils_1 = require("../utils/ast-utils"); const node_utils_1 = require("../utils/node-utils"); const selectors_1 = require("../utils/selectors"); const rule = { meta: { messages: { noTernaryInCircuit: 'A "ternary" statement should not be used in a circuit. Please use "Circuit.if" instead.', }, schema: [], type: 'suggestion', docs: { description: 'A "ternary" statement should not be used in a circuit. Please use "Circuit.if" instead.', recommended: 'warn', }, }, create(context) { const o1CircuitMap = new Map(); const ternarySet = new Set(); const callees = {}; const callStack = []; const currentFunction = () => callStack[callStack.length - 1]; function callsTernary(functionName) { var _a; return (ternarySet.has(functionName) || !!((_a = callees[functionName]) === null || _a === void 0 ? void 0 : _a.some(callsTernary))); } return { 'Program:exit': function () { for (const circuitNode of o1CircuitMap.values()) { (0, typescript_estree_1.simpleTraverse)(circuitNode, { enter: (node) => { if ((0, node_utils_1.isConditionalExpression)(node)) { context.report({ messageId: 'noTernaryInCircuit', loc: node.loc, }); } if ((0, node_utils_1.isCallExpression)(node) && (0, node_utils_1.isIdentifier)(node.callee) && callsTernary(node.callee.name)) { context.report({ messageId: `noTernaryInCircuit`, loc: node.loc, }); } }, }); } }, [selectors_1.CIRCUIT_METHOD_DECORATOR]: function (circuitMethodNode) { const functionName = (0, ast_utils_1.getFunctionName)(circuitMethodNode); if (functionName) o1CircuitMap.set(functionName, circuitMethodNode); }, ':function'(node) { callStack.push((0, ast_utils_1.getFunctionName)(node)); }, ':function:exit'() { callStack.pop(); }, ConditionalExpression() { const functionName = currentFunction(); if (functionName) ternarySet.add(functionName); }, CallExpression(node) { const functionName = currentFunction(); if (functionName && (0, node_utils_1.isIdentifier)(node.callee)) { const currentCallees = callees[functionName] || (callees[functionName] = []); currentCallees.push(node.callee.name); } }, }; }, }; module.exports = rule; //# sourceMappingURL=no-ternary-in-circuit.js.map