ruby_scribe 0.1.1 → 0.1.2

Sign up to get free protection for your applications and to get access to all the features.
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source "http://rubygems.org"
2
+
3
+ gemspec
@@ -0,0 +1,34 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ ruby_scribe (0.1.1)
5
+ activesupport (~> 3.0.10)
6
+ i18n (~> 0.6.0)
7
+ ruby_parser (~> 2.3.1)
8
+ thor (~> 0.13)
9
+
10
+ GEM
11
+ remote: http://rubygems.org/
12
+ specs:
13
+ activesupport (3.0.10)
14
+ diff-lcs (1.1.3)
15
+ i18n (0.6.0)
16
+ rspec (2.6.0)
17
+ rspec-core (~> 2.6.0)
18
+ rspec-expectations (~> 2.6.0)
19
+ rspec-mocks (~> 2.6.0)
20
+ rspec-core (2.6.4)
21
+ rspec-expectations (2.6.0)
22
+ diff-lcs (~> 1.1.2)
23
+ rspec-mocks (2.6.0)
24
+ ruby_parser (2.3.1)
25
+ sexp_processor (~> 3.0)
26
+ sexp_processor (3.0.7)
27
+ thor (0.14.6)
28
+
29
+ PLATFORMS
30
+ ruby
31
+
32
+ DEPENDENCIES
33
+ rspec (~> 2.0)
34
+ ruby_scribe!
@@ -1,10 +1,16 @@
1
1
  = Ruby Scribe
2
2
 
3
+ Generates nicely-formatted ruby source code given a ruby abstract syntax tree (from seattlerb's ruby_parser).
4
+
3
5
  == Introduction
4
6
 
5
- Seattle RB's ruby_parser is a great tool for parsing ruby code into syntax trees. Ruby Scribe is the reverse of this - a tool for taking an S-expression in Ruby and emitting clean code.
7
+ Ruby Scribe attempts to intelligently format code (from an AST) much as a real developer would, through a series of configurable option.
8
+
9
+ == Approach
6
10
 
7
- This project is similar to the ruby2ruby project (also by seattlerb) in that it is a pretty-printer for Ruby s-expressions, but ruby_scribe goes beyond emitting "runnable" Ruby code and attempts to intelligently format code much as a real developer would, through a series of configurable options. Running this against the Rails codebase (as in, parsing the file and emitting it back out) shows very few differences.
11
+ To approach creating a solid pretty-printer, I used the Rails codebase as "ideal" coding style for the emitter with default parameters. As I continued to refine ruby_scribe,
12
+ I ran it (with no AST transformations) against every ruby file in the Rails codebase. The smaller the remaining git diff after each iteration, the better the emitter was.
13
+ There are even some instances I think ruby_scribe emits more consistent code than what currently exists in the Rails codebase.
8
14
 
9
15
  == Example
10
16
 
@@ -14,13 +20,6 @@ Imagine this crappily-formatted Ruby code:
14
20
  # My Comment
15
21
  class Sample < Base;
16
22
  def method; do_something_here; end
17
-
18
- if(new_record?) then
19
- puts "Yes"
20
- else
21
- puts 'No'
22
- end
23
- end
24
23
  end
25
24
 
26
25
  Parse that with RubyParser:
@@ -39,12 +38,6 @@ And out pops this, nice and clean:
39
38
  def method
40
39
  do_something_here
41
40
  end
42
-
43
- if new_record?
44
- puts "Yes"
45
- else
46
- puts "No"
47
- end
48
41
  end
49
42
  end
50
43
 
@@ -1,5 +1,5 @@
1
1
  require "rubygems"
2
- require "active_support"
2
+ require "bundler/setup"
3
3
  require "ruby_parser"
4
4
 
5
5
  require "ruby_scribe/sexp_helpers"
@@ -10,6 +10,7 @@ module RubyScribe
10
10
  return "" unless e
11
11
  return e if e.is_a?(String)
12
12
  return e.to_s if e.is_a?(Symbol)
13
+ return e unless e.respond_to?(:kind)
13
14
 
14
15
  case e.kind
15
16
  when :block
@@ -86,7 +87,7 @@ module RubyScribe
86
87
  return "" if e.body.first == s(:nil)
87
88
 
88
89
  # Special case for handling rescue blocks around entire methods (excluding the indent):
89
- return emit_method_rescue(e.body.first) if e.body.first.rescue? && e.body.size == 1
90
+ return emit_method_rescue(e.body.first) if e.body.first.try(:rescue?) && e.body.size == 1
90
91
 
91
92
  e.body.map do |child|
92
93
  emit_block_member_prefix(e.body, child) +
@@ -414,6 +415,8 @@ module RubyScribe
414
415
  end
415
416
 
416
417
  def emit_assignments_as_arguments(e)
418
+ return nil unless e && e.respond_to?(:kind) && e.body.first
419
+
417
420
  if e.kind == :masgn
418
421
  e.body.first.body.map {|c| emit_assignments_as_arguments(c) }.join(", ")
419
422
  elsif e.kind == :lasgn
@@ -518,4 +521,4 @@ module RubyScribe
518
521
  nl("## RubyScribe-UNKNOWN: #{e.kind} ##")
519
522
  end
520
523
  end
521
- end
524
+ end
@@ -12,12 +12,17 @@ module RubyScribe
12
12
  desc :convert, "Takes a single file or multiple files, parses them, then replaces the original file(s) with the scribed version."
13
13
  def convert(*paths)
14
14
  expand_paths(paths).each do |path|
15
- sexp = RubyParser.new.parse(File.read(path))
16
- output = RubyScribe::Emitter.new.emit(sexp)
17
-
18
- File.open(path, "w") do |file|
19
- file.write(output)
20
- file.flush
15
+ begin
16
+ sexp = RubyParser.new.parse(File.read(path))
17
+ output = RubyScribe::Emitter.new.emit(sexp)
18
+
19
+ File.open(path, "w") do |file|
20
+ file.write(output)
21
+ file.flush
22
+ end
23
+ rescue Racc::ParseError, SyntaxError => e
24
+ $stderr.puts "! Failed to parse #{path}:"
25
+ $stderr.puts e.message
21
26
  end
22
27
  end
23
28
  end
@@ -26,7 +31,7 @@ module RubyScribe
26
31
 
27
32
  def expand_paths(paths = [])
28
33
  paths.map do |path|
29
- [path] + Dir[path + "**/*.rb"]
34
+ [path] + Dir[File.join(path,"**/*.rb")]
30
35
  end.flatten.uniq.reject {|f| File.directory?(f) }
31
36
  end
32
37
  end
@@ -1,3 +1,5 @@
1
+ require "active_support/concern"
2
+
1
3
  module RubyScribe
2
4
 
3
5
  # Various helpers for matching and constructing S-expressions without having to deal with
@@ -1,3 +1,3 @@
1
1
  module RubyScribe
2
- VERSION = "0.1.1"
3
- end
2
+ VERSION = "0.1.2"
3
+ end
metadata CHANGED
@@ -1,78 +1,79 @@
1
- --- !ruby/object:Gem::Specification
1
+ --- !ruby/object:Gem::Specification
2
2
  name: ruby_scribe
3
- version: !ruby/object:Gem::Version
4
- hash: 25
5
- prerelease: false
6
- segments:
7
- - 0
8
- - 1
9
- - 1
10
- version: 0.1.1
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.2
5
+ prerelease:
11
6
  platform: ruby
12
- authors:
7
+ authors:
13
8
  - Ben Hughes
14
9
  autorequire:
15
10
  bindir: bin
16
11
  cert_chain: []
17
-
18
- date: 2010-11-01 00:00:00 +01:00
19
- default_executable:
20
- dependencies:
21
- - !ruby/object:Gem::Dependency
22
- name: ruby_parser
12
+ date: 2011-09-27 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: thor
16
+ requirement: &70221043076100 !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ~>
20
+ - !ruby/object:Gem::Version
21
+ version: '0.13'
22
+ type: :runtime
23
23
  prerelease: false
24
- requirement: &id001 !ruby/object:Gem::Requirement
24
+ version_requirements: *70221043076100
25
+ - !ruby/object:Gem::Dependency
26
+ name: activesupport
27
+ requirement: &70221043075480 !ruby/object:Gem::Requirement
25
28
  none: false
26
- requirements:
29
+ requirements:
27
30
  - - ~>
28
- - !ruby/object:Gem::Version
29
- hash: 7
30
- segments:
31
- - 2
32
- - 0
33
- - 4
34
- version: 2.0.4
31
+ - !ruby/object:Gem::Version
32
+ version: 3.0.10
35
33
  type: :runtime
36
- version_requirements: *id001
37
- - !ruby/object:Gem::Dependency
38
- name: thor
39
34
  prerelease: false
40
- requirement: &id002 !ruby/object:Gem::Requirement
35
+ version_requirements: *70221043075480
36
+ - !ruby/object:Gem::Dependency
37
+ name: i18n
38
+ requirement: &70221043074880 !ruby/object:Gem::Requirement
41
39
  none: false
42
- requirements:
40
+ requirements:
43
41
  - - ~>
44
- - !ruby/object:Gem::Version
45
- hash: 17
46
- segments:
47
- - 0
48
- - 13
49
- version: "0.13"
42
+ - !ruby/object:Gem::Version
43
+ version: 0.6.0
50
44
  type: :runtime
51
- version_requirements: *id002
52
- - !ruby/object:Gem::Dependency
53
- name: rspec
54
45
  prerelease: false
55
- requirement: &id003 !ruby/object:Gem::Requirement
46
+ version_requirements: *70221043074880
47
+ - !ruby/object:Gem::Dependency
48
+ name: ruby_parser
49
+ requirement: &70221043074260 !ruby/object:Gem::Requirement
56
50
  none: false
57
- requirements:
51
+ requirements:
58
52
  - - ~>
59
- - !ruby/object:Gem::Version
60
- hash: 3
61
- segments:
62
- - 2
63
- - 0
64
- version: "2.0"
53
+ - !ruby/object:Gem::Version
54
+ version: 2.3.1
55
+ type: :runtime
56
+ prerelease: false
57
+ version_requirements: *70221043074260
58
+ - !ruby/object:Gem::Dependency
59
+ name: rspec
60
+ requirement: &70221043073680 !ruby/object:Gem::Requirement
61
+ none: false
62
+ requirements:
63
+ - - ~>
64
+ - !ruby/object:Gem::Version
65
+ version: '2.0'
65
66
  type: :development
66
- version_requirements: *id003
67
- description: A ruby formatting tool that takes S-expression as input and intelligently outputs formatted Ruby code.
67
+ prerelease: false
68
+ version_requirements: *70221043073680
69
+ description: A ruby formatting tool that takes S-expression as input and intelligently
70
+ outputs formatted Ruby code.
68
71
  email: ben@railsgarden.com
69
- executables:
72
+ executables:
70
73
  - rubyscribe
71
74
  extensions: []
72
-
73
75
  extra_rdoc_files: []
74
-
75
- files:
76
+ files:
76
77
  - lib/ruby_scribe/emitter.rb
77
78
  - lib/ruby_scribe/emitter_config.rb
78
79
  - lib/ruby_scribe/emitter_helpers.rb
@@ -90,45 +91,34 @@ files:
90
91
  - spec/ruby_scribe/emitter_spec.rb
91
92
  - spec/ruby_scribe/sexp_helpers_spec.rb
92
93
  - spec/spec_helper.rb
94
+ - Gemfile
95
+ - Gemfile.lock
93
96
  - LICENSE
94
97
  - Rakefile
95
98
  - README.rdoc
96
99
  - bin/rubyscribe
97
- has_rdoc: true
98
100
  homepage: http://github.com/rubiety/ruby_scribe
99
101
  licenses: []
100
-
101
102
  post_install_message:
102
103
  rdoc_options: []
103
-
104
- require_paths:
104
+ require_paths:
105
105
  - lib
106
- required_ruby_version: !ruby/object:Gem::Requirement
106
+ required_ruby_version: !ruby/object:Gem::Requirement
107
107
  none: false
108
- requirements:
109
- - - ">="
110
- - !ruby/object:Gem::Version
111
- hash: 3
112
- segments:
113
- - 0
114
- version: "0"
115
- required_rubygems_version: !ruby/object:Gem::Requirement
108
+ requirements:
109
+ - - ! '>='
110
+ - !ruby/object:Gem::Version
111
+ version: '0'
112
+ required_rubygems_version: !ruby/object:Gem::Requirement
116
113
  none: false
117
- requirements:
118
- - - ">="
119
- - !ruby/object:Gem::Version
120
- hash: 19
121
- segments:
122
- - 1
123
- - 3
124
- - 4
114
+ requirements:
115
+ - - ! '>='
116
+ - !ruby/object:Gem::Version
125
117
  version: 1.3.4
126
118
  requirements: []
127
-
128
119
  rubyforge_project: ruby_scribe
129
- rubygems_version: 1.3.7
120
+ rubygems_version: 1.8.10
130
121
  signing_key:
131
122
  specification_version: 3
132
123
  summary: Generates formatted ruby code from S-expressions (like from ruby_parser).
133
124
  test_files: []
134
-