Skip to content

Commit

Permalink
[Ruby] Refactor/30 0 1 things (#311)
Browse files Browse the repository at this point in the history
* Update changelog for python fix

* Fix issue with methodname not remedying parser

* AF: EmptyLineAfterMagicComment

* AF: EmptyLinesAroundBlockBody

* Remedy tabbing

* Simplify TokenScanner#read

* AF: Lint/UnusedMethodArgument

* Fix Useless Variable Assignment

* AF: Style/BlockDelimiters

* AF: Style/ExpandPathArguments

* Regenerate TODO

* Pull in cucumber-rails styles

* AF: FrozenStringLiteral

* AF: Style/MultilineTernaryOperator

* Alter razor template to give better name for state matching

* Change lookahead method name and variable name of current token

* Fix up todo file and add override to generic styles

* Regen parser

* Mutate frozen string

* Remove concept of mutating frozen string, store memoized token reference

* Only perform token formatting when result is attempting to be fetched

* Minor optimisation AstBuilder

* Add note about ruby fixes

* Add note about internal refactor
  • Loading branch information
luke-hill authored Nov 12, 2024
1 parent 2f55c59 commit 7cc1dad
Show file tree
Hide file tree
Showing 31 changed files with 322 additions and 339 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,12 @@ This project adheres to [Semantic Versioning](http://semver.org).
This document is formatted according to the principles of [Keep A CHANGELOG](http://keepachangelog.com).

## [Unreleased]
### Changed
- [Ruby] Fixed up some style autofix cops ([#311](https://github.com/cucumber/gherkin/pull/311))
- [Ruby] Internal rewrites to razor template variables and `TokenFormatterBuilder`) ([#311](https://github.com/cucumber/gherkin/pull/311))

### Fixed
- [Python] Release process didn't release to pypi correctly

### Fixed

Expand Down
37 changes: 31 additions & 6 deletions ruby/.rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,39 @@ inherit_mode:
merge:
- Exclude

#require:
# - rubocop-packaging
# - rubocop-performance
# - rubocop-rails
# - rubocop-rake
# - rubocop-rspec

AllCops:
NewCops: disable
# Keep this inline with the lowest ruby version in the gemspec
TargetRubyVersion: 3.0
# Display cop name / style guide references
DisplayCopNames: true
DisplayStyleGuide: true
NewCops: enable

# Disabled on our repo's to enable polyglot-release
Gemspec/RequireMFA:
Enabled: false

Layout/LineLength:
Max: 200

# This allows us to read the chmod action in a more reproducible way
Style/NumericLiteralPrefix:
EnforcedOctalStyle: zero_only

Style/Documentation:
Enabled: false

Style/RegexpLiteral:
EnforcedStyle: slashes
AllowInnerSlashes: true

## Disabled until rubocop-rspec is introduced
#RSpec/MessageSpies:
# EnforcedStyle: receive

# TODO: [LH] - Investigate this in terms of it not being fixed in rubocop?
# Hack to make rubocop not flag these (We need to re-analyse these later on)
Naming/MethodName:
Enabled: false
134 changes: 12 additions & 122 deletions ruby/.rubocop_todo.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# This configuration was generated by
# `rubocop --auto-gen-config`
# on 2024-09-10 17:08:57 UTC using RuboCop version 1.26.1.
# on 2024-11-05 17:37:25 UTC using RuboCop version 1.26.1.
# The point is for the user to remove these configuration records
# one by one as the offenses are removed from the code base.
# Note that changes in the inspected code, or installation of new
Expand All @@ -11,19 +11,15 @@
# Pass 0 - July 2024 - 26 files inspected, 3031 offenses detected, 2667 offenses auto-correctable
# Pass 1 - August 2024 (Fix up a load of Style autofix cops) - 27 files inspected, 2691 offenses detected, 2356 offenses auto-correctable
# Pass 2 - September 2024 (Reconciled issues) - 27 files inspected, 2823 offenses detected, 2491 offenses auto-correctable
# Pass 3 - October 2024 (Minor tweaks for python fix) - 27 files inspected, 2831 offenses detected, 2472 offenses auto-correctable
# Pass 4 - November 2024 (Minor tweaks ready for 30.0.1) - 27 files inspected, 2725 offenses detected, 2444 offenses auto-correctable

# Offense count: 63
# This cop supports safe auto-correction (--auto-correct).
Layout/EmptyLineAfterGuardClause:
Exclude:
- 'lib/gherkin/parser.rb'

# Offense count: 1
# This cop supports safe auto-correction (--auto-correct).
Layout/EmptyLineAfterMagicComment:
Exclude:
- 'Gemfile'

# Offense count: 51
# This cop supports safe auto-correction (--auto-correct).
# Configuration parameters: EmptyLineBetweenMethodDefs, EmptyLineBetweenClassDefs, EmptyLineBetweenModuleDefs, AllowAdjacentOneLineDefs, NumberOfEmptyLines.
Expand All @@ -38,14 +34,6 @@ Layout/EmptyLines:
- 'lib/gherkin/parser.rb'
- 'spec/gherkin/stream/parser_message_stream_spec.rb'

# Offense count: 1
# This cop supports safe auto-correction (--auto-correct).
# Configuration parameters: EnforcedStyle.
# SupportedStyles: empty_lines, no_empty_lines
Layout/EmptyLinesAroundBlockBody:
Exclude:
- 'lib/gherkin/pickles/compiler.rb'

# Offense count: 1
# This cop supports safe auto-correction (--auto-correct).
# Configuration parameters: EnforcedStyle.
Expand All @@ -62,14 +50,6 @@ Layout/EmptyLinesAroundModuleBody:
Exclude:
- 'lib/gherkin/parser.rb'

# Offense count: 6
# This cop supports safe auto-correction (--auto-correct).
# Configuration parameters: EnforcedStyle, IndentationWidth.
# SupportedStyles: aligned, indented
Layout/MultilineOperationIndentation:
Exclude:
- 'lib/gherkin/token_matcher.rb'

# Offense count: 2
# This cop supports safe auto-correction (--auto-correct).
Layout/SpaceAfterComma:
Expand Down Expand Up @@ -101,17 +81,6 @@ Layout/SpaceInsideParens:
Exclude:
- 'lib/gherkin/parser.rb'

# Offense count: 1
# Configuration parameters: AllowSafeAssignment.
Lint/AssignmentInCondition:
Exclude:
- 'lib/gherkin/token_scanner.rb'

# Offense count: 2
Lint/ImplicitStringConcatenation:
Exclude:
- 'spec/gherkin/query_spec.rb'

# Offense count: 4
Lint/LiteralAsCondition:
Exclude:
Expand All @@ -123,20 +92,6 @@ Lint/Loop:
Exclude:
- 'lib/gherkin/parser.rb'

# Offense count: 3
# This cop supports safe auto-correction (--auto-correct).
# Configuration parameters: AllowUnusedKeywordArguments, IgnoreEmptyMethods, IgnoreNotImplementedMethods.
Lint/UnusedMethodArgument:
Exclude:
- 'lib/gherkin/ast_builder.rb'
- 'lib/gherkin/errors.rb'

# Offense count: 2
Lint/UselessAssignment:
Exclude:
- 'lib/gherkin/ast_builder.rb'
- 'spec/gherkin/gherkin_spec.rb'

# Offense count: 59
# Configuration parameters: IgnoredMethods, CountRepeatedAttributes.
Metrics/AbcSize:
Expand All @@ -163,7 +118,7 @@ Metrics/ClassLength:
Metrics/CyclomaticComplexity:
Max: 51

# Offense count: 71
# Offense count: 72
# Configuration parameters: CountComments, CountAsOne, ExcludedMethods, IgnoredMethods.
Metrics/MethodLength:
Max: 141
Expand All @@ -186,39 +141,11 @@ Naming/AccessorMethodName:
- 'lib/gherkin/parser.rb'
- 'lib/gherkin/token_formatter_builder.rb'

# Offense count: 2
# Configuration parameters: MinNameLength, AllowNamesEndingInNumbers, AllowedNames, ForbiddenNames.
# AllowedNames: at, by, db, id, in, io, ip, of, on, os, pp, to
Naming/MethodParameterName:
Exclude:
- 'lib/gherkin/parser.rb'

# Offense count: 4
# Configuration parameters: EnforcedStyle, AllowedIdentifiers.
# Offense count: 29
# Configuration parameters: IgnoredPatterns.
# SupportedStyles: snake_case, camelCase
Naming/VariableName:
Exclude:
- 'lib/gherkin/parser.rb'

# Offense count: 52
# Configuration parameters: EnforcedStyle, CheckMethodNames, CheckSymbols, AllowedIdentifiers.
# SupportedStyles: snake_case, normalcase, non_integer
# AllowedIdentifiers: capture3, iso8601, rfc1123_date, rfc822, rfc2822, rfc3339
Naming/VariableNumber:
Exclude:
- 'lib/gherkin/parser.rb'

# Offense count: 6
# This cop supports safe auto-correction (--auto-correct).
# Configuration parameters: EnforcedStyle, ProceduralMethods, FunctionalMethods, IgnoredMethods, AllowBracesOnProceduralOneLiners, BracesRequiredMethods.
# SupportedStyles: line_count_based, semantic, braces_for_chaining, always_braces
# ProceduralMethods: benchmark, bm, bmbm, create, each_with_object, measure, new, realtime, tap, with_object
# FunctionalMethods: let, let!, subject, watch
# IgnoredMethods: lambda, proc, it
Style/BlockDelimiters:
Exclude:
- 'lib/gherkin/gherkin_line.rb'
- 'spec/gherkin/stream/parser_message_stream_spec.rb'
Naming/MethodName:
EnforcedStyle: snake_case

# Offense count: 2
# This cop supports unsafe auto-correction (--auto-correct-all).
Expand All @@ -233,30 +160,6 @@ Style/DefWithParentheses:
Exclude:
- 'lib/gherkin/parser.rb'

# Offense count: 18
# Configuration parameters: AllowedConstants.
Style/Documentation:
Exclude:
- 'lib/gherkin.rb'
- 'lib/gherkin/ast_builder.rb'
- 'lib/gherkin/ast_node.rb'
- 'lib/gherkin/dialect.rb'
- 'lib/gherkin/errors.rb'
- 'lib/gherkin/gherkin_line.rb'
- 'lib/gherkin/parser.rb'
- 'lib/gherkin/pickles/compiler.rb'
- 'lib/gherkin/query.rb'
- 'lib/gherkin/stream/parser_message_stream.rb'
- 'lib/gherkin/token.rb'
- 'lib/gherkin/token_formatter_builder.rb'
- 'lib/gherkin/token_matcher.rb'

# Offense count: 1
# This cop supports safe auto-correction (--auto-correct).
Style/ExpandPathArguments:
Exclude:
- 'Rakefile'

# Offense count: 2
# This cop supports safe auto-correction (--auto-correct).
# Configuration parameters: EnforcedStyle.
Expand All @@ -271,12 +174,13 @@ Style/FormatString:
Style/FormatStringToken:
EnforcedStyle: unannotated

# Offense count: 26
# Offense count: 1
# This cop supports safe auto-correction (--auto-correct).
# Configuration parameters: EnforcedStyle.
# SupportedStyles: always, always_true, never
Style/FrozenStringLiteralComment:
Enabled: false
Exclude:
- 'lib/gherkin/parser.rb'

# Offense count: 1
# This cop supports safe auto-correction (--auto-correct).
Expand All @@ -285,12 +189,6 @@ Style/MethodCallWithoutArgsParentheses:
Exclude:
- 'lib/gherkin/parser.rb'

# Offense count: 3
# This cop supports safe auto-correction (--auto-correct).
Style/MultilineTernaryOperator:
Exclude:
- 'lib/gherkin/pickles/compiler.rb'

# Offense count: 1
# This cop supports safe auto-correction (--auto-correct).
# Configuration parameters: EnforcedStyle.
Expand Down Expand Up @@ -372,13 +270,12 @@ Style/SignalException:
Exclude:
- 'lib/gherkin/token_scanner.rb'

# Offense count: 4
# Offense count: 3
# This cop supports unsafe auto-correction (--auto-correct-all).
Style/SlicingWithRange:
Exclude:
- 'lib/gherkin/ast_builder.rb'
- 'lib/gherkin/gherkin_line.rb'
- 'spec/gherkin/gherkin_spec.rb'

# Offense count: 58
# This cop supports safe auto-correction (--auto-correct).
Expand Down Expand Up @@ -480,10 +377,3 @@ Style/TrailingCommaInHashLiteral:
Style/ZeroLengthPredicate:
Exclude:
- 'lib/gherkin/gherkin_line.rb'

# Offense count: 124
# This cop supports safe auto-correction (--auto-correct).
# Configuration parameters: AllowHeredoc, AllowURI, URISchemes, IgnoreCopDirectives, IgnoredPatterns.
# URISchemes: http, https
Layout/LineLength:
Max: 177
1 change: 1 addition & 0 deletions ruby/Gemfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# frozen_string_literal: true

source 'https://rubygems.org'

gemspec
6 changes: 4 additions & 2 deletions ruby/Rakefile
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
# frozen_string_literal: true

require 'rubygems'
require 'bundler'
Bundler::GemHelper.install_tasks

$:.unshift File.expand_path('../lib', __FILE__)
$:.unshift File.expand_path('lib', __dir__)

Dir['./rake/*.rb'].sort.each { |f| require f }
Dir['./rake/*.rb'].each { |f| require f }

require 'rspec/core/rake_task'
RSpec::Core::RakeTask.new(:spec)
Expand Down
1 change: 1 addition & 0 deletions ruby/bin/gherkin
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#!/usr/bin/env ruby
# frozen_string_literal: true

$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '../lib'))

Expand Down
1 change: 1 addition & 0 deletions ruby/bin/gherkin-generate-tokens
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#!/usr/bin/env ruby
# frozen_string_literal: true

$LOAD_PATH.unshift(File.join(File.dirname(__FILE__),'../lib'))
require 'gherkin/parser'
Expand Down
2 changes: 2 additions & 0 deletions ruby/bin/gherkin-ruby
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
#!/usr/bin/env ruby
# frozen_string_literal: true

load(File.dirname(__FILE__) + '/gherkin')
2 changes: 2 additions & 0 deletions ruby/cucumber-gherkin.gemspec
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

version = File.read(File.expand_path('VERSION', __dir__)).strip

Gem::Specification.new do |s|
Expand Down
10 changes: 5 additions & 5 deletions ruby/gherkin-ruby.razor
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ module Gherkin
@foreach(var state in Model.States.Values.Where(s => !s.IsEndState))
{
@:when @state.Id
@:match_token_at_@(state.Id)(token, context)
@:match_token_at_state@(state.Id)(token, context)
}
else
raise InvalidOperationException, "Unknown state: #{state}"
Expand All @@ -145,15 +145,15 @@ module Gherkin
@foreach(var state in Model.States.Values.Where(s => !s.IsEndState))
{<text>
# @Raw(state.Comment)
def match_token_at_@(state.Id)(token, context)
def match_token_at_state@(state.Id)(token, context)
@foreach(var transition in state.Transitions)
{
@:if @MatchToken(transition.TokenType)
var extraIndent = "";
if (transition.LookAheadHint != null)
{
extraIndent = " ";
@:@("if lookahead_")@(transition.LookAheadHint.Id)(context, token)
@:@("if lookahead")@(transition.LookAheadHint.Id)(context, token)
}
foreach(var production in transition.Productions)
{
Expand All @@ -174,8 +174,8 @@ module Gherkin
@foreach(var lookAheadHint in Model.RuleSet.LookAheadHints)
{
<text>
def lookahead_@(lookAheadHint.Id)(context, currentToken)
currentToken.detach
def lookahead@(lookAheadHint.Id)(context, current_token)
current_token.detach
token = nil
queue = []
match = false
Expand Down
2 changes: 2 additions & 0 deletions ruby/lib/gherkin.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

require_relative 'gherkin/stream/parser_message_stream'

module Gherkin
Expand Down
Loading

0 comments on commit 7cc1dad

Please sign in to comment.