1
1
/*!
2
2
* code-context <https://github.com/jonschlinkert/code-context>
3
3
*
4
- * Copyright (c) 2014 Jon Schlinkert, contributors .
4
+ * Copyright (c) 2014-2015 Jon Schlinkert.
5
5
* Regex sourced from https://github.com/visionmedia/dox
6
6
* Licensed under the MIT License
7
7
*/
8
8
9
9
'use strict' ;
10
10
11
- var extractComments = require ( 'extract-comments ' ) ;
11
+ var parse = require ( 'parse-code-context ' ) ;
12
12
13
13
module . exports = function ( str ) {
14
- str = str . replace ( / \r / g, '' ) ;
15
- var context = [ ] ;
16
-
17
- var comments = extractComments ( str ) ;
18
-
19
- str . split ( / \n / g) . forEach ( function ( line , i ) {
20
- var strict = line . replace ( / ^ \s + / , '' ) ;
21
- var match ;
22
- i = i + 1 ;
23
-
24
- // Code comments
25
- if ( match = / ^ \/ \* / . exec ( strict ) ) {
26
- if ( i === 1 ) {
27
- context . push ( comments [ 0 ] ) ;
28
- } else {
29
- context . push ( comments [ i ] ) ;
30
- }
31
-
32
- // function statement
33
- } else if ( match = / ^ f u n c t i o n [ \t ] ( [ \w $ ] + ) [ \t ] * ( [ \w \W ] + ) ? / . exec ( strict ) ) {
34
- context . push ( {
35
- begin : i ,
36
- type : 'function statement' ,
37
- name : match [ 1 ] ,
38
- params : ( match [ 2 ] ) . split ( / \W / g) . filter ( Boolean ) ,
39
- string : match [ 1 ] + '()' ,
40
- original : strict
41
- } ) ;
42
- // function expression
43
- } else if ( match = / ^ v a r [ \t ] * ( [ \w $ ] + ) [ \t ] * = [ \t ] * f u n c t i o n ( [ \w \W ] + ) ? / . exec ( strict ) ) {
44
- context . push ( {
45
- begin : i ,
46
- type : 'function expression' ,
47
- name : match [ 1 ] ,
48
- params : ( match [ 2 ] ) . split ( / \W / g) . filter ( Boolean ) ,
49
- string : match [ 1 ] + '()' ,
50
- original : strict
51
- } ) ;
52
- // module.exports expression
53
- } else if ( match = / ^ ( m o d u l e \. e x p o r t s ) [ \t ] * = [ \t ] * f u n c t i o n [ \t ] ( [ \w $ ] + ) [ \t ] * ( [ \w \W ] + ) ? / . exec ( strict ) ) {
54
- context . push ( {
55
- begin : i ,
56
- type : 'function expression' ,
57
- receiver : match [ 1 ] ,
58
- name : match [ 2 ] ,
59
- params : ( match [ 3 ] ) . split ( / \W / g) . filter ( Boolean ) ,
60
- string : match [ 1 ] + '()' ,
61
- original : strict
62
- } ) ;
63
- // module.exports method
64
- } else if ( match = / ^ ( m o d u l e \. e x p o r t s ) [ \t ] * = [ \t ] * f u n c t i o n ( [ \w \W ] + ) ? / . exec ( strict ) ) {
65
- context . push ( {
66
- begin : i ,
67
- type : 'method' ,
68
- receiver : match [ 1 ] ,
69
- name : '' ,
70
- params : ( match [ 2 ] ) . split ( / \W / g) . filter ( Boolean ) ,
71
- string : match [ 1 ] + '.' + match [ 2 ] + '()' ,
72
- original : strict
73
- } ) ;
74
- // prototype method
75
- } else if ( match = / ^ ( [ \w $ ] + ) \. p r o t o t y p e \. ( [ \w $ ] + ) [ \t ] * = [ \t ] * f u n c t i o n ( [ \w \W ] + ) ? / . exec ( strict ) ) {
76
- context . push ( {
77
- begin : i ,
78
- type : 'prototype method' ,
79
- class : match [ 1 ] ,
80
- name : match [ 2 ] ,
81
- params : ( match [ 3 ] ) . split ( / \W / g) . filter ( Boolean ) ,
82
- string : match [ 1 ] + '.prototype.' + match [ 2 ] + '()' ,
83
- original : strict
84
- } ) ;
85
- // prototype property
86
- } else if ( match = / ^ ( [ \w $ ] + ) \. p r o t o t y p e \. ( [ \w $ ] + ) [ \t ] * = [ \t ] * ( [ ^ \n ; ] + ) / . exec ( strict ) ) {
87
- context . push ( {
88
- begin : i ,
89
- type : 'prototype property' ,
90
- class : match [ 1 ] ,
91
- name : match [ 2 ] ,
92
- value : match [ 3 ] ,
93
- string : match [ 1 ] + '.prototype.' + match [ 2 ] ,
94
- original : strict
95
- } ) ;
96
- // method
97
- } else if ( match = / ^ ( [ \w $ . ] + ) \. ( [ \w $ ] + ) [ \t ] * = [ \t ] * f u n c t i o n ( [ \w \W ] + ) ? / . exec ( strict ) ) {
98
- context . push ( {
99
- begin : i ,
100
- type : 'method' ,
101
- receiver : match [ 1 ] ,
102
- name : match [ 2 ] ,
103
- params : ( match [ 3 ] ) . split ( / \W / g) . filter ( Boolean ) ,
104
- string : match [ 1 ] + '.' + match [ 2 ] + '()' ,
105
- original : strict
106
- } ) ;
107
- // property
108
- } else if ( match = / ^ ( [ \w $ ] + ) \. ( [ \w $ ] + ) [ \t ] * = [ \t ] * ( [ ^ \n ; ] + ) / . exec ( strict ) ) {
109
- context . push ( {
110
- begin : i ,
111
- type : 'property' ,
112
- receiver : match [ 1 ] ,
113
- name : match [ 2 ] ,
114
- value : match [ 3 ] ,
115
- string : match [ 1 ] + '.' + match [ 2 ] ,
116
- original : strict
117
- } ) ;
118
- // declaration
119
- } else if ( match = / ^ v a r [ \t ] + ( [ \w $ ] + ) [ \t ] * = [ \t ] * ( [ ^ \n ; ] + ) / . exec ( line ) ) {
120
- context . push ( {
121
- begin : i ,
122
- type : 'declaration' ,
123
- name : match [ 1 ] ,
124
- value : match [ 2 ] ,
125
- string : match [ 1 ] ,
126
- original : line
127
- } ) ;
14
+ return str . split ( / [ \r \n ] / ) . reduce ( function ( acc , line , i ) {
15
+ var res = parse ( line . replace ( / ^ \s + / , '' ) , i + 1 ) ;
16
+ if ( res ) {
17
+ acc . push ( res ) ;
128
18
}
129
- } ) ;
130
-
131
- return context . filter ( Boolean ) ;
19
+ return acc ;
20
+ } , [ ] ) ;
132
21
} ;
0 commit comments