bade 0.3.1 → 0.3.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 9363fa63bbc18e9d9cb2af336438820127d766ef7e5b18306ddc37cdd608adfd
4
- data.tar.gz: 6a6c86dea0211e852507fa26e7ec2d6d35b5b03469f5c6919455749134f102bd
3
+ metadata.gz: 7a51d23b5cdbf2a6555552edd39164fc3b39b99f381cf454872cce827d5cae77
4
+ data.tar.gz: b7a98aaee99316d20f22d45c36430d8ba1c785e9d316615343bbb539e86c8891
5
5
  SHA512:
6
- metadata.gz: 37b06b4ef4aa918a88e96c5bfd164ef5d41b9bc0e22949d857b3af143e6dcb462d41f8c10e880f29f4a201e0ca115309e4734e46268f0b1c6fc9f0c7c50802d2
7
- data.tar.gz: a828c7d9c09450235b719c37381add83dcd0980d256f97b755dd6b5ceb287a1f6baca4f43a04c00523cf45a1d692aac6d7b7abd79f8b5694fe713f3686ceefa0
6
+ metadata.gz: 83a93a132f7b957b6d5cd18b4a83c955fa4cd5c68a68d057fa1fec9643ca1c4b2a965c56f7692a0ee3760fafb4732817697d44ca76d35fefdf421204565fe8c0
7
+ data.tar.gz: d59fc4be093b3a2f40f0999e3718f2a0f7bed68956860163cc6920b1cb2afb46459ef155af63fdb8496b9b772055e1d8ffdb956dba173347ab531bb7ca002705
data/Bade.gemspec CHANGED
@@ -1,3 +1,4 @@
1
+ # coding: utf-8
1
2
  # frozen_string_literal: true
2
3
 
3
4
  lib = File.expand_path('lib', __dir__)
@@ -16,7 +17,7 @@ Gem::Specification.new do |spec|
16
17
  spec.metadata = { 'rubygems_mfa_required' => 'true' }
17
18
  spec.required_ruby_version = '>= 2.5'
18
19
 
19
- spec.files = Dir['bin/**/*'] + Dir['lib/**/*'] + %w[Bade.gemspec Gemfile README.md]
20
+ spec.files = Dir['lib/**/*'] + %w[Bade.gemspec Gemfile README.md]
20
21
  spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
21
22
  spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
22
23
  spec.require_paths = ['lib']
@@ -41,6 +41,7 @@ module Bade
41
41
  code_indent do
42
42
  buff_code "self.#{NEW_LINE_NAME} = #{NEW_LINE_NAME}"
43
43
  buff_code "self.#{BASE_INDENT_NAME} = #{BASE_INDENT_NAME}"
44
+ buff_code "__buffs_push(#{location(filename: document.file_path, lineno: 0, label: '<top>')})"
44
45
 
45
46
  visit_document(document)
46
47
 
@@ -86,7 +87,6 @@ module Bade
86
87
  end
87
88
 
88
89
  buff_code("# ----- start file #{document.file_path}") unless document.file_path.nil?
89
- buff_code "__buffs_push(#{location(filename: document.file_path, lineno: 0, label: '<top>')})"
90
90
 
91
91
  new_root = if @optimize
92
92
  Optimizer.new(document.root).optimize
@@ -381,12 +381,13 @@ module Bade
381
381
  when :code
382
382
  value = node.value.strip
383
383
 
384
- %w[end else }].include?(value) || value.match(/^when /)
384
+ %w[end else }].include?(value) || value.match(/^(when|elsif) /)
385
385
  when :newline
386
386
  true
387
387
  else
388
388
  false
389
389
  end
390
+
390
391
  return if should_skip
391
392
  return if node.lineno.nil?
392
393
 
data/lib/bade/parser.rb CHANGED
@@ -29,7 +29,7 @@ module Bade
29
29
  def to_s
30
30
  line = @line.lstrip
31
31
  column = @column + line.size - @line.size
32
- <<-MSG.strip_heredoc
32
+ <<~MSG
33
33
  #{error}
34
34
  #{file}, Line #{lineno}, Column #{@column}
35
35
  #{line}
@@ -95,13 +95,4 @@ class String
95
95
 
96
96
  count
97
97
  end
98
-
99
- # source: http://apidock.com/rails/String/strip_heredoc
100
- # @return [String]
101
- #
102
- def strip_heredoc
103
- min_val = scan(/^[ \t]*(?=\S)/).min
104
- indent = min_val&.size || 0
105
- gsub(/^[ \t]{#{indent}}/, '')
106
- end
107
98
  end
@@ -1,6 +1,20 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require_relative 'utils/where'
3
+ unless Object.respond_to?(:const_source_location)
4
+ class Object
5
+ def Object.const_source_location(name)
6
+ require_relative 'utils/where'
7
+
8
+ konst = const_get(name)
9
+ is_meta = konst.is_a?(Module) || konst.is_a?(Class)
10
+ if is_meta
11
+ Bade.where_is(konst)
12
+ else
13
+ nil
14
+ end
15
+ end
16
+ end
17
+ end
4
18
 
5
19
  module Bade
6
20
  module Runtime
@@ -9,7 +23,7 @@ module Bade
9
23
  # @return [Array<Symbol>]
10
24
  attr_accessor :caught_variables
11
25
 
12
- # @return [Array<[Object, :Symbol]>]
26
+ # @return [Array<[Object, Symbol]>]
13
27
  attr_accessor :caught_constants
14
28
 
15
29
  # @return [Array<String>, nil]
@@ -66,20 +80,24 @@ module Bade
66
80
 
67
81
  # Filteres caught constants by location prefixes and returns ones that should be removed.
68
82
  #
69
- # @return [Array<[Object, :Symbol]>]
83
+ # @return [Array<[Object, Symbol]>]
70
84
  def _filtered_constants
71
85
  @caught_constants.select do |(obj, name)|
72
86
  next unless obj.const_defined?(name)
73
- next true if constants_location_prefixes.nil?
74
87
 
75
- konst = obj.const_get(name)
76
88
  begin
77
- location = Bade.where_is(konst)
89
+ location = obj.const_source_location(name)
78
90
  rescue ::ArgumentError
79
91
  next
80
92
  end
81
93
 
94
+ next true if location.nil?
95
+
82
96
  path = location.first
97
+ next false if $LOADED_FEATURES.include?(path)
98
+
99
+ next true if constants_location_prefixes.nil?
100
+
83
101
  constants_location_prefixes&.any? { |prefix| path.start_with?(prefix) }
84
102
  end
85
103
  end
@@ -49,6 +49,9 @@ module Bade
49
49
 
50
50
  raise Block::MissingBlockDefinitionError.new(e.name, e.context, msg, render_binding.__location_stack)
51
51
 
52
+ rescue RuntimeError
53
+ raise
54
+
52
55
  rescue Exception => e
53
56
  msg = "Exception raised during execution of mixin `#{name}`: #{e}"
54
57
  raise Bade::Runtime::RuntimeError.wrap_existing_error(msg, e, render_binding.__location_stack)
@@ -85,14 +85,10 @@ module Bade
85
85
 
86
86
  # @param [String] filename
87
87
  def __load(filename)
88
- # FakeFS does not fake `load` method
89
- if Object.const_defined?(:FakeFS) && Object.const_get(:FakeFS).activated?
90
- # rubocop:disable Security/Eval
91
- eval(File.read(filename), __get_binding, filename)
92
- # rubocop:enable Security/Eval
93
- else
94
- load(filename)
95
- end
88
+ # Universal way to load Ruby file (production or during tests)
89
+ # rubocop:disable Security/Eval
90
+ eval(File.read(filename), __get_binding, filename)
91
+ # rubocop:enable Security/Eval
96
92
  end
97
93
 
98
94
  # @param [String] filename
@@ -5,14 +5,22 @@
5
5
  module Bade
6
6
  module Where
7
7
  class << self
8
+ # @param [Proc] proc
9
+ # @return [[String, Integer], String]
8
10
  def is_proc(proc)
9
11
  source_location(proc)
10
12
  end
11
13
 
14
+ # @param [Class] klass
15
+ # @param [Symbol, String] method_name
16
+ # @return [[String, Integer], String]
12
17
  def is_method(klass, method_name)
13
18
  source_location(klass.method(method_name))
14
19
  end
15
20
 
21
+ # @param [Object] klass
22
+ # @param [Symbol, String] method_name
23
+ # @return [[String, Integer], String]
16
24
  def is_instance_method(klass, method_name)
17
25
  source_location(klass.instance_method(method_name))
18
26
  end
@@ -25,6 +33,8 @@ module Bade
25
33
  are_via_extractor(:method, klass, method_name)
26
34
  end
27
35
 
36
+ # @param [Class] klass
37
+ # @return [[String, Integer], String]
28
38
  def is_class(klass)
29
39
  defined_methods(klass)
30
40
  .group_by { |sl| sl[0] }
@@ -61,6 +71,8 @@ module Bade
61
71
 
62
72
  private
63
73
 
74
+ # @param [Method] method
75
+ # @return [[String, Integer], String]
64
76
  def source_location(method)
65
77
  method.source_location || (
66
78
  method.to_s =~ /: (.*)>/
@@ -77,6 +89,7 @@ module Bade
77
89
  .compact
78
90
  end
79
91
 
92
+ # @return [Array<Method>]
80
93
  def defined_methods(klass)
81
94
  methods = klass.methods(false).map { |m| klass.method(m) } +
82
95
  klass.instance_methods(false).map { |m| klass.instance_method(m) }
@@ -87,6 +100,9 @@ module Bade
87
100
  end
88
101
  end
89
102
 
103
+ # @param [Object, Class] klass
104
+ # @param [String, Symbol] method
105
+ # @return [[String, Integer], String]
90
106
  def self.where_is(klass, method = nil)
91
107
  if method
92
108
  begin
data/lib/bade/runtime.rb CHANGED
@@ -3,6 +3,10 @@
3
3
  module Bade
4
4
  module Runtime
5
5
  Location = Struct.new(:path, :lineno, :label, keyword_init: true) do
6
+ def template?
7
+ path == TEMPLATE_FILE_NAME || path&.include?('.bade')
8
+ end
9
+
6
10
  def to_s
7
11
  "#{path || TEMPLATE_FILE_NAME}:#{lineno}:in `#{label}'"
8
12
  end
@@ -13,23 +17,41 @@ module Bade
13
17
  #
14
18
  attr_reader :template_backtrace
15
19
 
20
+ # @return [Boolean]
21
+ #
22
+ attr_reader :print_locations_warning
23
+
16
24
  # @param [String] msg
17
25
  # @param [Array<Location>] template_backtrace
18
26
  # @param [Exception, nil] original
19
- def initialize(msg, template_backtrace = [], original: nil)
27
+ def initialize(msg, template_backtrace = [], original: nil, print_locations_warning: false)
20
28
  super(msg)
21
29
  @template_backtrace = template_backtrace
22
30
  @original = original
31
+ @print_locations_warning = print_locations_warning
23
32
  end
24
33
 
25
- def message
34
+ def to_s
26
35
  if @template_backtrace.empty?
27
36
  super
28
37
  else
38
+ warning = if print_locations_warning
39
+ <<~TEXT
40
+
41
+ !!! WARNING !!!, filenames and line numbers of functions can be misleading due to using Ruby
42
+ functions in different Bade file. Trust only functions names. Mixins are fine.
43
+
44
+ This will be fixed in https://github.com/epuber-io/bade/issues/32
45
+ TEXT
46
+ else
47
+ ''
48
+ end
49
+
29
50
  <<~MSG.rstrip
30
51
  #{super}
31
52
  template backtrace:
32
53
  #{__formatted_backtrace.join("\n")}
54
+ #{warning}
33
55
  MSG
34
56
  end
35
57
  end
@@ -53,14 +75,20 @@ module Bade
53
75
  def self.process_locations(locations)
54
76
  return [] if locations.nil?
55
77
 
56
- index = locations&.find_index { |loc| loc.path == TEMPLATE_FILE_NAME || loc.path&.include?('.bade') }
78
+ # map to Bade's Location
79
+ new_locations = locations.map { |loc| Location.new(path: loc.path, lineno: loc.lineno, label: loc.label) }
80
+
81
+ # find location to use or drop
82
+ index = new_locations.rindex(&:template?)
57
83
  return [] if index.nil?
58
84
 
59
- new_locations = locations[0...index] || []
85
+ # get only locations inside template
86
+ new_locations = new_locations[0...index] || []
60
87
 
61
- new_locations.map do |loc|
62
- Location.new(path: loc.path, lineno: loc.lineno, label: loc.label)
63
- end
88
+ # filter out not interested locations
89
+ new_locations
90
+ .reject { |loc| loc.path.start_with?(__dir__) }
91
+ .reject { |loc| loc.template? && loc.label.include?('lambda_instance') }
64
92
  end
65
93
 
66
94
  # @param [String] msg
@@ -68,8 +96,9 @@ module Bade
68
96
  # @param [Array<Location>] template_backtrace
69
97
  # @return [RuntimeError]
70
98
  def self.wrap_existing_error(msg, error, template_backtrace)
71
- locs = Bade::Runtime::RuntimeError.process_locations(error.backtrace_locations) + template_backtrace
72
- Bade::Runtime::RuntimeError.new(msg, locs, original: error)
99
+ ruby_locs = Bade::Runtime::RuntimeError.process_locations(error.backtrace_locations)
100
+ locs = ruby_locs + template_backtrace
101
+ Bade::Runtime::RuntimeError.new(msg, locs, original: error, print_locations_warning: !ruby_locs.empty?)
73
102
  end
74
103
  end
75
104
 
data/lib/bade/version.rb CHANGED
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Bade
4
- VERSION = '0.3.1'.freeze
4
+ VERSION = '0.3.4'.freeze
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: bade
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.1
4
+ version: 0.3.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Roman Kříž
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2022-03-22 00:00:00.000000000 Z
11
+ date: 2022-03-29 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: psych
@@ -134,7 +134,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
134
134
  - !ruby/object:Gem::Version
135
135
  version: '0'
136
136
  requirements: []
137
- rubygems_version: 3.3.9
137
+ rubygems_version: 3.3.10
138
138
  signing_key:
139
139
  specification_version: 4
140
140
  summary: Minimalistic template engine for Ruby.