Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement lint command feature #406

Merged
merged 11 commits into from Apr 26, 2021
1 change: 1 addition & 0 deletions src/cli.cr
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
@@ -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
gdotdesign marked this conversation as resolved.
Show resolved Hide resolved

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