eslint-plugin-o1js
Version:
o1js rules for ESLint
80 lines • 3.65 kB
JavaScript
;
const typescript_estree_1 = require("@typescript-eslint/typescript-estree");
const node_utils_1 = require("../utils/node-utils");
const ast_utils_1 = require("../utils/ast-utils");
const selectors_1 = require("../utils/selectors");
const rule = {
meta: {
messages: {
noJSONFunctionInCircuit: 'JavaScript JSON function usage should be avoided in a circuit. The resulting values do not make it into the circuit.',
},
schema: [],
type: 'suggestion',
docs: {
description: 'JavaScript JSON function usage should be avoided in a circuit. The resulting values do not make it into the circuit.',
recommended: 'warn',
},
},
create(context) {
const bannedImports = new Set(['JSON']);
const bannedFunctions = new Set(['stringify', 'parse']);
const o1CircuitMap = new Map();
const jsonSet = new Set();
const callees = {};
const callStack = [];
const currentFunction = () => callStack[callStack.length - 1];
function callsJSON(functionName) {
var _a;
return (jsonSet.has(functionName) || !!((_a = callees[functionName]) === null || _a === void 0 ? void 0 : _a.some(callsJSON)));
}
return {
'Program:exit': function () {
for (const circuitNode of o1CircuitMap.values()) {
(0, typescript_estree_1.simpleTraverse)(circuitNode, {
enter: (node) => {
if ((0, node_utils_1.isCallExpression)(node) &&
(0, ast_utils_1.isBannedCallExpression)(node, bannedImports, bannedFunctions)) {
context.report({
messageId: 'noJSONFunctionInCircuit',
loc: node.loc,
});
}
if ((0, node_utils_1.isCallExpression)(node) &&
(0, node_utils_1.isIdentifier)(node.callee) &&
callsJSON(node.callee.name)) {
context.report({
messageId: `noJSONFunctionInCircuit`,
loc: node.loc,
});
}
},
});
}
},
':function'(node) {
callStack.push((0, ast_utils_1.getFunctionName)(node));
},
':function:exit'() {
callStack.pop();
},
[selectors_1.CIRCUIT_METHOD_DECORATOR]: function (circuitMethodNode) {
const functionName = (0, ast_utils_1.getFunctionName)(circuitMethodNode);
if (functionName)
o1CircuitMap.set(functionName, circuitMethodNode);
},
CallExpression(node) {
const functionName = currentFunction();
if (functionName &&
(0, ast_utils_1.isBannedCallExpression)(node, bannedImports, bannedFunctions)) {
jsonSet.add(functionName);
}
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-json-functions-in-circuit.js.map