UNPKG

@slippy-lint/slippy

Version:

A simple but powerful linter for Solidity

55 lines 1.68 kB
import { Query } from "@nomicfoundation/slang/cst"; import * as z from "zod"; import { ignoreLeadingTrivia } from "../slang/trivia.js"; function isValidQuery(query) { try { Query.create(query); return true; } catch { return false; } } const ConfigSchema = z .array(z.strictObject({ query: z.string().refine(isValidQuery, { error: "Invalid Slang query", }), message: z.string(), })) .default([]); export const NoRestrictedSyntax = { name: "no-restricted-syntax", recommended: true, parseConfig: (config) => ConfigSchema.parse(config), create: function (config) { return new NoRestrictedSyntaxRule(this.name, config); }, }; class NoRestrictedSyntaxRule { constructor(name, config) { this.name = name; this.config = config; } run({ file }) { const diagnostics = []; const cursor = file.createTreeCursor(); for (const { query, message } of this.config) { const matches = cursor.query([Query.create(query)]); for (const match of matches) { const textRangeCursor = match.root.spawn(); ignoreLeadingTrivia(textRangeCursor); const textRange = textRangeCursor.textRange; diagnostics.push({ rule: this.name, sourceId: file.id, line: textRange.start.line, column: textRange.start.column, message: `Restricted syntax: ${message}`, }); } } return diagnostics; } } //# sourceMappingURL=no-restricted-syntax.js.map