Class: Steep::Project::SourceFile

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

Defined Under Namespace

Classes: AnnotationSyntaxErrorStatus, ParseErrorStatus, TypeCheckErrorStatus, TypeCheckStatus

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path:) ⇒ SourceFile

Returns a new instance of SourceFile.



16
17
18
19
20
# File 'lib/steep/project/file.rb', line 16

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

Instance Attribute Details

#contentObject

Returns the value of attribute content.



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

def content
  @content
end

#content_updated_atObject (readonly)

Returns the value of attribute content_updated_at.



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

def content_updated_at
  @content_updated_at
end

#factoryObject (readonly)

Returns the value of attribute factory.



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

def factory
  @factory
end

#pathObject (readonly)

Returns the value of attribute path.



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

def path
  @path
end

#statusObject

Returns the value of attribute status.



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

def status
  @status
end

Instance Method Details

#errorsObject



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/steep/project/file.rb', line 30

def errors
  case status
  when TypeCheckStatus
    status.typing.errors
    # 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
  else
    []
  end
end

#parse(factory) ⇒ Object



99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/steep/project/file.rb', line 99

def parse(factory)
  if status.is_a?(TypeCheckStatus)
    yield status.source
  else
    yield Source.parse(content, path: path.to_s, factory: factory, labeling: ASTUtils::Labeling.new)
  end
rescue AnnotationParser::SyntaxError => exn
  Steep.logger.warn { "Annotation syntax error on #{path}: #{exn.inspect}" }
  @status = AnnotationSyntaxErrorStatus.new(error: exn, location: exn.location)
rescue ::Parser::SyntaxError, EncodingError => exn
  Steep.logger.warn { "Source parsing error on #{path}: #{exn.inspect}" }
  @status = ParseErrorStatus.new(error: exn)
end

#type_check(subtyping, env_updated_at) ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
58
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
97
# File 'lib/steep/project/file.rb', line 47

def type_check(subtyping, env_updated_at)
  # skip type check
  return false if status.is_a?(TypeCheckStatus) && env_updated_at <= status.timestamp

  parse(subtyping.factory) do |source|
    typing = Typing.new

    if source
      annotations = source.annotations(block: source.node, factory: subtyping.factory, current_module: AST::Namespace.root)
      const_env = TypeInference::ConstantEnv.new(factory: subtyping.factory, context: nil)
      type_env = TypeInference::TypeEnv.build(annotations: annotations,
                                              subtyping: subtyping,
                                              const_env: const_env,
                                              signatures: subtyping.factory.env)

      construction = TypeConstruction.new(
        checker: subtyping,
        annotations: annotations,
        source: source,
        context: TypeInference::Context.new(
          block_context: nil,
          module_context: TypeInference::Context::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,
          break_context: nil,
          self_type: AST::Builtin::Object.instance_type,
          type_env: type_env
        ),
        typing: typing
      )

      construction.synthesize(source.node)
    end

    @status = TypeCheckStatus.new(
      typing: typing,
      source: source,
      timestamp: Time.now
    )
  rescue => exn
    @status = TypeCheckErrorStatus.new(error: exn)
  end

  true
end