mirror of
				https://gitea.invidious.io/iv-org/shard-ameba.git
				synced 2024-08-15 00:53:29 +00:00 
			
		
		
		
	Merge pull request #424 from crystal-ameba/report-string-literals-in-ascii-identifiers-rule
Report symbol literals in `Naming/AsciiIdentifiers` rule
This commit is contained in:
		
						commit
						3b87aa6490
					
				
					 3 changed files with 80 additions and 2 deletions
				
			
		|  | @ -49,6 +49,14 @@ module Ameba::Rule::Naming | ||||||
|         CRYSTAL |         CRYSTAL | ||||||
|     end |     end | ||||||
| 
 | 
 | ||||||
|  |     it "reports defs with parameter default values containing non-ascii characters" do | ||||||
|  |       expect_issue subject, <<-CRYSTAL | ||||||
|  |         def forest_adventure(animal_type = :🐺) | ||||||
|  |                                          # ^^ error: Identifier contains non-ascii characters | ||||||
|  |         end | ||||||
|  |         CRYSTAL | ||||||
|  |     end | ||||||
|  | 
 | ||||||
|     it "reports argument names containing non-ascii characters" do |     it "reports argument names containing non-ascii characters" do | ||||||
|       expect_issue subject, <<-CRYSTAL |       expect_issue subject, <<-CRYSTAL | ||||||
|         %w[wensleydale cheddar brie].each { |🧀| nil } |         %w[wensleydale cheddar brie].each { |🧀| nil } | ||||||
|  | @ -56,6 +64,20 @@ module Ameba::Rule::Naming | ||||||
|         CRYSTAL |         CRYSTAL | ||||||
|     end |     end | ||||||
| 
 | 
 | ||||||
|  |     it "reports calls with arguments containing non-ascii characters" do | ||||||
|  |       expect_issue subject, <<-CRYSTAL | ||||||
|  |         %i[🐺 🐿].index!(:🐺) | ||||||
|  |                      # ^^ error: Identifier contains non-ascii characters | ||||||
|  |         CRYSTAL | ||||||
|  |     end | ||||||
|  | 
 | ||||||
|  |     it "reports calls with named arguments containing non-ascii characters" do | ||||||
|  |       expect_issue subject, <<-CRYSTAL | ||||||
|  |         %i[🐺 🐿].index!(obj: :🐺) | ||||||
|  |                           # ^^ error: Identifier contains non-ascii characters | ||||||
|  |         CRYSTAL | ||||||
|  |     end | ||||||
|  | 
 | ||||||
|     it "reports aliases with names containing non-ascii characters" do |     it "reports aliases with names containing non-ascii characters" do | ||||||
|       expect_issue subject, <<-CRYSTAL |       expect_issue subject, <<-CRYSTAL | ||||||
|         alias JSON🧀 = JSON::Any |         alias JSON🧀 = JSON::Any | ||||||
|  | @ -84,11 +106,46 @@ module Ameba::Rule::Naming | ||||||
|         CRYSTAL |         CRYSTAL | ||||||
|     end |     end | ||||||
| 
 | 
 | ||||||
|  |     it "reports assignments with symbol literals containing non-ascii characters" do | ||||||
|  |       expect_issue subject, <<-CRYSTAL | ||||||
|  |         foo = :신장 | ||||||
|  |             # ^^^ error: Identifier contains non-ascii characters | ||||||
|  |         CRYSTAL | ||||||
|  |     end | ||||||
|  | 
 | ||||||
|  |     it "reports multiple assignments with symbol literals containing non-ascii characters" do | ||||||
|  |       expect_issue subject, <<-CRYSTAL | ||||||
|  |         foo, bar = :신장, true | ||||||
|  |                  # ^^^ error: Identifier contains non-ascii characters | ||||||
|  |         CRYSTAL | ||||||
|  |     end | ||||||
|  | 
 | ||||||
|     it "passes for strings with non-ascii characters" do |     it "passes for strings with non-ascii characters" do | ||||||
|       expect_no_issues subject, <<-CRYSTAL |       expect_no_issues subject, <<-CRYSTAL | ||||||
|         space = "👾" |         space = "👾" | ||||||
|         space = :invader # 👾 |         space = :invader # 👾 | ||||||
|         CRYSTAL |         CRYSTAL | ||||||
|     end |     end | ||||||
|  | 
 | ||||||
|  |     context "properties" do | ||||||
|  |       context "#ignore_symbols" do | ||||||
|  |         it "returns `false` by default" do | ||||||
|  |           rule = AsciiIdentifiers.new | ||||||
|  |           rule.ignore_symbols?.should be_false | ||||||
|  |         end | ||||||
|  | 
 | ||||||
|  |         it "stops reporting symbol literals if set to `true`" do | ||||||
|  |           rule = AsciiIdentifiers.new | ||||||
|  |           rule.ignore_symbols = true | ||||||
|  | 
 | ||||||
|  |           expect_no_issues rule, <<-CRYSTAL | ||||||
|  |             def forest_adventure(animal_type = :🐺); end | ||||||
|  |             %i[🐺 🐿].index!(:🐺) | ||||||
|  |             foo, bar = :신장, true | ||||||
|  |             foo = :신장 | ||||||
|  |             CRYSTAL | ||||||
|  |         end | ||||||
|  |       end | ||||||
|  |     end | ||||||
|   end |   end | ||||||
| end | end | ||||||
|  |  | ||||||
|  | @ -32,6 +32,7 @@ module Ameba::AST | ||||||
|       IsA, |       IsA, | ||||||
|       LibDef, |       LibDef, | ||||||
|       ModuleDef, |       ModuleDef, | ||||||
|  |       MultiAssign, | ||||||
|       NilLiteral, |       NilLiteral, | ||||||
|       StringInterpolation, |       StringInterpolation, | ||||||
|       Unless, |       Unless, | ||||||
|  |  | ||||||
|  | @ -20,10 +20,12 @@ module Ameba::Rule::Naming | ||||||
|   # ``` |   # ``` | ||||||
|   # Naming/AsciiIdentifiers: |   # Naming/AsciiIdentifiers: | ||||||
|   #   Enabled: true |   #   Enabled: true | ||||||
|  |   #   IgnoreSymbols: false | ||||||
|   # ``` |   # ``` | ||||||
|   class AsciiIdentifiers < Base |   class AsciiIdentifiers < Base | ||||||
|     properties do |     properties do | ||||||
|       description "Disallows non-ascii characters in identifiers" |       description "Disallows non-ascii characters in identifiers" | ||||||
|  |       ignore_symbols false | ||||||
|     end |     end | ||||||
| 
 | 
 | ||||||
|     MSG = "Identifier contains non-ascii characters" |     MSG = "Identifier contains non-ascii characters" | ||||||
|  | @ -32,11 +34,21 @@ module Ameba::Rule::Naming | ||||||
|       if (target = node.target).is_a?(Crystal::Path) |       if (target = node.target).is_a?(Crystal::Path) | ||||||
|         check_issue(source, target, target) |         check_issue(source, target, target) | ||||||
|       end |       end | ||||||
|  |       check_symbol_literal(source, node.value) | ||||||
|     end |     end | ||||||
| 
 | 
 | ||||||
|     def test(source, node : Crystal::MultiAssign) |     def test(source, node : Crystal::MultiAssign) | ||||||
|       node.targets.each do |target| |       node.values.each do |value| | ||||||
|         check_issue(source, target, target) |         check_symbol_literal(source, value) | ||||||
|  |       end | ||||||
|  |     end | ||||||
|  | 
 | ||||||
|  |     def test(source, node : Crystal::Call) | ||||||
|  |       node.args.each do |arg| | ||||||
|  |         check_symbol_literal(source, arg) | ||||||
|  |       end | ||||||
|  |       node.named_args.try &.each do |arg| | ||||||
|  |         check_symbol_literal(source, arg.value) | ||||||
|       end |       end | ||||||
|     end |     end | ||||||
| 
 | 
 | ||||||
|  | @ -45,6 +57,7 @@ module Ameba::Rule::Naming | ||||||
| 
 | 
 | ||||||
|       node.args.each do |arg| |       node.args.each do |arg| | ||||||
|         check_issue(source, arg, prefer_name_location: true) |         check_issue(source, arg, prefer_name_location: true) | ||||||
|  |         check_symbol_literal(source, arg.default_value) | ||||||
|       end |       end | ||||||
|     end |     end | ||||||
| 
 | 
 | ||||||
|  | @ -56,6 +69,13 @@ module Ameba::Rule::Naming | ||||||
|       check_issue(source, node.name, node.name) |       check_issue(source, node.name, node.name) | ||||||
|     end |     end | ||||||
| 
 | 
 | ||||||
|  |     private def check_symbol_literal(source, node) | ||||||
|  |       return if ignore_symbols? | ||||||
|  |       return unless node.is_a?(Crystal::SymbolLiteral) | ||||||
|  | 
 | ||||||
|  |       check_issue(source, node, node.value) | ||||||
|  |     end | ||||||
|  | 
 | ||||||
|     private def check_issue(source, location, end_location, name) |     private def check_issue(source, location, end_location, name) | ||||||
|       issue_for location, end_location, MSG unless name.to_s.ascii_only? |       issue_for location, end_location, MSG unless name.to_s.ascii_only? | ||||||
|     end |     end | ||||||
|  |  | ||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue