CoffeeTags 0.0.1.2 → 0.0.1.3
Sign up to get free protection for your applications and to get access to all the features.
- data/Guardfile +2 -2
- data/Rakefile +5 -0
- data/bin/coffeetags +5 -1
- data/lib/CoffeeTags/formatter.rb +24 -22
- data/lib/CoffeeTags/parser.rb +50 -20
- data/lib/CoffeeTags/version.rb +1 -1
- data/lib/CoffeeTags.rb +63 -35
- data/spec/coffeetags_spec.rb +69 -0
- data/spec/fixtures/test.coffee +25 -1
- data/spec/fixtures/test_tree.yaml +24 -1
- data/spec/fixtures/tree.yaml +14 -0
- data/spec/formatter_spec.rb +8 -2
- data/spec/parser_spec.rb +81 -14
- data/test.rb +5 -1
- metadata +6 -4
data/Guardfile
CHANGED
@@ -1,6 +1,6 @@
|
|
1
|
-
guard 'rspec', :version => 2, :bundler => true, :cli => ' --color', :all_on_start =>
|
1
|
+
guard 'rspec', :version => 2, :bundler => true, :cli => ' --color', :all_on_start =>true do
|
2
2
|
watch(%r{^spec/(.*).rb$}) { |m| m[0] }
|
3
3
|
watch(%r{^lib/(.*).rb$}) do |m|
|
4
|
-
"spec/#{m[0].split('/').last.split('.').first}_spec.rb"
|
4
|
+
"spec/#{m[0].split('/').last.split('.').first.downcase}_spec.rb"
|
5
5
|
end
|
6
6
|
end
|
data/Rakefile
CHANGED
data/bin/coffeetags
CHANGED
data/lib/CoffeeTags/formatter.rb
CHANGED
@@ -1,27 +1,23 @@
|
|
1
1
|
module Coffeetags
|
2
2
|
class Formatter
|
3
|
+
@@header = []
|
4
|
+
|
3
5
|
def initialize file, tree =[]
|
4
6
|
@file = file
|
5
7
|
@tree = tree
|
6
8
|
|
7
|
-
@header = [
|
8
|
-
"!_TAG_FILE_FORMAT 2 /extended format/",
|
9
|
-
"!_TAG_FILE_SORTED 0 /0=unsorted, 1=sorted, 2=foldcase/",
|
10
|
-
"!_TAG_PROGRAM_AUTHOR #{Coffeetags::AUTHOR}",
|
11
|
-
"!_TAG_PROGRAM_NAME #{Coffeetags::NAME} //",
|
12
|
-
"!_TAG_PROGRAM_URL #{Coffeetags::URL} /GitHub repository/",
|
13
|
-
"!_TAG_PROGRAM_VERSION #{Coffeetags::VERSION} //"
|
14
|
-
]
|
15
9
|
|
16
10
|
@types = {
|
17
11
|
'f' => 'type:function',
|
18
12
|
'c' => 'type:class',
|
13
|
+
'o' => 'type:object',
|
19
14
|
'v' => 'type:var'
|
20
15
|
}
|
21
16
|
end
|
22
17
|
|
23
18
|
def regex_line line
|
24
|
-
"/^#{line}$/;\""
|
19
|
+
"/^#{line.gsub('/', '\/')}$/;\""
|
20
|
+
"/^#{Regexp.escape line}$/;\""
|
25
21
|
end
|
26
22
|
|
27
23
|
def line_to_string entry
|
@@ -41,23 +37,29 @@ module Coffeetags
|
|
41
37
|
|
42
38
|
def parse_tree
|
43
39
|
@lines = @tree.map do | content|
|
44
|
-
line_to_string content
|
40
|
+
line_to_string content unless content[:line].nil? or content[:name].blank?
|
45
41
|
end.reject{|l| l.nil? }
|
46
42
|
end
|
47
43
|
|
44
|
+
|
45
|
+
def tags
|
46
|
+
@lines.map { |l| "#{l}\n"}
|
47
|
+
end
|
48
|
+
|
48
49
|
def to_file
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
50
|
+
self.header + tags
|
51
|
+
end
|
52
|
+
|
53
|
+
def self.header
|
54
|
+
|
55
|
+
header = [
|
56
|
+
"!_TAG_FILE_FORMAT 2 /extended format/",
|
57
|
+
"!_TAG_FILE_SORTED 1 /0=unsorted, 1=sorted, 2=foldcase/",
|
58
|
+
"!_TAG_PROGRAM_AUTHOR #{Coffeetags::AUTHOR}",
|
59
|
+
"!_TAG_PROGRAM_NAME #{Coffeetags::NAME} //",
|
60
|
+
"!_TAG_PROGRAM_URL #{Coffeetags::URL} /GitHub repository/",
|
61
|
+
"!_TAG_PROGRAM_VERSION #{Coffeetags::VERSION} //"
|
62
|
+
].map { |h| "#{h}\n"}
|
61
63
|
end
|
62
64
|
end
|
63
65
|
end
|
data/lib/CoffeeTags/parser.rb
CHANGED
@@ -2,16 +2,22 @@ require 'yaml'
|
|
2
2
|
module Coffeetags
|
3
3
|
class Parser
|
4
4
|
attr_reader :tree
|
5
|
-
def initialize source
|
5
|
+
def initialize source, include_vars = false
|
6
|
+
@include_vars = include_vars
|
6
7
|
@source = source
|
7
8
|
|
9
|
+
@fake_parent = 'window'
|
10
|
+
|
8
11
|
# tree maps the ... tree :-)
|
9
12
|
@tree = []
|
10
13
|
|
11
14
|
# regexes
|
12
|
-
@
|
13
|
-
@
|
14
|
-
@
|
15
|
+
@block = /^\s*(if|unless|switch|loop|do)/
|
16
|
+
@class_regex = /^\s*class\s*(\w*)/
|
17
|
+
@proto_meths = /^\s*([A-Za-z]*)::([@a-zA-Z0-9_]*)/
|
18
|
+
@var_regex = /([@a-zA-Z0-9_]*)\s*[=:]{1}\s*$/
|
19
|
+
@token_regex = /([@a-zA-Z0-9_]*)\s*[:=]{1}/
|
20
|
+
@iterator_regex = /^\s*for\s*([a-zA-Z0-9_]*)\s*/
|
15
21
|
end
|
16
22
|
|
17
23
|
def line_level line
|
@@ -25,11 +31,11 @@ module Coffeetags
|
|
25
31
|
idx = tree.index(element) || -1
|
26
32
|
|
27
33
|
current_level = element[:level]
|
28
|
-
tree[0..idx].reverse.
|
34
|
+
tree[0..idx].reverse.each_with_index do |item, index|
|
29
35
|
# uhmmmmmm
|
30
|
-
if
|
31
|
-
bf <<
|
32
|
-
current_level =
|
36
|
+
if item[:level] != current_level and item[:level] < current_level and item[:line] !~ @block
|
37
|
+
bf << item[:name] unless item[:kind] == 'b'
|
38
|
+
current_level = item[:level]
|
33
39
|
end
|
34
40
|
end
|
35
41
|
bf.uniq.reverse.join('.')
|
@@ -39,36 +45,60 @@ module Coffeetags
|
|
39
45
|
def execute!
|
40
46
|
line_n = 0
|
41
47
|
level = 0
|
42
|
-
sc = '__top__'
|
43
48
|
# indentify scopes
|
44
49
|
@source.each_line do |line|
|
45
50
|
line_n += 1
|
46
51
|
level = line_level line
|
47
52
|
|
48
53
|
if (_class = line.match @class_regex)
|
49
|
-
@tree << {
|
50
|
-
|
51
|
-
|
54
|
+
@tree << { :name => _class[1], :level => level }
|
55
|
+
end
|
56
|
+
|
57
|
+
if(_proto = line.match @proto_meths)
|
58
|
+
@tree << { :name => _proto[1], :level => level }
|
52
59
|
end
|
53
60
|
|
54
61
|
if(var = line.match @var_regex)
|
55
|
-
@tree << {
|
56
|
-
|
57
|
-
|
62
|
+
@tree << { :name => var[1], :level => level }
|
63
|
+
end
|
64
|
+
|
65
|
+
if(_block = line.match @block)
|
66
|
+
@tree << { :name => _block[1], :level => level , :kind => 'b'}
|
58
67
|
end
|
59
68
|
|
60
|
-
token = line.match
|
61
|
-
|
69
|
+
token = line.match(@token_regex )
|
70
|
+
token ||= line.match(@iterator_regex)
|
71
|
+
|
72
|
+
if not token.nil?
|
62
73
|
o = {
|
63
74
|
:name => token[1],
|
64
75
|
:level => level,
|
65
76
|
:parent => '',
|
66
77
|
:source => line.chomp,
|
67
|
-
:kind => 'f',
|
68
78
|
:line => line_n
|
69
79
|
}
|
70
|
-
|
71
|
-
|
80
|
+
|
81
|
+
# remove edge cases for now
|
82
|
+
# - if a line containes a line like: element.getElement('type=[checkbox]').lol()
|
83
|
+
is_in_string = line =~ /.*['"].*#{token[1]}.*=.*["'].*/
|
84
|
+
|
85
|
+
# - scope access and comparison in if x == 'lol'
|
86
|
+
is_in_comparison = line =~ /::|==/
|
87
|
+
|
88
|
+
# - objects with blank parent (parser bug?)
|
89
|
+
has_blank_parent = o[:parent] =~ /\.$/
|
90
|
+
|
91
|
+
# - multiple consecutive assignments
|
92
|
+
is_previous_not_the_same = !(@tree.last and @tree.last[:name] == o[:name] and @tree.last[:level] == o[:level])
|
93
|
+
|
94
|
+
if is_in_string.nil? and is_in_comparison.nil? and has_blank_parent.nil? and is_previous_not_the_same
|
95
|
+
o[:kind] = line =~ /[:=]{1}.*-\>/ ? 'f' : 'o'
|
96
|
+
o[:parent] = scope_path o
|
97
|
+
o[:parent] = @fake_parent if o[:parent].empty?
|
98
|
+
|
99
|
+
@tree << o if o[:kind] == 'f'
|
100
|
+
@tree << o if o[:kind] == 'o' and @include_vars
|
101
|
+
end
|
72
102
|
end
|
73
103
|
@tree.uniq!
|
74
104
|
end
|
data/lib/CoffeeTags/version.rb
CHANGED
data/lib/CoffeeTags.rb
CHANGED
@@ -17,57 +17,85 @@ module Coffeetags
|
|
17
17
|
AUTHOR = "Łukasz Korecki /lukasz@coffeesounds.com/"
|
18
18
|
NAME = "CoffeeTags"
|
19
19
|
URL = "https://github.com/lukaszkorecki/CoffeeTags"
|
20
|
-
TAGBAR_COFFEE_CONF = <<-CONF
|
21
|
-
let g:tagbar_type_coffee = {
|
22
|
-
\\ 'kinds' : [
|
23
|
-
\\ 'f:functions',
|
24
|
-
\\ 'o:object'
|
25
|
-
\\ ],
|
26
|
-
\\ 'kind2scope' : {
|
27
|
-
\\ 'f' : 'object',
|
28
|
-
\\ 'o' : 'object'
|
29
|
-
\\},
|
30
|
-
\\ 'sro' : ".",
|
31
|
-
\\ 'ctagsbin' : 'coffeetags',
|
32
|
-
\\ 'ctagsargs' : '',
|
33
|
-
\\}
|
34
|
-
CONF
|
35
20
|
|
21
|
+
STRINGS = {
|
22
|
+
'version' => "#{NAME} #{Coffeetags::VERSION} by #{AUTHOR} ( #{URL} )",
|
23
|
+
'help' => <<-HELP
|
24
|
+
--version - coffeetags version
|
25
|
+
--vim-conf - print out tagbar config for vim
|
26
|
+
--include-vars - include objects/variables in generated tags
|
27
|
+
combine --vim-conf and --include-vars to create a config which adds '--include-vars' option
|
28
|
+
HELP
|
29
|
+
}
|
36
30
|
class Utils
|
37
31
|
|
38
32
|
def self.option_parser args
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
" Add this type definition to your vimrc
|
48
|
-
" or do
|
49
|
-
" coffeetags vim_conf >> <PATH TO YOUR VIMRC>
|
50
|
-
HELP
|
51
|
-
puts Coffeetags::TAGBAR_COFFEE_CONF
|
52
|
-
else
|
53
|
-
self.run args
|
33
|
+
include_vars = ! args.delete('--include-vars').nil?
|
34
|
+
|
35
|
+
output = nil
|
36
|
+
|
37
|
+
unless args.index( '-f').nil?
|
38
|
+
output = args[ args.index('-f') + 1]
|
39
|
+
args.delete '-f'
|
40
|
+
args.delete output
|
54
41
|
end
|
55
42
|
|
43
|
+
to_print = [].tap do |_to_print|
|
44
|
+
_to_print << args.delete( '--help')
|
45
|
+
_to_print << args.delete( '--version')
|
46
|
+
end.reject { |i| i.nil? }.map { |i| i.sub '--', ''}.map { |s| STRINGS[s] }
|
47
|
+
( to_print << tagbar_conf(include_vars) ) unless args.delete('--vim-conf').nil?
|
48
|
+
|
49
|
+
to_print.each { |str| puts str }
|
50
|
+
|
51
|
+
[output, include_vars, args] unless args.empty?
|
56
52
|
end
|
57
53
|
|
58
|
-
def self.run files
|
59
|
-
files.each do |file|
|
60
|
-
sc = File.read file
|
61
54
|
|
62
|
-
|
55
|
+
def self.run output, include_vars, files
|
56
|
+
__out = if output.nil?
|
57
|
+
STDOUT
|
58
|
+
else
|
59
|
+
File.open output, 'w'
|
60
|
+
end
|
61
|
+
|
62
|
+
__out << Coffeetags::Formatter.header
|
63
|
+
|
64
|
+
files.reject { |f| f =~ /^--/}.each do |file|
|
65
|
+
sc = File.read file
|
66
|
+
parser = Coffeetags::Parser.new sc, include_vars
|
63
67
|
parser.execute!
|
64
68
|
|
65
69
|
formatter = Coffeetags::Formatter.new file, parser.tree
|
66
70
|
|
67
71
|
formatter.parse_tree
|
68
72
|
|
69
|
-
|
73
|
+
__out << formatter.tags
|
70
74
|
end
|
75
|
+
__out.close if __out.respond_to? :close
|
76
|
+
end
|
77
|
+
|
78
|
+
def self.tagbar_conf include_vars
|
79
|
+
<<-CONF
|
80
|
+
" Add this type definition to your vimrc
|
81
|
+
" or do
|
82
|
+
" coffeetags --vim-conf >> <PATH TO YOUR VIMRC>
|
83
|
+
" if you want your tags to include vars/objects do:
|
84
|
+
" coffeetags --vim-conf --include-vars
|
85
|
+
let g:tagbar_type_coffee = {
|
86
|
+
\\ 'kinds' : [
|
87
|
+
\\ 'f:functions',
|
88
|
+
\\ 'o:object'
|
89
|
+
\\ ],
|
90
|
+
\\ 'kind2scope' : {
|
91
|
+
\\ 'f' : 'object',
|
92
|
+
\\ 'o' : 'object'
|
93
|
+
\\},
|
94
|
+
\\ 'sro' : ".",
|
95
|
+
\\ 'ctagsbin' : 'coffeetags',
|
96
|
+
\\ 'ctagsargs' : '#{include_vars ? "--include-vars" : "" } ',
|
97
|
+
\\}
|
98
|
+
CONF
|
71
99
|
end
|
72
100
|
end
|
73
101
|
end
|
@@ -0,0 +1,69 @@
|
|
1
|
+
require 'lib/CoffeeTags'
|
2
|
+
include Coffeetags
|
3
|
+
describe Utils do
|
4
|
+
context 'Argument parsing' do
|
5
|
+
it "returns nil when nothing is passed" do
|
6
|
+
Utils.option_parser( []).should == nil
|
7
|
+
end
|
8
|
+
|
9
|
+
it "returns files list" do
|
10
|
+
Utils.option_parser( [ 'lol.coffee']).should == [ nil, false, ['lol.coffee']]
|
11
|
+
end
|
12
|
+
|
13
|
+
it "parses --include-vars option" do
|
14
|
+
Utils.option_parser( [ '--include-vars', 'lol.coffee']).should == [ nil, true, ['lol.coffee']]
|
15
|
+
end
|
16
|
+
|
17
|
+
it "parses -f <file> option" do
|
18
|
+
Utils.option_parser( [ '-f','tags' ,'lol.coffee']).should == [ 'tags', false, ['lol.coffee']]
|
19
|
+
end
|
20
|
+
|
21
|
+
it "parses --vim-conf option" do
|
22
|
+
pending 'learn how to catch STDOUT in specs :-)'
|
23
|
+
Utils.option_parser( [ '--vim-conf','lol.coffee']).should match /g:tagbar_type_coffee /
|
24
|
+
|
25
|
+
end
|
26
|
+
|
27
|
+
|
28
|
+
end
|
29
|
+
|
30
|
+
context 'Parser runner' do
|
31
|
+
before do
|
32
|
+
@fake_parser = mock('Parser')
|
33
|
+
@fake_parser.stub! :"execute!"
|
34
|
+
@fake_parser.stub!( :tree).and_return []
|
35
|
+
|
36
|
+
Parser.stub!(:new).and_return @fake_parser
|
37
|
+
|
38
|
+
@fake_formatter = mock 'Formatter'
|
39
|
+
@fake_formatter.stub! :parse_tree
|
40
|
+
@fake_formatter.stub!(:tags).and_return <<-TAG
|
41
|
+
tag
|
42
|
+
tag2
|
43
|
+
tag3
|
44
|
+
TAG
|
45
|
+
Coffeetags::Formatter.stub!(:new).and_return @fake_formatter
|
46
|
+
Coffeetags::Formatter.stub!(:header).and_return "header\n"
|
47
|
+
|
48
|
+
File.stub!(:read).and_return 'woot@'
|
49
|
+
|
50
|
+
end
|
51
|
+
|
52
|
+
|
53
|
+
it "opens the file and writes tags to it" do
|
54
|
+
Utils.run 'test.tags', false, ['woot']
|
55
|
+
`cat test.tags`.should== <<-FF
|
56
|
+
header
|
57
|
+
tag
|
58
|
+
tag2
|
59
|
+
tag3
|
60
|
+
FF
|
61
|
+
|
62
|
+
end
|
63
|
+
after :each do
|
64
|
+
`rm test.tags`
|
65
|
+
end
|
66
|
+
|
67
|
+
end
|
68
|
+
|
69
|
+
end
|
data/spec/fixtures/test.coffee
CHANGED
@@ -4,10 +4,34 @@ bump = (wat) ->
|
|
4
4
|
Wat =
|
5
5
|
ho : (x) ->
|
6
6
|
x = 'o'
|
7
|
+
x = 'z' if bamp
|
7
8
|
console.log 'ahhhh'
|
8
9
|
@lolWat =
|
9
10
|
bump : ->
|
10
11
|
console.log 'bump'
|
11
12
|
bump_up : ->
|
12
13
|
console.log 'bump up'
|
13
|
-
@
|
14
|
+
@filter = ->
|
15
|
+
filter.getElements('input[type="checkbox"]').addEvent('change', (e) ->
|
16
|
+
Wat.trackEvent('Track', "Filter::#{e.target.name}", e.target.value)
|
17
|
+
)
|
18
|
+
|
19
|
+
_loop = (x) ->
|
20
|
+
woop = 1
|
21
|
+
if z.isSomethingRidic
|
22
|
+
fu = 1
|
23
|
+
|
24
|
+
if wat
|
25
|
+
nice = 'ok'
|
26
|
+
|
27
|
+
|
28
|
+
for element in lol
|
29
|
+
do (element)
|
30
|
+
ugh = 2 * element
|
31
|
+
# for testing with issue #1 examples
|
32
|
+
dir = fs.readdirSync __dirname
|
33
|
+
x = dir + '/foo'
|
34
|
+
|
35
|
+
for f in dir
|
36
|
+
do (f) ->
|
37
|
+
console.log f
|
@@ -1,20 +1,38 @@
|
|
1
1
|
---
|
2
2
|
- :source: bump = (wat) ->
|
3
|
-
:parent: ""
|
3
|
+
:parent: "window"
|
4
4
|
:kind: f
|
5
5
|
:name: bump
|
6
6
|
:line: 1
|
7
7
|
:level: 0
|
8
|
+
- :source: " v = 'test'"
|
9
|
+
:parent: 'bump'
|
10
|
+
:kind: o
|
11
|
+
:name: v
|
12
|
+
:line: 2
|
13
|
+
:level: 2
|
8
14
|
- :name: Wat
|
15
|
+
:source: 'Wat ='
|
16
|
+
:parent: window
|
17
|
+
:kind: o
|
9
18
|
:level: 0
|
19
|
+
:line: 4
|
10
20
|
- :source: " ho : (x) ->"
|
11
21
|
:parent: Wat
|
12
22
|
:kind: f
|
13
23
|
:name: ho
|
14
24
|
:line: 5
|
15
25
|
:level: 2
|
26
|
+
- :source: " x = 'o'"
|
27
|
+
:level: 4
|
28
|
+
:kind: o
|
29
|
+
:line: 6
|
30
|
+
:name: x
|
16
31
|
- :name: '@lolWat'
|
17
32
|
:level: 4
|
33
|
+
:line: 8
|
34
|
+
:source: ' @lolWat ='
|
35
|
+
:kind: o
|
18
36
|
- :source: " bump : ->"
|
19
37
|
:parent: 'Wat.ho.@lolWat'
|
20
38
|
:kind: f
|
@@ -33,3 +51,8 @@
|
|
33
51
|
:name: '@BOOO__up'
|
34
52
|
:line: 13
|
35
53
|
:level: 4
|
54
|
+
- :source: 'Array::loop = (x) ->'
|
55
|
+
:line: 18
|
56
|
+
:kind: f
|
57
|
+
:parent: Array
|
58
|
+
:level: 0
|
data/spec/fixtures/tree.yaml
CHANGED
@@ -7,6 +7,12 @@
|
|
7
7
|
:name: constructor
|
8
8
|
:line: 8
|
9
9
|
:level: 2
|
10
|
+
- :source: ' @url = "https://#{host}.campfirenow.com/"'
|
11
|
+
:name: '@url'
|
12
|
+
:parent: 'Campfire.constructor'
|
13
|
+
:kind: o
|
14
|
+
:level: 4
|
15
|
+
:line: 9
|
10
16
|
- :source: " handlers: (callbacks) ->"
|
11
17
|
:parent: Campfire
|
12
18
|
:kind: f
|
@@ -45,6 +51,14 @@
|
|
45
51
|
:name: recent
|
46
52
|
:line: 40
|
47
53
|
:level: 2
|
54
|
+
- :source: ' url = "room/#{id}/recent.json"'
|
55
|
+
:parent: Campfire.recent
|
56
|
+
:kind: o
|
57
|
+
:name: url
|
58
|
+
:line: 41
|
59
|
+
:level: 4
|
60
|
+
|
61
|
+
|
48
62
|
- :name: Test
|
49
63
|
:level: 0
|
50
64
|
- :source: " bump : ->"
|
data/spec/formatter_spec.rb
CHANGED
@@ -14,12 +14,18 @@ describe 'CoffeeTags::Formatter' do
|
|
14
14
|
end
|
15
15
|
|
16
16
|
it "generates a line for method definition" do
|
17
|
-
exp = 'constructor
|
17
|
+
exp = [ 'constructor', 'test.coffee',
|
18
|
+
"/^#{Regexp.escape ' constructor: (api_key, host) ->'}$/;\"",
|
19
|
+
'f', 'lineno:8', 'object:Campfire', 'type:function'
|
20
|
+
].join("\t")
|
18
21
|
@instance.parse_tree.first.should == exp
|
19
22
|
end
|
20
23
|
|
21
24
|
it "generates line for second class" do
|
22
|
-
exp = 'bump
|
25
|
+
exp = [ 'bump', 'test.coffee',
|
26
|
+
"/^#{Regexp.escape ' bump : ->'}$/;\"",
|
27
|
+
'f', 'lineno:46', 'object:Test', 'type:function'
|
28
|
+
].join "\t"
|
23
29
|
@instance.parse_tree.last.should == exp
|
24
30
|
end
|
25
31
|
|
data/spec/parser_spec.rb
CHANGED
@@ -3,8 +3,9 @@ describe 'CoffeeTags::Parser' do
|
|
3
3
|
before :all do
|
4
4
|
@campfire_class = File.read File.expand_path('./spec/fixtures/campfire.coffee')
|
5
5
|
@test_file = File.read File.expand_path('./spec/fixtures/test.coffee')
|
6
|
-
|
6
|
+
|
7
7
|
@cf_tree = YAML::load_file File.expand_path('./spec/fixtures/tree.yaml')
|
8
|
+
@test_tree = YAML::load_file File.expand_path('./spec/fixtures/test_tree.yaml')
|
8
9
|
|
9
10
|
end
|
10
11
|
|
@@ -36,21 +37,20 @@ describe 'CoffeeTags::Parser' do
|
|
36
37
|
before(:each) do
|
37
38
|
@parser = Coffeetags::Parser.new ''
|
38
39
|
end
|
39
|
-
it 'gets the scope path for
|
40
|
+
it 'gets the scope path for function' do
|
40
41
|
@parser.scope_path(@cf_tree[1], @cf_tree[0...1] ).should == 'Campfire'
|
41
42
|
end
|
42
43
|
|
43
44
|
it 'gets the scope path for second function' do
|
44
|
-
@parser.scope_path(@cf_tree[
|
45
|
+
@parser.scope_path(@cf_tree[3], @cf_tree[0..2] ).should == 'Campfire'
|
45
46
|
end
|
46
47
|
|
47
48
|
it "gets the scope for nested function" do
|
48
|
-
@parser.scope_path(@cf_tree[
|
49
|
+
@parser.scope_path(@cf_tree[5], @cf_tree[0..4]).should == 'Campfire.handlers.resp'
|
49
50
|
end
|
50
51
|
|
51
52
|
it "gets the scope of a function which comes after nested function" do
|
52
|
-
|
53
|
-
@parser.scope_path(@cf_tree[6], @cf_tree[0..5]).should == 'Campfire'
|
53
|
+
@parser.scope_path(@cf_tree[7], @cf_tree[0..6]).should == 'Campfire'
|
54
54
|
end
|
55
55
|
|
56
56
|
it 'gets scope for last method defined in diff class' do
|
@@ -61,26 +61,93 @@ describe 'CoffeeTags::Parser' do
|
|
61
61
|
context 'Parsing' do
|
62
62
|
context 'Scoping' do
|
63
63
|
before(:each) do
|
64
|
-
@coffee_parser = Coffeetags::Parser.new @campfire_class
|
65
|
-
@test_parser = Coffeetags::Parser.new @test_file
|
64
|
+
@coffee_parser = Coffeetags::Parser.new @campfire_class, true
|
65
|
+
@test_parser = Coffeetags::Parser.new @test_file, true
|
66
|
+
@coffee_parser.execute!
|
66
67
|
end
|
67
68
|
|
68
|
-
it "
|
69
|
-
|
70
|
-
|
69
|
+
it "parses the class" do
|
70
|
+
c =@coffee_parser.tree.find { |i| i[:name] == 'Campfire'}
|
71
|
+
c.should == @cf_tree.find {|i| i[:name] == 'Campfire'}
|
72
|
+
end
|
73
|
+
|
74
|
+
it "parses the 2nd class" do
|
75
|
+
c =@coffee_parser.tree.find { |i| i[:name] == 'Test'}
|
76
|
+
c.should == @cf_tree.find {|i| i[:name] == 'Test'}
|
77
|
+
end
|
78
|
+
|
79
|
+
it "parses the instance variable" do
|
80
|
+
c =@coffee_parser.tree.find { |i| i[:name] == '@url'}
|
81
|
+
c.should == @cf_tree.find {|i| i[:name] == '@url'}
|
82
|
+
end
|
83
|
+
|
84
|
+
it "parses the object literal with functions" do
|
85
|
+
c =@coffee_parser.tree.find { |i| i[:name] == 'resp'}
|
86
|
+
c.should == @cf_tree.find {|i| i[:name] == 'resp'}
|
87
|
+
end
|
88
|
+
|
89
|
+
it "parses a nested function" do
|
90
|
+
c =@coffee_parser.tree.find { |i| i[:name] == 'onSuccess'}
|
91
|
+
c.should == @cf_tree.find {|i| i[:name] == 'onSuccess'}
|
92
|
+
end
|
93
|
+
|
94
|
+
it "parses a method var" do
|
95
|
+
c =@coffee_parser.tree.find { |i| i[:name] == 'url'}
|
96
|
+
c.should == @cf_tree.find {|i| i[:name] == 'url'}
|
71
97
|
end
|
72
98
|
end
|
73
99
|
end
|
74
100
|
|
75
101
|
context 'Test.coffee parsing' do
|
76
102
|
before(:each) do
|
77
|
-
@parser_test = Coffeetags::Parser.new @test_file
|
103
|
+
@parser_test = Coffeetags::Parser.new @test_file, true
|
78
104
|
@parser_test.execute!
|
79
105
|
end
|
80
106
|
|
81
|
-
it "
|
82
|
-
@parser_test.tree.should ==
|
107
|
+
it "doesnt extract a variable from a tricky line" do
|
108
|
+
@parser_test.tree.find { |i| i[:name] == 'Filter'}.should == nil
|
83
109
|
end
|
84
110
|
|
111
|
+
it 'correctly recognizes an object in if block' do
|
112
|
+
pro = @parser_test.tree.find { |i| i[:name] == 'fu'}
|
113
|
+
pro[:parent].should == '_loop'
|
114
|
+
|
115
|
+
pro = @parser_test.tree.find { |i| i[:name] == 'nice'}
|
116
|
+
pro[:parent].should == '_loop'
|
117
|
+
end
|
118
|
+
|
119
|
+
it 'correctly recognizes an object in for block' do
|
120
|
+
pro = @parser_test.tree.find { |i| i[:name] == 'ugh'}
|
121
|
+
pro[:parent].should == '_loop.element'
|
122
|
+
|
123
|
+
end
|
124
|
+
|
125
|
+
it "extracts a method defined in a prototype" do
|
126
|
+
pending 'methods defined on prototype needs implementing'
|
127
|
+
pro = @parser_test.tree.find { |i| i[:name] == '_loop'}
|
128
|
+
exp = @test_tree.find { |i| i[:name] == '_loop'}
|
129
|
+
pro.should_not be nil
|
130
|
+
pro.should == exp
|
131
|
+
end
|
132
|
+
|
133
|
+
context 'for loop' do
|
134
|
+
subject {
|
135
|
+
@parser_test = Coffeetags::Parser.new @test_file, true
|
136
|
+
@parser_test.execute!
|
137
|
+
@parser_test.tree.find { |i| i[:name] == 'element'}
|
138
|
+
}
|
139
|
+
it "finds variables defined in for loop" do
|
140
|
+
subject[:line].should == 28
|
141
|
+
end
|
142
|
+
|
143
|
+
it "extracts the name" do
|
144
|
+
subject[:name].should == 'element'
|
145
|
+
end
|
146
|
+
|
147
|
+
it "detects the scope" do
|
148
|
+
|
149
|
+
subject[:parent].should == '_loop'
|
150
|
+
end
|
151
|
+
end
|
85
152
|
end
|
86
153
|
end
|
data/test.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: CoffeeTags
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 77
|
5
5
|
prerelease:
|
6
6
|
segments:
|
7
7
|
- 0
|
8
8
|
- 0
|
9
9
|
- 1
|
10
|
-
-
|
11
|
-
version: 0.0.1.
|
10
|
+
- 3
|
11
|
+
version: 0.0.1.3
|
12
12
|
platform: ruby
|
13
13
|
authors:
|
14
14
|
- "\xC5\x81ukasz Korecki"
|
@@ -16,7 +16,7 @@ autorequire:
|
|
16
16
|
bindir: bin
|
17
17
|
cert_chain: []
|
18
18
|
|
19
|
-
date: 2011-
|
19
|
+
date: 2011-10-06 00:00:00 +01:00
|
20
20
|
default_executable:
|
21
21
|
dependencies: []
|
22
22
|
|
@@ -42,6 +42,7 @@ files:
|
|
42
42
|
- lib/CoffeeTags/formatter.rb
|
43
43
|
- lib/CoffeeTags/parser.rb
|
44
44
|
- lib/CoffeeTags/version.rb
|
45
|
+
- spec/coffeetags_spec.rb
|
45
46
|
- spec/fixtures/campfire.coffee
|
46
47
|
- spec/fixtures/campfire.js
|
47
48
|
- spec/fixtures/campfire.js.tags
|
@@ -86,6 +87,7 @@ signing_key:
|
|
86
87
|
specification_version: 3
|
87
88
|
summary: Simple tags generator for CoffeeScript
|
88
89
|
test_files:
|
90
|
+
- spec/coffeetags_spec.rb
|
89
91
|
- spec/fixtures/campfire.coffee
|
90
92
|
- spec/fixtures/campfire.js
|
91
93
|
- spec/fixtures/campfire.js.tags
|