'use strict';

var doiuse = require('doiuse');
var postcss = require('postcss');
var sourceMap = require('source-map');
var stylus = require('stylus');

var logger = require('./logger');
var utils = require('./utils');

/**
 * Test and report on changes to the given CSS stylesheet.
 */
function processCSS(githubClient, repo, branch, file, config, handleIncompatibility) {
    // Fetch the contents of the stylesheet
    githubClient.repo(repo).contents(file.filename, branch, function(error, fileContentsMetadata) {
        var fileContents;

        if (error) return logger.error('Error reading contents from ' + repo + ' / ' + branch + ' / ' + file.filename + ':', error);

        fileContents = utils.prepareContent(fileContentsMetadata.content);

        // Test the stylesheet with doiuse and call handleIncompatibility for
        // any incompatibilities that are found
        postcss(doiuse({
            browsers: config,
            onFeatureUsage: handleIncompatibility
        })).process(fileContents, {
            from: '/' + file.filename
        }).then(); // onFeatureUsage won't be called unless then() is called.
    });
}

/**
 * Test and report on changes to the given Stylus stylesheet.
 */
function processStylus(githubClient, repo, branch, file, config, handleIncompatibility) {
    // Fetch the contents of the stylesheet
    githubClient.repo(repo).contents(file.filename, branch, function(error, fileContentsMetadata) {
        var fileContents, compiler;

        if (error) return logger.error('Error reading contents from ' + repo + ' / ' + branch + ' / ' + file.filename + ':', error);

        fileContents = utils.prepareContent(fileContentsMetadata.content);
        compiler = stylus(fileContents)
            .set('filename', file.filename)
            .set('sourcemap', {});

        compiler.render(function(error, compiledCSS) {
            if (error) return logger.error('Error compiling Stylus of ' + repo + ' / ' + branch + ' / ' + file.filename + ':', error);

            // React to an incompatible line of Stylus. Compile the stylus with
            // a source map to get an accurate line number for the
            // incompatibility.
            function handleStylusIncompatibility(incompatibility) {
                var sourceMapConsumer = new sourceMap.SourceMapConsumer(compiler.sourcemap);
                var position = incompatibility.usage.source.start;
                var stylusPosition = sourceMapConsumer.originalPositionFor(position);

                position.line = stylusPosition.line;
                position.column = stylusPosition.column;

                handleIncompatibility(incompatibility);
            }

            // Test the stylesheet with doiuse and call
            // handleStylusIncompatibility for any incompatibilities that are
            // found
            postcss(doiuse({
                browsers: config,
                onFeatureUsage: handleStylusIncompatibility
            })).process(compiledCSS, {
                from: '/' + file.filename
            }).then(); // onFeatureUsage won't be called unless then() is called.
        });
    });
}

exports.processCSS = processCSS;
exports.processStylus = processStylus;

@@ -359,32 +359,67 @@
 ompatibility) %7B%0A
+    var filename = file.filename;%0A%0A
     // Fetch the
@@ -475,37 +475,32 @@
 (repo).contents(
-file.
 filename, branch
@@ -664,37 +664,32 @@
 ranch + ' / ' + 
-file.
 filename + ':', 
@@ -1056,37 +1056,32 @@
     from: '/' + 
-file.
 filename%0A       
@@ -1318,24 +1318,59 @@
 tibility) %7B%0A
+    var filename = file.filename;%0A%0A
     // Fetch
@@ -1438,21 +1438,16 @@
 ontents(
-file.
 filename
@@ -1629,37 +1629,32 @@
 ranch + ' / ' + 
-file.
 filename + ':', 
@@ -1806,21 +1806,16 @@
 ename', 
-file.
 filename
@@ -2009,29 +2009,24 @@
 h + ' / ' + 
-file.
 filename + '
@@ -3059,21 +3059,16 @@
 : '/' + 
-file.
 filename

Shortcut the filename property