<commit_before>var System = require('systemjs');
var traceur = require('traceur');

var ScopeTransformer = traceur.get('codegeneration/ScopeTransformer').ScopeTransformer;
var parseExpression = traceur.get('codegeneration/PlaceholderParser').parseExpression;

var CJSRequireTransformer = require('./cjs').CJSRequireTransformer;

// First of two-pass transform
// lists number of define statements, the named module it defines (if any), and deps
// second pass will do rewriting based on this info
// we set this.isAnon, which is true if there is one named define, or one anonymous define
// if there are more than one anonymous defines, it is invalid
function AMDDependenciesTransformer(map) {
  // optional mapping function
  this.map = map;
  this.anonDefine = false;
  this.defineBundle = false;
  this.deps = [];
  return ScopeTransformer.call(this, 'define');
}
AMDDependenciesTransformer.prototype = Object.create(ScopeTransformer.prototype);
AMDDependenciesTransformer.prototype.filterAMDDeps = function(deps) {
  var newDeps = [];
  deps.forEach(function(dep) {
    if (['require', 'exports', 'module'].indexOf(dep) != -1)
      return;
    newDeps.push(dep);
  });
  return newDeps;
}
AMDDependenciesTransformer.prototype.transformCallExpression = function(tree) {
  if (!tree.operand.identifierToken || tree.operand.identifierToken.value != 'define')
    return ScopeTransformer.prototype.transformCallExpression.call(this, tree);

  var args = tree.args.args;
  var name = args[0].type === 'LITERAL_EXPRESSION' && args[0].literalToken.processedValue;

  // anonymous define
  if (!name) {
    // already defined anonymously -> throw
    if (this.anonDefine)
      throw "Multiple defines for anonymous module";
    this.anonDefine = true;
  }
  // named define
  else {
    // if we don't have any other defines, 
    // then let this be an anonymous define
    if (!this.anonDefine && !this.defineBundle)
      this.anonDefine = true;

    // otherwise its a bundle only
    else {
      this.anonDefine = false;
      this.deps = [];
    }

    // the above is just to support single modules of the form:
    // define('jquery')
    // still loading anonymously
    // because it is done widely enough to be useful

    // note this is now a bundle
    this.defineBundle = true;
  }

  // only continue to extracting dependencies if we're anonymous
  if (!this.anonDefine)
    return;

  var depTree;

  if (args[0].type === 'ARRAY_LITERAL_EXPRESSION')
    depTree = args[0];
  else if (args[1] && args[1].type == 'ARRAY_LITERAL_EXPRESSION')
    depTree = args[1];

  if (depTree) {
    // apply the map to the tree
    if (this.map)
      depTree.elements = depTree.elements.map(this.map);
    this.deps = this.filterAMDDeps(depTree.elements.map(function(dep) {
      return dep.literalToken.processedValue;
    }));
    return;
  }

  var cjsFactory;

  if (args[0].type == 'FUNCTION_EXPRESSION')
    cjsFactory = args[0];
  else if (args[0].type == 'LITERAL_EXPRESSION' && args[1] && args[1].type == 'FUNCTION_EXPRESSION')
    cjsFactory = args[1];

  if (cjsFactory) {
    // now we need to do a scope transformer for the require function at this position
    var fnParameters = cjsFactory.parameterList.parameters;
    var reqName = fnParameters[0] && fnParameters[0].parameter.binding.identifierToken.value;
    
    // now we create a new scope transformer and apply it to this function to find every call of
    // the function reqName, noting the require
    var cjsRequires = new CJSRequireTransformer(reqName, this.map);
    cjsFactory.body = cjsRequires.transformAny(cjsFactory.body);
    this.deps = this.filterAMDDeps(cjsRequires.requires);
  }
}

// AMD System.register transpiler
// This is the second of the two pass transform
function AMDDefineRegisterTransformer(load, isAnon, depMap) {
  this.load = load;
  this.isAnon = isAnon;
  this.depMap = depMap;
  return ScopeTransformer.call(this, 'define');
}
AMDDefineRegisterTransformer.prototype = Object.create(ScopeTransformer.prototype);
AMDDefineRegisterTransformer.prototype.transformCallExpression = function(tree) {
  if (!tree.operand.identifierToken || tree.operand.identifierToken.value != 'define')
    return ScopeTransformer.prototype.transformCallExpression.call(this, tree);

  var self = this;
  var args = tree.args.args;

  // only when "exports" is present as an argument
  // or dependency, does it become "this" for AMD
  // otherwise "this" must reference the global
  var bindToExports = false;
  /*
    define(['some', 'deps', 'require'], function(some, deps, require) {

    });

    ->

    System.register(['some', 'deps', 'require'], false, function(__require, __exports, __module) {
      (function(some, deps, require) {

      })(__require('some'), __require('deps'), __require);
    });

    define(['dep'], factory)

    ->

    System.register(['dep'], false, function(__require, __exports, __module) {
      return (factory)(__require('dep'));
    });


    define('jquery', [], factory)

    ->

    System.register([], false, factory);

    IF it is the only define

    otherwise we convert an AMD bundle into a register bundle:

    System.register('jquery', [], false, factory);

    Note that when __module is imported, we decorate it with 'uri' and an empty 'config' function
  */
  if (args[0].type === 'ARRAY_LITERAL_EXPRESSION' || args[0].type === 'LITERAL_EXPRESSION') {

    var name = this.load.name;
    var deps = args[0];
    var factory = args[1];

    if (args[0].type === 'LITERAL_EXPRESSION') {
      if (!this.isAnon)
        name = args[0].literalToken.processedValue;
      deps = args[1];
      factory = args[2];
    }<commit_msg>allow varied named define forms<commit_after>var System = require('systemjs');
var traceur = require('traceur');

var ScopeTransformer = traceur.get('codegeneration/ScopeTransformer').ScopeTransformer;
var parseExpression = traceur.get('codegeneration/PlaceholderParser').parseExpression;

var CJSRequireTransformer = require('./cjs').CJSRequireTransformer;

// First of two-pass transform
// lists number of define statements, the named module it defines (if any), and deps
// second pass will do rewriting based on this info
// we set this.isAnon, which is true if there is one named define, or one anonymous define
// if there are more than one anonymous defines, it is invalid
function AMDDependenciesTransformer(map) {
  // optional mapping function
  this.map = map;
  this.anonDefine = false;
  this.defineBundle = false;
  this.deps = [];
  return ScopeTransformer.call(this, 'define');
}
AMDDependenciesTransformer.prototype = Object.create(ScopeTransformer.prototype);
AMDDependenciesTransformer.prototype.filterAMDDeps = function(deps) {
  var newDeps = [];
  deps.forEach(function(dep) {
    if (['require', 'exports', 'module'].indexOf(dep) != -1)
      return;
    newDeps.push(dep);
  });
  return newDeps;
}
AMDDependenciesTransformer.prototype.transformCallExpression = function(tree) {
  if (!tree.operand.identifierToken || tree.operand.identifierToken.value != 'define')
    return ScopeTransformer.prototype.transformCallExpression.call(this, tree);

  var args = tree.args.args;
  var name = args[0].type === 'LITERAL_EXPRESSION' && args[0].literalToken.processedValue;

  // anonymous define
  if (!name) {
    // already defined anonymously -> throw
    if (this.anonDefine)
      throw "Multiple defines for anonymous module";
    this.anonDefine = true;
  }
  // named define
  else {
    // if we don't have any other defines, 
    // then let this be an anonymous define
    if (!this.anonDefine && !this.defineBundle)
      this.anonDefine = true;

    // otherwise its a bundle only
    else {
      this.anonDefine = false;
      this.deps = [];
    }

    // the above is just to support single modules of the form:
    // define('jquery')
    // still loading anonymously
    // because it is done widely enough to be useful

    // note this is now a bundle
    this.defineBundle = true;
  }

  // only continue to extracting dependencies if we're anonymous
  if (!this.anonDefine)
    return;

  var depTree;

  if (args[0].type === 'ARRAY_LITERAL_EXPRESSION')
    depTree = args[0];
  else if (args[1] && args[1].type == 'ARRAY_LITERAL_EXPRESSION')
    depTree = args[1];

  if (depTree) {
    // apply the map to the tree
    if (this.map)
      depTree.elements = depTree.elements.map(this.map);
    this.deps = this.filterAMDDeps(depTree.elements.map(function(dep) {
      return dep.literalToken.processedValue;
    }));
    return;
  }

  var cjsFactory;

  if (args[0].type == 'FUNCTION_EXPRESSION')
    cjsFactory = args[0];
  else if (args[0].type == 'LITERAL_EXPRESSION' && args[1] && args[1].type == 'FUNCTION_EXPRESSION')
    cjsFactory = args[1];

  if (cjsFactory) {
    // now we need to do a scope transformer for the require function at this position
    var fnParameters = cjsFactory.parameterList.parameters;
    var reqName = fnParameters[0] && fnParameters[0].parameter.binding.identifierToken.value;
    
    // now we create a new scope transformer and apply it to this function to find every call of
    // the function reqName, noting the require
    var cjsRequires = new CJSRequireTransformer(reqName, this.map);
    cjsFactory.body = cjsRequires.transformAny(cjsFactory.body);
    this.deps = this.filterAMDDeps(cjsRequires.requires);
  }
}

// AMD System.register transpiler
// This is the second of the two pass transform
function AMDDefineRegisterTransformer(load, isAnon, depMap) {
  this.load = load;
  this.isAnon = isAnon;
  this.depMap = depMap;
  return ScopeTransformer.call(this, 'define');
}
AMDDefineRegisterTransformer.prototype = Object.create(ScopeTransformer.prototype);
AMDDefineRegisterTransformer.prototype.transformCallExpression = function(tree) {
  if (!tree.operand.identifierToken || tree.operand.identifierToken.value != 'define')
    return ScopeTransformer.prototype.transformCallExpression.call(this, tree);

  var self = this;
  var args = tree.args.args;

  // check for named modules
  if (args[0].type === 'LITERAL_EXPRESSION') {
    if (!this.isAnon)
      name = args[0].literalToken.processedValue;
    args = args.splice(1);
  }

  // only when "exports" is present as an argument
  // or dependency, does it become "this" for AMD
  // otherwise "this" must reference the global
  var bindToExports = false;
  /*
    define(['some', 'deps', 'require'], function(some, deps, require) {

    });

    ->

    System.register(['some', 'deps', 'require'], false, function(__require, __exports, __module) {
      (function(some, deps, require) {

      })(__require('some'), __require('deps'), __require);
    });

    define(['dep'], factory)

    ->

    System.register(['dep'], false, function(__require, __exports, __module) {
      return (factory)(__require('dep'));
    });


    define('jquery', [], factory)

    ->

    System.register([], false, factory);

    IF it is the only define

    otherwise we convert an AMD bundle into a register bundle:

    System.register('jquery', [], false, factory);

    Note that when __module is imported, we decorate it with 'uri' and an empty 'config' function
  */
  if (args[0].type === 'ARRAY_LITERAL_EXPRESSION') {

    var name = this.load.name;
    var deps = args[0];
    var factory = args[1];
{'fraction_non_alphanumeric': 0.1230782823820463, 'fraction_numerical': 0.004064322318430819, 'mean_word_length': 3.6256640784634246, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 11, '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': '6766308', 'n_tokens_mistral': 3474, 'n_tokens_neox': 3360, 'n_words': 950}