Skip to content

Commit 03a5aca

Browse files
committed
don't add requires that where added manually
1 parent 5e15ab0 commit 03a5aca

File tree

1 file changed

+62
-29
lines changed

1 file changed

+62
-29
lines changed

src/annotation/annotations/requires.js

+62-29
Original file line numberDiff line numberDiff line change
@@ -7,23 +7,34 @@ var logger = require('../../log');
77
var uniq = require('lodash').uniq;
88

99

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){
1118
var match;
1219
var matches = [];
1320
while ( (match = regex.exec(code)) ) {
14-
matches.push(match[index || 1]);
21+
if (!isAnnotatedByHand(match[1])){
22+
matches.push(match[1]);
23+
}
1524
}
1625
return uniq(matches);
1726
};
1827

1928

2029
var typeNameObject = function(type){
2130
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+
}
2738
};
2839
};
2940

@@ -73,6 +84,14 @@ module.exports = {
7384
autofill: function(item){
7485
var type = item.context.type;
7586
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+
}
7695

7796
// Searching for mixins and functions
7897
var mixins = [];
@@ -82,14 +101,18 @@ module.exports = {
82101
while ( (match = mixinFunctionRegex.exec(item.context.code)) ){
83102
// Try if this is a mixin or function
84103
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+
}
86107
} else {
87-
functions.push(match[1]);
108+
if (!isAnnotatedByHand(handWritten, 'function', match[1])) {
109+
functions.push(match[1]);
110+
}
88111
}
89112
}
90113

91-
var placeholders = searchForMatches(item.context.code, /@extend\s+%([^;\s]+)/ig);
92-
var variables = searchForMatches(item.context.code, /\$([a-z0-9_-]+)/ig);
114+
var placeholders = searchForMatches(item.context.code, /@extend\s+%([^;\s]+)/ig, isAnnotatedByHand.bind(null, handWritten, 'mixin'));
115+
var variables = searchForMatches(item.context.code, /\$([a-z0-9_-]+)/ig, isAnnotatedByHand.bind(null, handWritten, 'variable'));
93116

94117
// Create object for each required item.
95118
mixins = mixins.map(typeNameObject('mixin'));
@@ -105,6 +128,11 @@ module.exports = {
105128
all = all.concat(placeholders);
106129
all = all.concat(variables);
107130

131+
// Filter empty values.
132+
all = all.filter(function(item){
133+
return item !== undefined;
134+
});
135+
108136
// Merge in user supplyed requires if there are any
109137
if (item.requires && item.requires.length > 0){
110138
all = all.concat(item.requires);
@@ -143,33 +171,38 @@ module.exports = {
143171
reqItem.usedBy.push(item);
144172
req.item = reqItem;
145173

146-
}
147-
else if (req.autofill !== true) {
174+
} else if (req.autofill !== true) {
148175
logger.log('Item `' + item.context.name +
149176
'` requires `' + req.name + '` from type `' + req.type +
150177
'` but this item doesn\'t exist.');
178+
} else {
179+
return undefined;
151180
}
152181

153182
return req;
183+
}).filter(function(item){
184+
return item !== undefined;
154185
});
155186

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;
169203
}
170-
return obj;
171-
}
172-
);
204+
);
205+
}
173206
}
174207
});
175208

0 commit comments

Comments
 (0)