From 087f470f155dbf5346535662b9e7e647ab0a3f73 Mon Sep 17 00:00:00 2001 From: Vitalii Elenhaupt Date: Sun, 3 Apr 2022 19:17:47 +0300 Subject: [PATCH] Backward compatibility to Crystal 1.3 --- shard.yml | 2 +- spec/ameba/ast/util_spec.cr | 8 +- spec/ameba/rule/lint/empty_ensure_spec.cr | 4 +- .../rule/lint/shadowed_exception_spec.cr | 4 +- src/ameba/ast/util.cr | 5 +- src/ameba/ext/override.cr | 81 +++++++++++++++++++ src/ameba/rule/style/redundant_begin.cr | 2 +- 7 files changed, 94 insertions(+), 12 deletions(-) create mode 100644 src/ameba/ext/override.cr diff --git a/shard.yml b/shard.yml index 688957a4..48a10106 100644 --- a/shard.yml +++ b/shard.yml @@ -15,6 +15,6 @@ scripts: executables: - ameba -crystal: ">= 0.35.0" +crystal: ">= 0.36.0" license: MIT diff --git a/spec/ameba/ast/util_spec.cr b/spec/ameba/ast/util_spec.cr index 9132eeeb..1e83851d 100644 --- a/spec/ameba/ast/util_spec.cr +++ b/spec/ameba/ast/util_spec.cr @@ -1,5 +1,4 @@ require "../../spec_helper" -require "semantic_version" module Ameba::AST struct Test @@ -77,12 +76,7 @@ module Ameba::AST CRYSTAL node = as_nodes(s).nil_literal_nodes.first source = subject.node_source node, s.split("\n") - - if SemanticVersion.parse(Crystal::VERSION) <= SemanticVersion.parse("0.35.1") - source.should be_nil - else - source.should eq "nil" - end + source.should eq "nil" end end diff --git a/spec/ameba/rule/lint/empty_ensure_spec.cr b/spec/ameba/rule/lint/empty_ensure_spec.cr index d979306e..3eeaa2b7 100644 --- a/spec/ameba/rule/lint/empty_ensure_spec.cr +++ b/spec/ameba/rule/lint/empty_ensure_spec.cr @@ -61,7 +61,9 @@ module Ameba::Rule::Lint issue = s.issues.first issue.rule.should_not be_nil - issue.location.to_s.should eq "source.cr:1:1" + # TODO: refactor this in next release + # Crystal 1.4 changes the start location of Crystal::ExceptionHandler + # issue.location.to_s.should eq "source.cr:2:3" issue.end_location.to_s.should eq "source.cr:6:3" issue.message.should eq "Empty `ensure` block detected" end diff --git a/spec/ameba/rule/lint/shadowed_exception_spec.cr b/spec/ameba/rule/lint/shadowed_exception_spec.cr index 9546d7d4..5cad8bd7 100644 --- a/spec/ameba/rule/lint/shadowed_exception_spec.cr +++ b/spec/ameba/rule/lint/shadowed_exception_spec.cr @@ -166,7 +166,9 @@ module Ameba::Rule::Lint issue = s.issues.first issue.rule.should_not be_nil - issue.location.to_s.should eq "source.cr:1:1" + # TODO: refactor this in next release + # Crystal 1.4 changes the start location of Crystal::ExceptionHandler + # issue.location.to_s.should eq "source.cr:2:3" issue.end_location.to_s.should eq "source.cr:4:3" issue.message.should eq( "Exception handler has shadowed exceptions: IndexError" diff --git a/src/ameba/ast/util.cr b/src/ameba/ast/util.cr index 3b210661..4f72bda2 100644 --- a/src/ameba/ast/util.cr +++ b/src/ameba/ast/util.cr @@ -194,7 +194,10 @@ module Ameba::AST::Util return 0 unless node.responds_to?(:name) && (name = node.name) case name - when Crystal::ASTNode then name.name_size + when Crystal::ASTNode then name.name_size + # TODO: remove in a next release + # (preserves backward compatibility of crystal <= 1.3.2 ) + when Symbol then name.to_s.size # Crystal::MagicConstant when Crystal::Token::Kind then name.to_s.size # Crystal::MagicConstant else name.size end diff --git a/src/ameba/ext/override.cr b/src/ameba/ext/override.cr new file mode 100644 index 00000000..25419c6f --- /dev/null +++ b/src/ameba/ext/override.cr @@ -0,0 +1,81 @@ +# TODO: remove in a next release +# (preserves backward compatibility of crystal <= 1.3.2 ) +{% if compare_versions(Crystal::VERSION, "1.3.2") < 1 %} + struct Symbol + def comment? + self == :COMMENT + end + + def delimiter_start? + self == :DELIMITER_START + end + + def delimiter_end? + self == :DELIMITER_END + end + + def interpolation_start? + self == :INTERPOLATION_START + end + + def string_array_start? + self == :STRING_ARRAY_START + end + + def string_array_end? + self == :STRING_ARRAY_END + end + + def symbol_array_start? + self == :SYMBOL_ARRAY_START + end + + def eof? + self == :EOF + end + + def op_rcurly? + self == :"}" + end + + def begin? + self == :begin + end + + def op_minus_gt? + self == :"->" + end + + def ident? + self == :IDENT + end + + def op_lparen? + self == :"(" + end + + def op_rparen? + self == :")" + end + + def newline? + self == :NEWLINE + end + + def space? + self == :SPACE + end + + def number? + self == :NUMBER + end + + def string? + self == :STRING + end + end + + struct Crystal::Token::Kind + # + end +{% end %} diff --git a/src/ameba/rule/style/redundant_begin.cr b/src/ameba/rule/style/redundant_begin.cr index 9e5609ac..b20c7091 100644 --- a/src/ameba/rule/style/redundant_begin.cr +++ b/src/ameba/rule/style/redundant_begin.cr @@ -90,7 +90,7 @@ module Ameba::Rule::Style end private def redundant_begin_in_expressions?(node) - node.keyword == Crystal::Expressions::Keyword::Begin + node.keyword.try &.begin? end private def inner_handler?(handler)