Class: Solargraph::TypeChecker

Inherits:
Object
  • Object
show all
Includes:
ParserGem::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

all_types_match?, 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, nil) (defaults to: nil)
  • level (Symbol) (defaults to: :normal)


27
28
29
30
31
32
33
34
# 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)
  # @type [Array<Range>]
  @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)
  • level (Symbol) (defaults to: :normal)

Returns:

  • (self)


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

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)
  • level (Symbol) (defaults to: :normal)

Returns:

  • (self)


73
74
75
76
77
78
# File 'lib/solargraph/type_checker.rb', line 73

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:



47
48
49
50
51
52
53
54
55
56
# File 'lib/solargraph/type_checker.rb', line 47

def problems
  @problems ||= begin
     all = method_tag_problems
             .concat(variable_type_tag_problems)
             .concat(const_problems)
             .concat(call_problems)
     unignored = without_ignored(all)
     unignored.concat(unneeded_sgignore_problems)
  end
end

#sourceSource

Returns:



42
43
44
# File 'lib/solargraph/type_checker.rb', line 42

def source
  @source_map.source
end

#source_mapSourceMap

Returns:



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

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