-
-
Notifications
You must be signed in to change notification settings - Fork 156
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Static type checker #192
Comments
The normal type check only checks class Example
# Type check is validated here; both @param and @return have resolvable types
# @param num [Integer]
# @return [self]
def use_integer(num)
self
end
end
# This method call will report an error in strict type checks (Integer expected)
Example.new.use_integer('string') |
Gem 0.34.0 includes some updates to the type checker, including support for validating overload tags and preliminary support for validating duck types in strict mode. |
These are the proposed levels for type checking: Normal
Typed
Strict
Strong
Other notes:
|
Call it "unreasonable", haha. Glad to see continued progress on this! |
That could be useful for API stubs. We generate API stubs - a lot of it have |
The To run it from the command line, use To test diagnostics in the language server, add
You can also add an argument for the check level, e.g., |
# @type [Array]
resources = Dir.chdir(path) do # error: Declared type Array does not match inferred type Object for variable resources
Dir['**/*'].reject(&::File.method(:directory?))
end should support cast annotation from superclass to subclass. |
@castwide how to silence the infer error? check level is strict module A
# @return [Hash{String => Hash, Array}]
def index # A#index return type could not be inferred
@index ||= begin
recur = lambda do |i, resources|
table = {}
table[i] = recur[i + 1, resources]
table
end
recur[0, @resources]
end
end
end |
@SolaWing There's no way to silence specific errors. That might be a good feature to add. For now the only workaround is to reduce the check level. Typed doesn't report errors for uninferred return types. |
Also, regarding your
|
The type checker now handles inheritance in either direction when checking tagged vs. inferred types. |
Released in v0.39.0. More information: https://solargraph.org/guides/type-checking |
since the checker supported upcast and downcast, add a Object type to declare type, can suppress the wrong infer or wrong extern annotation type. just for someone what strict check but struggle with typechecker. |
Gem version 0.33.0 introduces the first version of the static type checker. You can use it from the command line or add it to your editor's diagnostics.
Command Line
Run it from the command line:
The output will list all methods that have missing or invalid
@return
and@param
tags.To check a specific file only, use
solargraph typecheck ./file.rb
. (The type checker will still map the rest of the workspace for better accuracy.)The optional
--strict
argument uses static code analysis to ensure that methods and params are tagged with the correct types. This option is highly experimental. You can expect a lot of false positives and potential bugs.Language Server Diagnostics
You can get typecheck diagnostics from the language server with the
typecheck
reporter. Enable it by adding it to your workspace's .solargraph.yml:For strict type checking (warning: expect bugs):
This is very much a work in progress. Any feedback is appreciated.
The text was updated successfully, but these errors were encountered: