textlint-ruby 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: dad83d3a7ac022d06493719fa2cdf9d1eb601d44dc5623c6ee7df9c653f1e7ec
4
+ data.tar.gz: 36cf1cc9bf08a768ec79fd805f2e510dbabac2daf69a3cad7c557d0188416093
5
+ SHA512:
6
+ metadata.gz: 8f75e3aeb4c5dff45f7c45ca740e6a69ec79a21fb40e4a40c8c327671aa083c9045127aa4c336b580c498f46495e58c6c7b42a8f147590324a8627ac73c36527
7
+ data.tar.gz: c5cdcd705da25d6b2a679e2aec3679ae50612dd60bbda58c63ccb3a13adacd1e3c461f422936f9490fab8cbec41bf52ab7a6afa297d786d58a0590e5407cbbf7
@@ -0,0 +1,58 @@
1
+ version: 2.1
2
+
3
+ executors:
4
+ ruby_2_6:
5
+ docker:
6
+ - image: ruby:2.6
7
+
8
+ ruby_2_7:
9
+ docker:
10
+ - image: ruby:2.7
11
+
12
+ ruby_3_0:
13
+ docker:
14
+ - image: ruby:3.0
15
+
16
+ commands:
17
+ run_test:
18
+ steps:
19
+ - checkout
20
+ - run: gem install bundler
21
+ - run: bundle install
22
+ - run: bundle exec rspec
23
+ - run: bundle exec rubocop
24
+
25
+ jobs:
26
+ ruby_2_6:
27
+ executor: ruby_2_6
28
+ steps:
29
+ - run_test
30
+
31
+ ruby_2_7:
32
+ executor: ruby_2_7
33
+ steps:
34
+ - run_test
35
+
36
+ ruby_3_0:
37
+ executor: ruby_3_0
38
+ steps:
39
+ - run_test
40
+
41
+ workflows:
42
+ version: 2
43
+
44
+ test:
45
+ jobs: &jobs
46
+ - ruby_2_6
47
+ - ruby_2_7
48
+ - ruby_3_0
49
+
50
+ daily_test:
51
+ triggers:
52
+ - schedule:
53
+ cron: "0 23 * * *"
54
+ filters:
55
+ branches:
56
+ only:
57
+ - master
58
+ jobs: *jobs
data/.gitignore ADDED
@@ -0,0 +1,12 @@
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
+ Gemfile.lock
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/.rubocop.yml ADDED
@@ -0,0 +1,91 @@
1
+ AllCops:
2
+ Exclude:
3
+ - '.bundle/**/*'
4
+ - 'gemfiles/**/*'
5
+ - 'spec/samples/**/*'
6
+ - Gemfile
7
+ TargetRubyVersion: 2.6
8
+ DisplayCopNames: true
9
+ NewCops: disable
10
+
11
+ Bundler:
12
+ Enabled: false
13
+
14
+ Naming:
15
+ Enabled: false
16
+
17
+ Metrics/BlockNesting:
18
+ Enabled: false
19
+
20
+ Metrics/ClassLength:
21
+ Enabled: false
22
+
23
+ Layout/LineLength:
24
+ Enabled: false
25
+
26
+ Metrics/MethodLength:
27
+ Enabled: false
28
+
29
+ Metrics/BlockLength:
30
+ Enabled: false
31
+
32
+ Metrics/ModuleLength:
33
+ Enabled: false
34
+
35
+ Style/AsciiComments:
36
+ Enabled: false
37
+
38
+ Style/IfUnlessModifier:
39
+ Enabled: false
40
+
41
+ Style/Documentation:
42
+ Enabled: false
43
+
44
+ Style/DocumentationMethod:
45
+ Enabled: true
46
+
47
+ Style/BlockDelimiters:
48
+ Enabled: false
49
+
50
+ Style/DoubleNegation:
51
+ Enabled: false
52
+
53
+ Style/GuardClause:
54
+ Enabled: false
55
+
56
+ Style/SpecialGlobalVars:
57
+ Enabled: false
58
+
59
+ Style/NumericPredicate:
60
+ Enabled: false
61
+
62
+ Style/Lambda:
63
+ Enabled: false
64
+
65
+ Layout/ParameterAlignment:
66
+ EnforcedStyle: with_fixed_indentation
67
+
68
+ Layout/ArgumentAlignment:
69
+ EnforcedStyle: with_fixed_indentation
70
+
71
+ Layout/MultilineMethodCallIndentation:
72
+ EnforcedStyle: indented
73
+
74
+ Lint/AmbiguousRegexpLiteral:
75
+ Enabled: false
76
+
77
+ Lint/DisjunctiveAssignmentInConstructor:
78
+ Enabled: false
79
+
80
+ Metrics/AbcSize:
81
+ Enabled: false
82
+
83
+ Metrics/CyclomaticComplexity:
84
+ Max: 10
85
+
86
+ Metrics/PerceivedComplexity:
87
+ Max: 10
88
+
89
+ Lint/UselessAccessModifier:
90
+ ContextCreatingMethods:
91
+ - concerning
data/CHANGELOG.md ADDED
@@ -0,0 +1,5 @@
1
+ ## [Unreleased]
2
+
3
+ ## [0.1.0]
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 alpaca-tc@alpaca.tc. 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/Gemfile ADDED
@@ -0,0 +1,13 @@
1
+ # frozen_string_literal: true
2
+
3
+ source "https://rubygems.org"
4
+
5
+ gemspec
6
+
7
+ gem "rspec"
8
+ gem "rake"
9
+ gem "rubocop"
10
+ gem "guard"
11
+ gem "guard-rspec"
12
+ gem 'guard-rubocop'
13
+ gem 'pry'
data/Guardfile ADDED
@@ -0,0 +1,23 @@
1
+ # frozen_string_literal: true
2
+
3
+ guard :rspec, cmd: 'bundle exec rspec' do
4
+ require 'guard/rspec/dsl'
5
+ dsl = Guard::RSpec::Dsl.new(self)
6
+
7
+ # Feel free to open issues for suggestions and improvements
8
+
9
+ # RSpec files
10
+ rspec = dsl.rspec
11
+ watch(rspec.spec_helper) { rspec.spec_dir }
12
+ watch(rspec.spec_support) { rspec.spec_dir }
13
+ watch(rspec.spec_files)
14
+
15
+ # Ruby files
16
+ ruby = dsl.ruby
17
+ dsl.watch_spec_files_for(ruby.lib_files)
18
+ watch(%r{spec/samples/.*\.(rb|json)}) { 'spec/textlint/parser_spec.rb' }
19
+ end
20
+
21
+ guard :rubocop, all_on_start: false, cli: ['--auto-correct-all'] do
22
+ watch(/.+\.rb$/)
23
+ end
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2021 alpaca-tc
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,95 @@
1
+ # textlint-ruby
2
+
3
+ Ruby AST parser for [textlint-ruby-plugin](https://github.com/alpaca-tc/textlint-ruby-plugin).
4
+
5
+ ## Installation
6
+
7
+ **TODO: release gems**
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'textlint-ruby'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle install
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install textlint-ruby
22
+
23
+ ## Usage
24
+
25
+ Parse ruby to textlint AST
26
+
27
+ ```sh
28
+ $ textlint-ruby ./path/to/file.rb
29
+ {"type":"Document","raw":"...","range":[0,465],"loc":{"start":{"line":1,"column":0},"end":{"line":26,"column":0}},"children":[...]}
30
+
31
+ # textlint-ruby-optimized is 10x faster but many features of ruby are disabled.
32
+ $ textlint-ruby-optimized ./path/to/file.rb
33
+ ```
34
+
35
+ ### Supported nodes
36
+
37
+ These types are defined in `@textlint/ast-node-types`.
38
+ Supported node types are only `Document` and `Str` because this plugin is used to format strings embedded in ruby programs.
39
+
40
+ | Type name | Node type | Supported |
41
+ | ------------------------------- | ------------- | ------------------------------------ |
42
+ | ASTNodeTypes.Document | TxtParentNode | yes |
43
+ | ASTNodeTypes.DocumentExit | TxtParentNode | |
44
+ | ASTNodeTypes.Paragraph | TxtParentNode | |
45
+ | ASTNodeTypes.ParagraphExit | TxtParentNode | |
46
+ | ASTNodeTypes.BlockQuote | TxtParentNode | |
47
+ | ASTNodeTypes.BlockQuoteExit | TxtParentNode | |
48
+ | ASTNodeTypes.List | TxtParentNode | |
49
+ | ASTNodeTypes.ListExit | TxtParentNode | |
50
+ | ASTNodeTypes.ListItem | TxtParentNode | |
51
+ | ASTNodeTypes.ListItemExit | TxtParentNode | |
52
+ | ASTNodeTypes.Header | TxtParentNode | |
53
+ | ASTNodeTypes.HeaderExit | TxtParentNode | |
54
+ | ASTNodeTypes.CodeBlock | TxtParentNode | |
55
+ | ASTNodeTypes.CodeBlockExit | TxtParentNode | |
56
+ | ASTNodeTypes.HtmlBlock | TxtParentNode | |
57
+ | ASTNodeTypes.HtmlBlockExit | TxtParentNode | |
58
+ | ASTNodeTypes.Link | TxtParentNode | |
59
+ | ASTNodeTypes.LinkExit | TxtParentNode | |
60
+ | ASTNodeTypes.Delete | TxtParentNode | |
61
+ | ASTNodeTypes.DeleteExit | TxtParentNode | |
62
+ | ASTNodeTypes.Emphasis | TxtParentNode | |
63
+ | ASTNodeTypes.EmphasisExit | TxtParentNode | |
64
+ | ASTNodeTypes.Strong | TxtParentNode | |
65
+ | ASTNodeTypes.StrongExit | TxtParentNode | |
66
+ | ASTNodeTypes.Break | TxtNode | |
67
+ | ASTNodeTypes.BreakExit | TxtNode | |
68
+ | ASTNodeTypes.Image | TxtNode | |
69
+ | ASTNodeTypes.ImageExit | TxtNode | |
70
+ | ASTNodeTypes.HorizontalRule | TxtNode | |
71
+ | ASTNodeTypes.HorizontalRuleExit | TxtNode | |
72
+ | ASTNodeTypes.Comment | TxtTextNode | |
73
+ | ASTNodeTypes.CommentExit | TxtTextNode | |
74
+ | ASTNodeTypes.Str | TxtTextNode | yes |
75
+ | ASTNodeTypes.StrExit | TxtTextNode | |
76
+ | ASTNodeTypes.Code | TxtTextNode | |
77
+ | ASTNodeTypes.CodeExit | TxtTextNode | |
78
+ | ASTNodeTypes.Html | TxtTextNode | |
79
+ | ASTNodeTypes.HtmlExit | TxtTextNode | |
80
+
81
+ ## Development
82
+
83
+ Run `bundle exec rspec` to run the tests.
84
+
85
+ ## Contributing
86
+
87
+ Bug reports and pull requests are welcome on GitHub at https://github.com/alpaca-tc/textlint-ruby. 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/alpaca-tc/textlint-ruby/blob/master/CODE_OF_CONDUCT.md).
88
+
89
+ ## License
90
+
91
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
92
+
93
+ ## Code of Conduct
94
+
95
+ Everyone interacting in the `textlint-ruby` project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/alpaca-tc/textlint-ruby/blob/master/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]
data/bin/textlint-ruby ADDED
@@ -0,0 +1,25 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ $LOAD_PATH.unshift(File.expand_path('../lib', __dir__))
5
+
6
+ require 'textlint'
7
+
8
+ path = ARGV[0]
9
+
10
+ unless File.exist?(path.to_s)
11
+ warn("Error: No such file or directory: #{path}")
12
+ exit(1)
13
+ end
14
+
15
+ content = File.read(path)
16
+
17
+ begin
18
+ ast = Textlint::Parser.parse(content)
19
+ puts(ast.as_textlint_json.to_json)
20
+ rescue Textlint::SyntaxError => error
21
+ warn("Failed to compile: #{path}")
22
+ warn('')
23
+ warn(error)
24
+ exit(1)
25
+ end
@@ -0,0 +1,4 @@
1
+ #!/usr/bin/env ruby --disable-all
2
+ # frozen_string_literal: true
3
+
4
+ load "#{__dir__}/textlint-ruby"
@@ -0,0 +1,213 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Textlint
4
+ # Clone nodes from [textlint/ast-node-types](https://github.com/textlint/textlint/blob/d4c5075b5a7bcbf875d7cee122de7dc5253d325e/packages/%40textlint/ast-node-types/src/index.ts)
5
+ module Nodes
6
+ DOCUMENT = 'Document'
7
+ DOCUMENT_EXIT = 'Document:exit'
8
+ PARAGRAPH = 'Paragraph'
9
+ PARAGRAPH_EXIT = 'Paragraph:exit'
10
+ BLOCK_QUOTE = 'BlockQuote'
11
+ BLOCK_QUOTE_EXIT = 'BlockQuote:exit'
12
+ LIST_ITEM = 'ListItem'
13
+ LIST_ITEM_EXIT = 'ListItem:exit'
14
+ LIST = 'List'
15
+ LIST_EXIT = 'List:exit'
16
+ HEADER = 'Header'
17
+ HEADER_EXIT = 'Header:exit'
18
+ CODE_BLOCK = 'CodeBlock'
19
+ CODE_BLOCK_EXIT = 'CodeBlock:exit'
20
+ HTML_BLOCK = 'HtmlBlock'
21
+ HTML_BLOCK_EXIT = 'HtmlBlock:exit'
22
+ HORIZONTAL_RULE = 'HorizontalRule'
23
+ HORIZONTAL_RULE_EXIT = 'HorizontalRule:exit'
24
+ COMMENT = 'Comment'
25
+ COMMENT_EXIT = 'Comment:exit'
26
+ STR = 'Str'
27
+ STR_EXIT = 'Str:exit'
28
+ BREAK = 'Break' # well-known Hard Break
29
+ BREAK_EXIT = 'Break:exit' # well-known Hard Break
30
+ EMPHASIS = 'Emphasis'
31
+ EMPHASIS_EXIT = 'Emphasis:exit'
32
+ STRONG = 'Strong'
33
+ STRONG_EXIT = 'Strong:exit'
34
+ HTML = 'Html'
35
+ HTML_EXIT = 'Html:exit'
36
+ LINK = 'Link'
37
+ LINK_EXIT = 'Link:exit'
38
+ IMAGE = 'Image'
39
+ IMAGE_EXIT = 'Image:exit'
40
+ CODE = 'Code'
41
+ CODE_EXIT = 'Code:exit'
42
+ DELETE = 'Delete'
43
+ DELETE_EXIT = 'Delete:exit'
44
+
45
+ TXT_NODE_TYPES = [
46
+ DOCUMENT,
47
+ DOCUMENT_EXIT,
48
+ PARAGRAPH,
49
+ PARAGRAPH_EXIT,
50
+ BLOCK_QUOTE,
51
+ BLOCK_QUOTE_EXIT,
52
+ LIST_ITEM,
53
+ LIST_ITEM_EXIT,
54
+ LIST,
55
+ LIST_EXIT,
56
+ HEADER,
57
+ HEADER_EXIT,
58
+ CODE_BLOCK,
59
+ CODE_BLOCK_EXIT,
60
+ HTML_BLOCK,
61
+ HTML_BLOCK_EXIT,
62
+ HORIZONTAL_RULE,
63
+ HORIZONTAL_RULE_EXIT,
64
+ COMMENT,
65
+ COMMENT_EXIT,
66
+ STR,
67
+ STR_EXIT,
68
+ BREAK,
69
+ BREAK_EXIT,
70
+ EMPHASIS,
71
+ EMPHASIS_EXIT,
72
+ STRONG,
73
+ STRONG_EXIT,
74
+ HTML,
75
+ HTML_EXIT,
76
+ LINK,
77
+ LINK_EXIT,
78
+ IMAGE,
79
+ IMAGE_EXIT,
80
+ CODE,
81
+ CODE_EXIT,
82
+ DELETE,
83
+ DELETE_EXIT
84
+ ].freeze
85
+
86
+ # export interface TxtNode {
87
+ # type: TxtNodeType;
88
+ # raw: string;
89
+ # range: TextNodeRange;
90
+ # loc: TxtNodeLineLocation;
91
+ # // parent is runtime information
92
+ # // Not need in AST
93
+ # // For example, top Root Node like `Document` has not parent.
94
+ # parent?: TxtNode;
95
+ #
96
+ # [index: string]: any;
97
+ # }
98
+ class TxtNode
99
+ attr_reader :type, :raw, :range, :loc
100
+
101
+ # @param type [String]
102
+ # @param raw [String]
103
+ # @param range [Range]
104
+ # @param loc [Textlint::Nodes::TxtNodeLineLocation]
105
+ def initialize(type:, raw:, range:, loc:)
106
+ @type = type
107
+ @raw = raw
108
+ @range = range
109
+ @loc = loc
110
+ end
111
+
112
+ # @return [Hash]
113
+ def as_textlint_json
114
+ {
115
+ type: type,
116
+ raw: raw,
117
+ range: [
118
+ range.begin,
119
+ (range.exclude_end? ? range.end : range.end - 1)
120
+ ],
121
+ loc: loc.as_textlint_json
122
+ }
123
+ end
124
+ end
125
+
126
+ # export interface TxtParentNode extends TxtNode {
127
+ # children: Array<TxtNode | TxtTextNode>;
128
+ # }
129
+ class TxtParentNode < TxtNode
130
+ attr_reader :children
131
+
132
+ # @param children [Array<Textlint::Nodes::TxtNode>]
133
+ def initialize(children: [], **attributes)
134
+ @children = children
135
+ super(**attributes)
136
+ end
137
+
138
+ # @return [Hash]
139
+ def as_textlint_json
140
+ h = super
141
+ h[:children] = children.map(&:as_textlint_json)
142
+ h
143
+ end
144
+ end
145
+
146
+ # export interface TxtNodeLineLocation {
147
+ # start: TxtNodePosition;
148
+ # end: TxtNodePosition;
149
+ # }
150
+ class TxtNodeLineLocation
151
+ attr_reader :start, :end
152
+
153
+ # @param start [Textlint::Nodes::TxtNodePosition]
154
+ # @param end [Textlint::Nodes::TxtNodePosition]
155
+ def initialize(start:, end:)
156
+ @start = start
157
+ @end = binding.local_variable_get(:end)
158
+ end
159
+
160
+ # @return [Hash]
161
+ def as_textlint_json
162
+ {
163
+ start: start.as_textlint_json,
164
+ end: self.end.as_textlint_json
165
+ }
166
+ end
167
+ end
168
+
169
+ # export interface TxtTextNode extends TxtNode {
170
+ # value: string;
171
+ # }
172
+ class TxtTextNode < TxtNode
173
+ attr_reader :value
174
+
175
+ # @param value [String]
176
+ # @param attributes [Hash]
177
+ def initialize(value:, **attributes)
178
+ @value = value
179
+ super(**attributes)
180
+ end
181
+
182
+ # @return [Hash]
183
+ def as_textlint_json
184
+ h = super
185
+ h[:value] = value
186
+ h
187
+ end
188
+ end
189
+
190
+ # export interface TxtNodePosition {
191
+ # line: number; // start with 1
192
+ # column: number; // start with 0
193
+ # }
194
+ class TxtNodePosition
195
+ attr_reader :line, :column
196
+
197
+ # @param line [Integer]
198
+ # @param column [Integer]
199
+ def initialize(line:, column:)
200
+ @line = line
201
+ @column = column
202
+ end
203
+
204
+ # @return [Hash]
205
+ def as_textlint_json
206
+ {
207
+ line: line,
208
+ column: column
209
+ }
210
+ end
211
+ end
212
+ end
213
+ end
@@ -0,0 +1,127 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'ripper'
4
+
5
+ module Textlint
6
+ class Parser
7
+ class RubyToTextlintAST < ::Ripper::Filter
8
+ # @param src [String]
9
+ # @param lines [Array<String>]
10
+ def initialize(src)
11
+ super(src)
12
+ @src = src
13
+ @pos = 0
14
+ @lines = @src.lines
15
+ end
16
+
17
+ private
18
+
19
+ # NOTE: Instance variables are allowed to assign only here to readable code.
20
+ def on_default(event, token, node)
21
+ @token = token
22
+
23
+ method_name = :"custom_#{event}"
24
+
25
+ if respond_to?(method_name, true)
26
+ @range = @pos...(@pos + @token.size)
27
+ @raw = @src[@range]
28
+ node = send(method_name, node)
29
+ end
30
+
31
+ @pos += @token.size
32
+
33
+ node
34
+ end
35
+
36
+ def default_node_attributes(type:, **attributes)
37
+ {
38
+ type: type,
39
+ raw: @raw,
40
+ range: @range,
41
+ loc: Textlint::Nodes::TxtNodeLineLocation.new(
42
+ start: Textlint::Nodes::TxtNodePosition.new(
43
+ line: lineno,
44
+ column: column
45
+ ),
46
+ end: end_txt_node_position
47
+ )
48
+ }.merge(attributes)
49
+ end
50
+
51
+ def custom_on_tstring_content(parentNode)
52
+ node = Textlint::Nodes::TxtTextNode.new(
53
+ **default_node_attributes(
54
+ type: Textlint::Nodes::STR,
55
+ value: @token
56
+ )
57
+ )
58
+
59
+ parentNode.children.push(node)
60
+
61
+ parentNode
62
+ end
63
+
64
+ def end_txt_node_position
65
+ break_count = @token.scan(Textlint::BREAK_RE).size
66
+
67
+ last_column = if break_count == 0
68
+ column + @token.size
69
+ else
70
+ @token.match(LAST_LINE_RE).to_s.size
71
+ end
72
+
73
+ Textlint::Nodes::TxtNodePosition.new(
74
+ line: lineno + break_count,
75
+ column: last_column
76
+ )
77
+ end
78
+ end
79
+
80
+ # Parse ruby code to AST for textlint
81
+ #
82
+ # @param src [String]
83
+ #
84
+ # @return [Textlint::Nodes::TxtParentNode]
85
+ def self.parse(src)
86
+ new(src).call
87
+ end
88
+
89
+ # @param src [String] ruby source code
90
+ def initialize(src)
91
+ @src = src
92
+ end
93
+
94
+ # Parse ruby code to AST for textlint
95
+ #
96
+ # @return [Textlint::Nodes::TxtParentNode]
97
+ def call
98
+ check_syntax!
99
+
100
+ document = Textlint::Nodes::TxtParentNode.new(
101
+ type: Textlint::Nodes::DOCUMENT,
102
+ raw: @src,
103
+ range: 0...@src.size,
104
+ loc: Textlint::Nodes::TxtNodeLineLocation.new(
105
+ start: Textlint::Nodes::TxtNodePosition.new(
106
+ line: 1,
107
+ column: 0
108
+ ),
109
+ end: Textlint::Nodes::TxtNodePosition.new(
110
+ line: @src.split(Textlint::BREAK_RE).size + 1,
111
+ column: @src.match(LAST_LINE_RE).to_s.size # extract last line
112
+ )
113
+ )
114
+ )
115
+
116
+ RubyToTextlintAST.new(@src).parse(document)
117
+ end
118
+
119
+ private
120
+
121
+ def check_syntax!
122
+ RubyVM::InstructionSequence.compile(@src)
123
+ rescue ::SyntaxError => error
124
+ raise Textlint::SyntaxError, error.message
125
+ end
126
+ end
127
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Textlint
4
+ VERSION = '0.1.0'
5
+ end
@@ -0,0 +1,3 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'textlint'
data/lib/textlint.rb ADDED
@@ -0,0 +1,13 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'json'
4
+ require 'textlint/version'
5
+ require 'textlint/nodes'
6
+ require 'textlint/parser'
7
+
8
+ module Textlint
9
+ BREAK_RE = /\r?\n/.freeze
10
+ LAST_LINE_RE = /(?!\r?\n).*\z/.freeze
11
+
12
+ class SyntaxError < StandardError; end
13
+ end
data/textlint.gemspec ADDED
@@ -0,0 +1,29 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'lib/textlint/version'
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = 'textlint-ruby'
7
+ spec.version = Textlint::VERSION
8
+ spec.authors = ['alpaca-tc']
9
+ spec.email = ['alpaca-tc@alpaca.tc']
10
+
11
+ spec.summary = 'ruby source code parser for textlint'
12
+ spec.description = ''
13
+ spec.homepage = 'https://github.com/alpaca-tc/textlint-ruby'
14
+ spec.license = 'MIT'
15
+ spec.required_ruby_version = '>= 2.6.0'
16
+
17
+ spec.metadata['homepage_uri'] = spec.homepage
18
+ spec.metadata['source_code_uri'] = 'https://github.com/alpaca-tc/textlint-ruby'
19
+ spec.metadata['changelog_uri'] = 'https://github.com/alpaca-tc/textlint-ruby/blob/main/CHANGELOG.md'
20
+
21
+ # Specify which files should be added to the gem when it is released.
22
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
23
+ spec.files = Dir.chdir(File.expand_path(__dir__)) do
24
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{\A(?:test|spec|features)/}) }
25
+ end
26
+ spec.bindir = 'bin'
27
+ spec.executables = spec.files.grep(%r{\Abin/}) { |f| File.basename(f) }
28
+ spec.require_paths = ['lib']
29
+ end
metadata ADDED
@@ -0,0 +1,67 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: textlint-ruby
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - alpaca-tc
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2021-11-29 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: ''
14
+ email:
15
+ - alpaca-tc@alpaca.tc
16
+ executables:
17
+ - textlint-ruby
18
+ - textlint-ruby-optimized
19
+ extensions: []
20
+ extra_rdoc_files: []
21
+ files:
22
+ - ".circleci/config.yml"
23
+ - ".gitignore"
24
+ - ".rspec"
25
+ - ".rubocop.yml"
26
+ - CHANGELOG.md
27
+ - CODE_OF_CONDUCT.md
28
+ - Gemfile
29
+ - Guardfile
30
+ - LICENSE.txt
31
+ - README.md
32
+ - Rakefile
33
+ - bin/textlint-ruby
34
+ - bin/textlint-ruby-optimized
35
+ - lib/textlint-ruby.rb
36
+ - lib/textlint.rb
37
+ - lib/textlint/nodes.rb
38
+ - lib/textlint/parser.rb
39
+ - lib/textlint/version.rb
40
+ - textlint.gemspec
41
+ homepage: https://github.com/alpaca-tc/textlint-ruby
42
+ licenses:
43
+ - MIT
44
+ metadata:
45
+ homepage_uri: https://github.com/alpaca-tc/textlint-ruby
46
+ source_code_uri: https://github.com/alpaca-tc/textlint-ruby
47
+ changelog_uri: https://github.com/alpaca-tc/textlint-ruby/blob/main/CHANGELOG.md
48
+ post_install_message:
49
+ rdoc_options: []
50
+ require_paths:
51
+ - lib
52
+ required_ruby_version: !ruby/object:Gem::Requirement
53
+ requirements:
54
+ - - ">="
55
+ - !ruby/object:Gem::Version
56
+ version: 2.6.0
57
+ required_rubygems_version: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ requirements: []
63
+ rubygems_version: 3.2.32
64
+ signing_key:
65
+ specification_version: 4
66
+ summary: ruby source code parser for textlint
67
+ test_files: []