<commit_before>var contexts = require("../contexts"),
    Visitor = require("./visitor"),
    ImportSequencer = require("./import-sequencer");

var ImportVisitor = function(importer, finish, evalEnv, onceFileDetectionMap, recursionDetector) {
    this._visitor = new Visitor(this);
    this._importer = importer;
    this._finish = finish;
    this.context = evalEnv || new contexts.Eval();
    this.importCount = 0;
    this.onceFileDetectionMap = onceFileDetectionMap || {};
    this.recursionDetector = {};
    if (recursionDetector) {
        for(var fullFilename in recursionDetector) {
            if (recursionDetector.hasOwnProperty(fullFilename)) {
                this.recursionDetector[fullFilename] = true;
            }
        }
    }
    this._sequencer = new ImportSequencer();
};

ImportVisitor.prototype = {
    isReplacing: true,
    run: function (root) {
        var error;
        try {
            // process the contents
            this._visitor.visit(root);
        }
        catch(e) {
            error = e;
        }

        this.isFinished = true;
        if (this.importCount === 0) {
            this._finish(error);
        }
    },
    visitImport: function (importNode, visitArgs) {
        var evaldImportNode,
            inlineCSS = importNode.options.inline;

        if (!importNode.css || inlineCSS) {

            try {
                evaldImportNode = importNode.evalForImport(this.context);
            } catch(e){
                if (!e.filename) { e.index = importNode.index; e.filename = importNode.currentFileInfo.filename; }
                // attempt to eval properly and treat as css
                importNode.css = true;
                // if that fails, this error will be thrown
                importNode.error = e;
            }

            if (evaldImportNode && (!evaldImportNode.css || inlineCSS)) {
                importNode = evaldImportNode;
                this.importCount++;
                var context = new contexts.Eval(this.context, this.context.frames.slice(0));

                if (importNode.options.multiple) {
                    context.importMultiple = true;
                }

                // try appending if we haven't determined if it is css or not
                var tryAppendLessExtension = importNode.css === undefined;

                var onImported = this.onImported.bind(this, importNode, context),
                    sequencedOnImported = this._sequencer.addImport(onImported);

                this._importer.push(importNode.getPath(), tryAppendLessExtension, importNode.currentFileInfo, importNode.options, sequencedOnImported);
            }
        }
        visitArgs.visitDeeper = false;
        return importNode;
    },
    onImported: function (importNode, context, e, root, importedAtRoot, fullPath) {
        if (e && !e.filename) {
            e.index = importNode.index; e.filename = importNode.currentFileInfo.filename;
        }

        var importVisitor = this,
            inlineCSS = importNode.options.inline,
            duplicateImport = importedAtRoot || fullPath in importVisitor.recursionDetector;

        if (!context.importMultiple) {
            if (duplicateImport) {
                importNode.skip = true;
            } else {
                importNode.skip = function() {
                    if (fullPath in importVisitor.onceFileDetectionMap) {
                        return true;
                    }
                    importVisitor.onceFileDetectionMap[fullPath] = true;
                    return false;
                };
            }
        }

        var subFinish = function(e) {
            importVisitor.importCount--;

            if (importVisitor.importCount === 0 && importVisitor.isFinished) {
                importVisitor._finish(e);
            }
        };

        if (root) {
            importNode.root = root;
            importNode.importedFilename = fullPath;

            if (!inlineCSS && (context.importMultiple || !duplicateImport)) {
                importVisitor.recursionDetector[fullPath] = true;
                new ImportVisitor(importVisitor._importer, subFinish, context, importVisitor.onceFileDetectionMap, importVisitor.recursionDetector)
                    .run(root);
                return;
            }
        }<commit_msg>make sure an error is not lost<commit_after>var contexts = require("../contexts"),
    Visitor = require("./visitor"),
    ImportSequencer = require("./import-sequencer");

var ImportVisitor = function(importer, finish, evalEnv, onceFileDetectionMap, recursionDetector) {
    this._visitor = new Visitor(this);
    this._importer = importer;
    this._finish = finish;
    this.context = evalEnv || new contexts.Eval();
    this.importCount = 0;
    this.onceFileDetectionMap = onceFileDetectionMap || {};
    this.recursionDetector = {};
    if (recursionDetector) {
        for(var fullFilename in recursionDetector) {
            if (recursionDetector.hasOwnProperty(fullFilename)) {
                this.recursionDetector[fullFilename] = true;
            }
        }
    }
    this._sequencer = new ImportSequencer();
};

ImportVisitor.prototype = {
    isReplacing: true,
    run: function (root) {
        var error;
        try {
            // process the contents
            this._visitor.visit(root);
        }
        catch(e) {
            error = e;
        }

        this.isFinished = true;
        if (this.importCount === 0) {
            this._finish(error || this.error);
        }
    },
    visitImport: function (importNode, visitArgs) {
        var evaldImportNode,
            inlineCSS = importNode.options.inline;

        if (!importNode.css || inlineCSS) {

            try {
                evaldImportNode = importNode.evalForImport(this.context);
            } catch(e){
                if (!e.filename) { e.index = importNode.index; e.filename = importNode.currentFileInfo.filename; }
                // attempt to eval properly and treat as css
                importNode.css = true;
                // if that fails, this error will be thrown
                importNode.error = e;
            }

            if (evaldImportNode && (!evaldImportNode.css || inlineCSS)) {
                importNode = evaldImportNode;
                this.importCount++;
                var context = new contexts.Eval(this.context, this.context.frames.slice(0));

                if (importNode.options.multiple) {
                    context.importMultiple = true;
                }

                // try appending if we haven't determined if it is css or not
                var tryAppendLessExtension = importNode.css === undefined;

                var onImported = this.onImported.bind(this, importNode, context),
                    sequencedOnImported = this._sequencer.addImport(onImported);

                this._importer.push(importNode.getPath(), tryAppendLessExtension, importNode.currentFileInfo, importNode.options, sequencedOnImported);
            }
        }
        visitArgs.visitDeeper = false;
        return importNode;
    },
    onImported: function (importNode, context, e, root, importedAtRoot, fullPath) {
        if (e && !e.filename) {
            e.index = importNode.index; e.filename = importNode.currentFileInfo.filename;
        }

        var importVisitor = this,
            inlineCSS = importNode.options.inline,
            duplicateImport = importedAtRoot || fullPath in importVisitor.recursionDetector;

        if (!context.importMultiple) {
            if (duplicateImport) {
                importNode.skip = true;
            } else {
                importNode.skip = function() {
                    if (fullPath in importVisitor.onceFileDetectionMap) {
                        return true;
                    }
                    importVisitor.onceFileDetectionMap[fullPath] = true;
                    return false;
                };
            }
        }

        var subFinish = function(e) {
            importVisitor.importCount--;

            if (importVisitor.importCount === 0 && importVisitor.isFinished) {
                importVisitor._finish(e || importVisitor.error);
            } else if (e) {
                importVisitor.error = e;
            }
        };

        if (root) {
            importNode.root = root;
            importNode.importedFilename = fullPath;

            if (!inlineCSS && (context.importMultiple || !duplicateImport)) {
                importVisitor.recursionDetector[fullPath] = true;
                new ImportVisitor(importVisitor._importer, subFinish, context, importVisitor.onceFileDetectionMap, importVisitor.recursionDetector)
                    .run(root);
                return;
            }
        }
{'fraction_non_alphanumeric': 0.09669298546744479, 'fraction_numerical': 0.000915436548804211, 'mean_word_length': 1.7852135117909496, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '955654', 'n_tokens_mistral': 2325, 'n_tokens_neox': 2192, 'n_words': 467}