Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/cli.cr
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ module Mint
register_sub_command docs, type: Docs
register_sub_command loc, type: Loc
register_sub_command ls, type: Ls
register_sub_command lint, type: Lint

def run
execute "Help" do
Expand Down
69 changes: 69 additions & 0 deletions src/commands/lint.cr
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
module Mint
class Cli < Admiral::Command
class Lint < Admiral::Command
include Command

define_help description: "Lints the project for syntax and type errors."

define_flag json : Bool,
description: "Output errors to a JSON file",
default: false,
required: false

def run
execute "Linting" do
lint
end
end

def lint
sources = [] of String
errors = [] of Exception

ast =
Ast.new
.merge(Core.ast)

begin
sources =
Dir.glob(SourceFiles.all)
rescue ex
errors << ex
end

sources.reduce(ast) do |memo, file|
begin
parsed =
Parser.parse(file)

memo.merge(parsed)
rescue ex
errors << ex
end
memo
end

if errors.empty?
type_checker =
TypeChecker.new(ast)

loop do
type_checker.check
rescue ex
errors << ex
else
break
end
end

if flags.json
puts errors.compact_map(&.message.presence).to_json
else
errors.each { |error| puts error }
end

exit(errors.empty? ? 0 : 1)
end
end
end
end