Class: Solargraph::TypeChecker

Inherits:
Object
  • Object
show all
Includes:
(rubyvm? ? Rubyvm::NodeMethods : Legacy(rubyvm? ? Rubyvm::NodeMethods : Legacy::NodeMethods), Checks
Defined in:
lib/solargraph/type_checker.rb,
lib/solargraph/type_checker/rules.rb,
lib/solargraph/type_checker/checks.rb,
lib/solargraph/type_checker/problem.rb,
lib/solargraph/type_checker/param_def.rb

Overview

A static analysis tool for validating data types.

Defined Under Namespace

Modules: Checks Classes: ParamDef, Problem, Rules

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Checks

any_types_match?, duck_types_match?, either_way?, fuzz, types_match?

Constructor Details

#initialize(filename, api_map: nil, level: :normal) ⇒ TypeChecker

Returns a new instance of TypeChecker.

Parameters:

  • filename (String)
  • api_map (ApiMap) (defaults to: nil)
  • level (Symbol) (defaults to: :normal)


27
28
29
30
31
32
33
# File 'lib/solargraph/type_checker.rb', line 27

def initialize filename, api_map: nil, level: :normal
  @filename = filename
  # @todo Smarter directory resolution
  @api_map = api_map || Solargraph::ApiMap.load(File.dirname(filename))
  @rules = Rules.new(level)
  @marked_ranges = []
end

Instance Attribute Details

#api_mapApiMap (readonly)

Returns:



22
23
24
# File 'lib/solargraph/type_checker.rb', line 22

def api_map
  @api_map
end

#filenameString (readonly)

Returns:

  • (String)


16
17
18
# File 'lib/solargraph/type_checker.rb', line 16

def filename
  @filename
end

#rulesRules (readonly)

Returns:



19
20
21
# File 'lib/solargraph/type_checker.rb', line 19

def rules
  @rules
end

Class Method Details

.load(filename, level = :normal) ⇒ self

Parameters:

  • filename (String)

Returns:

  • (self)


53
54
55
56
57
58
# File 'lib/solargraph/type_checker.rb', line 53

def load filename, level = :normal
  source = Solargraph::Source.load(filename)
  api_map = Solargraph::ApiMap.new
  api_map.map(source)
  new(filename, api_map: api_map, level: level)
end

.load_string(code, filename = nil, level = :normal) ⇒ self

Parameters:

  • code (String)
  • filename (String, nil) (defaults to: nil)

Returns:

  • (self)


63
64
65
66
67
68
# File 'lib/solargraph/type_checker.rb', line 63

def load_string code, filename = nil, level = :normal
  source = Solargraph::Source.load_string(code, filename)
  api_map = Solargraph::ApiMap.new
  api_map.map(source)
  new(filename, api_map: api_map, level: level)
end

Instance Method Details

#problemsArray<Problem>

Returns:



41
42
43
44
45
46
47
48
# File 'lib/solargraph/type_checker.rb', line 41

def problems
  @problems ||= begin
    method_tag_problems
      .concat variable_type_tag_problems
      .concat const_problems
      .concat call_problems
  end
end

#source_mapSourceMap

Returns:



36
37
38
# File 'lib/solargraph/type_checker.rb', line 36

def source_map
  @source_map ||= api_map.source_map(filename)
end