yard-sig 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: f35a0b2f65d1d710a4e41c9d8a40c2f0d70fc00d05e2dc6562c34342d53fb66d
4
+ data.tar.gz: 688ec18293586b50e61dbb2bfb2a9a9f99c00bbc0f31786861a99f31dbb687ea
5
+ SHA512:
6
+ metadata.gz: 30c4739f18458e18084c42ed9836a33923ed389939db04caeebd73f2ca7f39fa9f9fdb3206800f544eb0828547d08a550766f79e753f2cb61856218abd10c04b
7
+ data.tar.gz: 19fc367e95f0447f3e387580d4f8ce62f16771d2f4b1e8ced043bda3e7ea1c65e19d1fee462e593500a91c69b4868ce8441acdd41dbeabf528966e185fca87f5
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/.rubocop.yml ADDED
@@ -0,0 +1,29 @@
1
+ AllCops:
2
+ TargetRubyVersion: 2.6
3
+ NewCops: disable
4
+ SuggestExtensions: false
5
+
6
+ Style/StringLiterals:
7
+ Enabled: true
8
+ EnforcedStyle: double_quotes
9
+
10
+ Style/StringLiteralsInInterpolation:
11
+ Enabled: true
12
+ EnforcedStyle: double_quotes
13
+
14
+ Layout/LineLength:
15
+ Max: 120
16
+
17
+ Naming/FileName:
18
+ Exclude:
19
+ - "lib/yard-sig.rb"
20
+
21
+ Layout/FirstArrayElementIndentation:
22
+ EnforcedStyle: consistent
23
+
24
+ Style/Documentation:
25
+ Enabled: false
26
+
27
+ Metrics/BlockLength:
28
+ Exclude:
29
+ - "spec/**/*_spec.rb"
data/.yardopts ADDED
@@ -0,0 +1 @@
1
+ --plugin sig
data/CHANGELOG.md ADDED
@@ -0,0 +1,5 @@
1
+ ## [Unreleased]
2
+
3
+ ## [0.1.0] - 2023-06-17
4
+
5
+ - Initial release
@@ -0,0 +1,84 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ We as members, contributors, and leaders pledge to make participation in our community a harassment-free experience for everyone, regardless of age, body size, visible or invisible disability, ethnicity, sex characteristics, gender identity and expression, level of experience, education, socio-economic status, nationality, personal appearance, race, religion, or sexual identity and orientation.
6
+
7
+ We pledge to act and interact in ways that contribute to an open, welcoming, diverse, inclusive, and healthy community.
8
+
9
+ ## Our Standards
10
+
11
+ Examples of behavior that contributes to a positive environment for our community include:
12
+
13
+ * Demonstrating empathy and kindness toward other people
14
+ * Being respectful of differing opinions, viewpoints, and experiences
15
+ * Giving and gracefully accepting constructive feedback
16
+ * Accepting responsibility and apologizing to those affected by our mistakes, and learning from the experience
17
+ * Focusing on what is best not just for us as individuals, but for the overall community
18
+
19
+ Examples of unacceptable behavior include:
20
+
21
+ * The use of sexualized language or imagery, and sexual attention or
22
+ advances of any kind
23
+ * Trolling, insulting or derogatory comments, and personal or political attacks
24
+ * Public or private harassment
25
+ * Publishing others' private information, such as a physical or email
26
+ address, without their explicit permission
27
+ * Other conduct which could reasonably be considered inappropriate in a
28
+ professional setting
29
+
30
+ ## Enforcement Responsibilities
31
+
32
+ Community leaders are responsible for clarifying and enforcing our standards of acceptable behavior and will take appropriate and fair corrective action in response to any behavior that they deem inappropriate, threatening, offensive, or harmful.
33
+
34
+ Community leaders have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct, and will communicate reasons for moderation decisions when appropriate.
35
+
36
+ ## Scope
37
+
38
+ This Code of Conduct applies within all community spaces, and also applies when an individual is officially representing the community in public spaces. Examples of representing our community include using an official e-mail address, posting via an official social media account, or acting as an appointed representative at an online or offline event.
39
+
40
+ ## Enforcement
41
+
42
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be reported to the community leaders responsible for enforcement at sinsoku.listy@gmail.com. All complaints will be reviewed and investigated promptly and fairly.
43
+
44
+ All community leaders are obligated to respect the privacy and security of the reporter of any incident.
45
+
46
+ ## Enforcement Guidelines
47
+
48
+ Community leaders will follow these Community Impact Guidelines in determining the consequences for any action they deem in violation of this Code of Conduct:
49
+
50
+ ### 1. Correction
51
+
52
+ **Community Impact**: Use of inappropriate language or other behavior deemed unprofessional or unwelcome in the community.
53
+
54
+ **Consequence**: A private, written warning from community leaders, providing clarity around the nature of the violation and an explanation of why the behavior was inappropriate. A public apology may be requested.
55
+
56
+ ### 2. Warning
57
+
58
+ **Community Impact**: A violation through a single incident or series of actions.
59
+
60
+ **Consequence**: A warning with consequences for continued behavior. No interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, for a specified period of time. This includes avoiding interactions in community spaces as well as external channels like social media. Violating these terms may lead to a temporary or permanent ban.
61
+
62
+ ### 3. Temporary Ban
63
+
64
+ **Community Impact**: A serious violation of community standards, including sustained inappropriate behavior.
65
+
66
+ **Consequence**: A temporary ban from any sort of interaction or public communication with the community for a specified period of time. No public or private interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, is allowed during this period. Violating these terms may lead to a permanent ban.
67
+
68
+ ### 4. Permanent Ban
69
+
70
+ **Community Impact**: Demonstrating a pattern of violation of community standards, including sustained inappropriate behavior, harassment of an individual, or aggression toward or disparagement of classes of individuals.
71
+
72
+ **Consequence**: A permanent ban from any sort of public interaction within the community.
73
+
74
+ ## Attribution
75
+
76
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 2.0,
77
+ available at https://www.contributor-covenant.org/version/2/0/code_of_conduct.html.
78
+
79
+ Community Impact Guidelines were inspired by [Mozilla's code of conduct enforcement ladder](https://github.com/mozilla/diversity).
80
+
81
+ [homepage]: https://www.contributor-covenant.org
82
+
83
+ For answers to common questions about this code of conduct, see the FAQ at
84
+ https://www.contributor-covenant.org/faq. Translations are available at https://www.contributor-covenant.org/translations.
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2023 Takumi Shotoku
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.
data/README.md ADDED
@@ -0,0 +1,46 @@
1
+ # yard-sig
2
+
3
+ A YARD plugin for writing documentations with [RBS syntax](https://github.com/ruby/rbs).
4
+
5
+ ## Installation
6
+
7
+ Install the gem and add to the application's Gemfile by executing:
8
+
9
+ $ bundle add yard-sig
10
+
11
+ Then specify it as an argument when running YARD.
12
+
13
+ $ bundle exec yard doc --plugin sig
14
+
15
+ ## Usage
16
+
17
+ This plugin provides a `@!sig` directive to define types as follows.
18
+
19
+ ```ruby
20
+ class Foo
21
+ # @!sig (Integer, Integer) -> Integer
22
+ def sum(a, b)
23
+ a + b
24
+ end
25
+ end
26
+ ```
27
+
28
+ Using this directive will cause it to be replaced internally by `@param` and `@return` tags when YARD runs.
29
+
30
+ ## Development
31
+
32
+ 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.
33
+
34
+ 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 the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
35
+
36
+ ## Contributing
37
+
38
+ Bug reports and pull requests are welcome on GitHub at https://github.com/sinsoku/yard-sig. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/sinsoku/yard-sig/blob/main/CODE_OF_CONDUCT.md).
39
+
40
+ ## License
41
+
42
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
43
+
44
+ ## Code of Conduct
45
+
46
+ Everyone interacting in the Yard::Sig project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/sinsoku/yard-sig/blob/main/CODE_OF_CONDUCT.md).
data/Rakefile ADDED
@@ -0,0 +1,12 @@
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
+ require "rubocop/rake_task"
9
+
10
+ RuboCop::RakeTask.new
11
+
12
+ task default: %i[spec rubocop]
@@ -0,0 +1,105 @@
1
+ # frozen_string_literal: true
2
+
3
+ module YardSig
4
+ class Sig
5
+ TAG_ORDER = %w[param yieldparam yieldreturn return].freeze
6
+
7
+ # @!sig (String source, ?Array[Array[String]] parameters, ?String namespace) -> self
8
+ def initialize(source, parameters = [], namespace = "")
9
+ @source = source
10
+ @parameters = parameters
11
+ @namespace = namespace
12
+ end
13
+
14
+ # @!sig () -> Array[YARD::Tags::Tag]
15
+ def to_tags
16
+ tags = rbs_type_to_tags(rbs_method_type.type)
17
+ tags += rbs_type_to_tags(rbs_method_type.block.type, within_block: true) if rbs_method_type.block
18
+
19
+ tags.compact.sort_by { |tag| TAG_ORDER.index(tag.tag_name) }
20
+ end
21
+
22
+ private
23
+
24
+ def rbs_method_type
25
+ @rbs_method_type ||= RBS::Parser.parse_method_type(@source)
26
+ end
27
+
28
+ def rbs_type_to_tags(rbs_type, within_block: false)
29
+ positionals = rbs_type.required_positionals + rbs_type.optional_positionals
30
+
31
+ tags = positionals.map.with_index do |param, i|
32
+ build_param_tag(param, :positionals, pos: i, within_block: within_block)
33
+ end
34
+ tags << build_param_tag(rbs_type.rest_positionals, :rest, within_block: within_block)
35
+ tags << build_param_tag(rbs_type.rest_keywords, :keyrest, within_block: within_block)
36
+ tags << build_return_tag(rbs_type.return_type, within_block: within_block)
37
+
38
+ tags.compact
39
+ end
40
+
41
+ def build_param_tag(type, kind, pos: nil, within_block: false) # rubocop:disable Metrics/MethodLength
42
+ return unless type
43
+
44
+ yard_types = to_yard_type(type.type)
45
+ return unless yard_types
46
+
47
+ tag_name = within_block ? :yieldparam : :param
48
+ name = type.name ? type.name.to_s : find_name_from_yard(kind, pos)
49
+
50
+ if kind == :rest
51
+ yard_types = "Array<#{yard_types}>"
52
+ elsif kind == :keyrest
53
+ yard_types = "Hash<Symbol, #{yard_types}>"
54
+ end
55
+
56
+ YARD::Tags::Tag.new(tag_name, "", yard_types, name)
57
+ end
58
+
59
+ def build_return_tag(type, within_block: false)
60
+ return unless type
61
+
62
+ yard_types = to_yard_type(type)
63
+ return unless yard_types
64
+
65
+ tag_name = within_block ? :yieldreturn : :return
66
+ YARD::Tags::Tag.new(tag_name, "", yard_types)
67
+ end
68
+
69
+ def to_yard_type(type) # rubocop:disable Metrics
70
+ case type
71
+ when RBS::Types::Bases::Void, RBS::Types::Bases::Any, RBS::Types::Bases::Bottom
72
+ nil
73
+ when RBS::Types::Bases::Top
74
+ "Object"
75
+ when RBS::Types::Union
76
+ type.types.map { |t| to_yard_type(t) }.join(", ")
77
+ when RBS::Types::Tuple
78
+ args = type.types.map { |t| to_yard_type(t) }.join(", ")
79
+ "Array[#{args}]"
80
+ when RBS::Types::Bases::Bool
81
+ "Boolean"
82
+ when RBS::Types::Bases::Instance
83
+ @namespace.to_s
84
+ when RBS::Types::ClassInstance
85
+ args = type.args.map { |t| to_yard_type(t) }.join(", ")
86
+ args.empty? ? type.to_s : "#{type.name}<#{args}>"
87
+ else
88
+ type.to_s
89
+ end
90
+ end
91
+
92
+ def find_name_from_yard(kind, pos) # rubocop:disable Metrics
93
+ @param_names ||= {}.tap do |h|
94
+ names = @parameters.map(&:first)
95
+
96
+ h[:rest] = names.find { |s| s.start_with?(/\*\w/) }&.then { |name| name.delete_prefix("*") }
97
+ h[:keyrest] = names.find { |s| s.start_with?(/\*\*\w/) }&.then { |name| name.delete_prefix("**") }
98
+ h[:block] = names.find { |s| s.start_with?("&") }&.then { |name| name.delete_prefix("&") }
99
+ h[:keywords] = names.select { |s| s.end_with?(":") }.map { |name| name.delete_suffix(":") }
100
+ h[:positionals] = names.grep(/\w/)
101
+ end
102
+ kind == :positionals ? @param_names.dig(kind, pos) : @param_names[kind]
103
+ end
104
+ end
105
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module YardSig
4
+ VERSION = "0.1.0"
5
+ end
data/lib/yard-sig.rb ADDED
@@ -0,0 +1,33 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "rbs"
4
+ require "yard"
5
+
6
+ require_relative "yard-sig/version"
7
+ require_relative "yard-sig/sig"
8
+
9
+ module YardSig
10
+ class Error < StandardError; end
11
+
12
+ class SigDirective < ::YARD::Tags::Directive
13
+ # @!sig () -> void
14
+ def call
15
+ validate_arguments_forwarding!
16
+
17
+ sig = Sig.new(tag.text, object.parameters, object.namespace.path)
18
+ parser.tags += sig.to_tags
19
+ rescue Error => e
20
+ log.warn(e.message)
21
+ end
22
+
23
+ private
24
+
25
+ def validate_arguments_forwarding!
26
+ return unless object.signature.include?("(...)")
27
+
28
+ raise Error, "arguments forwarding is not supported."
29
+ end
30
+ end
31
+ end
32
+
33
+ YARD::Tags::Library.define_directive :sig, YardSig::SigDirective
data/sig/yard/sig.rbs ADDED
@@ -0,0 +1,6 @@
1
+ module Yard
2
+ module Sig
3
+ VERSION: String
4
+ # See the writing guide of rbs: https://github.com/ruby/rbs#guides
5
+ end
6
+ end
metadata ADDED
@@ -0,0 +1,87 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: yard-sig
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Takumi Shotoku
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2023-07-06 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rbs
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '3.0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '3.0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: yard
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0.9'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0.9'
41
+ description: It provides a way to define types with `@!sig` directive instead of other
42
+ tags.
43
+ email:
44
+ - sinsoku.listy@gmail.com
45
+ executables: []
46
+ extensions: []
47
+ extra_rdoc_files: []
48
+ files:
49
+ - ".rspec"
50
+ - ".rubocop.yml"
51
+ - ".yardopts"
52
+ - CHANGELOG.md
53
+ - CODE_OF_CONDUCT.md
54
+ - LICENSE.txt
55
+ - README.md
56
+ - Rakefile
57
+ - lib/yard-sig.rb
58
+ - lib/yard-sig/sig.rb
59
+ - lib/yard-sig/version.rb
60
+ - sig/yard/sig.rbs
61
+ homepage: https://github.com/sinsoku/yard-sig
62
+ licenses:
63
+ - MIT
64
+ metadata:
65
+ homepage_uri: https://github.com/sinsoku/yard-sig
66
+ source_code_uri: https://github.com/sinsoku/yard-sig/blob/v0.1.0/CHANGELOG.md
67
+ changelog_uri: https://github.com/sinsoku/yard-sig/tree/v0.1.0
68
+ post_install_message:
69
+ rdoc_options: []
70
+ require_paths:
71
+ - lib
72
+ required_ruby_version: !ruby/object:Gem::Requirement
73
+ requirements:
74
+ - - ">="
75
+ - !ruby/object:Gem::Version
76
+ version: 2.6.0
77
+ required_rubygems_version: !ruby/object:Gem::Requirement
78
+ requirements:
79
+ - - ">="
80
+ - !ruby/object:Gem::Version
81
+ version: '0'
82
+ requirements: []
83
+ rubygems_version: 3.4.14
84
+ signing_key:
85
+ specification_version: 4
86
+ summary: A YARD plugin for writing documentations with RBS syntax
87
+ test_files: []