From 8068edf8892e7cd02727c990bb640c7a97d14b08 Mon Sep 17 00:00:00 2001 From: Dan Knights Date: Sat, 24 Apr 2021 20:07:21 +0100 Subject: [PATCH 01/11] lint command first implementation --- src/cli.cr | 1 + src/commands/lint.cr | 80 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 81 insertions(+) create mode 100644 src/commands/lint.cr diff --git a/src/cli.cr b/src/cli.cr index 8bac8cf88..2976bfd88 100644 --- a/src/cli.cr +++ b/src/cli.cr @@ -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 diff --git a/src/commands/lint.cr b/src/commands/lint.cr new file mode 100644 index 000000000..fa53da241 --- /dev/null +++ b/src/commands/lint.cr @@ -0,0 +1,80 @@ +module Mint + class Cli < Admiral::Command + class Lint < Admiral::Command + include Command + + define_help description: "Lints the project" + + define_flag json : Bool, + description: "Output errors to a JSON file", + default: false, + required: false + + define_flag output : String, + description: "The output filename", + default: "lint.json", + required: false, + short: "o" + + def run + execute "Linting" do + lint + end + end + + def lint + json = + MintJson.parse_current + + sources = + Dir.glob(SourceFiles.all) + + ast = + Ast.new + .merge(Core.ast) + + runtime = + Assets.read("runtime.js") + + errors = [] of String + + terminal.measure " #{ARROW} Parsing #{sources.size} source files... " do + sources.reduce(ast) do |memo, file| + parsed = Parser.parse(file) + + if memo + memo.merge parsed + end + rescue ex + ex_handler errors, ex + end + end + + type_checker = + TypeChecker.new(ast) + + terminal.measure " #{ARROW} Type checking: " do + type_checker.check + rescue ex + ex_handler errors, ex + end + + if flags.json + File.write(flags.output, errors) + end + + if errors.size > 0 + exit 1 + end + end + + def ex_handler(errors, ex) + if ex.message.is_a?(String) + errors << ex.message.as(String).to_json + end + + puts ex + end + end + end +end From 2f4af103b2dc833baddfdaea02a29624619867da Mon Sep 17 00:00:00 2001 From: Dan Knights Date: Sat, 24 Apr 2021 20:40:29 +0100 Subject: [PATCH 02/11] remove unnecessary runtime var --- src/commands/lint.cr | 3 --- 1 file changed, 3 deletions(-) diff --git a/src/commands/lint.cr b/src/commands/lint.cr index fa53da241..4b6fed4ba 100644 --- a/src/commands/lint.cr +++ b/src/commands/lint.cr @@ -33,9 +33,6 @@ module Mint Ast.new .merge(Core.ast) - runtime = - Assets.read("runtime.js") - errors = [] of String terminal.measure " #{ARROW} Parsing #{sources.size} source files... " do From fa753302e205fdb2f365e448b73f736fbebde210 Mon Sep 17 00:00:00 2001 From: Dan Knights Date: Sun, 25 Apr 2021 07:44:42 +0100 Subject: [PATCH 03/11] lint: update description, catch mint.json errors, remove terminal.measure, allow for multiple type-errors --- src/commands/lint.cr | 50 +++++++++++++++++++++++++------------------- 1 file changed, 29 insertions(+), 21 deletions(-) diff --git a/src/commands/lint.cr b/src/commands/lint.cr index 4b6fed4ba..a15448c3e 100644 --- a/src/commands/lint.cr +++ b/src/commands/lint.cr @@ -3,7 +3,7 @@ module Mint class Lint < Admiral::Command include Command - define_help description: "Lints the project" + define_help description: "Lints the project for syntax and type errors." define_flag json : Bool, description: "Output errors to a JSON file", @@ -23,37 +23,45 @@ module Mint end def lint - json = - MintJson.parse_current + errors = [] of String + sources = [] of String - sources = - Dir.glob(SourceFiles.all) + begin + sources = + Dir.glob(SourceFiles.all) + rescue ex + ex_handler errors, ex + end ast = Ast.new .merge(Core.ast) - errors = [] of String - - terminal.measure " #{ARROW} Parsing #{sources.size} source files... " do - sources.reduce(ast) do |memo, file| - parsed = Parser.parse(file) + sources.reduce(ast) do |memo, file| + parsed = Parser.parse(file) - if memo - memo.merge parsed - end - rescue ex - ex_handler errors, ex + if memo + memo.merge parsed end + rescue ex + ex_handler errors, ex end - type_checker = - TypeChecker.new(ast) + if errors.size == 0 + type_checker = + TypeChecker.new(ast) - terminal.measure " #{ARROW} Type checking: " do - type_checker.check - rescue ex - ex_handler errors, ex + done = false + + while !done + begin + type_checker.check + rescue ex + ex_handler errors, ex + else + done = true + end + end end if flags.json From a564d143eeab33bc73dca69419380cb33e7906fc Mon Sep 17 00:00:00 2001 From: Dan Knights Date: Sun, 25 Apr 2021 07:49:18 +0100 Subject: [PATCH 04/11] move assignment to the top --- src/commands/lint.cr | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/commands/lint.cr b/src/commands/lint.cr index a15448c3e..a8c692f75 100644 --- a/src/commands/lint.cr +++ b/src/commands/lint.cr @@ -26,6 +26,10 @@ module Mint errors = [] of String sources = [] of String + ast = + Ast.new + .merge(Core.ast) + begin sources = Dir.glob(SourceFiles.all) @@ -33,10 +37,6 @@ module Mint ex_handler errors, ex end - ast = - Ast.new - .merge(Core.ast) - sources.reduce(ast) do |memo, file| parsed = Parser.parse(file) From ebf032ea5c150e5503bcc208c197d8f0894c109c Mon Sep 17 00:00:00 2001 From: Dan Knights Date: Sun, 25 Apr 2021 18:43:10 +0100 Subject: [PATCH 05/11] output to stdout instead --- src/commands/lint.cr | 16 ++++------------ 1 file changed, 4 insertions(+), 12 deletions(-) diff --git a/src/commands/lint.cr b/src/commands/lint.cr index a8c692f75..2fe688cc0 100644 --- a/src/commands/lint.cr +++ b/src/commands/lint.cr @@ -10,12 +10,6 @@ module Mint default: false, required: false - define_flag output : String, - description: "The output filename", - default: "lint.json", - required: false, - short: "o" - def run execute "Linting" do lint @@ -23,8 +17,8 @@ module Mint end def lint - errors = [] of String sources = [] of String + errors = [] of Exception ast = Ast.new @@ -47,7 +41,7 @@ module Mint ex_handler errors, ex end - if errors.size == 0 + if errors.empty? type_checker = TypeChecker.new(ast) @@ -65,7 +59,7 @@ module Mint end if flags.json - File.write(flags.output, errors) + puts errors.compact_map(&.message.presence).to_json end if errors.size > 0 @@ -74,9 +68,7 @@ module Mint end def ex_handler(errors, ex) - if ex.message.is_a?(String) - errors << ex.message.as(String).to_json - end + errors << ex puts ex end From b18d1e180b8fe9173f15239b721d2833cb2ed7db Mon Sep 17 00:00:00 2001 From: Dan Knights Date: Mon, 26 Apr 2021 06:32:24 +0100 Subject: [PATCH 06/11] exit ternary --- src/commands/lint.cr | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/src/commands/lint.cr b/src/commands/lint.cr index 2fe688cc0..c2002e213 100644 --- a/src/commands/lint.cr +++ b/src/commands/lint.cr @@ -62,9 +62,7 @@ module Mint puts errors.compact_map(&.message.presence).to_json end - if errors.size > 0 - exit 1 - end + exit(errors.empty? ? 0 : 1) end def ex_handler(errors, ex) From c21806066e681a6e05344dfe094d1624826c4e00 Mon Sep 17 00:00:00 2001 From: Dan Knights Date: Mon, 26 Apr 2021 09:30:23 +0100 Subject: [PATCH 07/11] print all errors --- src/commands/lint.cr | 14 +++++--------- 1 file changed, 5 insertions(+), 9 deletions(-) diff --git a/src/commands/lint.cr b/src/commands/lint.cr index c2002e213..8b56c06c7 100644 --- a/src/commands/lint.cr +++ b/src/commands/lint.cr @@ -28,7 +28,7 @@ module Mint sources = Dir.glob(SourceFiles.all) rescue ex - ex_handler errors, ex + errors << ex end sources.reduce(ast) do |memo, file| @@ -38,7 +38,7 @@ module Mint memo.merge parsed end rescue ex - ex_handler errors, ex + errors << ex end if errors.empty? @@ -51,7 +51,7 @@ module Mint begin type_checker.check rescue ex - ex_handler errors, ex + errors << ex else done = true end @@ -60,16 +60,12 @@ module Mint if flags.json puts errors.compact_map(&.message.presence).to_json + else + puts errors end exit(errors.empty? ? 0 : 1) end - - def ex_handler(errors, ex) - errors << ex - - puts ex - end end end end From 9cc36f2050db9b1a460713e1d6cbc221ecbc436c Mon Sep 17 00:00:00 2001 From: Dan Knights Date: Mon, 26 Apr 2021 09:34:57 +0100 Subject: [PATCH 08/11] print errors with loop --- src/commands/lint.cr | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/commands/lint.cr b/src/commands/lint.cr index 8b56c06c7..130b6518e 100644 --- a/src/commands/lint.cr +++ b/src/commands/lint.cr @@ -61,7 +61,7 @@ module Mint if flags.json puts errors.compact_map(&.message.presence).to_json else - puts errors + errors.each { |error| puts error } end exit(errors.empty? ? 0 : 1) From 890c231e5f60b2a0afce95a1b85af57898a0149d Mon Sep 17 00:00:00 2001 From: Sijawusz Pur Rahnama Date: Mon, 26 Apr 2021 14:03:52 +0200 Subject: [PATCH 09/11] Update src/commands/lint.cr --- src/commands/lint.cr | 16 ++++++---------- 1 file changed, 6 insertions(+), 10 deletions(-) diff --git a/src/commands/lint.cr b/src/commands/lint.cr index 130b6518e..ee600b70c 100644 --- a/src/commands/lint.cr +++ b/src/commands/lint.cr @@ -45,16 +45,12 @@ module Mint type_checker = TypeChecker.new(ast) - done = false - - while !done - begin - type_checker.check - rescue ex - errors << ex - else - done = true - end + loop do + type_checker.check + rescue ex + errors << ex + else + break end end From 540a468bcb42730cf91348a48e465fb9704ac41b Mon Sep 17 00:00:00 2001 From: Sijawusz Pur Rahnama Date: Mon, 26 Apr 2021 14:19:29 +0200 Subject: [PATCH 10/11] Update src/commands/lint.cr --- src/commands/lint.cr | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/commands/lint.cr b/src/commands/lint.cr index ee600b70c..aa99d47c3 100644 --- a/src/commands/lint.cr +++ b/src/commands/lint.cr @@ -32,13 +32,13 @@ module Mint end sources.reduce(ast) do |memo, file| - parsed = Parser.parse(file) - - if memo - memo.merge parsed + begin + parsed = Parser.parse(file) + memo.merge(parsed) + rescue ex + errors << ex end - rescue ex - errors << ex + memo end if errors.empty? From 4555561339e89138b6156deed3d67a363101897c Mon Sep 17 00:00:00 2001 From: Sijawusz Pur Rahnama Date: Mon, 26 Apr 2021 14:25:29 +0200 Subject: [PATCH 11/11] Update src/commands/lint.cr --- src/commands/lint.cr | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/commands/lint.cr b/src/commands/lint.cr index aa99d47c3..3f557efd9 100644 --- a/src/commands/lint.cr +++ b/src/commands/lint.cr @@ -33,7 +33,9 @@ module Mint sources.reduce(ast) do |memo, file| begin - parsed = Parser.parse(file) + parsed = + Parser.parse(file) + memo.merge(parsed) rescue ex errors << ex