@@ -7,23 +7,34 @@ var logger = require('../../log');
7
7
var uniq = require ( 'lodash' ) . uniq ;
8
8
9
9
10
- var searchForMatches = function ( code , regex , index ) {
10
+ var isAnnotatedByHand = function ( handWritten , type , name ) {
11
+ if ( type && name && handWritten ) {
12
+ return handWritten [ type + '-' + name ] ;
13
+ }
14
+ return false ;
15
+ } ;
16
+
17
+ var searchForMatches = function ( code , regex , isAnnotatedByHand ) {
11
18
var match ;
12
19
var matches = [ ] ;
13
20
while ( ( match = regex . exec ( code ) ) ) {
14
- matches . push ( match [ index || 1 ] ) ;
21
+ if ( ! isAnnotatedByHand ( match [ 1 ] ) ) {
22
+ matches . push ( match [ 1 ] ) ;
23
+ }
15
24
}
16
25
return uniq ( matches ) ;
17
26
} ;
18
27
19
28
20
29
var typeNameObject = function ( type ) {
21
30
return function ( name ) {
22
- return {
23
- type : type ,
24
- name : name ,
25
- autofill : true
26
- } ;
31
+ if ( name . length > 0 ) {
32
+ return {
33
+ type : type ,
34
+ name : name ,
35
+ autofill : true
36
+ } ;
37
+ }
27
38
} ;
28
39
} ;
29
40
@@ -73,6 +84,14 @@ module.exports = {
73
84
autofill : function ( item ) {
74
85
var type = item . context . type ;
75
86
if ( type === 'mixin' || type === 'placeholder' || type === 'function' ) {
87
+ var handWritten ;
88
+
89
+ if ( item . requires ) {
90
+ handWritten = { } ;
91
+ item . requires . forEach ( function ( reqObj ) {
92
+ handWritten [ reqObj . type + '-' + reqObj . name ] = true ;
93
+ } ) ;
94
+ }
76
95
77
96
// Searching for mixins and functions
78
97
var mixins = [ ] ;
@@ -82,14 +101,18 @@ module.exports = {
82
101
while ( ( match = mixinFunctionRegex . exec ( item . context . code ) ) ) {
83
102
// Try if this is a mixin or function
84
103
if ( compareBefore ( item . context . code , '@include' , match . index ) ) {
85
- mixins . push ( match [ 1 ] ) ;
104
+ if ( ! isAnnotatedByHand ( handWritten , 'mixin' , match [ 1 ] ) ) {
105
+ mixins . push ( match [ 1 ] ) ;
106
+ }
86
107
} else {
87
- functions . push ( match [ 1 ] ) ;
108
+ if ( ! isAnnotatedByHand ( handWritten , 'function' , match [ 1 ] ) ) {
109
+ functions . push ( match [ 1 ] ) ;
110
+ }
88
111
}
89
112
}
90
113
91
- var placeholders = searchForMatches ( item . context . code , / @ e x t e n d \s + % ( [ ^ ; \s ] + ) / ig) ;
92
- var variables = searchForMatches ( item . context . code , / \$ ( [ a - z 0 - 9 _ - ] + ) / ig) ;
114
+ var placeholders = searchForMatches ( item . context . code , / @ e x t e n d \s + % ( [ ^ ; \s ] + ) / ig, isAnnotatedByHand . bind ( null , handWritten , 'mixin' ) ) ;
115
+ var variables = searchForMatches ( item . context . code , / \$ ( [ a - z 0 - 9 _ - ] + ) / ig, isAnnotatedByHand . bind ( null , handWritten , 'variable' ) ) ;
93
116
94
117
// Create object for each required item.
95
118
mixins = mixins . map ( typeNameObject ( 'mixin' ) ) ;
@@ -105,6 +128,11 @@ module.exports = {
105
128
all = all . concat ( placeholders ) ;
106
129
all = all . concat ( variables ) ;
107
130
131
+ // Filter empty values.
132
+ all = all . filter ( function ( item ) {
133
+ return item !== undefined ;
134
+ } ) ;
135
+
108
136
// Merge in user supplyed requires if there are any
109
137
if ( item . requires && item . requires . length > 0 ) {
110
138
all = all . concat ( item . requires ) ;
@@ -143,33 +171,38 @@ module.exports = {
143
171
reqItem . usedBy . push ( item ) ;
144
172
req . item = reqItem ;
145
173
146
- }
147
- else if ( req . autofill !== true ) {
174
+ } else if ( req . autofill !== true ) {
148
175
logger . log ( 'Item `' + item . context . name +
149
176
'` requires `' + req . name + '` from type `' + req . type +
150
177
'` but this item doesn\'t exist.' ) ;
178
+ } else {
179
+ return undefined ;
151
180
}
152
181
153
182
return req ;
183
+ } ) . filter ( function ( item ) {
184
+ return item !== undefined ;
154
185
} ) ;
155
186
156
- item . requires . toJSON = utils . mapArray . bind ( null , item . requires ,
157
- function ( item ) {
158
- var obj = {
159
- type : item . type ,
160
- name : item . name ,
161
- external : item . external ,
162
- } ;
163
- if ( item . external ) {
164
- obj . url = item . url ;
165
- }
166
- else {
167
- obj . description = item . description ;
168
- obj . context = item . context ;
187
+ if ( item . requires . length > 0 ) {
188
+ item . requires . toJSON = utils . mapArray . bind ( null , item . requires ,
189
+ function ( item ) {
190
+ var obj = {
191
+ type : item . type ,
192
+ name : item . name ,
193
+ external : item . external ,
194
+ } ;
195
+ if ( item . external ) {
196
+ obj . url = item . url ;
197
+ }
198
+ else {
199
+ obj . description = item . description ;
200
+ obj . context = item . context ;
201
+ }
202
+ return obj ;
169
203
}
170
- return obj ;
171
- }
172
- ) ;
204
+ ) ;
205
+ }
173
206
}
174
207
} ) ;
175
208
0 commit comments