<commit_before>require(
    [
    'jquery',
    'sakai/sakai.api.core',
    'qunitjs/qunit',
    '../../../../tests/qunit/js/sakai_qunit_lib.js',
    '../../../../tests/qunit/js/dev.js',
    '../../../../tests/qunit/js/devwidgets.js'
    ], function($, sakai) {

        module('JavaScript formatting');

        var doRegexTest = function(jsFile, regex, testString) {
            // remove comments from file to test
            var testFile = jsFile.replace(/(\/\*([\s\S]*?)\*\/)|(\/\/(.*)$)/gm, '');
            var match = '';
            var pass = true;
            var errorString = '';
            var count = 0;

            if (regex.test(testFile)) {
                while ((match = regex.exec(jsFile)) !== null) {
                    var beforeMatch = jsFile.substring(0, match.index);
                    var matchLine = beforeMatch.split(/\n/).length;
                    count++;
                    pass = false;
                    errorString = errorString + '\n\nLine: ' + matchLine + '\nString:\n' + match + '';
                }
            }

            if (pass) {
                ok(true, testString);
            } else {
                ok(false, testString + ', ' + count + ' error(s): ' + errorString);
            }
        }

        var checkJs = function(jsFile) {

            // test function declarations
            var regex = /^\s*function\s.*/gm;
            var testString = 'Use \"var <functionName> = function() {\"';
            doRegexTest(jsFile, regex, testString);

            // test opening braces
            regex = /\)\s*$(\n|\r)^\s*\{.*/gm;
            testString = 'Put opening braces on the same line as the statement';
            doRegexTest(jsFile, regex, testString);

            // test literal notation
            regex = /new\s+(Object|Array|Number|String|Boolean).*/gm;
            testString = 'Use literal notation';
            doRegexTest(jsFile, regex, testString);

            regex = /[^=!]==[^=]/gm;
            testString = 'Use \"===\" instead of \"==\"';
            doRegexTest(jsFile, regex, testString);

            regex = /!=[^=]/gm;
            testString = 'Use \"!==\" instead of \"!=\"';<commit_msg>SAKIII-6057 Adding unit tests for spacing issues with braces https://jira.sakaiproject.org/browse/SAKIII-6057<commit_after>require(
    [
    'jquery',
    'sakai/sakai.api.core',
    'qunitjs/qunit',
    '../../../../tests/qunit/js/sakai_qunit_lib.js',
    '../../../../tests/qunit/js/dev.js',
    '../../../../tests/qunit/js/devwidgets.js'
    ], function($, sakai) {

        module('JavaScript formatting');

        var doRegexTest = function(jsFile, regex, testString) {
            // remove comments from file to test
            var testFile = jsFile.replace(/(\/\*([\s\S]*?)\*\/)|(\/\/(.*)$)/gm, '');
            var match = '';
            var pass = true;
            var errorString = '';
            var count = 0;

            if (regex.test(testFile)) {
                while ((match = regex.exec(jsFile)) !== null) {
                    var beforeMatch = jsFile.substring(0, match.index);
                    var matchLine = beforeMatch.split(/\n/).length;
                    count++;
                    pass = false;
                    errorString = errorString + '\n\nLine: ' + matchLine + '\nString:\n' + match + '';
                }
            }

            if (pass) {
                ok(true, testString);
            } else {
                ok(false, testString + ', ' + count + ' error(s): ' + errorString);
            }
        }

        var checkJs = function(jsFile) {

            // test function declarations
            var regex = /^\s*function\s.*/gm;
            var testString = 'Use \"var <functionName> = function() {\"';
            doRegexTest(jsFile, regex, testString);

            // test opening braces
            regex = /\)\s*$(\n|\r)^\s*\{.*/gm;
            testString = 'Put opening braces on the same line as the statement';
            doRegexTest(jsFile, regex, testString);

            // test spaces before opening brace
            regex = /.+\)(\s{0}|\s{2,})\{/gm;
            testString = 'Use exactly one space before an opening brace';
            doRegexTest(jsFile, regex, testString);

            // test no spaces after opening brace
            regex = /.*\).*\{( |\t)+(\n|\r)/gm;
            testString = 'Don\'t put whitespace after an opening brace';
            doRegexTest(jsFile, regex, testString);

            // test no spaces after closing braces
            regex = /.*\}( |\t)+(\n|\r)/gm;
            testString = 'Don\'t put whitespace after a closing brace';
            doRegexTest(jsFile, regex, testString);

            // test literal notation
            regex = /new\s+(Object|Array|Number|String|Boolean).*/gm;
            testString = 'Use literal notation';
            doRegexTest(jsFile, regex, testString);

            regex = /[^=!]==[^=]/gm;
            testString = 'Use \"===\" instead of \"==\"';
            doRegexTest(jsFile, regex, testString);

            regex = /!=[^=]/gm;
            testString = 'Use \"!==\" instead of \"!=\"';
{'fraction_non_alphanumeric': 0.17208487444033482, 'fraction_numerical': 0.002725326065797158, 'mean_word_length': 1.5136986301369864, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 1, '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': '6020921', 'n_tokens_mistral': 1529, 'n_tokens_neox': 1445, 'n_words': 328}