-
Notifications
You must be signed in to change notification settings - Fork 172
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Generalize scope so that it can be used with any locals (#2603)
* Rename ParameterScope to Scope * Rename << to add * Generalize scope to be used with any locals
- Loading branch information
Showing
6 changed files
with
98 additions
and
82 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
# typed: strict | ||
# frozen_string_literal: true | ||
|
||
module RubyLsp | ||
class Scope | ||
extend T::Sig | ||
|
||
sig { returns(T.nilable(Scope)) } | ||
attr_reader :parent | ||
|
||
sig { params(parent: T.nilable(Scope)).void } | ||
def initialize(parent = nil) | ||
@parent = parent | ||
|
||
# A hash of name => type | ||
@locals = T.let({}, T::Hash[Symbol, Local]) | ||
end | ||
|
||
# Add a new local to this scope. The types should only be `:parameter` or `:variable` | ||
sig { params(name: T.any(String, Symbol), type: Symbol).void } | ||
def add(name, type) | ||
@locals[name.to_sym] = Local.new(type) | ||
end | ||
|
||
sig { params(name: T.any(String, Symbol)).returns(T.nilable(Local)) } | ||
def lookup(name) | ||
sym = name.to_sym | ||
entry = @locals[sym] | ||
return entry if entry | ||
return unless @parent | ||
|
||
@parent.lookup(sym) | ||
end | ||
|
||
class Local | ||
extend T::Sig | ||
|
||
sig { returns(Symbol) } | ||
attr_reader :type | ||
|
||
sig { params(type: Symbol).void } | ||
def initialize(type) | ||
@type = type | ||
end | ||
end | ||
end | ||
end |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
# typed: true | ||
# frozen_string_literal: true | ||
|
||
require "test_helper" | ||
|
||
class ScopeTest < Minitest::Test | ||
def test_finding_parameter_in_immediate_scope | ||
scope = RubyLsp::Scope.new | ||
scope.add("foo", :parameter) | ||
|
||
assert_equal(:parameter, T.must(scope.lookup("foo")).type) | ||
end | ||
|
||
def test_finding_parameter_in_parent_scope | ||
parent = RubyLsp::Scope.new | ||
parent.add("foo", :parameter) | ||
|
||
scope = RubyLsp::Scope.new(parent) | ||
assert_equal(:parameter, T.must(scope.lookup("foo")).type) | ||
end | ||
|
||
def test_not_finding_parameter | ||
scope = RubyLsp::Scope.new | ||
refute(scope.lookup("foo")) | ||
end | ||
end |