Class: Steep::Project::SourceFile

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path:, options:) ⇒ SourceFile

Returns a new instance of SourceFile.



13
14
15
16
17
# File 'lib/steep/project/file.rb', line 13

def initialize(path:, options:)
  @path = path
  @options = options
  self.content = ""
end

Instance Attribute Details

#contentObject

Returns the value of attribute content.



6
7
8
# File 'lib/steep/project/file.rb', line 6

def content
  @content
end

#content_updated_atObject (readonly)

Returns the value of attribute content_updated_at.



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

def content_updated_at
  @content_updated_at
end

#last_type_checked_atObject (readonly)

Returns the value of attribute last_type_checked_at.



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

def last_type_checked_at
  @last_type_checked_at
end

#optionsObject (readonly)

Returns the value of attribute options.



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

def options
  @options
end

#pathObject (readonly)

Returns the value of attribute path.



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

def path
  @path
end

#sourceObject (readonly)

Returns the value of attribute source.



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

def source
  @source
end

#typingObject (readonly)

Returns the value of attribute typing.



10
11
12
# File 'lib/steep/project/file.rb', line 10

def typing
  @typing
end

Instance Method Details

#errorsObject



48
49
50
51
52
53
54
55
56
57
# File 'lib/steep/project/file.rb', line 48

def errors
  typing&.errors&.reject do |error|
    case
    when error.is_a?(Errors::FallbackAny)
      !options.fallback_any_is_error
    when error.is_a?(Errors::MethodDefinitionMissing)
      options.allow_missing_definitions
    end
  end
end

#invalidateObject



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

def invalidate
  @source = nil
  @typing = nil
  @last_type_checked_at = nil
end

#parseObject



38
39
40
41
42
43
44
45
46
# File 'lib/steep/project/file.rb', line 38

def parse
  _ = @source =
    begin
      Source.parse(content, path: path.to_s, labeling: ASTUtils::Labeling.new)
    rescue ::Parser::SyntaxError => exn
      Steep.logger.warn { "Syntax error on #{path}: #{exn.inspect}" }
      exn
    end
end

#requires_type_check?Boolean

Returns:

  • (Boolean)


24
25
26
27
28
29
30
# File 'lib/steep/project/file.rb', line 24

def requires_type_check?
  if last = last_type_checked_at
    last < content_updated_at
  else
    true
  end
end

#type_check(check) ⇒ Object



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/steep/project/file.rb', line 59

def type_check(check)
  case source = self.source
  when Source
    @typing = Typing.new

    annotations = source.annotations(block: source.node, builder: check.builder, current_module: AST::Namespace.root)

    const_env = TypeInference::ConstantEnv.new(builder: check.builder, context: nil)
    type_env = TypeInference::TypeEnv.build(annotations: annotations,
                                            subtyping: check,
                                            const_env: const_env,
                                            signatures: check.builder.signatures)

    construction = TypeConstruction.new(
      checker: check,
      annotations: annotations,
      source: source,
      self_type: AST::Builtin::Object.instance_type,
      block_context: nil,
      module_context: TypeConstruction::ModuleContext.new(
        instance_type: nil,
        module_type: nil,
        implement_name: nil,
        current_namespace: AST::Namespace.root,
        const_env: const_env,
        class_name: nil
      ),
      method_context: nil,
      typing: typing,
      break_context: nil,
      type_env: type_env
    )

    construction.synthesize(source.node)

    @last_type_checked_at = Time.now
  end
end