steep 0.1.0 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: e44524f5daae1f0506108ead6a30bc8abff67ab100d2639e21dd75e3384dab7e
4
- data.tar.gz: 1a5595ef390995476f5d6dd88fb3c1684bdca27a87ec26d048b071e4a5ccf9d0
3
+ metadata.gz: a5bc8e25ce0d9d4c9946d0100d3a011ca486086997b4ba86644939cfed1a9b08
4
+ data.tar.gz: 7d4c26a383e9387a9f4c15c09f488eb24dc5d7ede91b9acc8da1d033f32389ff
5
5
  SHA512:
6
- metadata.gz: 97c834ae154ac5bc617f4f65be66820531524888237ace9bc316c9cbecb1e2433f8a40b947c9c4d1ae97da7701b55622fe2a7407464fb9db90666e6fd790897b
7
- data.tar.gz: fe8b9e5ab3367df5b28281a0179b58af6b96fd497ee84293b69533808d2b52ffa7ebc801021456fd0fc528c21d2cd07eb63f9b6ca49899052b9cf746eb184cdd
6
+ metadata.gz: d491cda774d07a99a41be32e9bf7559104d84a68bb36ba590e70d1592a1a4cda40a1bba416c50969279b7e6c1cfd3f4b80274d32e7ef29fa8b71851de4d214f7
7
+ data.tar.gz: 5140446569e665f76971fbf4dec158a664f8e2e4ae32339480069288c1e3fdbbc50237db5999c3f9d124190859a334696dcb579f09009762c25aa5bdd3361edc
@@ -0,0 +1,8 @@
1
+ # CHANGELOG
2
+
3
+ ## master
4
+
5
+ ## 0.2.0 (2018-05-30)
6
+
7
+ * Add `attr_reader` and `attr_accessor` syntax to signature (#33)
8
+ * Fix parsing on union with `any`
@@ -65,6 +65,30 @@ module Steep
65
65
  @type = type
66
66
  end
67
67
  end
68
+
69
+ class Attr
70
+ attr_reader :location
71
+ attr_reader :kind
72
+ attr_reader :name
73
+ attr_reader :ivar
74
+ attr_reader :type
75
+
76
+ def initialize(location:, kind:, name:, ivar:, type:)
77
+ @location = location
78
+ @kind = kind
79
+ @name = name
80
+ @ivar = ivar
81
+ @type = type
82
+ end
83
+
84
+ def reader?
85
+ kind == :reader
86
+ end
87
+
88
+ def accessor?
89
+ kind == :accessor
90
+ end
91
+ end
68
92
  end
69
93
  end
70
94
  end
@@ -20,9 +20,9 @@ module Steep
20
20
  end.map do |type|
21
21
  case type
22
22
  when AST::Types::Any
23
- return AST::Types::Any.new()
23
+ return AST::Types::Any.new(location: location)
24
24
  when AST::Types::Bot
25
- return AST::Types::Bot.new()
25
+ return AST::Types::Bot.new(location: location)
26
26
  when AST::Types::Top
27
27
  nil
28
28
  else
@@ -20,9 +20,9 @@ module Steep
20
20
  end.map do |type|
21
21
  case type
22
22
  when AST::Types::Any
23
- return AST::Types::Any.new()
23
+ return AST::Types::Any.new(location: location)
24
24
  when AST::Types::Top
25
- return AST::Types::Top.new()
25
+ return AST::Types::Top.new(location: location)
26
26
  when AST::Types::Bot
27
27
  nil
28
28
  else
@@ -358,6 +358,45 @@ module Steep
358
358
  when AST::Signature::Members::Ivar
359
359
  merge_ivars(ivar_chains,
360
360
  { member.name => absolute_type(member.type, current: sig.name) })
361
+ when AST::Signature::Members::Attr
362
+ if member.ivar != false
363
+ ivar_name = member.ivar || "@#{member.name}".to_sym
364
+ merge_ivars(ivar_chains,
365
+ { ivar_name => absolute_type(member.type, current: sig.name) })
366
+ end
367
+
368
+ reader_method = AST::Signature::Members::Method.new(
369
+ location: member.location,
370
+ name: member.name,
371
+ kind: :instance,
372
+ types: [
373
+ AST::MethodType.new(location: member.type.location,
374
+ type_params: nil,
375
+ params: nil,
376
+ block: nil,
377
+ return_type: member.type)
378
+ ],
379
+ attributes: []
380
+ )
381
+ add_method(type_name, reader_method, methods: methods)
382
+
383
+ if member.accessor?
384
+ writer_method = AST::Signature::Members::Method.new(
385
+ location: member.location,
386
+ name: "#{member.name}=".to_sym,
387
+ kind: :instance,
388
+ types: [
389
+ AST::MethodType.new(location: member.type.location,
390
+ type_params: nil,
391
+ params: AST::MethodType::Params::Required.new(location: member.type.location,
392
+ type: member.type),
393
+ block: nil,
394
+ return_type: member.type)
395
+ ],
396
+ attributes: []
397
+ )
398
+ add_method(type_name, writer_method, methods: methods)
399
+ end
361
400
  end
362
401
  end
363
402
 
@@ -293,6 +293,8 @@ class_member: instance_method_member
293
293
  | include_member
294
294
  | extend_member
295
295
  | ivar_member
296
+ | attr_reader_member
297
+ | attr_accessor_member
296
298
 
297
299
  ivar_member: IVAR_NAME COLON type {
298
300
  loc = val.first.location + val.last.location
@@ -353,6 +355,18 @@ extend_member: EXTEND module_name {
353
355
  name = val[1].value
354
356
  result = AST::Signature::Members::Extend.new(name: name, location: loc, args: val[3])
355
357
  }
358
+ attr_reader_member: ATTR_READER method_name attr_ivar_opt COLON type {
359
+ loc = val.first.location + val.last.location
360
+ result = AST::Signature::Members::Attr.new(location: loc, name: val[1].value, kind: :reader, ivar: val[2], type: val[4])
361
+ }
362
+ attr_accessor_member: ATTR_ACCESSOR method_name attr_ivar_opt COLON type {
363
+ loc = val.first.location + val.last.location
364
+ result = AST::Signature::Members::Attr.new(location: loc, name: val[1].value, kind: :accessor, ivar: val[2], type: val[4])
365
+ }
366
+
367
+ attr_ivar_opt: { result = nil }
368
+ | LPAREN RPAREN { result = false }
369
+ | LPAREN IVAR_NAME RPAREN { result = val[1].value }
356
370
 
357
371
  constructor_method: { result = false }
358
372
  | LPAREN CONSTRUCTOR RPAREN { result = true }
@@ -421,6 +435,8 @@ method_name: IDENT
421
435
  raise ParseError, "\nunexpected method name > >" unless val[0].location.pred?(val[1].location)
422
436
  result = LocatedValue.new(location: val[0].location + val[1].location, value: :>>)
423
437
  }
438
+ | ATTR_READER
439
+ | ATTR_ACCESSOR
424
440
 
425
441
  annotation: AT_TYPE VAR subject COLON type {
426
442
  loc = val.first.location + val.last.location
@@ -640,6 +656,10 @@ def next_token
640
656
  new_token(:SELF, :self)
641
657
  when input.scan(/'\w+/)
642
658
  new_token(:TVAR, input.matched.gsub(/\A'/, '').to_sym)
659
+ when input.scan(/attr_reader\b/)
660
+ new_token(:ATTR_READER, :attr_reader)
661
+ when input.scan(/attr_accessor\b/)
662
+ new_token(:ATTR_ACCESSOR, :attr_accessor)
643
663
  when input.scan(/instance\b/)
644
664
  new_token(:INSTANCE, :instance)
645
665
  when input.scan(/class\b/)
@@ -1984,46 +1984,56 @@ module Steep
1984
1984
 
1985
1985
  def validate_method_definitions(node, module_name)
1986
1986
  signature = checker.builder.signatures.find_class_or_module(module_name.name)
1987
+ expected_instance_method_names = Set.new()
1988
+ expected_module_method_names = Set.new
1987
1989
 
1988
1990
  signature.members.each do |member|
1989
- if member.is_a?(AST::Signature::Members::Method)
1991
+ case member
1992
+ when AST::Signature::Members::Method
1990
1993
  if member.instance_method?
1991
- case
1992
- when module_context.defined_instance_methods.include?(member.name)
1993
- # ok
1994
- when annotations.dynamics[member.name]&.instance_method?
1995
- # ok
1996
- else
1997
- typing.add_error Errors::MethodDefinitionMissing.new(node: node,
1998
- module_name: module_name.name,
1999
- kind: :instance,
2000
- missing_method: member.name)
2001
- end
1994
+ expected_instance_method_names << member.name
2002
1995
  end
2003
-
2004
1996
  if member.module_method?
2005
- case
2006
- when module_context.defined_module_methods.include?(member.name)
2007
- # ok
2008
- when annotations.dynamics[member.name]&.module_method?
2009
- # ok
2010
- else
2011
- typing.add_error Errors::MethodDefinitionMissing.new(node: node,
2012
- module_name: module_name.name,
2013
- kind: :module,
2014
- missing_method: member.name)
2015
- end
1997
+ expected_module_method_names << member.name
2016
1998
  end
1999
+ when AST::Signature::Members::Attr
2000
+ expected_instance_method_names << member.name
2001
+ expected_instance_method_names << "#{member.name}=".to_sym if member.accessor?
2017
2002
  end
2018
2003
  end
2019
2004
 
2020
- annotations.dynamics.each do |method_name, annotation|
2021
- method_signature = signature.members.find {|sig| sig.is_a?(AST::Signature::Members::Method) && sig.name == method_name }
2005
+ expected_instance_method_names.each do |method_name|
2006
+ case
2007
+ when module_context.defined_instance_methods.include?(method_name)
2008
+ # ok
2009
+ when annotations.dynamics[method_name]&.instance_method?
2010
+ # ok
2011
+ else
2012
+ typing.add_error Errors::MethodDefinitionMissing.new(node: node,
2013
+ module_name: module_name.name,
2014
+ kind: :instance,
2015
+ missing_method: method_name)
2016
+ end
2017
+ end
2018
+ expected_module_method_names.each do |method_name|
2019
+ case
2020
+ when module_context.defined_module_methods.include?(method_name)
2021
+ # ok
2022
+ when annotations.dynamics[method_name]&.module_method?
2023
+ # ok
2024
+ else
2025
+ typing.add_error Errors::MethodDefinitionMissing.new(node: node,
2026
+ module_name: module_name.name,
2027
+ kind: :module,
2028
+ missing_method: method_name)
2029
+ end
2030
+ end
2022
2031
 
2032
+ annotations.dynamics.each do |method_name, annotation|
2023
2033
  case
2024
- when annotation.module_method? && method_signature&.module_method?
2034
+ when annotation.module_method? && expected_module_method_names.member?(method_name)
2025
2035
  # ok
2026
- when annotation.instance_method? && method_signature&.instance_method?
2036
+ when annotation.instance_method? && expected_instance_method_names.member?(method_name)
2027
2037
  # ok
2028
2038
  else
2029
2039
  typing.add_error Errors::UnexpectedDynamicMethod.new(node: node,
@@ -1,3 +1,3 @@
1
1
  module Steep
2
- VERSION = "0.1.0"
2
+ VERSION = "0.2.0"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: steep
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Soutaro Matsumoto
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2018-05-28 00:00:00.000000000 Z
11
+ date: 2018-05-30 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -132,6 +132,7 @@ extra_rdoc_files: []
132
132
  files:
133
133
  - ".gitignore"
134
134
  - ".travis.yml"
135
+ - CHANGELOG.md
135
136
  - Gemfile
136
137
  - README.md
137
138
  - Rakefile
@@ -277,7 +278,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
277
278
  version: '0'
278
279
  requirements: []
279
280
  rubyforge_project:
280
- rubygems_version: 2.7.6
281
+ rubygems_version: 2.7.3
281
282
  signing_key:
282
283
  specification_version: 4
283
284
  summary: Gradual Typing for Ruby