Skip to content

Commit

Permalink
Merge pull request #484 from crystal-ameba/cleanups
Browse files Browse the repository at this point in the history
Cleanups
  • Loading branch information
Sija authored Oct 14, 2024
2 parents 7eb16a5 + 21f3f52 commit e5c0715
Show file tree
Hide file tree
Showing 10 changed files with 84 additions and 86 deletions.
6 changes: 3 additions & 3 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/doc/
/docs/
/lib/
/bin/ameba
/bin/ameba.dwarf
Expand Down
24 changes: 10 additions & 14 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -42,19 +41,24 @@ build: $(BUILD_TARGET)
$(BUILD_TARGET): $(SRC_SOURCES)
$(SHARDS_BIN) build $(CRFLAGS)

.PHONY: docs
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
Expand All @@ -65,17 +69,9 @@ 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
bin: build
mkdir -p $(SHARD_BIN)
cp "$(BUILD_TARGET)" $(SHARD_BIN)

.PHONY: test
test: ## Run the spec suite and linter
test: spec lint

.PHONY: help
help: ## Show this help
@printf '\033[34mtargets:\033[0m\n'
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions spec/ameba/ast/scope_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
13 changes: 11 additions & 2 deletions spec/ameba/rule/lint/formatting_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
64 changes: 28 additions & 36 deletions spec/ameba/rule/lint/percent_arrays_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion spec/spec_helper.cr
Original file line number Diff line number Diff line change
Expand Up @@ -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 }}

Expand Down
52 changes: 26 additions & 26 deletions src/ameba/rule/lint/typos.cr
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion src/ameba/rule/naming/block_parameter_name.cr
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit e5c0715

Please sign in to comment.