<commit_before>require('jasmine-only');
describe('amdclean specs', function() {
  var amdclean = require('../../build/amdclean'),
    requirejs = require('requirejs'),
    _ = require('lodash'),
    fs = require('fs'),
    defaultOptions = {
      // We don't need the IIFE wrapper by default, since it makes the tests more verbose
      'wrap': {
        'start': '',
        'end': ''
      },
      // Makes sure the code output is on one line (easier to test)
      'escodegen': {
        'format': {
          'compact': true
        }
      }
    }

  describe('define() method conversions', function() {

    describe('functions', function() {

      it('should convert function return values to immediately invoked function declarations', function() {
        var AMDcode = "define('example', [], function() {});",
          cleanedCode = amdclean.clean(AMDcode, defaultOptions);
        standardJavaScript = "var example;example=undefined;";

        expect(cleanedCode).toBe(standardJavaScript);
      });

      it('should preserve single line comments when converting function return values to immediately invoked function declarations', function() {
        var AMDcode = "define('example', [], function() {  // Answer\n var test = true; return test; });",
          cleanedCode = amdclean.clean(AMDcode, defaultOptions),
          standardJavaScript = "var example;example=function (){// Answer\nvar test=true;return test;}();";

        expect(cleanedCode).toBe(standardJavaScript);
      });

      it('should preserve multi-line comments when converting function return values to immediately invoked function declarations', function() {
        var AMDcode = "define('example', [], function() {  /* Answer */\nvar test = true; return test; });",
          cleanedCode = amdclean.clean(AMDcode, defaultOptions),
          standardJavaScript = "var example;example=function (){/* Answer */\nvar test=true;return test;}();";

        expect(cleanedCode).toBe(standardJavaScript);
      });

      it('should pass a file path instead of the code directly', function() {
        var options = _.merge(_.cloneDeep(defaultOptions), {
            'filePath': __dirname + '/../filePathTest.js'
          }),
          cleanedCode = amdclean.clean(options),
          standardJavaScript = "var example;example=undefined;";

        expect(cleanedCode).toBe(standardJavaScript);
      });

      it('should correctly set callback parameters to the callback function', function() {
        var AMDcode = "define('example', ['example1', 'example2'], function(one, two) {var test = true;});",
          cleanedCode = amdclean.clean(AMDcode, defaultOptions),
          standardJavaScript = "var example;example=function (one,two){var test=true;}(example1,example2);";

        expect(cleanedCode).toBe(standardJavaScript);
      });

      it('should not hoist and remove callback and IIFE parameters that are only used once', function() {
        var AMDcode = "define('example1', function() { var count = 0;return 'firstModule'; });define('example2', function() { var count = 0;return 'secondModule'; });define('example', ['example1', 'example2'], function(yo, yoyo) {var test = true;});define('blah', ['example2'], function(yoooo, comon) { return false; });",
          options = _.merge(_.cloneDeep(defaultOptions), {
            'aggressiveOptimizations': true
          });
        cleanedCode = amdclean.clean(AMDcode, options),
        standardJavaScript = "var example1,example2,example,blah;example1=function (){var count=0;return'firstModule';}();example2=function (){var count=0;return'secondModule';}();example=function (yo,yoyo){var test=true;}(example1,example2);blah=function (yoooo,comon){return false;}(example2);";

        expect(cleanedCode).toBe(standardJavaScript);
      });

      it('should hoist and remove callback and IIFE parameters that are used more than once', function() {
        var AMDcode = "define('example1', function() { var count = 0;return 'firstModule'; });define('example2', function() { var count = 0;return 'secondModule'; });define('example', ['example1', 'example2'], function(yo, yoyo) {var test = true;});define('blah', ['example1', 'example2'], function(yo, yoooo, comon) { var test; return false; });",
          options = _.merge(_.cloneDeep(defaultOptions), {
            'aggressiveOptimizations': true
          });
        cleanedCode = amdclean.clean(AMDcode, options),
        standardJavaScript = "var example1,example2,example,blah,yo;example1=yo=function (){var count=0;return'firstModule';}();example2=function (){var count=0;return'secondModule';}();example=function (yoyo){var test=true;}(example2);blah=function (yoooo,comon){var test;return false;}(example2);";

        expect(cleanedCode).toBe(standardJavaScript);
      });

      it('should correctly normalize relative file paths', function() {
        var AMDcode = "define('./modules/example', ['example1', 'example2'], function(one, two) {var test = true;});",
          cleanedCode = amdclean.clean(AMDcode, defaultOptions),
          standardJavaScript = "var modules_example;modules_example=function (one,two){var test=true;}(example1,example2);";

        expect(cleanedCode).toBe(standardJavaScript);
      });

      it('should correctly normalize relative file paths dependencies', function() {
        var AMDcode = "define('./modules/example', ['./example1', './example2', '../example3'], function(one, two, three) {var test = true;});",
          cleanedCode = amdclean.clean(AMDcode, defaultOptions),
          standardJavaScript = "var modules_example;modules_example=function (one,two,three){var test=true;}(modules_example1,modules_example2,example3);";

        expect(cleanedCode).toBe(standardJavaScript);
      });

      it('should correctly normalize multi-level relative file paths dependencies', function() {
        var AMDcode = "define('./foo/prototype/subModule/myModule', ['example1','example2', '/anotherModule/example3', '../../example4','../anotherModule/example5'], function(one, two, three, four, five) { var test = true;});",
          cleanedCode = amdclean.clean(AMDcode, defaultOptions),
          standardJavaScript = "var foo_prototype_subModule_myModule;foo_prototype_subModule_myModule=function (one,two,three,four,five){var test=true;}(example1,example2,anotherModule_example3,foo_example4,foo_prototype_anotherModule_example5);";

        expect(cleanedCode).toBe(standardJavaScript);
      });

      it('should correctly normalize multi-level relative file paths', function() {
        var AMDcode = "define('./foo/prototype/commonMethodName.js', ['example1', 'example2'], function(one, two) { var test = true;});",
          cleanedCode = amdclean.clean(AMDcode, defaultOptions),
          standardJavaScript = "var foo_prototype_commonMethodNamejs;foo_prototype_commonMethodNamejs=function (one,two){var test=true;}(example1,example2);";

        expect(cleanedCode).toBe(standardJavaScript);
      });

      it('should correctly prefix reserved keywords with an underscore', function() {
        var AMDcode = "define('foo', ['./function'], function(fn){ fn.bar(); });",
          cleanedCode = amdclean.clean(AMDcode, defaultOptions),
          standardJavaScript = "var foo;foo=function (fn){fn.bar();}(_function);";

        expect(cleanedCode).toBe(standardJavaScript);
      });

      it('should allow underscores and dollar signs as module names', function() {
        var AMDcode = "define('fo.o', ['./function'], function(fn){ fn.bar(); });",
          cleanedCode = amdclean.clean(AMDcode, defaultOptions),
          standardJavaScript = "var foo;foo=function (fn){fn.bar();}(_function);";

        expect(cleanedCode).toBe(standardJavaScript);
      });

      it('should not convert defines with an /*amdclean*/ comment before it', function() {
        var AMDcode = "/*amdclean*/define('./modules/example', ['example1', 'example2'], function(one, two) {});",
          cleanedCode = amdclean.clean(AMDcode, defaultOptions),
          standardJavaScript = "/*amdclean*/\ndefine('./modules/example',['example1','example2'],function(one,two){});";

        expect(cleanedCode).toBe(standardJavaScript);
      });

      it('should not convert defines with a custom commentCleanName comment before it', function() {
        var AMDcode = "/*donotremove*/define('./modules/example', ['example1', 'example2'], function(one, two) {});",
          options = _.merge(_.cloneDeep(defaultOptions), {
            'commentCleanName': 'donotremove'
          });
        cleanedCode = amdclean.clean(AMDcode, options),
        standardJavaScript = "/*donotremove*/\ndefine('./modules/example',['example1','example2'],function(one,two){});";

        expect(cleanedCode).toBe(standardJavaScript);
      });

      it('should not convert defines that are added to the ignoreModules options array', function() {
        var AMDcode = "define('exampleModule', ['example1', 'example2'], function(one, two) {});",
          options = _.merge(_.cloneDeep(defaultOptions), {
            'ignoreModules': ['exampleModule']
          }),
          cleanedCode = amdclean.clean(AMDcode, options),
          standardJavaScript = "define('exampleModule',['example1','example2'],function(one,two){});";

        expect(cleanedCode).toBe(standardJavaScript);
      });

      it('should remove defines that are added to the removeModules options array', function() {
        var AMDcode = "define('exampleModule', ['example1', 'example2'], function(one, two) {});define('exampleModule2', function() {})",
          options = _.merge(_.cloneDeep(defaultOptions), {
            'removeModules': ['exampleModule']
          }),
          cleanedCode = amdclean.clean(AMDcode, options),
          standardJavaScript = "var exampleModule,exampleModule2;exampleModule2=undefined;";

        expect(cleanedCode).toBe(standardJavaScript);
      });

      it('should support global modules', function() {
        var AMDcode = "define('foo', ['require', 'exports', './bar'], function(require, exports){exports.bar = require('./bar');});",
          options = _.merge(_.cloneDeep(defaultOptions), {
            'globalModules': ['foo']
          }),
          cleanedCode = amdclean.clean(AMDcode, options),
          standardJavaScript = "var foo;foo=function (exports){exports.bar=bar;return exports;}({});window.foo=foo;";

        expect(cleanedCode).toBe(standardJavaScript);
      });

      it('should support converting shimmed modules that export a global object', function() {
        var AMDcode = "define('backbone', ['underscore', 'jquery'], (function (global) { return function () { var ret, fn; return ret || global.Backbone; }; }(this)));",
          cleanedCode = amdclean.clean(AMDcode, defaultOptions),
          standardJavaScript = "var backbone;backbone=window.Backbone;";

        expect(cleanedCode).toBe(standardJavaScript);
      });

      it('should support setting the module return value via the shimOverrides option', function() {
        var AMDcode = "define('backbone', ['underscore', 'jquery'], (function (global) { return function () { var ret, fn; return ret || global.Backbone; }; }(this)));",
          options = _.merge(_.cloneDeep(defaultOptions), {
            'shimOverrides': {
              'backbone': 'window.Backbone'
            }
          }),
          cleanedCode = amdclean.clean(AMDcode, options),<commit_msg>Ensure that test excercises unique code path<commit_after>require('jasmine-only');
describe('amdclean specs', function() {
  var amdclean = require('../../build/amdclean'),
    requirejs = require('requirejs'),
    _ = require('lodash'),
    fs = require('fs'),
    defaultOptions = {
      // We don't need the IIFE wrapper by default, since it makes the tests more verbose
      'wrap': {
        'start': '',
        'end': ''
      },
      // Makes sure the code output is on one line (easier to test)
      'escodegen': {
        'format': {
          'compact': true
        }
      }
    }

  describe('define() method conversions', function() {

    describe('functions', function() {

      it('should convert function return values to immediately invoked function declarations', function() {
        var AMDcode = "define('example', [], function() {});",
          cleanedCode = amdclean.clean(AMDcode, defaultOptions);
        standardJavaScript = "var example;example=undefined;";

        expect(cleanedCode).toBe(standardJavaScript);
      });

      it('should preserve single line comments when converting function return values to immediately invoked function declarations', function() {
        var AMDcode = "define('example', [], function() {  // Answer\n var test = true; return test; });",
          cleanedCode = amdclean.clean(AMDcode, defaultOptions),
          standardJavaScript = "var example;example=function (){// Answer\nvar test=true;return test;}();";

        expect(cleanedCode).toBe(standardJavaScript);
      });

      it('should preserve multi-line comments when converting function return values to immediately invoked function declarations', function() {
        var AMDcode = "define('example', [], function() {  /* Answer */\nvar test = true; return test; });",
          cleanedCode = amdclean.clean(AMDcode, defaultOptions),
          standardJavaScript = "var example;example=function (){/* Answer */\nvar test=true;return test;}();";

        expect(cleanedCode).toBe(standardJavaScript);
      });

      it('should pass a file path instead of the code directly', function() {
        var options = _.merge(_.cloneDeep(defaultOptions), {
            'filePath': __dirname + '/../filePathTest.js'
          }),
          cleanedCode = amdclean.clean(options),
          standardJavaScript = "var example;example=undefined;";

        expect(cleanedCode).toBe(standardJavaScript);
      });

      it('should correctly set callback parameters to the callback function', function() {
        var AMDcode = "define('example', ['example1', 'example2'], function(one, two) {var test = true;});",
          cleanedCode = amdclean.clean(AMDcode, defaultOptions),
          standardJavaScript = "var example;example=function (one,two){var test=true;}(example1,example2);";

        expect(cleanedCode).toBe(standardJavaScript);
      });

      it('should not hoist and remove callback and IIFE parameters that are only used once', function() {
        var AMDcode = "define('example1', function() { var count = 0;return 'firstModule'; });define('example2', function() { var count = 0;return 'secondModule'; });define('example', ['example1', 'example2'], function(yo, yoyo) {var test = true;});define('blah', ['example2'], function(yoooo, comon) { return false; });",
          options = _.merge(_.cloneDeep(defaultOptions), {
            'aggressiveOptimizations': true
          });
        cleanedCode = amdclean.clean(AMDcode, options),
        standardJavaScript = "var example1,example2,example,blah;example1=function (){var count=0;return'firstModule';}();example2=function (){var count=0;return'secondModule';}();example=function (yo,yoyo){var test=true;}(example1,example2);blah=function (yoooo,comon){return false;}(example2);";

        expect(cleanedCode).toBe(standardJavaScript);
      });

      it('should hoist and remove callback and IIFE parameters that are used more than once', function() {
        var AMDcode = "define('example1', function() { var count = 0;return 'firstModule'; });define('example2', function() { var count = 0;return 'secondModule'; });define('example', ['example1', 'example2'], function(yo, yoyo) {var test = true;});define('blah', ['example1', 'example2'], function(yo, yoooo, comon) { var test; return false; });",
          options = _.merge(_.cloneDeep(defaultOptions), {
            'aggressiveOptimizations': true
          });
        cleanedCode = amdclean.clean(AMDcode, options),
        standardJavaScript = "var example1,example2,example,blah,yo;example1=yo=function (){var count=0;return'firstModule';}();example2=function (){var count=0;return'secondModule';}();example=function (yoyo){var test=true;}(example2);blah=function (yoooo,comon){var test;return false;}(example2);";

        expect(cleanedCode).toBe(standardJavaScript);
      });

      it('should correctly normalize relative file paths', function() {
        var AMDcode = "define('./modules/example', ['example1', 'example2'], function(one, two) {var test = true;});",
          cleanedCode = amdclean.clean(AMDcode, defaultOptions),
          standardJavaScript = "var modules_example;modules_example=function (one,two){var test=true;}(example1,example2);";

        expect(cleanedCode).toBe(standardJavaScript);
      });

      it('should correctly normalize relative file paths dependencies', function() {
        var AMDcode = "define('./modules/example', ['./example1', './example2', '../example3'], function(one, two, three) {var test = true;});",
          cleanedCode = amdclean.clean(AMDcode, defaultOptions),
          standardJavaScript = "var modules_example;modules_example=function (one,two,three){var test=true;}(modules_example1,modules_example2,example3);";

        expect(cleanedCode).toBe(standardJavaScript);
      });

      it('should correctly normalize multi-level relative file paths dependencies', function() {
        var AMDcode = "define('./foo/prototype/subModule/myModule', ['example1','example2', '/anotherModule/example3', '../../example4','../anotherModule/example5'], function(one, two, three, four, five) { var test = true;});",
          cleanedCode = amdclean.clean(AMDcode, defaultOptions),
          standardJavaScript = "var foo_prototype_subModule_myModule;foo_prototype_subModule_myModule=function (one,two,three,four,five){var test=true;}(example1,example2,anotherModule_example3,foo_example4,foo_prototype_anotherModule_example5);";

        expect(cleanedCode).toBe(standardJavaScript);
      });

      it('should correctly normalize multi-level relative file paths', function() {
        var AMDcode = "define('./foo/prototype/commonMethodName.js', ['example1', 'example2'], function(one, two) { var test = true;});",
          cleanedCode = amdclean.clean(AMDcode, defaultOptions),
          standardJavaScript = "var foo_prototype_commonMethodNamejs;foo_prototype_commonMethodNamejs=function (one,two){var test=true;}(example1,example2);";

        expect(cleanedCode).toBe(standardJavaScript);
      });

      it('should correctly prefix reserved keywords with an underscore', function() {
        var AMDcode = "define('foo', ['./function'], function(fn){ fn.bar(); });",
          cleanedCode = amdclean.clean(AMDcode, defaultOptions),
          standardJavaScript = "var foo;foo=function (fn){fn.bar();}(_function);";

        expect(cleanedCode).toBe(standardJavaScript);
      });

      it('should allow underscores and dollar signs as module names', function() {
        var AMDcode = "define('fo.o', ['./function'], function(fn){ fn.bar(); });",
          cleanedCode = amdclean.clean(AMDcode, defaultOptions),
          standardJavaScript = "var foo;foo=function (fn){fn.bar();}(_function);";

        expect(cleanedCode).toBe(standardJavaScript);
      });

      it('should not convert defines with an /*amdclean*/ comment before it', function() {
        var AMDcode = "/*amdclean*/define('./modules/example', ['example1', 'example2'], function(one, two) {});",
          cleanedCode = amdclean.clean(AMDcode, defaultOptions),
          standardJavaScript = "/*amdclean*/\ndefine('./modules/example',['example1','example2'],function(one,two){});";

        expect(cleanedCode).toBe(standardJavaScript);
      });

      it('should not convert defines with a custom commentCleanName comment before it', function() {
        var AMDcode = "/*donotremove*/define('./modules/example', ['example1', 'example2'], function(one, two) {});",
          options = _.merge(_.cloneDeep(defaultOptions), {
            'commentCleanName': 'donotremove'
          });
        cleanedCode = amdclean.clean(AMDcode, options),
        standardJavaScript = "/*donotremove*/\ndefine('./modules/example',['example1','example2'],function(one,two){});";

        expect(cleanedCode).toBe(standardJavaScript);
      });

      it('should not convert defines that are added to the ignoreModules options array', function() {
        var AMDcode = "define('exampleModule', ['example1', 'example2'], function(one, two) {});",
          options = _.merge(_.cloneDeep(defaultOptions), {
            'ignoreModules': ['exampleModule']
          }),
          cleanedCode = amdclean.clean(AMDcode, options),
          standardJavaScript = "define('exampleModule',['example1','example2'],function(one,two){});";

        expect(cleanedCode).toBe(standardJavaScript);
      });

      it('should remove defines that are added to the removeModules options array', function() {
        var AMDcode = "define('exampleModule', ['example1', 'example2'], function(one, two) {});define('exampleModule2', function() {})",
          options = _.merge(_.cloneDeep(defaultOptions), {
            'removeModules': ['exampleModule']
          }),
          cleanedCode = amdclean.clean(AMDcode, options),
          standardJavaScript = "var exampleModule,exampleModule2;exampleModule2=undefined;";

        expect(cleanedCode).toBe(standardJavaScript);
      });

      it('should support global modules', function() {
        var AMDcode = "define('foo', ['require', 'exports', './bar'], function(require, exports){exports.bar = require('./bar');});",
          options = _.merge(_.cloneDeep(defaultOptions), {
            'globalModules': ['foo']
          }),
          cleanedCode = amdclean.clean(AMDcode, options),
          standardJavaScript = "var foo;foo=function (exports){exports.bar=bar;return exports;}({});window.foo=foo;";

        expect(cleanedCode).toBe(standardJavaScript);
      });

      it('should support converting shimmed modules that export a global object', function() {
        var AMDcode = "define('backbone', ['underscore', 'jquery'], (function (global) { return function () { var ret, fn; return ret || global.Backbone; }; }(this)));",
          cleanedCode = amdclean.clean(AMDcode, defaultOptions),
          standardJavaScript = "var backbone;backbone=window.Backbone;";

        expect(cleanedCode).toBe(standardJavaScript);
      });

      it('should support setting the module return value via the shimOverrides option', function() {
        var AMDcode = "define('backbone', ['underscore', 'jquery'], (function (global) { return {}; }(this)));",
          options = _.merge(_.cloneDeep(defaultOptions), {
            'shimOverrides': {
              'backbone': 'window.Backbone'
            }
          }),
          cleanedCode = amdclean.clean(AMDcode, options),
{'fraction_non_alphanumeric': 0.154635558284361, 'fraction_numerical': 0.006753793526883606, 'mean_word_length': 3.746669442131557, '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': '6399469', 'n_tokens_mistral': 6205, 'n_tokens_neox': 6001, 'n_words': 1396}