ts-morph 0.1.1

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: a6b5b9fdaac8abc66b8c8ae99cd6d7632538fae7741234df6bc1212a54d7bd67
4
+ data.tar.gz: d12a438996b9c0ac99665899db97c2a6b262a89175157778a2b1e854dd8573b6
5
+ SHA512:
6
+ metadata.gz: 051e9cea3cabdca69b383191947dd2258d995cf4b320cb0d4248e53063f6967b7a0c0f7bbed304197ea918a15210d547da566c0257326381e5f142c7eddf414f
7
+ data.tar.gz: 1b5da2e9707139b8487785fad9dfccb8fa6ba30cfa09b0ef264848da0e05422741689247439fa885de591be8c7c4acb7499497f36d42fc2f340a44c770afb97f
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/.rubocop.yml ADDED
@@ -0,0 +1,8 @@
1
+ AllCops:
2
+ TargetRubyVersion: 3.1
3
+
4
+ Style/StringLiterals:
5
+ EnforcedStyle: double_quotes
6
+
7
+ Style/StringLiteralsInInterpolation:
8
+ EnforcedStyle: double_quotes
data/CHANGELOG.md ADDED
@@ -0,0 +1,33 @@
1
+ # Changelog
2
+
3
+ All notable changes to this project will be documented in this file.
4
+
5
+ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
6
+ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
7
+
8
+ ## [Unreleased]
9
+
10
+ ## [0.1.0] - 2025-09-09
11
+
12
+ ### Added
13
+ - Initial release of ts-morph Ruby gem
14
+ - Core transformation capabilities:
15
+ - Add default props to components
16
+ - Wrap components with other components
17
+ - Rename components
18
+ - Add import statements
19
+ - Inject inline styles
20
+ - Remove components
21
+ - Extract components
22
+ - TypeScript/TSX parsing to AST
23
+ - Code formatting functionality
24
+ - Comprehensive test suite
25
+ - Examples for basic usage and Rails integration
26
+ - Full documentation
27
+
28
+ ### Dependencies
29
+ - Requires Node.js and npm for ts-morph integration
30
+ - Ruby >= 3.1.0
31
+
32
+ [Unreleased]: https://github.com/saulfuhrmann/ts-morph-ruby/compare/v0.1.0...HEAD
33
+ [0.1.0]: https://github.com/saulfuhrmann/ts-morph-ruby/releases/tag/v0.1.0
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2025 Socratic Systems
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/PUBLISHING.md ADDED
@@ -0,0 +1,184 @@
1
+ # Publishing ts-morph Ruby Gem
2
+
3
+ This guide walks through the steps to publish the ts-morph gem to RubyGems.org and set up the GitHub repository.
4
+
5
+ ## Prerequisites
6
+
7
+ 1. **RubyGems.org Account**
8
+ - Sign up at https://rubygems.org/sign_up if you don't have an account
9
+ - Note your username and password
10
+
11
+ 2. **GitHub Account**
12
+ - Ensure you're logged into GitHub
13
+ - Have SSH keys set up for GitHub
14
+
15
+ ## Step 1: Set up GitHub Repository
16
+
17
+ ```bash
18
+ # Make sure you're in the gem directory
19
+ cd /Users/saulfuhrmann/dev/ts-morph-ruby
20
+
21
+ # Add all files and commit
22
+ git add .
23
+ git commit -m "Initial commit: ts-morph Ruby gem"
24
+
25
+ # Create repository on GitHub (using GitHub CLI)
26
+ gh repo create saulfuhrmann/ts-morph-ruby --public --source=. --remote=origin --push
27
+ ```
28
+
29
+ If you don't have GitHub CLI, create the repository manually:
30
+ 1. Go to https://github.com/new
31
+ 2. Repository name: `ts-morph-ruby`
32
+ 3. Description: "Ruby wrapper for ts-morph TypeScript Compiler API"
33
+ 4. Make it public
34
+ 5. Don't initialize with README (we already have one)
35
+ 6. Create repository
36
+
37
+ Then push your code:
38
+ ```bash
39
+ git remote add origin git@github.com:saulfuhrmann/ts-morph-ruby.git
40
+ git branch -M main
41
+ git push -u origin main
42
+ ```
43
+
44
+ ## Step 2: Run Tests and Linting
45
+
46
+ Before publishing, ensure everything passes:
47
+
48
+ ```bash
49
+ # Install dependencies
50
+ bundle install
51
+
52
+ # Run tests
53
+ bundle exec rake spec
54
+
55
+ # Run linting
56
+ bundle exec rake rubocop
57
+
58
+ # Or run both (default task)
59
+ bundle exec rake
60
+ ```
61
+
62
+ ## Step 3: Build the Gem
63
+
64
+ ```bash
65
+ # Build the gem
66
+ gem build ts-morph.gemspec
67
+
68
+ # This creates ts-morph-0.1.0.gem
69
+ ```
70
+
71
+ ## Step 4: Test Local Installation
72
+
73
+ ```bash
74
+ # Install locally to test
75
+ gem install ./ts-morph-0.1.0.gem
76
+
77
+ # Test in IRB
78
+ irb
79
+ require 'ts/morph'
80
+ Ts::Morph::VERSION
81
+ exit
82
+ ```
83
+
84
+ ## Step 5: Set up RubyGems Credentials
85
+
86
+ ```bash
87
+ # Set up your RubyGems API key
88
+ gem signin
89
+
90
+ # Or if you already have credentials
91
+ curl -u YOUR_USERNAME https://rubygems.org/api/v1/api_key.yaml > ~/.gem/credentials
92
+ chmod 0600 ~/.gem/credentials
93
+ ```
94
+
95
+ ## Step 6: Publish to RubyGems.org
96
+
97
+ ```bash
98
+ # Push the gem to RubyGems.org
99
+ gem push ts-morph-0.1.0.gem
100
+ ```
101
+
102
+ ## Step 7: Verify Publication
103
+
104
+ 1. Visit https://rubygems.org/gems/ts-morph
105
+ 2. Verify the gem appears with correct information
106
+ 3. Test installation from RubyGems:
107
+
108
+ ```bash
109
+ # In a different directory
110
+ gem install ts-morph
111
+ ```
112
+
113
+ ## Step 8: Tag the Release
114
+
115
+ ```bash
116
+ # Create a git tag for the version
117
+ git tag -a v0.1.0 -m "Release version 0.1.0"
118
+ git push origin v0.1.0
119
+ ```
120
+
121
+ ## Step 9: Create GitHub Release
122
+
123
+ Using GitHub CLI:
124
+ ```bash
125
+ gh release create v0.1.0 --title "v0.1.0" --notes "Initial release of ts-morph Ruby gem"
126
+ ```
127
+
128
+ Or manually:
129
+ 1. Go to https://github.com/saulfuhrmann/ts-morph-ruby/releases
130
+ 2. Click "Create a new release"
131
+ 3. Choose tag v0.1.0
132
+ 4. Title: v0.1.0
133
+ 5. Add release notes
134
+ 6. Publish release
135
+
136
+ ## Updating the Gem
137
+
138
+ When you need to release a new version:
139
+
140
+ 1. Update version in `lib/ts/morph/version.rb`
141
+ 2. Update CHANGELOG.md
142
+ 3. Commit changes
143
+ 4. Build and push new version:
144
+
145
+ ```bash
146
+ git add .
147
+ git commit -m "Bump version to 0.2.0"
148
+ gem build ts-morph.gemspec
149
+ gem push ts-morph-0.2.0.gem
150
+ git tag -a v0.2.0 -m "Release version 0.2.0"
151
+ git push origin main --tags
152
+ ```
153
+
154
+ ## Troubleshooting
155
+
156
+ ### "gem push" fails with authentication error
157
+ - Ensure you've run `gem signin` with correct credentials
158
+ - Check ~/.gem/credentials exists and has correct permissions (600)
159
+
160
+ ### Name already taken on RubyGems
161
+ - The name "ts-morph" might be taken. If so, consider:
162
+ - `ts-morph-ruby`
163
+ - `ruby-ts-morph`
164
+ - `tsmorph`
165
+
166
+ ### Tests fail before publishing
167
+ - Ensure Node.js is installed
168
+ - Run `Ts::Morph.install_dependencies` manually
169
+ - Check for any linting errors with `bundle exec rubocop -a`
170
+
171
+ ## GitHub Actions CI
172
+
173
+ The gem includes GitHub Actions configuration. After pushing to GitHub:
174
+ 1. Go to Settings > Actions > General
175
+ 2. Ensure Actions are enabled
176
+ 3. The CI will run automatically on push and pull requests
177
+
178
+ ## Gem Maintenance
179
+
180
+ - Respond to issues on GitHub
181
+ - Keep dependencies updated
182
+ - Follow semantic versioning
183
+ - Update documentation as needed
184
+ - Consider adding more features based on user feedback
data/README.md ADDED
@@ -0,0 +1,287 @@
1
+ # ts-morph Ruby Gem
2
+
3
+ A Ruby wrapper for [ts-morph](https://github.com/dsherret/ts-morph), providing programmatic TypeScript and TSX code analysis and transformation capabilities.
4
+
5
+ ## Features
6
+
7
+ - Transform TypeScript/TSX code programmatically
8
+ - Parse TypeScript/TSX into AST
9
+ - Format TypeScript/TSX code
10
+ - Add/modify/remove components and imports
11
+ - Inject styles and props
12
+ - Extract and manipulate React components
13
+
14
+ ## Installation
15
+
16
+ Add this line to your application's Gemfile:
17
+
18
+ ```ruby
19
+ gem 'ts-morph'
20
+ ```
21
+
22
+ And then execute:
23
+
24
+ ```bash
25
+ bundle install
26
+ ```
27
+
28
+ Or install it yourself as:
29
+
30
+ ```bash
31
+ gem install ts-morph
32
+ ```
33
+
34
+ ### Node.js Dependencies
35
+
36
+ This gem requires Node.js and npm to be installed. After installing the gem, run:
37
+
38
+ ```ruby
39
+ require 'ts/morph'
40
+ Ts::Morph.install_dependencies
41
+ ```
42
+
43
+ This will install the necessary Node.js packages (ts-morph, TypeScript, and Babel).
44
+
45
+ ## Usage
46
+
47
+ ### Basic Transformation
48
+
49
+ ```ruby
50
+ require 'ts/morph'
51
+
52
+ code = <<~TSX
53
+ import React from 'react';
54
+
55
+ const MyComponent = () => {
56
+ return <Button>Click me</Button>;
57
+ };
58
+ TSX
59
+
60
+ # Transform the code
61
+ transformations = [
62
+ {
63
+ type: 'addDefaultProps',
64
+ component: 'Button',
65
+ props: { variant: 'primary', size: 'medium' }
66
+ }
67
+ ]
68
+
69
+ transformed = Ts::Morph.transform(code, transformations)
70
+ puts transformed
71
+ # Output includes: <Button variant="primary" size="medium">Click me</Button>
72
+ ```
73
+
74
+ ### Using Transformation Helpers
75
+
76
+ ```ruby
77
+ require 'ts/morph'
78
+ require 'ts/morph/transformations'
79
+
80
+ # Build transformations using helper methods
81
+ transformations = [
82
+ Ts::Morph::Transformations.add_default_props('Button', {
83
+ variant: 'primary',
84
+ size: 'medium'
85
+ }),
86
+ Ts::Morph::Transformations.add_imports([
87
+ Ts::Morph::Transformations.import_spec(
88
+ from: '@/components/ui',
89
+ named: ['Button', 'Card']
90
+ )
91
+ ]),
92
+ Ts::Morph::Transformations.wrap_components('form', 'FormProvider')
93
+ ]
94
+
95
+ transformed = Ts::Morph.transform(code, transformations)
96
+ ```
97
+
98
+ ### Parsing AST
99
+
100
+ ```ruby
101
+ code = <<~TSX
102
+ const App = () => <div>Hello World</div>;
103
+ TSX
104
+
105
+ ast = Ts::Morph.parse(code)
106
+ puts JSON.pretty_generate(ast)
107
+ ```
108
+
109
+ ### Formatting Code
110
+
111
+ ```ruby
112
+ ugly_code = "const App=()=><div>Hello</div>;"
113
+ formatted = Ts::Morph.format(ugly_code)
114
+ puts formatted
115
+ # Output: const App = () => <div>Hello</div>;
116
+ ```
117
+
118
+ ## Available Transformations
119
+
120
+ ### addDefaultProps
121
+ Add default props to components:
122
+
123
+ ```ruby
124
+ {
125
+ type: 'addDefaultProps',
126
+ component: 'Button',
127
+ props: { variant: 'primary', disabled: false }
128
+ }
129
+ ```
130
+
131
+ ### wrapComponents
132
+ Wrap components with another component:
133
+
134
+ ```ruby
135
+ {
136
+ type: 'wrapComponents',
137
+ target: 'form',
138
+ wrapper: 'FormProvider'
139
+ }
140
+ ```
141
+
142
+ ### renameComponents
143
+ Rename components throughout the file:
144
+
145
+ ```ruby
146
+ {
147
+ type: 'renameComponents',
148
+ oldName: 'Container',
149
+ newName: 'AppContainer'
150
+ }
151
+ ```
152
+
153
+ ### addImports
154
+ Add import statements:
155
+
156
+ ```ruby
157
+ {
158
+ type: 'addImports',
159
+ imports: [
160
+ { from: 'react', named: ['useState', 'useEffect'] },
161
+ { from: './utils', default: 'utils' }
162
+ ]
163
+ }
164
+ ```
165
+
166
+ ### injectStyles
167
+ Inject inline styles:
168
+
169
+ ```ruby
170
+ {
171
+ type: 'injectStyles',
172
+ styles: {
173
+ 'Container' => { padding: '1rem', maxWidth: '1200px' },
174
+ 'Card' => { borderRadius: '8px' }
175
+ }
176
+ }
177
+ ```
178
+
179
+ ### removeComponent
180
+ Remove all instances of a component:
181
+
182
+ ```ruby
183
+ {
184
+ type: 'removeComponent',
185
+ component: 'DebugPanel'
186
+ }
187
+ ```
188
+
189
+ ## Rails Integration
190
+
191
+ Example service for Rails applications:
192
+
193
+ ```ruby
194
+ # app/services/tsx_transformer_service.rb
195
+ class TsxTransformerService
196
+ include Ts::Morph::Transformations
197
+
198
+ def self.transform_for_site(tsx_code, site_config = {})
199
+ transformations = []
200
+
201
+ # Add consistent styling
202
+ if site_config[:theme]
203
+ transformations << inject_styles(theme_styles(site_config[:theme]))
204
+ end
205
+
206
+ # Standardize components
207
+ transformations << add_default_props('Button', {
208
+ variant: site_config[:button_variant] || 'primary'
209
+ })
210
+
211
+ # Add necessary imports
212
+ transformations << add_imports([
213
+ import_spec(from: '@/components/ui', named: ['Button', 'Card']),
214
+ import_spec(from: 'react', named: ['useState'])
215
+ ])
216
+
217
+ Ts::Morph.transform(tsx_code, transformations)
218
+ end
219
+
220
+ private
221
+
222
+ def self.theme_styles(theme)
223
+ case theme
224
+ when 'dark'
225
+ {
226
+ 'Container' => {
227
+ backgroundColor: '#1a1a1a',
228
+ color: '#ffffff'
229
+ }
230
+ }
231
+ else
232
+ {
233
+ 'Container' => {
234
+ backgroundColor: '#ffffff',
235
+ color: '#000000'
236
+ }
237
+ }
238
+ end
239
+ end
240
+ end
241
+ ```
242
+
243
+ ## Performance Considerations
244
+
245
+ For better performance in production:
246
+
247
+ 1. **Install dependencies once** during deployment
248
+ 2. **Consider running a Node.js service** for transformations instead of spawning processes
249
+ 3. **Cache transformed results** when possible
250
+
251
+ ## Error Handling
252
+
253
+ ```ruby
254
+ begin
255
+ transformed = Ts::Morph.transform(code, transformations)
256
+ rescue Ts::Morph::ServiceUnavailableError => e
257
+ # Node service not available, install dependencies
258
+ Ts::Morph.install_dependencies
259
+ retry
260
+ rescue Ts::Morph::TransformationError => e
261
+ # Handle transformation errors
262
+ Rails.logger.error "Transformation failed: #{e.message}"
263
+ end
264
+ ```
265
+
266
+ ## Development
267
+
268
+ After checking out the repo, run:
269
+
270
+ ```bash
271
+ bundle install
272
+ bundle exec rake spec
273
+ ```
274
+
275
+ To install this gem onto your local machine:
276
+
277
+ ```bash
278
+ bundle exec rake install
279
+ ```
280
+
281
+ ## Contributing
282
+
283
+ Bug reports and pull requests are welcome on GitHub at https://github.com/saulfuhrmann/ts-morph-ruby.
284
+
285
+ ## License
286
+
287
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,10 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bundler/gem_tasks"
4
+ require "rspec/core/rake_task"
5
+ require "rubocop/rake_task"
6
+
7
+ RSpec::Core::RakeTask.new(:spec)
8
+ RuboCop::RakeTask.new
9
+
10
+ task default: [:spec, :rubocop]
data/SUMMARY.md ADDED
@@ -0,0 +1,114 @@
1
+ # ts-morph Ruby Gem - Summary
2
+
3
+ ## What We've Created
4
+
5
+ We've built a complete Ruby gem that wraps the ts-morph TypeScript Compiler API, providing Ruby developers with powerful TypeScript/TSX transformation capabilities.
6
+
7
+ ### Key Features
8
+
9
+ 1. **TypeScript/TSX Transformations**
10
+ - Add default props to React components
11
+ - Wrap components with other components
12
+ - Rename components throughout files
13
+ - Add import statements
14
+ - Inject inline styles
15
+ - Remove/extract components
16
+
17
+ 2. **Additional Capabilities**
18
+ - Parse TypeScript/TSX to AST
19
+ - Format TypeScript/TSX code
20
+ - Rails integration examples
21
+
22
+ 3. **Complete Test Suite**
23
+ - Unit tests for all major functionality
24
+ - RSpec configuration
25
+ - CI/CD with GitHub Actions
26
+
27
+ 4. **Documentation**
28
+ - Comprehensive README
29
+ - Usage examples
30
+ - Rails integration guide
31
+ - Publishing instructions
32
+
33
+ ## Should We Add Specs?
34
+
35
+ ✅ **Already Done!** We've added a comprehensive test suite:
36
+ - `spec/ts/morph_spec.rb` - Main module tests
37
+ - `spec/ts/morph/transformer_spec.rb` - Transformation tests
38
+ - `spec/ts/morph/transformations_spec.rb` - Helper method tests
39
+ - Configured RSpec and included it in the default Rake task
40
+
41
+ ## How to Publish to RubyGems.org
42
+
43
+ ### Quick Steps:
44
+
45
+ 1. **Create GitHub Repository**
46
+ ```bash
47
+ gh repo create saulfuhrmann/ts-morph-ruby --public --source=. --remote=origin --push
48
+ ```
49
+
50
+ 2. **Run Tests**
51
+ ```bash
52
+ bundle exec rake
53
+ ```
54
+
55
+ 3. **Build Gem**
56
+ ```bash
57
+ gem build ts-morph.gemspec
58
+ ```
59
+
60
+ 4. **Sign in to RubyGems**
61
+ ```bash
62
+ gem signin
63
+ ```
64
+
65
+ 5. **Publish**
66
+ ```bash
67
+ gem push ts-morph-0.1.0.gem
68
+ ```
69
+
70
+ 6. **Tag Release**
71
+ ```bash
72
+ git tag -a v0.1.0 -m "Initial release"
73
+ git push origin v0.1.0
74
+ ```
75
+
76
+ ### Detailed Instructions
77
+
78
+ See `PUBLISHING.md` for comprehensive publishing instructions including:
79
+ - Setting up RubyGems credentials
80
+ - Testing local installation
81
+ - Creating GitHub releases
82
+ - Troubleshooting common issues
83
+ - Future version updates
84
+
85
+ ## File Structure
86
+
87
+ ```
88
+ ts-morph-ruby/
89
+ ├── lib/
90
+ │ └── ts/
91
+ │ ├── morph.rb # Main module
92
+ │ └── morph/
93
+ │ ├── version.rb # Version constant
94
+ │ ├── error.rb # Error classes
95
+ │ ├── node_service.rb # Node.js integration
96
+ │ ├── transformer.rb # Core transformation logic
97
+ │ └── transformations.rb # Helper methods
98
+ ├── spec/ # Test suite
99
+ ├── examples/ # Usage examples
100
+ ├── .github/workflows/main.yml # CI configuration
101
+ ├── README.md # Main documentation
102
+ ├── PUBLISHING.md # Publishing guide
103
+ ├── LICENSE.txt # MIT License
104
+ └── ts-morph.gemspec # Gem specification
105
+ ```
106
+
107
+ ## Next Steps
108
+
109
+ 1. **Push to GitHub** - The repository is ready to be pushed
110
+ 2. **Verify CI** - GitHub Actions will run tests automatically
111
+ 3. **Publish to RubyGems** - Follow PUBLISHING.md instructions
112
+ 4. **Announce** - Consider announcing on Ruby forums/social media
113
+
114
+ The gem is fully functional and ready for publication!