Class: Steep::Project

Inherits:
Object
  • Object
show all
Defined in:
lib/steep/project.rb,
lib/steep/project/file.rb,
lib/steep/project/options.rb,
lib/steep/project/listener.rb

Defined Under Namespace

Classes: NullListener, Options, SignatureFile, SignatureHasError, SignatureHasSyntaxError, SignatureLoaded, SourceFile, SyntaxErrorRaisingListener

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(listener = nil) ⇒ Project

Returns a new instance of Project.



37
38
39
40
41
# File 'lib/steep/project.rb', line 37

def initialize(listener = nil)
  @listener = listener || NullListener.new
  @source_files = {}
  @signature_files = {}
end

Instance Attribute Details

#listenerObject (readonly)

Returns the value of attribute listener.



33
34
35
# File 'lib/steep/project.rb', line 33

def listener
  @listener
end

#signatureObject (readonly)

Returns the value of attribute signature.



35
36
37
# File 'lib/steep/project.rb', line 35

def signature
  @signature
end

#signature_filesObject (readonly)

Returns the value of attribute signature_files.



32
33
34
# File 'lib/steep/project.rb', line 32

def signature_files
  @signature_files
end

#source_filesObject (readonly)

Returns the value of attribute source_files.



31
32
33
# File 'lib/steep/project.rb', line 31

def source_files
  @source_files
end

Instance Method Details

#clearObject



43
44
45
46
47
48
49
50
# File 'lib/steep/project.rb', line 43

def clear
  listener.clear_project project: self do
    @signature = nil
    source_files.each_value do |file|
      file.invalidate
    end
  end
end

#each_updated_source(force: false) ⇒ Object



92
93
94
95
96
97
98
99
100
101
102
# File 'lib/steep/project.rb', line 92

def each_updated_source(force: false)
  if block_given?
    source_files.each_value do |file|
      if force || file.requires_type_check?
        yield file
      end
    end
  else
    enum_for :each_updated_source, force: force
  end
end

#errorsObject



85
86
87
88
89
# File 'lib/steep/project.rb', line 85

def errors
  source_files.flat_map do |_, file|
    file.errors || []
  end
end

#has_type_error?Boolean

Returns:

  • (Boolean)


79
80
81
82
83
# File 'lib/steep/project.rb', line 79

def has_type_error?
  source_files.any? do |_, file|
    file.errors&.any?
  end
end

#reload_signatureObject



114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
# File 'lib/steep/project.rb', line 114

def reload_signature
  @signature = nil

  env = AST::Signature::Env.new
  builder = Interface::Builder.new(signatures: env)
  check = Subtyping::Check.new(builder: builder)

  # @type var syntax_errors: Hash<Pathname, any>
  syntax_errors = {}

  listener.load_signature(project: self) do
    signature_files.each_value do |file|
      sigs = listener.parse_signature(project: self, file: file) do
        file.parse
      end

      sigs.each do |sig|
        env.add sig
      end
    rescue Racc::ParseError => exn
      Steep.logger.warn { "Syntax error on #{file.path}: #{exn.inspect}" }
      syntax_errors[file.path] = exn
    end

    if syntax_errors.empty?
      listener.validate_signature(project: self) do
        errors = validate_signature(check)
        @signature = if errors.empty?
                       SignatureLoaded.new(check: check, loaded_at: Time.now, file_paths: signature_files.keys)
                     else
                       SignatureHasError.new(errors: errors)
                     end
      end
    else
      @signature = SignatureHasSyntaxError.new(errors: syntax_errors)
    end
  end
end

#signature_updated?Boolean

Returns:

  • (Boolean)


104
105
106
107
108
109
110
111
112
# File 'lib/steep/project.rb', line 104

def signature_updated?
  case sig = signature
  when SignatureLoaded
    signature_files.keys != sig.file_paths ||
      signature_files.any? {|_, file| file.content_updated_at >= sig.loaded_at }
  else
    true
  end
end

#success?Boolean

Returns:

  • (Boolean)


74
75
76
77
# File 'lib/steep/project.rb', line 74

def success?
  signature.is_a?(SignatureLoaded) &&
    source_files.all? {|_, file| file.source.is_a?(Source) && file.typing }
end

#type_check(force_signatures: false, force_sources: false) ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/steep/project.rb', line 52

def type_check(force_signatures: false, force_sources: false)
  listener.check(project: self) do
    should_reload_signature = force_signatures || signature_updated?
    reload_signature if should_reload_signature

    case sig = signature
    when SignatureLoaded
      each_updated_source(force: force_sources || should_reload_signature) do |file|
        file.invalidate

        listener.parse_source(project: self, file: file) do
          file.parse()
        end

        listener.type_check_source(project: self, file: file) do
          file.type_check(sig.check)
        end
      end
    end
  end
end

#validate_signature(check) ⇒ Object



153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
# File 'lib/steep/project.rb', line 153

def validate_signature(check)
  errors = []

  builder = check.builder

  check.builder.signatures.each do |sig|
    Steep.logger.debug { "Validating signature: #{sig.inspect}" }

    case sig
    when AST::Signature::Interface
      yield_self do
        instance_interface = builder.build_interface(sig.name)

        args = instance_interface.params.map {|var| AST::Types::Var.fresh(var) }
        instance_type = AST::Types::Name::Interface.new(name: sig.name, args: args)

        instance_interface.instantiate(type: instance_type,
                                       args: args,
                                       instance_type: instance_type,
                                       module_type: nil).validate(check)
      end

    when AST::Signature::Module
      yield_self do
        instance_interface = builder.build_instance(sig.name)
        instance_args = instance_interface.params.map {|var| AST::Types::Var.fresh(var) }

        module_interface = builder.build_module(sig.name)
        module_args = module_interface.params.map {|var| AST::Types::Var.fresh(var) }

        instance_type = AST::Types::Name::Instance.new(name: sig.name, args: instance_args)
        module_type = AST::Types::Name::Module.new(name: sig.name)

        Steep.logger.debug { "Validating instance methods..." }
        instance_interface.instantiate(type: instance_type,
                                       args: instance_args,
                                       instance_type: instance_type,
                                       module_type: module_type).validate(check)

        Steep.logger.debug { "Validating class methods..." }
        module_interface.instantiate(type: module_type,
                                     args: module_args,
                                     instance_type: instance_type,
                                     module_type: module_type).validate(check)
      end

    when AST::Signature::Class
      yield_self do
        instance_interface = builder.build_instance(sig.name)
        instance_args = instance_interface.params.map {|var| AST::Types::Var.fresh(var) }

        module_interface = builder.build_class(sig.name, constructor: true)
        module_args = module_interface.params.map {|var| AST::Types::Var.fresh(var) }

        instance_type = AST::Types::Name::Instance.new(name: sig.name, args: instance_args)
        module_type = AST::Types::Name::Class.new(name: sig.name, constructor: true)

        Steep.logger.debug { "Validating instance methods..." }
        instance_interface.instantiate(type: instance_type,
                                       args: instance_args,
                                       instance_type: instance_type,
                                       module_type: module_type).validate(check)

        Steep.logger.debug { "Validating class methods..." }
        module_interface.instantiate(type: module_type,
                                     args: module_args,
                                     instance_type: instance_type,
                                     module_type: module_type).validate(check)
      end
    end

  rescue => exn
    errors << exn
  end

  errors
end