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, workspace: filename ? Workspace.new(File.dirname(filename)) : nil, rules: workspace ? workspace.rules(level) : Rules.new(level, {})) ⇒ TypeChecker

Returns a new instance of TypeChecker.

Parameters:

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

    Don’t complain about anything above this level

  • workspace (Workspace, nil) (defaults to: filename ? Workspace.new(File.dirname(filename)) : nil)

    Workspace to use for loading type checker rules modified by user config

  • type_checker_rules (Hash{Symbol => Symbol})

    Overrides for type checker rules - e.g., :report_undefined => :strong

  • rules (Rules) (defaults to: workspace ? workspace.rules(level) : Rules.new(level, {}))

    Type checker rules object



32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/solargraph/type_checker.rb', line 32

def initialize filename,
               api_map: nil,
               level: :normal,
               workspace: filename ? Workspace.new(File.dirname(filename)) : nil,
               rules: workspace ? workspace.rules(level) : Rules.new(level, {})
  @filename = filename
  # @todo Smarter directory resolution
  @api_map = api_map || Solargraph::ApiMap.load(File.dirname(filename))
  @rules = rules
  # @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)


71
72
73
74
75
76
# File 'lib/solargraph/type_checker.rb', line 71

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)


82
83
84
85
86
87
# File 'lib/solargraph/type_checker.rb', line 82

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:



56
57
58
59
60
61
62
63
64
65
# File 'lib/solargraph/type_checker.rb', line 56

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:



51
52
53
# File 'lib/solargraph/type_checker.rb', line 51

def source
  @source_map.source
end

#source_mapSourceMap

Returns:



46
47
48
# File 'lib/solargraph/type_checker.rb', line 46

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