Class: Steep::Project::Target

Inherits:
Object
  • Object
show all
Defined in:
lib/steep/project/target.rb

Defined Under Namespace

Classes: SignatureSyntaxErrorStatus, SignatureValidationErrorStatus, TypeCheckStatus

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name:, options:, source_patterns:, ignore_patterns:, signature_patterns:) ⇒ Target

Returns a new instance of Target.



20
21
22
23
24
25
26
27
28
29
# File 'lib/steep/project/target.rb', line 20

def initialize(name:, options:, source_patterns:, ignore_patterns:, signature_patterns:)
  @name = name
  @options = options
  @source_patterns = source_patterns
  @ignore_patterns = ignore_patterns
  @signature_patterns = signature_patterns

  @source_files = {}
  @signature_files = {}
end

Instance Attribute Details

#ignore_patternsObject (readonly)

Returns the value of attribute ignore_patterns.



8
9
10
# File 'lib/steep/project/target.rb', line 8

def ignore_patterns
  @ignore_patterns
end

#nameObject (readonly)

Returns the value of attribute name.



4
5
6
# File 'lib/steep/project/target.rb', line 4

def name
  @name
end

#optionsObject (readonly)

Returns the value of attribute options.



5
6
7
# File 'lib/steep/project/target.rb', line 5

def options
  @options
end

#signature_filesObject (readonly)

Returns the value of attribute signature_files.



12
13
14
# File 'lib/steep/project/target.rb', line 12

def signature_files
  @signature_files
end

#signature_patternsObject (readonly)

Returns the value of attribute signature_patterns.



9
10
11
# File 'lib/steep/project/target.rb', line 9

def signature_patterns
  @signature_patterns
end

#source_filesObject (readonly)

Returns the value of attribute source_files.



11
12
13
# File 'lib/steep/project/target.rb', line 11

def source_files
  @source_files
end

#source_patternsObject (readonly)

Returns the value of attribute source_patterns.



7
8
9
# File 'lib/steep/project/target.rb', line 7

def source_patterns
  @source_patterns
end

#statusObject (readonly)

Returns the value of attribute status.



14
15
16
# File 'lib/steep/project/target.rb', line 14

def status
  @status
end

Class Method Details

.test_pattern(patterns, path) ⇒ Object



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

def self.test_pattern(patterns, path)
  patterns.any? do |pattern|
    p = pattern.end_with?(File::Separator) ? pattern : pattern + File::Separator
    path.to_s.start_with?(p) || File.fnmatch(pattern, path.to_s)
  end
end

Instance Method Details

#add_signature(path, content) ⇒ Object



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

def add_signature(path, content)
  file = SignatureFile.new(path: path)
  file.content = content
  signature_files[path] = file
end

#add_source(path, content) ⇒ Object



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

def add_source(path, content)
  file = SourceFile.new(path: path)
  file.content = content
  source_files[path] = file
end

#environmentObject



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

def environment
  @environment ||= Ruby::Signature::Environment.new().tap do |env|
    stdlib_root = options.vendored_stdlib_path || Ruby::Signature::EnvironmentLoader::STDLIB_ROOT
    gem_vendor_path = options.vendored_gems_path
    loader = Ruby::Signature::EnvironmentLoader.new(stdlib_root: stdlib_root, gem_vendor_path: gem_vendor_path)
    options.libraries.each do |lib|
      loader.add(library: lib)
    end
    loader.load(env: env)
  end
end

#errorsObject



180
181
182
183
184
185
186
187
# File 'lib/steep/project/target.rb', line 180

def errors
  case status
  when TypeCheckStatus
    source_files.each_value.flat_map(&:errors)
  else
    []
  end
end

#load_signaturesObject



103
104
105
106
107
108
109
110
111
112
113
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
152
153
154
155
156
157
158
159
160
161
# File 'lib/steep/project/target.rb', line 103

def load_signatures
  timestamp = case status
              when TypeCheckStatus
                status.timestamp
              end

  updated_files = []

  signature_files.each_value do |file|
    if !timestamp || file.content_updated_at >= timestamp
      updated_files << file
      file.load!()
    end
  end

  if signature_files.each_value.all? {|file| file.status.is_a?(SignatureFile::DeclarationsStatus) }
    if status.is_a?(TypeCheckStatus) && updated_files.empty?
      yield status.environment, status.subtyping, status.timestamp
    else
      env = environment.dup

      signature_files.each_value do |file|
        if file.status.is_a?(SignatureFile::DeclarationsStatus)
          file.status.declarations.each do |decl|
            env << decl
          end
        end
      end

      definition_builder = Ruby::Signature::DefinitionBuilder.new(env: env)
      factory = AST::Types::Factory.new(builder: definition_builder)
      check = Subtyping::Check.new(factory: factory)

      validator = Signature::Validator.new(checker: check)
      validator.validate()

      if validator.no_error?
        yield env, check, Time.now
      else
        @status = SignatureValidationErrorStatus.new(
          errors: validator.each_error.to_a,
          timestamp: Time.now
        )
      end
    end

  else
    errors = signature_files.each_value.with_object([]) do |file, errors|
      if file.status.is_a?(SignatureFile::ParseErrorStatus)
        errors << file.status.error
      end
    end

    @status = SignatureSyntaxErrorStatus.new(
      errors: errors,
      timestamp: Time.now
    )
  end
end

#possible_signature_file?(path) ⇒ Boolean

Returns:

  • (Boolean)


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

def possible_signature_file?(path)
  self.class.test_pattern(signature_patterns, path)
end

#possible_source_file?(path) ⇒ Boolean

Returns:

  • (Boolean)


69
70
71
72
# File 'lib/steep/project/target.rb', line 69

def possible_source_file?(path)
  self.class.test_pattern(source_patterns, path) &&
    !self.class.test_pattern(ignore_patterns, path)
end

#remove_signature(path) ⇒ Object



52
53
54
# File 'lib/steep/project/target.rb', line 52

def remove_signature(path)
  signature_files.delete(path)
end

#remove_source(path) ⇒ Object



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

def remove_source(path)
  source_files.delete(path)
end

#run_type_check(env, check, timestamp) ⇒ Object



163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
# File 'lib/steep/project/target.rb', line 163

def run_type_check(env, check, timestamp)
  type_check_sources = []

  source_files.each_value do |file|
    if file.type_check(check, timestamp)
      type_check_sources << file
    end
  end

  @status = TypeCheckStatus.new(
    environment: env,
    subtyping: check,
    type_check_sources: type_check_sources,
    timestamp: timestamp
  )
end

#signature_file?(path) ⇒ Boolean

Returns:

  • (Boolean)


65
66
67
# File 'lib/steep/project/target.rb', line 65

def signature_file?(path)
  signature_files.key?(path)
end

#source_file?(path) ⇒ Boolean

Returns:

  • (Boolean)


61
62
63
# File 'lib/steep/project/target.rb', line 61

def source_file?(path)
  source_files.key?(path)
end

#type_checkObject



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

def type_check
  load_signatures do |env, check, timestamp|
    run_type_check(env, check, timestamp)
  end
end

#update_signature(path, content) ⇒ Object



56
57
58
59
# File 'lib/steep/project/target.rb', line 56

def update_signature(path, content)
  file = signature_files[path]
  file.content = content
end

#update_source(path, content) ⇒ Object



41
42
43
44
# File 'lib/steep/project/target.rb', line 41

def update_source(path, content)
  file = source_files[path]
  file.content = content
end