UNPKG

@slippy-lint/slippy

Version:

A simple but powerful linter for Solidity

41 lines 1.5 kB
import { NonterminalKind, TerminalKind } from "@nomicfoundation/slang/cst"; export const ImportsOnTop = { name: "imports-on-top", recommended: true, create: function () { return new ImportsOnTopRule(this.name); }, }; class ImportsOnTopRule { constructor(name) { this.name = name; } run({ file }) { const diagnostics = []; const cursor = file.createTreeCursor(); let hasSeenNonImport = false; while (cursor.goToNextNonterminalWithKind(NonterminalKind.SourceUnitMember)) { if (!cursor.goToFirstChild()) { continue; } if (cursor.node.kind === NonterminalKind.ImportDirective) { if (hasSeenNonImport) { cursor.goToNextTerminalWithKind(TerminalKind.ImportKeyword); // If we have seen a non-import directive before this import, diagnostics.push({ rule: this.name, message: "Imports should be at the top of the file.", sourceId: file.id, line: cursor.textRange.start.line, column: cursor.textRange.start.column, }); } } else if (cursor.node.kind !== NonterminalKind.PragmaDirective) { hasSeenNonImport = true; } } return diagnostics; } } //# sourceMappingURL=imports-on-top.js.map