@slippy-lint/slippy
Version:
A simple but powerful linter for Solidity
58 lines • 2 kB
JavaScript
import * as z from "zod";
import { TerminalKind } from "@nomicfoundation/slang/cst";
const ConfigSchema = z.enum(["always", "never"]).default("always");
export const ExplicitTypes = {
name: "explicit-types",
recommended: true,
parseConfig: (config) => {
return ConfigSchema.parse(config);
},
create: function (config) {
return new ExplicitTypesRule(this.name, config);
},
};
class ExplicitTypesRule {
constructor(name, config) {
this.name = name;
this.config = config;
}
run({ file }) {
const results = [];
const cursor = file.createTreeCursor();
while (cursor.goToNextTerminalWithKinds([
TerminalKind.UintKeyword,
TerminalKind.IntKeyword,
TerminalKind.UfixedKeyword,
TerminalKind.FixedKeyword,
])) {
const typeText = cursor.node.unparse();
if (this.config === "always") {
if (typeText === "uint" ||
typeText === "int" ||
typeText === "ufixed" ||
typeText === "fixed") {
results.push(this._buildLintResult(typeText, file.id, cursor.textRange));
}
}
else {
if (typeText === "uint256" ||
typeText === "int256" ||
typeText === "ufixed256x18" ||
typeText === "fixed128x18") {
results.push(this._buildLintResult(typeText, file.id, cursor.textRange));
}
}
}
return results;
}
_buildLintResult(typeText, sourceId, textRange) {
return {
rule: this.name,
sourceId,
message: `${this.config === "always" ? "implicit" : "explicit"} type '${typeText}' should be avoided`,
line: textRange.start.line,
column: textRange.start.column,
};
}
}
//# sourceMappingURL=explicit-types.js.map