packwerk_slim_template 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 9fd735a5469d37ee86529f99a8e2878ae65cc2615c7902842de944ea38bfe752
4
+ data.tar.gz: '049d75aa416b08b1750982f4770dd9e82757620c612788c70c9f4a66d0a8ff6d'
5
+ SHA512:
6
+ metadata.gz: 68f13e9594755d317676ecca7d96c96414d4c8d1fdf0ce089b8ec3ee5e2f6939ee9a690673b78aad709817ce54f1ede9e409e6b17ad20f95ab812007a56e8ecb
7
+ data.tar.gz: d12cc1b600eeb02b06c294013a9512d5af5b7518354be7c02ca1e4539f303c429a6173b6961967b50f4fe818cce8ecfdda0d189d1024a69fd03d517124423721
@@ -0,0 +1,10 @@
1
+ # Code of Conduct
2
+
3
+ "packwerk_slim_template" follows [The Ruby Community Conduct Guideline](https://www.ruby-lang.org/en/conduct) in all "collaborative space", which is defined as community communications channels (such as mailing lists, submitted patches, commit comments, etc.):
4
+
5
+ * Participants will be tolerant of opposing views.
6
+ * Participants must ensure that their language and actions are free of personal attacks and disparaging personal remarks.
7
+ * When interpreting the words and actions of others, participants should always assume good intentions.
8
+ * Behaviour which can be reasonably considered harassment will not be tolerated.
9
+
10
+ If you have any concerns about behaviour within this project, please contact us at ["yuuji.yaginuma@gmail.com"](mailto:"yuuji.yaginuma@gmail.com").
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2026 Yuji Yaginuma
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,44 @@
1
+ # PackwerkSlimTemplate
2
+
3
+ PackwerkSlimTemplate supports Slim templates in [Packwerk](https://github.com/Shopify/packwerk)
4
+
5
+ ## Installation
6
+
7
+ Add the gem to your application's `Gemfile`:
8
+
9
+ ```ruby
10
+ gem "packwerk_slim_template"
11
+ ```
12
+
13
+ Then install it:
14
+
15
+ ```bash
16
+ bundle install
17
+ ```
18
+
19
+ ## Usage
20
+
21
+ 1. Ensure Packwerk knows it should scan Slim files. Update `config/packwerk.yml` (or your equivalent Packwerk configuration) so the `include` list contains Slim alongside the existing extensions:
22
+
23
+ ```yaml
24
+ include:
25
+ - "**/*.{rb,rake,erb,slim}"
26
+ ```
27
+
28
+ 2. Run Packwerk as usual:
29
+
30
+ ## Development
31
+
32
+ After checking out the repo, run `bin/setup` to install dependencies. Then run `bundle rake test` to execute the test suite. You can also run `bin/console` for an interactive prompt that lets you experiment with the converters.
33
+
34
+ ## Contributing
35
+
36
+ Bug reports and pull requests are welcome on GitHub at https://github.com/y-yagi/packwerk_slim_template. 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/y-yagi/packwerk_slim_template/blob/main/CODE_OF_CONDUCT.md).
37
+
38
+ ## License
39
+
40
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
41
+
42
+ ## Code of Conduct
43
+
44
+ Everyone interacting in the PackwerkSlimTemplate project's codebases, issue trackers, chat rooms, and mailing lists is expected to follow the [code of conduct](https://github.com/y-yagi/packwerk_slim_template/blob/main/CODE_OF_CONDUCT.md).
data/Rakefile ADDED
@@ -0,0 +1,8 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bundler/gem_tasks"
4
+ require "minitest/test_task"
5
+
6
+ Minitest::TestTask.create
7
+
8
+ task default: :test
@@ -0,0 +1,25 @@
1
+ # frozen_string_literal: true
2
+
3
+ module PackwerkSlimTemplate
4
+ class Error < StandardError; end
5
+
6
+ class SlimSyntaxError < Error
7
+ attr_reader :file_path, :line_number, :column
8
+
9
+ def initialize(message, file_path:, line_number: nil, column: nil)
10
+ @file_path = file_path
11
+ @line_number = line_number
12
+ @column = column
13
+ super(formatted_message(message))
14
+ end
15
+
16
+ private
17
+
18
+ def formatted_message(msg)
19
+ location = [@file_path, @line_number, @column].compact.join(":")
20
+ "#{location} - #{msg}"
21
+ end
22
+ end
23
+
24
+ class ParseError < Error; end
25
+ end
@@ -0,0 +1,57 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "packwerk"
4
+ require "stringio"
5
+
6
+ module PackwerkSlimTemplate
7
+ class Parser
8
+ include Packwerk::Parsers::ParserInterface
9
+
10
+ def initialize(ruby_parser: Packwerk::Parsers::Ruby.new)
11
+ @ruby_parser = ruby_parser
12
+ end
13
+
14
+ def call(io:, file_path:)
15
+ slim_content = io.read
16
+ result = SlimConverter.convert(slim_content, file_path: file_path)
17
+
18
+ # If no Ruby code extracted, return nil (Packwerk will skip)
19
+ return nil if result.ruby_code.empty?
20
+
21
+ @ruby_parser.call(
22
+ io: StringIO.new(result.ruby_code),
23
+ file_path: file_path
24
+ )
25
+ rescue Slim::Parser::SyntaxError => e
26
+ parse_result = Packwerk::Parsers::ParseResult.new(
27
+ file: file_path,
28
+ message: "#{file_path} - Slim syntax error at line #{e.lineno}: #{e.message}"
29
+ )
30
+ raise Packwerk::Parsers::ParseError, parse_result
31
+ rescue Packwerk::Parsers::ParseError => e
32
+ if e.message.include?(file_path)
33
+ raise
34
+ else
35
+ parse_result = Packwerk::Parsers::ParseResult.new(
36
+ file: file_path,
37
+ message: "#{file_path} - #{e.message}"
38
+ )
39
+ raise Packwerk::Parsers::ParseError, parse_result
40
+ end
41
+ end
42
+ end
43
+ end
44
+
45
+ module PackwerkSlimTemplate
46
+ module FactoryExtension
47
+ SLIM_REGEX = /\.slim\Z/
48
+
49
+ def for_path(path)
50
+ return @slim_parser ||= PackwerkSlimTemplate::Parser.new if SLIM_REGEX.match?(path)
51
+
52
+ super
53
+ end
54
+ end
55
+ end
56
+
57
+ Packwerk::Parsers::Factory.prepend(PackwerkSlimTemplate::FactoryExtension)
@@ -0,0 +1,138 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "slim"
4
+
5
+ module PackwerkSlimTemplate
6
+ class LineMapper
7
+ def initialize
8
+ @mappings = {}
9
+ end
10
+
11
+ def add_mapping(ruby_line:, slim_line:, line_count: 1)
12
+ @mappings[ruby_line] = { slim_line: slim_line, span: line_count }
13
+ end
14
+
15
+ def slim_line_for(ruby_line)
16
+ mapping = @mappings.select { |k, v| k <= ruby_line && ruby_line < k + v[:span] }
17
+ .max_by { |k, _| k }
18
+ mapping&.last&.dig(:slim_line)
19
+ end
20
+
21
+ def to_h
22
+ @mappings
23
+ end
24
+ end
25
+
26
+
27
+ class SlimConverter
28
+ ConversionResult = Struct.new(:ruby_code, :line_mapper, :ruby_snippets)
29
+
30
+ def self.convert(slim_content, file_path:)
31
+ new(slim_content, file_path).convert
32
+ end
33
+
34
+ def initialize(slim_content, file_path)
35
+ @slim_content = slim_content
36
+ @file_path = file_path
37
+ @line_mapper = LineMapper.new
38
+ @ruby_snippets = []
39
+ @current_ruby_line = 1
40
+ end
41
+
42
+ def convert
43
+ return empty_result if @slim_content.empty?
44
+
45
+ ast = parse_slim(@slim_content)
46
+ extract_ruby_nodes(ast)
47
+
48
+ ruby_code = @ruby_snippets.map { |s| s[:code] }.join("\n")
49
+
50
+ ConversionResult.new(ruby_code, @line_mapper, @ruby_snippets)
51
+ rescue Slim::Parser::SyntaxError => e
52
+ raise SlimSyntaxError.new(
53
+ e.message,
54
+ file_path: @file_path,
55
+ line_number: e.lineno
56
+ )
57
+ end
58
+
59
+ private
60
+
61
+ def empty_result
62
+ ConversionResult.new("", @line_mapper, [])
63
+ end
64
+
65
+ def parse_slim(content)
66
+ Slim::Parser.new.call(content)
67
+ end
68
+
69
+ def extract_ruby_nodes(node, slim_line = 1)
70
+ return unless node.is_a?(Array)
71
+
72
+ case node.first
73
+ when :multi
74
+ node[1..].each_with_index { |child, idx| extract_ruby_nodes(child, slim_line + idx) }
75
+ when :slim
76
+ handle_slim_node(node, slim_line)
77
+ when :html
78
+ node[1..].each_with_index { |child, idx| extract_ruby_nodes(child, slim_line + idx) }
79
+ end
80
+ end
81
+
82
+ def handle_slim_node(node, slim_line)
83
+ case node[1]
84
+ when :output
85
+ # [:slim, :output, escape, code, content]
86
+ code = node[3]
87
+ add_ruby_snippet(code, slim_line) if code && !code.empty?
88
+
89
+ nested_nodes = node.length > 4 ? node[4..] : nil
90
+ has_block_content = nested_nodes&.any? { |child| significant_child_node?(child) }
91
+
92
+ # Process nested content if present
93
+ if nested_nodes
94
+ nested_nodes.each_with_index do |child, idx|
95
+ extract_ruby_nodes(child, slim_line + idx + 1)
96
+ end
97
+ end
98
+
99
+ add_ruby_snippet("end", slim_line + (nested_nodes&.length || 0)) if has_block_content
100
+ when :control
101
+ # [:slim, :control, code, content]
102
+ code = node[2]
103
+ add_ruby_snippet(code, slim_line) if code && !code.empty?
104
+
105
+ # Process nested content (at index 3)
106
+ if node[3]
107
+ extract_ruby_nodes(node[3], slim_line + 1)
108
+ end
109
+
110
+ # Add closing 'end' for control structures
111
+ add_ruby_snippet("end", slim_line)
112
+ end
113
+ end
114
+
115
+ def add_ruby_snippet(code, slim_line)
116
+ @line_mapper.add_mapping(
117
+ ruby_line: @current_ruby_line,
118
+ slim_line: slim_line
119
+ )
120
+
121
+ @ruby_snippets << { code: code, slim_line: slim_line, ruby_line: @current_ruby_line }
122
+ @current_ruby_line += code.lines.count
123
+ end
124
+
125
+ def significant_child_node?(node)
126
+ return false unless node.is_a?(Array)
127
+
128
+ case node.first
129
+ when :newline
130
+ false
131
+ when :multi
132
+ node[1..].any? { |child| significant_child_node?(child) }
133
+ else
134
+ true
135
+ end
136
+ end
137
+ end
138
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module PackwerkSlimTemplate
4
+ VERSION = "0.1.0"
5
+ end
@@ -0,0 +1,9 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "packwerk_slim_template/version"
4
+ require_relative "packwerk_slim_template/errors"
5
+ require_relative "packwerk_slim_template/slim_converter"
6
+ require_relative "packwerk_slim_template/parser"
7
+
8
+ module PackwerkSlimTemplate
9
+ end
metadata ADDED
@@ -0,0 +1,91 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: packwerk_slim_template
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Yuji Yaginuma
8
+ bindir: exe
9
+ cert_chain: []
10
+ date: 1980-01-02 00:00:00.000000000 Z
11
+ dependencies:
12
+ - !ruby/object:Gem::Dependency
13
+ name: packwerk
14
+ requirement: !ruby/object:Gem::Requirement
15
+ requirements:
16
+ - - "~>"
17
+ - !ruby/object:Gem::Version
18
+ version: '3.0'
19
+ type: :runtime
20
+ prerelease: false
21
+ version_requirements: !ruby/object:Gem::Requirement
22
+ requirements:
23
+ - - "~>"
24
+ - !ruby/object:Gem::Version
25
+ version: '3.0'
26
+ - !ruby/object:Gem::Dependency
27
+ name: slim
28
+ requirement: !ruby/object:Gem::Requirement
29
+ requirements:
30
+ - - "~>"
31
+ - !ruby/object:Gem::Version
32
+ version: '5.0'
33
+ type: :runtime
34
+ prerelease: false
35
+ version_requirements: !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - "~>"
38
+ - !ruby/object:Gem::Version
39
+ version: '5.0'
40
+ - !ruby/object:Gem::Dependency
41
+ name: parser
42
+ requirement: !ruby/object:Gem::Requirement
43
+ requirements:
44
+ - - "~>"
45
+ - !ruby/object:Gem::Version
46
+ version: '3.0'
47
+ type: :runtime
48
+ prerelease: false
49
+ version_requirements: !ruby/object:Gem::Requirement
50
+ requirements:
51
+ - - "~>"
52
+ - !ruby/object:Gem::Version
53
+ version: '3.0'
54
+ email:
55
+ - yuuji.yaginuma@gmail.com
56
+ executables: []
57
+ extensions: []
58
+ extra_rdoc_files: []
59
+ files:
60
+ - CODE_OF_CONDUCT.md
61
+ - LICENSE.txt
62
+ - README.md
63
+ - Rakefile
64
+ - lib/packwerk_slim_template.rb
65
+ - lib/packwerk_slim_template/errors.rb
66
+ - lib/packwerk_slim_template/parser.rb
67
+ - lib/packwerk_slim_template/slim_converter.rb
68
+ - lib/packwerk_slim_template/version.rb
69
+ homepage: https://github.com/y-yagi/packwerk_slim_template
70
+ licenses:
71
+ - MIT
72
+ metadata:
73
+ homepage_uri: https://github.com/y-yagi/packwerk_slim_template
74
+ rdoc_options: []
75
+ require_paths:
76
+ - lib
77
+ required_ruby_version: !ruby/object:Gem::Requirement
78
+ requirements:
79
+ - - ">="
80
+ - !ruby/object:Gem::Version
81
+ version: 3.2.0
82
+ required_rubygems_version: !ruby/object:Gem::Requirement
83
+ requirements:
84
+ - - ">="
85
+ - !ruby/object:Gem::Version
86
+ version: '0'
87
+ requirements: []
88
+ rubygems_version: 4.0.3
89
+ specification_version: 4
90
+ summary: Slim support for packwerk
91
+ test_files: []