empirical 0.0.2 → 0.0.3

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: 3e5f02cbba70ea2ac807d23f7a60cdd1e7bbd35f8df0b4fd0c9bd7e7d38dd18f
4
- data.tar.gz: 927a9d036d77e2974b1dbe6d3ea1ab922a234627a653c50c5746bde132a4045b
3
+ metadata.gz: 7fede1fc1d6079a9a1b13ae0875ac41bf61a6b7fe812688ca06e8ccd4e1cb9f5
4
+ data.tar.gz: cadaa32b10e496014d25418b594dd0195958e8aa15eee50f45549a1390cf9012
5
5
  SHA512:
6
- metadata.gz: c9237b1059fa32b97c00cab01ff5d66a1131db3bca172abe55fddb44f13277496f921ede321421eb55672628c4666ad84a2077b3a6f87a5ab0e7f3041871a379
7
- data.tar.gz: aa14bc1a19ab39363c500bf0c4d012f39e6c48cdb9b7221f7cfce9b748eafea147df40733eae240729f9b96378cba94f1a64c4cde378cc3dd258c12b57935253
6
+ metadata.gz: 8a5371864bd72f10a9a65648ea13227af2e621978018f151d6d4dae360d08dd909d105663d0954a98f378c2d464b1d46fc5cca098ae547c5148319f7f6acac95
7
+ data.tar.gz: 6fa32faa04f2e919616ee06a0e4fe60e3c0b316eb5e07936eb16dac1a5fd6200aace0e8b821b43c62bf5902a3eb7a65b6f0d3af5646d478ea182dca34beba808
data/README.md CHANGED
@@ -1,8 +1,27 @@
1
1
  # Empirical
2
2
 
3
+ > (_adjective_) based on what is experienced or seen rather than on theory \
4
+ > (_noun_) enhancements for Ruby with a runtime type system
5
+
6
+ Empirical catches bugs early and makes your code self-documenting by enhancing Ruby with beautiful syntax to define runtime type assertions.
7
+
8
+ ```ruby
9
+ fun word_frequency(text: String) => _Hash(String, Integer) do
10
+ text
11
+ .downcase
12
+ .scan(/\w+/)
13
+ .tally
14
+ .sort_by { |word, count| -count }
15
+ .first(10)
16
+ .to_h
17
+ end
18
+ ```
19
+
20
+ (see [below](#runtime-typing)).
21
+
3
22
  ## Setup
4
23
 
5
- Install the gem by adding it to your `Gemfile` and running `bundle install`. You’ll probably want to set it to `require: false` here because you should require it manually at precisely the right moment.
24
+ Install the gem by adding it to your <kbd>Gemfile</kbd> and running <kbd>bundle install</kbd>. You’ll probably want to set it to `require: false` here because you should require it manually at precisely the right moment.
6
25
 
7
26
  ```ruby
8
27
  gem "empirical", require: false
@@ -13,11 +13,11 @@ class Class
13
13
  end
14
14
 
15
15
  class Empirical::ClassCallbacksProcessor < Empirical::BaseProcessor
16
- def visit_class_node(node)
17
- @annotations << [node.end_keyword_loc.start_offset, 0, ";class_defined();"]
18
- end
16
+ # def visit_class_node(node)
17
+ # @annotations << [node.end_keyword_loc.start_offset, 0, ";class_defined();"]
18
+ # end
19
19
 
20
- def visit_module_node(node)
21
- @annotations << [node.end_keyword_loc.start_offset, 0, ";module_defined();"]
22
- end
20
+ # def visit_module_node(node)
21
+ # @annotations << [node.end_keyword_loc.start_offset, 0, ";module_defined();"]
22
+ # end
23
23
  end
@@ -56,7 +56,7 @@ class Empirical::SignatureProcessor < Empirical::BaseProcessor
56
56
  case signature
57
57
  # parameterless method defs (e.g. `fun foo` or `fun foo()`)
58
58
  in Prism::LocalVariableReadNode | Prism::ConstantReadNode
59
- # no-op
59
+ # no-op
60
60
  # parameterful method defs (e.g. `fun foo(a: Type)` or `fun foo(a = Type)`)
61
61
  in Prism::CallNode
62
62
  raise SyntaxError if signature.block
@@ -107,6 +107,20 @@ class Empirical::SignatureProcessor < Empirical::BaseProcessor
107
107
  in Prism::KeywordHashNode
108
108
  argument.elements.each do |argument|
109
109
  name = argument.key.unescaped
110
+
111
+ nilable = false
112
+
113
+ if name.end_with?("?")
114
+ name = name[0..-2]
115
+ nilable = true
116
+
117
+ @annotations << [
118
+ argument.key.location.end_offset - 2,
119
+ 1,
120
+ "",
121
+ ]
122
+ end
123
+
110
124
  typed_param = argument.value
111
125
 
112
126
  case typed_param
@@ -131,10 +145,20 @@ class Empirical::SignatureProcessor < Empirical::BaseProcessor
131
145
  case typed_param
132
146
  # Keyword with default
133
147
  in Prism::CallNode[name: :|, receiver: type, arguments: Prism::ArgumentsNode[arguments: [default]]]
134
- type_slice = type.slice
148
+ type_slice = if nilable
149
+ "::Literal::_Nilable(#{type.slice})"
150
+ else
151
+ type.slice
152
+ end
153
+
135
154
  default_string = default.slice
136
155
  else
137
- type_slice = typed_param.slice
156
+ type_slice = if nilable
157
+ "::Literal::_Nilable(#{typed_param.slice})"
158
+ else
159
+ typed_param.slice
160
+ end
161
+
138
162
  default_string = "nil"
139
163
  end
140
164
 
@@ -229,7 +253,7 @@ class Empirical::SignatureProcessor < Empirical::BaseProcessor
229
253
  [
230
254
  node.location.end_offset,
231
255
  0,
232
- ");(raise ::Empirical::TypeError.return_type_error(value: __literally_returning__, expected: #{@return_type}, method_name: __method__, context: self) unless #{@return_type} === __literally_returning__);return(__literally_returning__))",
256
+ ");(raise ::Empirical::TypeError.return_type_error(value: __literally_returning__, expected: #{@return_type.slice}, method_name: __method__, context: self) unless #{@return_type.slice} === __literally_returning__);return(__literally_returning__))",
233
257
  ]
234
258
  )
235
259
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Empirical
4
- VERSION = "0.0.2"
4
+ VERSION = "0.0.3"
5
5
  end
@@ -0,0 +1,13 @@
1
+ # frozen_string_literal: true
2
+
3
+ class RuboCop::Cop::Empirical::NoDefs < RuboCop::Cop::Base
4
+ MSG = "Use `fun` method definitions instead of `def` method definitions."
5
+
6
+ def on_def(node)
7
+ add_offense(node) unless node.arguments.any?(&:forward_args_type?)
8
+ end
9
+
10
+ def on_defs(node)
11
+ add_offense(node) unless node.arguments.any?(&:forward_args_type?)
12
+ end
13
+ end
@@ -0,0 +1,9 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RuboCop
4
+ module Cop
5
+ module Empirical; end
6
+ end
7
+ end
8
+
9
+ require_relative "empirical/no_defs"
@@ -0,0 +1,4 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "rubocop"
4
+ require_relative "rubocop/cop/empirical"
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: empirical
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Joel Drapper
@@ -56,6 +56,7 @@ description: Based on, concerned with, or verifiable by observation or experienc
56
56
  rather than theory or pure logic.
57
57
  email:
58
58
  - joel@drapper.me
59
+ - stephen.margheim@gmail.com
59
60
  executables: []
60
61
  extensions: []
61
62
  extra_rdoc_files: []
@@ -72,6 +73,9 @@ files:
72
73
  - lib/empirical/signature_processor.rb
73
74
  - lib/empirical/type_error.rb
74
75
  - lib/empirical/version.rb
76
+ - lib/rubocop-empirical.rb
77
+ - lib/rubocop/cop/empirical.rb
78
+ - lib/rubocop/cop/empirical/no_defs.rb
75
79
  - lib/ruby_lsp/empirical/addon.rb
76
80
  homepage: https://github.com/yippee-fun/empirical
77
81
  licenses: