UNPKG

@slippy-lint/slippy

Version:

A simple but powerful linter for Solidity

39 lines 1.56 kB
import { StateVariableDefinition } from "@nomicfoundation/slang/ast"; import { assertNonterminalNode, NonterminalKind, } from "@nomicfoundation/slang/cst"; import { isConstant, isImmutable, isPrivate, } from "../slang/state-variables.js"; export const PrivateVars = { name: "private-vars", recommended: false, create: function () { return new PrivateVarsRule(this.name); }, }; class PrivateVarsRule { constructor(name) { this.name = name; } run({ file }) { const diagnostics = []; const cursor = file.createTreeCursor(); while (cursor.goToNextNonterminalWithKind(NonterminalKind.StateVariableDefinition)) { assertNonterminalNode(cursor.node); const stateVariable = new StateVariableDefinition(cursor.node); while (cursor.node.id !== stateVariable.name.id && cursor.goToNext()) { // Move to the next node until we find the name of the state variable } if (!isConstant(stateVariable) && !isImmutable(stateVariable) && !isPrivate(stateVariable)) { diagnostics.push({ rule: this.name, sourceId: file.id, message: `State variable '${stateVariable.name.unparse()}' should be private`, line: cursor.textRange.start.line, column: cursor.textRange.start.column, }); } } return diagnostics; } } //# sourceMappingURL=private-vars.js.map