Skip to content

Commit

Permalink
Merge pull request #406 from Daniel-Knights/lint-command-feature
Browse files Browse the repository at this point in the history
  • Loading branch information
gdotdesign committed Apr 26, 2021
2 parents 581100c + 4555561 commit 538dc78
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 0 deletions.
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

0 comments on commit 538dc78

Please sign in to comment.