From 8529224c6a5fa5af7d5b030376458f3c2bfbaafd Mon Sep 17 00:00:00 2001 From: Wolfgang Teuber Date: Tue, 3 Sep 2024 19:10:17 +0100 Subject: [PATCH] Allow annotating models and routes using sorbet's static checks See https://sorbet.org/docs/static --- .rubocop_todo.yml | 3 ++- lib/annotate/annotate_models.rb | 10 +++++++++- lib/annotate/annotate_routes/helpers.rb | 10 +++++++++- spec/lib/annotate/annotate_models_spec.rb | 4 +++- spec/lib/annotate/annotate_routes_spec.rb | 4 +++- 5 files changed, 26 insertions(+), 5 deletions(-) diff --git a/.rubocop_todo.yml b/.rubocop_todo.yml index 8d3c7701d..918dc2cda 100644 --- a/.rubocop_todo.yml +++ b/.rubocop_todo.yml @@ -768,6 +768,7 @@ Style/StderrPuts: Style/StringConcatenation: Exclude: - 'lib/annotate/annotate_models.rb' + - 'lib/annotate/annotate_routes/helpers.rb' # Offense count: 57 # Cop supports --auto-correct. @@ -817,4 +818,4 @@ Style/TrailingCommaInArrayLiteral: # Configuration parameters: AutoCorrect, AllowHeredoc, AllowURI, URISchemes, IgnoreCopDirectives, IgnoredPatterns. # URISchemes: http, https Layout/LineLength: - Max: 264 + Max: 245 diff --git a/lib/annotate/annotate_models.rb b/lib/annotate/annotate_models.rb index dc2901a32..f5ab32ba5 100644 --- a/lib/annotate/annotate_models.rb +++ b/lib/annotate/annotate_models.rb @@ -39,7 +39,15 @@ module AnnotateModels } }.freeze - MAGIC_COMMENT_MATCHER = Regexp.new(/(^#\s*encoding:.*(?:\n|r\n))|(^# coding:.*(?:\n|\r\n))|(^# -\*- coding:.*(?:\n|\r\n))|(^# -\*- encoding\s?:.*(?:\n|\r\n))|(^#\s*frozen_string_literal:.+(?:\n|\r\n))|(^# -\*- frozen_string_literal\s*:.+-\*-(?:\n|\r\n))/).freeze + MAGIC_COMMENT_MATCHER = Regexp.new( + '(^#\s*encoding:.*(?:\n|\r\n))' \ + + '|(^# coding:.*(?:\n|\r\n))' \ + + '|(^# -\*- coding:.*(?:\n|\r\n))' \ + + '|(^# -\*- encoding\s?:.*(?:\n|\r\n))' \ + + '|(^#\s*frozen_string_literal:.+(?:\n|\r\n))' \ + + '|(^# -\*- frozen_string_literal\s*:.+-\*-(?:\n|\r\n))' \ + + '|(^#\s*typed:.+(?:\n|\r\n))' + ).freeze class << self def annotate_pattern(options = {}) diff --git a/lib/annotate/annotate_routes/helpers.rb b/lib/annotate/annotate_routes/helpers.rb index 1dba65bbe..0d9219c52 100644 --- a/lib/annotate/annotate_routes/helpers.rb +++ b/lib/annotate/annotate_routes/helpers.rb @@ -1,6 +1,14 @@ module AnnotateRoutes module Helpers - MAGIC_COMMENT_MATCHER = Regexp.new(/(^#\s*encoding:.*)|(^# coding:.*)|(^# -\*- coding:.*)|(^# -\*- encoding\s?:.*)|(^#\s*frozen_string_literal:.+)|(^# -\*- frozen_string_literal\s*:.+-\*-)/).freeze + MAGIC_COMMENT_MATCHER = Regexp.new( + '(^#\s*encoding:.*)' \ + + '|(^# coding:.*)' \ + + '|(^# -\*- coding:.*)' \ + + '|(^# -\*- encoding\s?:.*)' \ + + '|(^#\s*frozen_string_literal:.+)' \ + + '|(^# -\*- frozen_string_literal\s*:.+-\*-)' \ + + '|(^#\s*typed:.*)' + ).freeze class << self # TODO: write the method doc using ruby rdoc formats diff --git a/spec/lib/annotate/annotate_models_spec.rb b/spec/lib/annotate/annotate_models_spec.rb index 096474610..8ba1e395f 100644 --- a/spec/lib/annotate/annotate_models_spec.rb +++ b/spec/lib/annotate/annotate_models_spec.rb @@ -18,7 +18,9 @@ "# frozen_string_literal: true\n# encoding: utf-8", '# frozen_string_literal: true', '#frozen_string_literal: false', - '# -*- frozen_string_literal : true -*-' + '# -*- frozen_string_literal : true -*-', + '#typed: false', + '# typed: true' ].freeze unless const_defined?(:MAGIC_COMMENTS) def mock_index(name, params = {}) diff --git a/spec/lib/annotate/annotate_routes_spec.rb b/spec/lib/annotate/annotate_routes_spec.rb index 2d27b7458..26b493487 100644 --- a/spec/lib/annotate/annotate_routes_spec.rb +++ b/spec/lib/annotate/annotate_routes_spec.rb @@ -20,7 +20,9 @@ "# frozen_string_literal: true\n# encoding: utf-8", '# frozen_string_literal: true', '#frozen_string_literal: false', - '# -*- frozen_string_literal : true -*-' + '# -*- frozen_string_literal : true -*-', + '#typed: false', + '# typed: true' ].freeze unless const_defined?(:MAGIC_COMMENTS) let :stubs do