Skip to content

Commit e416681

Browse files
committed
Parse imports right away after discovery
Makes import resolve depth-first instead of breadth-first Fixes #1262
1 parent 9b07cbd commit e416681

File tree

3 files changed

+36
-18
lines changed

3 files changed

+36
-18
lines changed

src/context.cpp

Lines changed: 30 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -240,6 +240,8 @@ namespace Sass {
240240
if (char* contents = read_file(resolved)) {
241241
add_source(path, resolved, contents);
242242
style_sheets[path] = 0;
243+
size_t i = queue.size() - 1;
244+
process_queue_entry(queue[i], i);
243245
return path;
244246
}
245247
return std::string("");
@@ -268,6 +270,8 @@ namespace Sass {
268270
if (char* contents = read_file(resolved[0].abs_path)) {
269271
add_source(base_file, resolved[0].abs_path, contents);
270272
style_sheets[base_file] = 0;
273+
size_t i = queue.size() - 1;
274+
process_queue_entry(queue[i], i);
271275
return base_file;
272276
}
273277
}
@@ -300,28 +304,34 @@ namespace Sass {
300304
return sass_strdup(output.c_str());
301305
}
302306

307+
void Context::process_queue_entry(Sass_Queued& entry, size_t i)
308+
{
309+
if (style_sheets[queue[i].load_path]) return;
310+
Sass_Import_Entry import = sass_make_import(
311+
queue[i].load_path.c_str(),
312+
queue[i].abs_path.c_str(),
313+
0, 0
314+
);
315+
import_stack.push_back(import);
316+
// keep a copy of the path around (for parser states)
317+
strings.push_back(sass_strdup(queue[i].abs_path.c_str()));
318+
ParserState pstate(strings.back(), queue[i].source, i);
319+
Parser p(Parser::from_c_str(queue[i].source, *this, pstate));
320+
Block* ast = p.parse();
321+
sass_delete_import(import_stack.back());
322+
import_stack.pop_back();
323+
// ToDo: we store by load_path, which can lead
324+
// to duplicates if importer reports the same path
325+
// Maybe we should add an error for duplicates!?
326+
style_sheets[queue[i].load_path] = ast;
327+
}
328+
303329
Block* Context::parse_file()
304330
{
305331
Block* root = 0;
306332
for (size_t i = 0; i < queue.size(); ++i) {
307-
Sass_Import_Entry import = sass_make_import(
308-
queue[i].load_path.c_str(),
309-
queue[i].abs_path.c_str(),
310-
0, 0
311-
);
312-
import_stack.push_back(import);
313-
// keep a copy of the path around (for parser states)
314-
strings.push_back(sass_strdup(queue[i].abs_path.c_str()));
315-
ParserState pstate(strings.back(), queue[i].source, i);
316-
Parser p(Parser::from_c_str(queue[i].source, *this, pstate));
317-
Block* ast = p.parse();
318-
sass_delete_import(import_stack.back());
319-
import_stack.pop_back();
320-
if (i == 0) root = ast;
321-
// ToDo: we store by load_path, which can lead
322-
// to duplicates if importer reports the same path
323-
// Maybe we should add an error for duplicates!?
324-
style_sheets[queue[i].load_path] = ast;
333+
process_queue_entry(queue[i], i);
334+
if (i == 0) root = style_sheets[queue[i].load_path];
325335
}
326336
if (root == 0) return 0;
327337

@@ -368,6 +378,8 @@ namespace Sass {
368378
return parse_file();
369379
}
370380
add_source(input_path, input_path, source_c_str);
381+
size_t idx = queue.size() - 1;
382+
process_queue_entry(queue[idx], idx);
371383
return parse_file();
372384
}
373385

src/context.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,7 @@ namespace Sass {
113113
std::string add_file(const std::string& imp_path);
114114
std::string add_file(const std::string& imp_path, const std::string& abs_path, ParserState pstate);
115115

116+
void process_queue_entry(Sass_Queued& entry, size_t idx);
116117

117118
// allow to optionally overwrite the input path
118119
// default argument for input_path is std::string("stdin")

src/parser.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -332,6 +332,7 @@ namespace Sass {
332332
size_t i = 0;
333333
bool has_import = false;
334334
std::string load_path = unquote(import_path);
335+
// std::cerr << "-- " << load_path << "\n";
335336
for (Sass_Importer_Entry& importer : importers) {
336337
// int priority = sass_importer_get_priority(importer);
337338
Sass_Importer_Fn fn = sass_importer_get_function(importer);
@@ -359,9 +360,13 @@ namespace Sass {
359360
if (abs_path) {
360361
ctx.add_source(uniq_path, abs_path, source);
361362
imp->files().push_back(uniq_path);
363+
size_t i = ctx.queue.size() - 1;
364+
ctx.process_queue_entry(ctx.queue[i], i);
362365
} else {
363366
ctx.add_source(uniq_path, uniq_path, source);
364367
imp->files().push_back(uniq_path);
368+
size_t i = ctx.queue.size() - 1;
369+
ctx.process_queue_entry(ctx.queue[i], i);
365370
}
366371
} else if(abs_path) {
367372
import_single_file(imp, abs_path);

0 commit comments

Comments
 (0)