From c311fb99e940b1ac93d7de6e8178536a34011a69 Mon Sep 17 00:00:00 2001 From: Sijawusz Pur Rahnama Date: Sat, 12 Oct 2024 14:09:37 +0200 Subject: [PATCH 01/10] Cleanup `Naming/BlockParameterName` doc comment --- src/ameba/rule/naming/block_parameter_name.cr | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/ameba/rule/naming/block_parameter_name.cr b/src/ameba/rule/naming/block_parameter_name.cr index 21ede43a1..f94be0d40 100644 --- a/src/ameba/rule/naming/block_parameter_name.cr +++ b/src/ameba/rule/naming/block_parameter_name.cr @@ -20,7 +20,7 @@ module Ameba::Rule::Naming # Enabled: true # MinNameLength: 3 # AllowNamesEndingInNumbers: true - # AllowedNames: [_, e, i, j, k, v, x, y, ex, io, ws, op, tx, id, ip, k1, k2, v1, v2] + # AllowedNames: [e, i, j, k, v, x, y, ex, io, ws, op, tx, id, ip, k1, k2, v1, v2] # ForbiddenNames: [] # ``` class BlockParameterName < Base From a1d64be04fb23aea8876095038173d5ef2bb94f1 Mon Sep 17 00:00:00 2001 From: Sijawusz Pur Rahnama Date: Sat, 12 Oct 2024 14:09:48 +0200 Subject: [PATCH 02/10] Run specs before building binary in CI --- .github/workflows/ci.yml | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 03ca07c34..9b23d37aa 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -36,11 +36,11 @@ jobs: if: matrix.os == 'macos-latest' run: brew install typos-cli - - name: Build ameba binary - run: make build - - name: Run specs run: make spec + - name: Build ameba binary + run: make build + - name: Run ameba linter run: make lint From 5b497f40e72370dbee4da3293e15695d3a4edda0 Mon Sep 17 00:00:00 2001 From: Sijawusz Pur Rahnama Date: Sat, 12 Oct 2024 14:10:01 +0200 Subject: [PATCH 03/10] Fix git-ignored docs path --- .gitignore | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.gitignore b/.gitignore index 2e28b915a..c0a602c3d 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,4 @@ -/doc/ +/docs/ /lib/ /bin/ameba /bin/ameba.dwarf From aeb54ab0fc0b87cac5b20bfd3af7e2c319a5dfbb Mon Sep 17 00:00:00 2001 From: Sijawusz Pur Rahnama Date: Sat, 12 Oct 2024 14:10:14 +0200 Subject: [PATCH 04/10] Fix URLs in README.md --- README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index ecddc3957..58b74e6e0 100644 --- a/README.md +++ b/README.md @@ -232,8 +232,8 @@ time = Time.epoch(1483859302) # ameba:disable Style, Lint ## Credits & inspirations - [Crystal Language](https://crystal-lang.org) -- [Rubocop](https://rubocop.readthedocs.io/en/latest/) -- [Credo](http://credo-ci.org/) +- [Rubocop](https://rubocop.org) +- [Credo](http://credo-ci.org) - [Dogma](https://github.com/lpil/dogma) ## Contributors From 38b2c92c64c481df587d1311942b38a5bb48590d Mon Sep 17 00:00:00 2001 From: Sijawusz Pur Rahnama Date: Sat, 12 Oct 2024 14:10:28 +0200 Subject: [PATCH 05/10] Spec tweaks --- spec/ameba/ast/scope_spec.cr | 1 + spec/ameba/rule/lint/formatting_spec.cr | 13 ++++- spec/ameba/rule/lint/percent_arrays_spec.cr | 64 +++++++++------------ spec/spec_helper.cr | 2 +- 4 files changed, 41 insertions(+), 39 deletions(-) diff --git a/spec/ameba/ast/scope_spec.cr b/spec/ameba/ast/scope_spec.cr index 1b957574b..05f80d1d8 100644 --- a/spec/ameba/ast/scope_spec.cr +++ b/spec/ameba/ast/scope_spec.cr @@ -187,6 +187,7 @@ module Ameba::AST outer_scope = Scope.new nodes.def_nodes.first scope = Scope.new nodes.block_nodes.first, outer_scope scope.def?(check_outer_scopes: true).should be_true + scope.def?.should be_false end end diff --git a/spec/ameba/rule/lint/formatting_spec.cr b/spec/ameba/rule/lint/formatting_spec.cr index 8959280f3..054f3c1f3 100644 --- a/spec/ameba/rule/lint/formatting_spec.cr +++ b/spec/ameba/rule/lint/formatting_spec.cr @@ -14,10 +14,19 @@ module Ameba::Rule::Lint end it "reports if source is not formatted" do - expect_issue subject, <<-CRYSTAL - def method(a,b) + source = expect_issue subject, <<-CRYSTAL + def method(a,b,c=0) # ^{} error: Use built-in formatter to format this source + a+b+c end + + CRYSTAL + + expect_correction source, <<-CRYSTAL + def method(a, b, c = 0) + a + b + c + end + CRYSTAL end diff --git a/spec/ameba/rule/lint/percent_arrays_spec.cr b/spec/ameba/rule/lint/percent_arrays_spec.cr index c279671a9..7301fde8d 100644 --- a/spec/ameba/rule/lint/percent_arrays_spec.cr +++ b/spec/ameba/rule/lint/percent_arrays_spec.cr @@ -5,7 +5,7 @@ module Ameba::Rule::Lint subject = PercentArrays.new it "passes if percent arrays are written correctly" do - s = Source.new %q( + expect_no_issues subject, <<-CRYSTAL %i[one two three] %w[one two three] @@ -14,72 +14,64 @@ module Ameba::Rule::Lint %i[] %w[] - ) - subject.catch(s).should be_valid + CRYSTAL end it "fails if string percent array has commas" do - s = Source.new %( %w[one, two] ) - subject.catch(s).should_not be_valid + expect_issue subject, <<-CRYSTAL + %w[one, two] + # ^{} error: Symbols `,"` may be unwanted in %w array literals + CRYSTAL end it "fails if string percent array has quotes" do - s = Source.new %( %w["one" "two"] ) - subject.catch(s).should_not be_valid + expect_issue subject, <<-CRYSTAL + %w["one" "two"] + # ^{} error: Symbols `,"` may be unwanted in %w array literals + CRYSTAL end it "fails if symbols percent array has commas" do - s = Source.new %( %i[one, two] ) - subject.catch(s).should_not be_valid + expect_issue subject, <<-CRYSTAL + %i[one, two] + # ^{} error: Symbols `,:` may be unwanted in %i array literals + CRYSTAL end it "fails if symbols percent array has a colon" do - s = Source.new %( %i[:one :two] ) - subject.catch(s).should_not be_valid + expect_issue subject, <<-CRYSTAL + %i[:one :two] + # ^{} error: Symbols `,:` may be unwanted in %i array literals + CRYSTAL end it "reports rule, location and message for %i" do - s = Source.new %( + expect_issue subject, <<-CRYSTAL %i[:one] - ), "source.cr" - - subject.catch(s).should_not be_valid - issue = s.issues.first - issue.rule.should_not be_nil - issue.location.to_s.should eq "source.cr:1:1" - issue.message.should eq( - "Symbols `,:` may be unwanted in %i array literals" - ) + # ^{} error: Symbols `,:` may be unwanted in %i array literals + CRYSTAL end it "reports rule, location and message for %w" do - s = Source.new %( + expect_issue subject, <<-CRYSTAL %w["one"] - ), "source.cr" - - subject.catch(s).should_not be_valid - issue = s.issues.first - issue.rule.should_not be_nil - issue.location.to_s.should eq "source.cr:1:1" - issue.end_location.should be_nil - issue.message.should eq( - "Symbols `,\"` may be unwanted in %w array literals" - ) + # ^{} error: Symbols `,"` may be unwanted in %w array literals + CRYSTAL end context "properties" do it "#string_array_unwanted_symbols" do rule = PercentArrays.new rule.string_array_unwanted_symbols = "," - s = Source.new %( %w["one"] ) - rule.catch(s).should be_valid + + expect_no_issues rule, %( %w[one] ) end it "#symbol_array_unwanted_symbols" do rule = PercentArrays.new rule.symbol_array_unwanted_symbols = "," - s = Source.new %( %i[:one] ) - rule.catch(s).should be_valid + + expect_no_issues rule, %( %i[:one] ) end end end diff --git a/spec/spec_helper.cr b/spec/spec_helper.cr index 04dfa74cf..8f0c3b928 100644 --- a/spec/spec_helper.cr +++ b/spec/spec_helper.cr @@ -271,7 +271,7 @@ module Ameba end {% for node in NODES %} - {{ getter_name = node.stringify.split("::").last.underscore + "_nodes" }} + {% getter_name = node.stringify.split("::").last.underscore + "_nodes" %} getter {{ getter_name.id }} = [] of {{ node }} From 42bd042b7fb67f08a83720a91b084bb92a08337e Mon Sep 17 00:00:00 2001 From: Sijawusz Pur Rahnama Date: Sat, 12 Oct 2024 14:10:50 +0200 Subject: [PATCH 06/10] Move record definition to the top of the file --- src/ameba/rule/lint/typos.cr | 52 ++++++++++++++++++------------------ 1 file changed, 26 insertions(+), 26 deletions(-) diff --git a/src/ameba/rule/lint/typos.cr b/src/ameba/rule/lint/typos.cr index 9f19f690a..567fa3af0 100644 --- a/src/ameba/rule/lint/typos.cr +++ b/src/ameba/rule/lint/typos.cr @@ -26,13 +26,38 @@ module Ameba::Rule::Lint @@mutex = Mutex.new + protected record Typo, + typo : String, + corrections : Array(String), + location : {Int32, Int32}, + end_location : {Int32, Int32} do + def self.parse(str) : self? + issue = JSON.parse(str) + + return unless issue["type"] == "typo" + + typo = issue["typo"].as_s + corrections = issue["corrections"].as_a.map(&.as_s) + + return if typo.empty? || corrections.empty? + + line_no = issue["line_num"].as_i + col_no = issue["byte_offset"].as_i + 1 + end_col_no = col_no + typo.size - 1 + + new(typo, corrections, + {line_no, col_no}, + {line_no, end_col_no}) + end + end + protected def self.typos_from(bin_path : String, source : Source) : Array(Typo)? result = @@mutex.synchronize do status = Process.run(bin_path, args: %w[--format json -], input: IO::Memory.new(source.code), output: output = IO::Memory.new, ) - output.to_s unless status.success? + output.to_s.presence unless status.success? end return unless result @@ -67,31 +92,6 @@ module Ameba::Rule::Lint raise ex if fail_on_error? end - private record Typo, - typo : String, - corrections : Array(String), - location : {Int32, Int32}, - end_location : {Int32, Int32} do - def self.parse(str) : self? - issue = JSON.parse(str) - - return unless issue["type"] == "typo" - - typo = issue["typo"].as_s - corrections = issue["corrections"].as_a.map(&.as_s) - - return if typo.empty? || corrections.empty? - - line_no = issue["line_num"].as_i - col_no = issue["byte_offset"].as_i + 1 - end_col_no = col_no + typo.size - 1 - - new(typo, corrections, - {line_no, col_no}, - {line_no, end_col_no}) - end - end - protected def typos_from(source : Source) : Array(Typo)? if bin_path = self.bin_path return Typos.typos_from(bin_path, source) From 938a2a7d8ddec55231ec5120e00572e93e282f61 Mon Sep 17 00:00:00 2001 From: Sijawusz Pur Rahnama Date: Sat, 12 Oct 2024 15:19:49 +0200 Subject: [PATCH 07/10] =?UTF-8?q?Create=20`BINDIR`=20if=20doesn=E2=80=99t?= =?UTF-8?q?=20exist=20on=20`make=20install`?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Makefile | 1 + 1 file changed, 1 insertion(+) diff --git a/Makefile b/Makefile index a5995d0b1..536202a0d 100644 --- a/Makefile +++ b/Makefile @@ -65,6 +65,7 @@ clean: .PHONY: install install: ## Install application binary into $DESTDIR install: $(BUILD_TARGET) + mkdir -p "$(BINDIR)" $(INSTALL_BIN) -m 0755 "$(BUILD_TARGET)" "$(BINDIR)/ameba" .PHONY: bin From db1ba61e56c30e9bb377567c17a4b9b765878910 Mon Sep 17 00:00:00 2001 From: Sijawusz Pur Rahnama Date: Sat, 12 Oct 2024 15:26:36 +0200 Subject: [PATCH 08/10] Remove obsolete `bin` recipe from `Makefile` --- Makefile | 6 ------ 1 file changed, 6 deletions(-) diff --git a/Makefile b/Makefile index 536202a0d..8a8ae42fb 100644 --- a/Makefile +++ b/Makefile @@ -27,7 +27,6 @@ SHARDS_BIN ?= shards # The install command to use INSTALL_BIN ?= /usr/bin/install -SHARD_BIN ?= ../../bin CRFLAGS ?= -Dpreview_mt SRC_SOURCES := $(shell find src -name '*.cr' 2>/dev/null) @@ -68,11 +67,6 @@ install: $(BUILD_TARGET) mkdir -p "$(BINDIR)" $(INSTALL_BIN) -m 0755 "$(BUILD_TARGET)" "$(BINDIR)/ameba" -.PHONY: bin -bin: build - mkdir -p $(SHARD_BIN) - cp "$(BUILD_TARGET)" $(SHARD_BIN) - .PHONY: test test: ## Run the spec suite and linter test: spec lint From 0a9cd4ba9e15727cf9d1b6ff6c6cceb1153c3062 Mon Sep 17 00:00:00 2001 From: Sijawusz Pur Rahnama Date: Sat, 12 Oct 2024 16:09:25 +0200 Subject: [PATCH 09/10] Reorder targets in `Makefile` --- Makefile | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/Makefile b/Makefile index 8a8ae42fb..5c408fce7 100644 --- a/Makefile +++ b/Makefile @@ -45,15 +45,19 @@ docs: ## Generate API docs docs: $(SRC_SOURCES) $(CRYSTAL_BIN) docs +.PHONY: spec +spec: ## Run the spec suite +spec: + $(CRYSTAL_BIN) spec + .PHONY: lint lint: ## Run ameba on its own code base lint: $(BUILD_TARGET) $(BUILD_TARGET) -.PHONY: spec -spec: ## Run the spec suite -spec: - $(CRYSTAL_BIN) spec +.PHONY: test +test: ## Run the spec suite and linter +test: spec lint .PHONY: clean clean: ## Remove application binary and API docs @@ -67,10 +71,6 @@ install: $(BUILD_TARGET) mkdir -p "$(BINDIR)" $(INSTALL_BIN) -m 0755 "$(BUILD_TARGET)" "$(BINDIR)/ameba" -.PHONY: test -test: ## Run the spec suite and linter -test: spec lint - .PHONY: help help: ## Show this help @printf '\033[34mtargets:\033[0m\n' From 21f3f52ea789ca4dba401c38e5b814ebeee3bc86 Mon Sep 17 00:00:00 2001 From: Sijawusz Pur Rahnama Date: Sat, 12 Oct 2024 16:12:22 +0200 Subject: [PATCH 10/10] Mark `docs` recipe as `.PHONY` --- Makefile | 1 + 1 file changed, 1 insertion(+) diff --git a/Makefile b/Makefile index 5c408fce7..417007fec 100644 --- a/Makefile +++ b/Makefile @@ -41,6 +41,7 @@ build: $(BUILD_TARGET) $(BUILD_TARGET): $(SRC_SOURCES) $(SHARDS_BIN) build $(CRFLAGS) +.PHONY: docs docs: ## Generate API docs docs: $(SRC_SOURCES) $(CRYSTAL_BIN) docs