-
-
Notifications
You must be signed in to change notification settings - Fork 135
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #406 from Daniel-Knights/lint-command-feature
- Loading branch information
Showing
2 changed files
with
70 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |