doc_type_checker 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: b0c5eff8a5f546b4d4a0483fb329bc28bf01eff351b41c25afd3cbf710d8febd
4
+ data.tar.gz: 248fa2619df37cfc9d56ff1fa1e096999ac9b736b4ba200f084533e4bbd05538
5
+ SHA512:
6
+ metadata.gz: 4411c9653ff3cda9bb5f67c74ab7888d1c9f3b8f6c37df67d81c5a5b3f37aa226e1b20f50eb7db7490fa30ef300c84cd9a3fc6b6576f6a5682915433b3e1651b
7
+ data.tar.gz: fa1d6e926586ec46a532bb9a81d6a89a2d775acd9d5c9336b4c0a3c63872e50687c5db48f5741fa5535bf306d46a81c4e39a9c1e35a149f86509eb4905274fa5
@@ -0,0 +1,13 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+
10
+ # rspec failure tracking
11
+ .rspec_status
12
+
13
+ Gemfile.lock
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
@@ -0,0 +1,5 @@
1
+ AllCops:
2
+ NewCops: enable
3
+ Metrics/BlockLength:
4
+ Exclude:
5
+ - spec/**/*_spec.rb
data/Gemfile ADDED
@@ -0,0 +1,9 @@
1
+ # frozen_string_literal: true
2
+
3
+ source 'https://rubygems.org'
4
+
5
+ # Specify your gem's dependencies in doc_type_checker.gemspec
6
+ gemspec
7
+
8
+ gem 'rake', '~> 12.0'
9
+ gem 'rspec', '~> 3.0'
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2020 shoma07
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
@@ -0,0 +1,53 @@
1
+ # DocTypeChecker
2
+
3
+ This is type checker for Ruby at runtime using [YARD](https://github.com/lsegal/yard).
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'doc_type_checker'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle install
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install doc_type_checker
20
+
21
+ ## Usage
22
+
23
+ ### Configuration
24
+
25
+ ```ruby
26
+ DocTypeChecker.configure do |config|
27
+ config.enabled = true # or false, default is false
28
+ config.yard_run_arguments = ['--hide-void-return']
29
+ # Throw exception if type validation fails.
30
+ config.strict = true # or false, default is false
31
+ config.logger = Logger.new(STDOUT) # default is nil
32
+ end
33
+ ```
34
+
35
+ ## Features
36
+
37
+ - Support enumerate types `Array<String>, Hash<Symbol, String>`
38
+ - Support method types `#to_s`
39
+
40
+ ## Development
41
+
42
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
43
+
44
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
45
+
46
+ ## Contributing
47
+
48
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/doc_type_checker.
49
+
50
+
51
+ ## License
52
+
53
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
@@ -0,0 +1,8 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'bundler/gem_tasks'
4
+ require 'rspec/core/rake_task'
5
+
6
+ RSpec::Core::RakeTask.new(:spec)
7
+
8
+ task default: :spec
@@ -0,0 +1,15 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ require 'bundler/setup'
5
+ require 'doc_type_checker'
6
+
7
+ # You can add fixtures and/or initialization code here to make experimenting
8
+ # with your gem easier. You can also use a different console, if you like.
9
+
10
+ # (If you use this, don't forget to add pry to your Gemfile!)
11
+ # require "pry"
12
+ # Pry.start
13
+
14
+ require 'irb'
15
+ IRB.start(__FILE__)
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,30 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'lib/doc_type_checker/version'
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = 'doc_type_checker'
7
+ spec.version = DocTypeChecker::VERSION
8
+ spec.authors = ['shoma07']
9
+ spec.email = ['23730734+shoma07@users.noreply.github.com']
10
+
11
+ spec.summary = 'Type Checker for Ruby at runtime using YARD'
12
+ spec.description = 'Type Checker for Ruby at runtime using YARD'
13
+ spec.homepage = 'https://github.com/shoma07/doc_type_checker'
14
+ spec.license = 'MIT'
15
+ spec.required_ruby_version = Gem::Requirement.new('>= 2.3.0')
16
+
17
+ spec.metadata['homepage_uri'] = spec.homepage
18
+ spec.metadata['source_code_uri'] = spec.homepage
19
+
20
+ # Specify which files should be added to the gem when it is released.
21
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
22
+ spec.files = Dir.chdir(File.expand_path(__dir__)) do
23
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
24
+ end
25
+ spec.bindir = 'exe'
26
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
27
+ spec.require_paths = ['lib']
28
+
29
+ spec.add_dependency 'yard'
30
+ end
@@ -0,0 +1,17 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'tracer'
4
+ require 'yard'
5
+ require 'doc_type_checker/version'
6
+ require 'doc_type_checker/store'
7
+ require 'doc_type_checker/trace_point'
8
+ require 'doc_type_checker/configuration'
9
+ require 'doc_type_checker/tag_type'
10
+ require 'doc_type_checker/definition'
11
+
12
+ # DocTypeChecker
13
+ module DocTypeChecker
14
+ # DocTypeChecker::Error
15
+ class Error < StandardError; end
16
+ extend Configuration
17
+ end
@@ -0,0 +1,29 @@
1
+ # frozen_string_literal: true
2
+
3
+ module DocTypeChecker
4
+ # DocTypeChecker::Configuration
5
+ module Configuration
6
+ def configure
7
+ @trace_point = DocTypeChecker::TracePoint.new
8
+ yield self
9
+ end
10
+
11
+ # @param [TrueClass, FalseClass] enabled
12
+ # @return [TrueClass, FalseClass]
13
+ def enabled=(enabled)
14
+ @trace_point.enabled = enabled
15
+ end
16
+
17
+ # @param [TrueClass, FalseClass] strict
18
+ # @return [TrueClass, FalseClass]
19
+ def strict=(strict)
20
+ @trace_point.strict = strict
21
+ end
22
+
23
+ # @param [Array] arguments
24
+ # @return [TrueClass, FalseClass]
25
+ def yard_run_arguments=(arguments)
26
+ YARD::CLI::CommandParser.run(*arguments)
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,70 @@
1
+ # frozen_string_literal: true
2
+
3
+ module DocTypeChecker
4
+ # DocTypeChecker::Definition
5
+ class Definition
6
+ attr_reader :name, :params, :ret
7
+
8
+ # @param [YARD::CodeObjects::MethodObject] method_object
9
+ # @return [DocTypeChecker::Definition]
10
+ def initialize(method_object)
11
+ @name = method_object.path
12
+ init_params(method_object)
13
+ init_return(method_object)
14
+ end
15
+
16
+ # @param [Hash<Symbol, Class>] arguments
17
+ # @return [Array<String>]
18
+ def validate_params(arguments)
19
+ params.filter_map do |key, value|
20
+ object = arguments[key]
21
+ unless value.valid_type(object)
22
+ "#{name}: #{key} isn't any of #{value.inspect_type}. actual type is #{object.class}"
23
+ end
24
+ end
25
+ end
26
+
27
+ # @param [Object] object
28
+ # @return [String, NilClass]
29
+ def validate_return(object)
30
+ return if !ret.types.empty? && ret.valid_type(object)
31
+
32
+ "#{name}: return value isn't any of #{ret.inspect_type}. actual type is #{object.class}"
33
+ end
34
+
35
+ private
36
+
37
+ # @param [YARD::CodeObjects::MethodObject] method_object
38
+ # @return [Hash<Symbol, DocTypeChecker::TagType>]
39
+ # @raise [ArgumentError]
40
+ def init_params(method_object)
41
+ @params = method_object.tags.filter_map do |tag|
42
+ [tag.name.to_sym, TagType.new(tag)] if tag.tag_name == 'param'
43
+ end.to_h
44
+ unless @params.empty? || @params.keys == actual_params_definition(method_object)
45
+ raise ArgumentError, "params doesn't match actual arguments"
46
+ end
47
+
48
+ @params
49
+ end
50
+
51
+ # @param [YARD::CodeObjects::MethodObject] method_object
52
+ # @return [Array<Symbol>]
53
+ def actual_params_definition(method_object)
54
+ method_object.parameters.map { |param| param.first.sub(/:\z/, '').to_sym }
55
+ end
56
+
57
+ # @param [YARD::CodeObjects::MethodObject] method_object
58
+ # @return [DocTypeChecker::TagType]
59
+ def init_return(method_object)
60
+ @ret =
61
+ TagType.new(
62
+ YARD::Tags::Tag.new(
63
+ 'return',
64
+ '',
65
+ method_object.tags.filter_map { |tag| tag.types if tag.tag_name == 'return' }.flatten
66
+ )
67
+ )
68
+ end
69
+ end
70
+ end
@@ -0,0 +1,30 @@
1
+ # frozen_string_literal: true
2
+
3
+ module DocTypeChecker
4
+ # DocTypeChecker::Store
5
+ class Store
6
+ # @return [DocTypeChecker::Store]
7
+ def initialize
8
+ reload!
9
+ end
10
+
11
+ # @param [String] class_name
12
+ # @param [String] method_name
13
+ # @return [DocTypeChecker::Definition]
14
+ def fetch(class_name, method_name)
15
+ @definitions.fetch(class_name, nil)&.fetch(method_name, nil)
16
+ end
17
+
18
+ # @return [Hash<String, Hash<String, DocTypeChecker::Definition>>]
19
+ def reload!
20
+ @definitions = YARD::Registry.load!.all(:class).to_h do |klass|
21
+ [
22
+ klass.path,
23
+ klass.meths.to_h do |meth|
24
+ [meth.name(true), DocTypeChecker::Definition.new(meth)]
25
+ end
26
+ ]
27
+ end
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,39 @@
1
+ # frozen_string_literal: true
2
+
3
+ module DocTypeChecker
4
+ # DocTypeChecker::TagType
5
+ class TagType
6
+ ALLOW_TAG_NAMES = %w[param return].freeze
7
+ attr_reader :types
8
+
9
+ # @param [YARD::Tags::Tag] tag
10
+ # @return [DocTypeChecker::TagType]
11
+ def initialize(tag)
12
+ raise ArgumentError unless ALLOW_TAG_NAMES.include?(tag.tag_name)
13
+
14
+ @types = tag.types.to_a.map { |type_comment| const_get(type_comment) }
15
+ end
16
+
17
+ # @param [Object] object
18
+ # @return [TrueClass, FalseClass]
19
+ def valid_type(object)
20
+ types.any? { |type| object.is_a?(type) }
21
+ end
22
+
23
+ # @return [String]
24
+ def inspect_type
25
+ types.join(', ')
26
+ end
27
+
28
+ private
29
+
30
+ # @todo support definition Array<Integer>
31
+ # @param [String] type_comment
32
+ # @return [Class, Module]
33
+ def const_get(type_comment)
34
+ type_comment.match(/\A([\w:]+)(<.+>)?\z/) do
35
+ Object.const_get(Regexp.last_match(1))
36
+ end || (raise ArgumentError, "#{type_comment} isn't matched class")
37
+ end
38
+ end
39
+ end
@@ -0,0 +1,87 @@
1
+ # frozen_string_literal: true
2
+
3
+ module DocTypeChecker
4
+ # DocTypeChecker::TracePoint
5
+ class TracePoint
6
+ attr_writer :logger, :strict
7
+
8
+ # @return [DocTypeChecker::TracePoint]
9
+ def initialize
10
+ @logger = nil
11
+ @strict = false
12
+ @store = DocTypeChecker::Store.new
13
+ init_trace_point
14
+ end
15
+
16
+ # @param [TrueClass, FalseClass] enabled
17
+ # @return [TrueClass, FalseClass]
18
+ def enabled=(enabled)
19
+ enabled ? @trace_point.enable : @trace_point.disable
20
+ end
21
+
22
+ private
23
+
24
+ # @return [TracePoint]
25
+ def init_trace_point
26
+ @trace_point = ::TracePoint.new(:call, :return) do |trace_point|
27
+ case trace_point.event
28
+ when :call
29
+ trace_event(trace_point)
30
+ when :return
31
+ trace_return(trace_point) if trace_point.method_id != :initialize
32
+ end
33
+ end
34
+ end
35
+
36
+ # @param [TracePoint] trace_point
37
+ # @return [String]
38
+ def trace_event(trace_point)
39
+ definition = fetch_definition(trace_point)
40
+ return if definition.nil?
41
+
42
+ errors = definition.validate_params(event_arguments(trace_point))
43
+ trace_error(errors.join("\n")) unless errors.empty?
44
+ end
45
+
46
+ # @param [TracePoint] trace_point
47
+ def trace_return(trace_point)
48
+ definition = fetch_definition(trace_point)
49
+ return if definition.nil?
50
+
51
+ error = definition.validate_return(trace_point.return_value)
52
+ trace_error(error) unless error.nil?
53
+ end
54
+
55
+ # @param [String] error
56
+ # @return [String]
57
+ # @raise [DocTypeChecker::Error]
58
+ def trace_error(error)
59
+ info(error)
60
+ raise Error, error if @strict
61
+ end
62
+
63
+ # @param [TracePoint] trace_point
64
+ # @return [Hash<Symbol, Class>]
65
+ def event_arguments(trace_point)
66
+ binding = trace_point.binding
67
+ binding.local_variables.to_h { |name| [name, binding.local_variable_get(name)] }
68
+ end
69
+
70
+ # @param [TracePoint] trace_point
71
+ # @return [DocTypeChecker::Definition]
72
+ def fetch_definition(trace_point)
73
+ @store.fetch(
74
+ trace_point.defined_class.name,
75
+ "#{'#' unless trace_point.self.is_a?(Class)}#{trace_point.method_id}"
76
+ )
77
+ end
78
+
79
+ # @param [String] message
80
+ # @return [String]
81
+ def info(message)
82
+ return if @logger.nil?
83
+
84
+ @logger.info(message)
85
+ end
86
+ end
87
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module DocTypeChecker
4
+ VERSION = '0.1.0'
5
+ end
metadata ADDED
@@ -0,0 +1,76 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: doc_type_checker
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - shoma07
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2020-09-22 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: yard
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ description: Type Checker for Ruby at runtime using YARD
28
+ email:
29
+ - 23730734+shoma07@users.noreply.github.com
30
+ executables: []
31
+ extensions: []
32
+ extra_rdoc_files: []
33
+ files:
34
+ - ".gitignore"
35
+ - ".rspec"
36
+ - ".rubocop.yml"
37
+ - Gemfile
38
+ - LICENSE.txt
39
+ - README.md
40
+ - Rakefile
41
+ - bin/console
42
+ - bin/setup
43
+ - doc_type_checker.gemspec
44
+ - lib/doc_type_checker.rb
45
+ - lib/doc_type_checker/configuration.rb
46
+ - lib/doc_type_checker/definition.rb
47
+ - lib/doc_type_checker/store.rb
48
+ - lib/doc_type_checker/tag_type.rb
49
+ - lib/doc_type_checker/trace_point.rb
50
+ - lib/doc_type_checker/version.rb
51
+ homepage: https://github.com/shoma07/doc_type_checker
52
+ licenses:
53
+ - MIT
54
+ metadata:
55
+ homepage_uri: https://github.com/shoma07/doc_type_checker
56
+ source_code_uri: https://github.com/shoma07/doc_type_checker
57
+ post_install_message:
58
+ rdoc_options: []
59
+ require_paths:
60
+ - lib
61
+ required_ruby_version: !ruby/object:Gem::Requirement
62
+ requirements:
63
+ - - ">="
64
+ - !ruby/object:Gem::Version
65
+ version: 2.3.0
66
+ required_rubygems_version: !ruby/object:Gem::Requirement
67
+ requirements:
68
+ - - ">="
69
+ - !ruby/object:Gem::Version
70
+ version: '0'
71
+ requirements: []
72
+ rubygems_version: 3.1.2
73
+ signing_key:
74
+ specification_version: 4
75
+ summary: Type Checker for Ruby at runtime using YARD
76
+ test_files: []