<commit_before>/**
 * Tests the nesting linter.
 */
var assert = require("assert");
var less = require("less");
var postcss = require("postcss");
var sourceMap = require("source-map");

var overqualifiedLint = require("../lib/overqualified-lint");

// Run the nesting linter on a given snippet of Less code
function lintCode(code) {
    var errors = [];
    var options = {
        sourceMap: {
            outputSourceFiles: true,
        },
    };

    less.render(code, options, function(err, result) {
        var smc = new sourceMap.SourceMapConsumer(result.map);
        var ast = postcss.parse(result.css);
        overqualifiedLint(ast, smc, function(err) {
            errors.push(err);
        });
    });

    return errors;
}

describe("Overqualified linter", function() {
    it("should pass for elements without ids and classes", function() {
        var lessCode = `
            ul {
                color: red;

                li {
                    padding: 0;
                }
            }
        `;

        var errors = lintCode(lessCode);
        assert(errors.length === 0);
    });

    it("should pass for ids and classes without elements", function() {
        var lessCode = `
            #id {
                color: red;
            }

            .class1.class2 {
                color: green;
            }

            #main.class {
                color: gray;
            }
        `;

        var errors = lintCode(lessCode);
        assert(errors.length === 0);
    });

    it("should fail for element#id", function() {
        var lessCode = `
            div#main {
                color: black;
            }

            div {
                &#id {
                    color: red;
                }
            }
        `;

        var errors = lintCode(lessCode);
        assert(errors.length === 2);

        // Grab the line numbers from the error messages
        var lineNos = errors.map(function(error) {
            var pos = error.split(" ").slice(-1)[0];
            var line = pos.split(":")[0];

            return parseInt(line);
        });

        assert(lineNos[0] === 2);
        assert(lineNos[1] === 7);
    });

    it("should fail for element.class", function() {
        var lessCode = `
            div.centered {
                margin: 0 auto;
            }

            div {
                &.class {
                    color: green;
                }
            }
        `;

        var errors = lintCode(lessCode);
        assert(errors.length === 2);

        // Grab the line numbers from the error messages
        var lineNos = errors.map(function(error) {
            var pos = error.split(" ").slice(-1)[0];
            var line = pos.split(":")[0];

            return parseInt(line);
        });

        assert(lineNos[0] === 2);
        assert(lineNos[1] === 7);
    });

    it("should pass for nested selectors", function() {
        // NOTE: "div #id" is still unnecessary, but not for the purposes of
        // this linter
        var lessCode = `
            div {
                #id {
                    color: red;
                }
            }
        `;<commit_msg>search the error messages as well<commit_after>/**
 * Tests the nesting linter.
 */
var assert = require("assert");
var less = require("less");
var postcss = require("postcss");
var sourceMap = require("source-map");

var overqualifiedLint = require("../lib/overqualified-lint");

// Run the nesting linter on a given snippet of Less code
function lintCode(code) {
    var errors = [];
    var options = {
        sourceMap: {
            outputSourceFiles: true,
        },
    };

    less.render(code, options, function(err, result) {
        var smc = new sourceMap.SourceMapConsumer(result.map);
        var ast = postcss.parse(result.css);
        overqualifiedLint(ast, smc, function(err) {
            errors.push(err);
        });
    });

    return errors;
}

describe("Overqualified linter", function() {
    it("should pass for elements without ids and classes", function() {
        var lessCode = `
            ul {
                color: red;

                li {
                    padding: 0;
                }
            }
        `;

        var errors = lintCode(lessCode);
        assert(errors.length === 0);
    });

    it("should pass for ids and classes without elements", function() {
        var lessCode = `
            #id {
                color: red;
            }

            .class1.class2 {
                color: green;
            }

            #main.class {
                color: gray;
            }
        `;

        var errors = lintCode(lessCode);
        assert(errors.length === 0);
    });

    it("should fail for element#id", function() {
        var lessCode = `
            div#main {
                color: black;
            }

            div {
                &#id {
                    color: red;
                }
            }
        `;

        var errors = lintCode(lessCode);
        assert(errors.length === 2);

        // Grab the line numbers from the error messages
        var lineNos = errors.map(function(error) {
            var pos = error.split(" ").slice(-1)[0];
            var line = pos.split(":")[0];

            return parseInt(line);
        });

        assert(lineNos[0] === 2);
        assert(errors[0].indexOf("div#main") > -1);

        assert(lineNos[1] === 7);
        assert(errors[1].indexOf("div#id") > -1);
    });

    it("should fail for element.class", function() {
        var lessCode = `
            div.centered {
                margin: 0 auto;
            }

            div {
                &.class {
                    color: green;
                }
            }
        `;

        var errors = lintCode(lessCode);
        assert(errors.length === 2);

        // Grab the line numbers from the error messages
        var lineNos = errors.map(function(error) {
            var pos = error.split(" ").slice(-1)[0];
            var line = pos.split(":")[0];

            return parseInt(line);
        });

        assert(lineNos[0] === 2);
        assert(errors[0].indexOf("div.centered") > -1);

        assert(lineNos[1] === 7);
        assert(errors[1].indexOf("div.class") > -1);
    });

    it("should pass for nested selectors", function() {
        // NOTE: "div #id" is still unnecessary, but not for the purposes of
        // this linter
        var lessCode = `
            div {
                #id {
                    color: red;
                }
            }
        `;
{'fraction_non_alphanumeric': 0.12844315933647846, 'fraction_numerical': 0.007913559579972607, 'mean_word_length': 1.3725631768953068, 'pattern_counts': {'":': 4, '<': 3, '<?xml version=': 0, '>': 7, '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': '5972146', 'n_tokens_mistral': 1879, 'n_tokens_neox': 1750, 'n_words': 453}