flay-js 0.0.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.
data/.gitignore ADDED
@@ -0,0 +1,18 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ .rvmrc
7
+ Gemfile.lock
8
+ InstalledFiles
9
+ _yardoc
10
+ coverage
11
+ doc/
12
+ lib/bundler/man
13
+ pkg
14
+ rdoc
15
+ spec/reports
16
+ test/tmp
17
+ test/version_tmp
18
+ tmp
data/Gemfile ADDED
@@ -0,0 +1,2 @@
1
+ source 'https://rubygems.org'
2
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 Eugene Kalenkovich
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,40 @@
1
+ # flay-js
2
+
3
+ Flay your JavaScript!
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'flay-js'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install flay-js
18
+
19
+ ## Usage
20
+
21
+ For general flay usage please see [flay documentation](https://github.com/seattlerb/flay)
22
+
23
+ flay-js options:
24
+
25
+ -e, --exclude PATH Path to file with regular expressions for files to be skipped
26
+ use '-e default' to skip jquery, jquery-ui, *.min.js and versioned file names
27
+ -j, --javascript Run flay in javascript mode
28
+ (in addition to *.js process javascript fragments in *.erb and *.haml
29
+
30
+ For exclusion file example check [default file](data/flay_js_exclude)
31
+
32
+
33
+
34
+ ## Contributing
35
+
36
+ 1. Fork it
37
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
38
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
39
+ 4. Push to the branch (`git push origin my-new-feature`)
40
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,12 @@
1
+ #!/usr/bin/env rake
2
+ require "bundler/gem_tasks"
3
+ require 'rake/testtask'
4
+
5
+ Rake::TestTask.new(:test) do |t|
6
+ t.libs << 'lib'
7
+ t.libs << 'test'
8
+ t.pattern = 'test/**/test_*.rb'
9
+ t.verbose = false
10
+ end
11
+
12
+ task :default => :test
@@ -0,0 +1,5 @@
1
+ jquery\.js$
2
+ jquery-ui
3
+ \.min\.js$
4
+ \.minified\.js$
5
+ \.\d+\.\d+
data/flay-js.gemspec ADDED
@@ -0,0 +1,20 @@
1
+ # -*- encoding: utf-8 -*-
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+
5
+ Gem::Specification.new do |gem|
6
+ gem.name = "flay-js"
7
+ gem.version = "0.0.1"
8
+ gem.authors = ["Eugene Kalenkovich"]
9
+ gem.email = ["rubify@softover.com"]
10
+ gem.description = "Helps flay to find duplicate code in javascript files"
11
+ gem.summary = "Flay your JS"
12
+ gem.homepage = "https://github.com/UncleGene/flay-js"
13
+
14
+ gem.files = `git ls-files`.split($/)
15
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
16
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
17
+ gem.require_paths = ["lib"]
18
+ gem.add_dependency 'flay', '~> 2.4'
19
+ gem.add_dependency 'rkelly'
20
+ end
data/lib/flay_js.rb ADDED
@@ -0,0 +1,157 @@
1
+ require 'rkelly'
2
+ require 'flay'
3
+
4
+ class Flay
5
+ def self.load_plugins
6
+ %w(js erb haml)
7
+ end
8
+
9
+ def self.options_js o, options
10
+ o.separator nil
11
+ o.separator "flay-js options:"
12
+ o.separator nil
13
+ o.on("-e", "--exclude PATH", String,
14
+ "Path to file with regular expressions for files to be skipped",
15
+ " use '-e default' to skip jquery, jquery-ui, *.min.js and versioned file names") do |p|
16
+ p = File.dirname(__FILE__) + "/../data/flay_js_exclude" if p == 'default'
17
+ rexps = File.readlines(p).
18
+ map(&:strip).
19
+ reject(&:empty?).
20
+ map{ |s| Regexp.new s }
21
+ options[:exclude] = Regexp.union(rexps) unless rexps.empty?
22
+ end
23
+ o.on("-j", "--javascript",
24
+ "Run flay in javascript mode",
25
+ " (in addition to *.js process javascript fragments in *.erb and *.haml ") do
26
+ @@plugins = %w(js haml erb)
27
+ alias_method :null_erb, :process_erb
28
+ alias_method :process_erb, :do_erb
29
+ alias_method :sexp_to_erb, :sexp_to_js
30
+ alias_method :null_haml, :process_haml if respond_to? :process_haml
31
+ alias_method :process_haml, :do_haml
32
+ alias_method :sexp_to_haml, :sexp_to_js
33
+ end
34
+ end
35
+
36
+ def do_haml(file)
37
+ return nil if option[:exclude] && option[:exclude] =~ file
38
+ haml = File.read file
39
+ return nil if haml !~ /$\s*:javascript/
40
+
41
+ blocks = js_haml_blocks(haml, file)
42
+ return nil if blocks.empty?
43
+ sexp = s(:block, *blocks)
44
+ sexp.line 1
45
+ end
46
+
47
+ def js_haml_blocks(haml, file)
48
+ lines = haml.lines.to_a
49
+ blocks = []
50
+ off = 0
51
+ while lines && (off = lines.find_index{ |s| s =~ /^(\s*):javascript\s*$/})
52
+ indent = $1.size
53
+ offset = off + 1
54
+ js = ''
55
+ while (line = lines[offset]) &&
56
+ ( (line =~/^(\s*)[^\s]/ && $1.size > indent) || line.strip.empty? )
57
+ js << line
58
+ offset += 1
59
+ end
60
+ blocks << js_sexp(js, file, off + 1)
61
+ lines = lines[offset..-1]
62
+ end
63
+ blocks
64
+ end
65
+
66
+ def do_erb(file)
67
+ return nil if option[:exclude] && option[:exclude] =~ file
68
+ return process_js(file) if file =~ /\.js\./
69
+ erb = File.read file
70
+ return nil if erb !~ /<\s*script/i
71
+
72
+ blocks = js_erb_blocks(erb, file)
73
+ return nil if blocks.empty?
74
+
75
+ sexp = s(:block, *blocks)
76
+ sexp.line 1
77
+ end
78
+
79
+ def js_erb_blocks(erb, file)
80
+ lines = erb.lines.to_a
81
+ blocks = []
82
+ offset = 0
83
+ while lines && (off = lines.find_index{ |s| s =~ /<\s*script/i })
84
+ str = lines.join
85
+ str =~ /(<\s*script[^>]*>[\n\s]*)(.*?)(<\s*\/\s*script\s*>[\n\s]*)/im
86
+ off = off + $1.count("\n")
87
+ blocks << js_sexp($2, file, offset + off)
88
+ offset += ($2 + $3).count("\n") + off + 1
89
+ lines = lines[offset .. -1]
90
+ end
91
+ blocks
92
+ end
93
+
94
+ def process_js(file)
95
+ return nil if option[:exclude] && option[:exclude] =~ file
96
+ js = File.read file
97
+ js_sexp(js, file, 0)
98
+ end
99
+
100
+ def js_sexp(js, file, offset)
101
+ js = unerb(js) if file =~ /\.erb$/
102
+ js = uninterpolate(js) if file =~ /\.haml$/
103
+
104
+ asx = RKelly::Parser.new.parse(js, file)
105
+ puts js if asx.nil? && option[:verbose]
106
+ raise "JS syntax error in #{file}" unless asx
107
+ if option[:diff]
108
+ @asx ||= Hash.new{|h,k| h[k] = []}
109
+ @asx[file] << asx
110
+ end
111
+ fixed(asx.to_real_sexp, offset)
112
+ end
113
+
114
+ def fixed(sexp, offset = 0)
115
+ sexp.line ||= 0
116
+ sexp.line += offset
117
+ file = sexp.file
118
+ sexp.deep_each do |s|
119
+ s.line ||= 0
120
+ s.line += offset
121
+ file ||= s.file
122
+ end
123
+ sexp.file = file
124
+ sexp.deep_each do |s|
125
+ s.file = file
126
+ end
127
+ sexp
128
+ end
129
+
130
+ def unerb(js)
131
+ js.gsub(/(<%=.*?%>)/) do |erb|
132
+ erb.gsub(/[^\w]+/,'_')
133
+ end
134
+ end
135
+
136
+ def uninterpolate(js)
137
+ # WOW!!!!!
138
+ js.gsub(/(\#{[^{}]*})/) do |str|
139
+ str.gsub(/[^\w]+/,'_')
140
+ end.gsub(/(\#{.*})/) do |str|
141
+ str.gsub(/[^#]({[^{}]*})/) do |sub|
142
+ sub.gsub(/[^\w]+/,'_')
143
+ end.gsub(/(\#?{[^{}]*})/) do |sub|
144
+ sub.gsub(/[^\w]+/,'_')
145
+ end
146
+ end
147
+ end
148
+
149
+ def sexp_to_js(sexp)
150
+ @asx[sexp.file].each do |asx|
151
+ asx.each do |sa|
152
+ return sa.to_ecma if sa.to_real_sexp == sexp
153
+ end
154
+ end
155
+ 'Conversion to javascript failed'
156
+ end
157
+ end
@@ -0,0 +1,9 @@
1
+ require 'minitest/autorun'
2
+ require 'flay_js'
3
+
4
+
5
+ describe Flay do
6
+ it 'can process js' do
7
+
8
+ end
9
+ end
metadata ADDED
@@ -0,0 +1,88 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: flay-js
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Eugene Kalenkovich
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-07-26 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: flay
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ~>
20
+ - !ruby/object:Gem::Version
21
+ version: '2.4'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ~>
28
+ - !ruby/object:Gem::Version
29
+ version: '2.4'
30
+ - !ruby/object:Gem::Dependency
31
+ name: rkelly
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ type: :runtime
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ description: Helps flay to find duplicate code in javascript files
47
+ email:
48
+ - rubify@softover.com
49
+ executables: []
50
+ extensions: []
51
+ extra_rdoc_files: []
52
+ files:
53
+ - .gitignore
54
+ - Gemfile
55
+ - LICENSE.txt
56
+ - README.md
57
+ - Rakefile
58
+ - data/flay_js_exclude
59
+ - flay-js.gemspec
60
+ - lib/flay_js.rb
61
+ - test/test_flay_js.rb
62
+ homepage: https://github.com/UncleGene/flay-js
63
+ licenses: []
64
+ post_install_message:
65
+ rdoc_options: []
66
+ require_paths:
67
+ - lib
68
+ required_ruby_version: !ruby/object:Gem::Requirement
69
+ none: false
70
+ requirements:
71
+ - - ! '>='
72
+ - !ruby/object:Gem::Version
73
+ version: '0'
74
+ required_rubygems_version: !ruby/object:Gem::Requirement
75
+ none: false
76
+ requirements:
77
+ - - ! '>='
78
+ - !ruby/object:Gem::Version
79
+ version: '0'
80
+ requirements: []
81
+ rubyforge_project:
82
+ rubygems_version: 1.8.24
83
+ signing_key:
84
+ specification_version: 3
85
+ summary: Flay your JS
86
+ test_files:
87
+ - test/test_flay_js.rb
88
+ has_rdoc: