<commit_before>"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});

exports.default = function (_ref4) {
  var t = _ref4.types;

  return {
    visitor: {
      JSXOpeningElement: {
        exit: function exit(_ref5 /*, parent, scope, file */) {
          var node = _ref5.node;

          var key = undefined;
          var recycle = false;
          var props = node.attributes.length && node.attributes.reduce(function (props, attr) {
            var propName = attr.name.name;
            var value = transformProp(t, attr.value);

            if (propName === "key") {
              key = value;
            } else if (propName === "recycle") {
              recycle = true;
            } else if (value != null) {
              props[propName] = value;
            }

            return props;
          }, {});

          node.__xvdom_desc = {
            key: key,
            recycle: recycle,
            props: props,
            el: node.name.name
          };
        }
      },

      JSXClosingElement: {
        exit: function exit(path) {
          path.remove();
        }
      },

      JSXElement: {
        exit: function exit(path, state) {
          var node = path.node;
          var children = (0, _helpers.buildChildren)(t, node.children);
          var desc = node.openingElement.__xvdom_desc;
          desc.children = children;

          if (!t.isJSX(path.parent)) {
            path.replaceWith(createInstanceObject(t, state.file, desc));
          }
        }
      }
    }
  };
};

var _helpers = require("./helpers");

var _genId = require("./genId");

var _genId2 = _interopRequireDefault(_genId);

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

function _toConsumableArray(arr) { if (Array.isArray(arr)) { for (var i = 0, arr2 = Array(arr.length); i < arr.length; i++) { arr2[i] = arr[i]; } return arr2; } else { return Array.from(arr); } }

var EMPTY_ARRAY = [];

function isComponentName(name) {
  return (/^[A-Z]/.test(name)
  );
}

function hasDynamicComponentProps(t, props) {
  return props && Object.keys(props).some(function (prop) {
    return isDynamic(t, props[prop]);
  });
}

function isDynamic(t, astNode) {
  if (t.isLiteral(astNode)) {
    return false;
  }
  if (t.isLogicalExpression(astNode) || t.isBinaryExpression(astNode)) {
    return isDynamic(t, astNode.left) || isDynamic(t, astNode.right);
  }
  if (t.isUnaryExpression(astNode)) {
    return isDynamic(t, astNode.argument);
  }
  return true;
}

function objProp(t, key, value) {
  key = t.isIdentifier(key) ? key : t.identifier(key);
  return t.objectProperty(key, value);
}

function transformProp(t, prop) {
  return t.isJSXExpressionContainer(prop) ? transformProp(t, prop.expression) : t.isJSXEmptyExpression(prop) ? null : t.isIdentifier(prop) || t.isMemberExpression(prop) ? (0, _helpers.toReference)(t, prop) : prop == null ? t.booleanLiteral(true) : prop;
}

function createPropsStatements(t, instanceParamId, genDynamicIdentifiers, nodeId, props) {
  if (!props) return [];
  var contextId = undefined;

  // >>> _n.prop1 = prop1Value;
  // >>> _n.prop2 = prop2Value;
  // >>> ...
  return Object.keys(props).reduce(function (acc, prop) {
    var value = props[prop];
    var dyn = undefined;
    var rhs = !isDynamic(t, value) ? value : t.memberExpression(instanceParamId, (dyn = genDynamicIdentifiers(value, prop, null, null, contextId)).valueId);

    return [].concat(_toConsumableArray(acc), _toConsumableArray(!contextId && dyn ? (contextId = dyn.contextId, [t.expressionStatement(t.assignmentExpression("=", t.memberExpression(instanceParamId, dyn.contextId), nodeId))]) : EMPTY_ARRAY), [t.expressionStatement(t.assignmentExpression("=", t.memberExpression(nodeId, t.identifier(prop)), rhs))]);
  }, []);
}

function createComponentCode(t, elCompName, props, instanceParamId, dynamicIds) {
  props = props || {};
  var componentPropMap = dynamicIds.componentPropMap;
  var objProps = Object.keys(props).map(function (prop) {
    return objProp(t, prop, isDynamic(t, props[prop]) ? t.memberExpression(instanceParamId, componentPropMap[prop].id) : props[prop]);
  });

  // >>> xvdom.createComponent(MyComponent, props, )
  return t.callExpression(t.memberExpression(t.identifier("xvdom"), t.identifier("createComponent")), [t.identifier(elCompName), objProps.length ? t.objectExpression(objProps) : t.nullLiteral(), instanceParamId, t.stringLiteral(dynamicIds.rerenderId.name), t.stringLiteral(dynamicIds.contextId.name), t.stringLiteral(dynamicIds.componentId.name)]);
}

function createElementsCode(t, instanceParamId, genUidIdentifier, genDynamicIdentifiers, parentId, children) {
  var result = { variableDeclarators: [], statements: [] };
  if (!children) return result;

  var tmpNodeId = undefined;
  return children.reduce(function (acc, child) {
    var createEl = undefined;
    if (t.isJSXElement(child)) {
      var _child$openingElement = child.openingElement.__xvdom_desc;
      var el = _child$openingElement.el;
      var props = _child$openingElement.props;
      var _children = _child$openingElement.children;

      var hasProps = props && Object.keys(props).length;
      var hasChildren = _children && _children.length;
      var isComponent = isComponentName(el);

      // >>> document.createElement("div")
      if (isComponent) {
        createEl = createComponentCode(t, el, props, instanceParamId, genDynamicIdentifiers(null, null, el, props));
      } else {
        createEl = t.callExpression(t.memberExpression(t.identifier("document"), t.identifier("createElement")), [t.stringLiteral(el)]);
      }

      if (hasProps || hasChildren) {
        if (!tmpNodeId) {
          tmpNodeId = genUidIdentifier();
          acc.variableDeclarators.push(t.variableDeclarator(tmpNodeId));
        }

        acc.statements.push(
        // >>> _n = document.createElement("div")
        t.expressionStatement(t.assignmentExpression("=", tmpNodeId, createEl)));
        createEl = tmpNodeId;

        if (!isComponent && hasProps) {
          var _acc$statements;

          (_acc$statements = acc.statements).push.apply(_acc$statements, _toConsumableArray(createPropsStatements(t, instanceParamId, genDynamicIdentifiers, tmpNodeId, props)));
        }

        if (hasChildren) {
          var _acc$variableDeclarat, _acc$statements2;

          var _createElementsCode = createElementsCode(t, instanceParamId, genUidIdentifier, genDynamicIdentifiers, tmpNodeId, _children);

          var variableDeclarators = _createElementsCode.variableDeclarators;
          var statements = _createElementsCode.statements;

          (_acc$variableDeclarat = acc.variableDeclarators).push.apply(_acc$variableDeclarat, _toConsumableArray(variableDeclarators));
          (_acc$statements2 = acc.statements).push.apply(_acc$statements2, _toConsumableArray(statements));
        }
      }
    } else if (isDynamic(t, child)) {
      var _genDynamicIdentifier = genDynamicIdentifiers(child);

      var valueId = _genDynamicIdentifier.valueId;
      var rerenderId = _genDynamicIdentifier.rerenderId;
      var contextId = _genDynamicIdentifier.contextId;

      // >>> xvdom.createDynamic(inst.v0, inst, "r0", "c0");

      createEl = t.callExpression(t.memberExpression(t.identifier("xvdom"), t.identifier("createDynamic")), [t.memberExpression(instanceParamId, valueId), instanceParamId, t.stringLiteral(rerenderId.name), t.stringLiteral(contextId.name)]);
    } else {
      createEl = t.callExpression(t.memberExpression(t.identifier("document"), t.identifier("createTextNode")), [t.logicalExpression("||", t.sequenceExpression([child]), t.stringLiteral(""))]);
    }

    acc.statements.push(
    // >>> parentNode.appendChild(...);
    t.expressionStatement(t.callExpression(t.memberExpression(parentId, t.identifier("appendChild")), [createEl])));
    return acc;
  }, result);
}

function createRootElementCode(t, instanceParamId, genUidIdentifier, genDynamicIdentifiers, _ref) {
  var el = _ref.el;
  var props = _ref.props;
  var children = _ref.children;

  var nodeId = genUidIdentifier();
  var isComponent = isComponentName(el);

  // >>> document.createElement("div")
  var createEl = isComponent ? createComponentCode(t, el, props, instanceParamId, genDynamicIdentifiers(null, null, el, props)) : t.callExpression(t.memberExpression(t.identifier("document"), t.identifier("createElement")), [t.stringLiteral(el)]);<commit_msg>rebuild gen<commit_after>"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});

exports.default = function (_ref4) {
  var t = _ref4.types;

  return {
    visitor: {
      JSXOpeningElement: {
        exit: function exit(_ref5 /*, parent, scope, file */) {
          var node = _ref5.node;

          var key = undefined;
          var recycle = false;
          var props = node.attributes.length && node.attributes.reduce(function (props, attr) {
            var propName = attr.name.name;
            var value = transformProp(t, attr.value);

            if (propName === "key") {
              key = value;
            } else if (propName === "recycle") {
              recycle = true;
            } else if (value != null) {
              props[propName] = value;
            }

            return props;
          }, {});

          node.__xvdom_desc = {
            key: key,
            recycle: recycle,
            props: props,
            el: node.name.name
          };
        }
      },

      JSXClosingElement: {
        exit: function exit(path) {
          path.remove();
        }
      },

      JSXElement: {
        exit: function exit(path, state) {
          var node = path.node;
          var children = (0, _helpers.buildChildren)(t, node.children);
          var desc = node.openingElement.__xvdom_desc;
          desc.children = children;

          if (!t.isJSX(path.parent)) {
            path.replaceWith(createInstanceObject(t, state.file, desc));
          }
        }
      }
    }
  };
};

var _helpers = require("./helpers");

var _genId = require("./genId");

var _genId2 = _interopRequireDefault(_genId);

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

function _toConsumableArray(arr) { if (Array.isArray(arr)) { for (var i = 0, arr2 = Array(arr.length); i < arr.length; i++) { arr2[i] = arr[i]; } return arr2; } else { return Array.from(arr); } }

var EMPTY_ARRAY = [];

function isComponentName(name) {
  return (/^[A-Z]/.test(name)
  );
}

function hasDynamicComponentProps(t, props) {
  return props && Object.keys(props).some(function (prop) {
    return isDynamic(t, props[prop]);
  });
}

function isDynamic(t, astNode) {
  if (t.isLiteral(astNode)) {
    return false;
  }
  if (t.isLogicalExpression(astNode) || t.isBinaryExpression(astNode)) {
    return isDynamic(t, astNode.left) || isDynamic(t, astNode.right);
  }
  if (t.isUnaryExpression(astNode)) {
    return isDynamic(t, astNode.argument);
  }
  return true;
}

function objProp(t, key, value) {
  key = t.isIdentifier(key) ? key : t.identifier(key);
  return t.objectProperty(key, value);
}

function transformProp(t, prop) {
  return t.isJSXExpressionContainer(prop) ? transformProp(t, prop.expression) : t.isJSXEmptyExpression(prop) ? null : t.isIdentifier(prop) || t.isMemberExpression(prop) ? (0, _helpers.toReference)(t, prop) : prop == null ? t.booleanLiteral(true) : prop;
}

function createPropsStatements(t, instanceParamId, genDynamicIdentifiers, nodeId, props) {
  if (!props) return [];
  var contextId = undefined;

  // >>> _n.prop1 = prop1Value;
  // >>> _n.prop2 = prop2Value;
  // >>> ...
  return Object.keys(props).reduce(function (acc, prop) {
    var value = props[prop];
    var dyn = undefined;
    var rhs = !isDynamic(t, value) ? value : t.memberExpression(instanceParamId, (dyn = genDynamicIdentifiers(value, prop, null, null, contextId)).valueId);

    return [].concat(_toConsumableArray(acc), _toConsumableArray(!contextId && dyn ? (contextId = dyn.contextId, [t.expressionStatement(t.assignmentExpression("=", t.memberExpression(instanceParamId, dyn.contextId), nodeId))]) : EMPTY_ARRAY), [t.expressionStatement(t.assignmentExpression("=", t.memberExpression(nodeId, t.identifier(prop)), rhs))]);
  }, []);
}

function createComponentCode(t, elCompName, props, instanceParamId, dynamicIds) {
  props = props || {};
  var componentPropMap = dynamicIds.componentPropMap;
  var objProps = Object.keys(props).map(function (prop) {
    return objProp(t, prop, isDynamic(t, props[prop]) ? t.memberExpression(instanceParamId, componentPropMap[prop].id) : props[prop]);
  });

  // >>> xvdom.createComponent(MyComponent, props, )
  return t.callExpression(t.memberExpression(t.identifier("xvdom"), t.identifier("createComponent")), [t.identifier(elCompName), objProps.length ? t.objectExpression(objProps) : t.nullLiteral(), instanceParamId, t.stringLiteral(dynamicIds.rerenderId.name), t.stringLiteral(dynamicIds.contextId.name), t.stringLiteral(dynamicIds.componentId.name)]);
}

function createElementsCode(t, instanceParamId, genUidIdentifier, genDynamicIdentifiers, parentId, children) {
  var result = { variableDeclarators: [], statements: [] };
  if (!children) return result;

  var tmpNodeId = undefined;
  return children.reduce(function (acc, child) {
    var createEl = undefined;
    if (t.isJSXElement(child)) {
      var _child$openingElement = child.openingElement.__xvdom_desc;
      var el = _child$openingElement.el;
      var props = _child$openingElement.props;
      var _children = _child$openingElement.children;

      var hasProps = props && Object.keys(props).length;
      var hasChildren = _children && _children.length;
      var isComponent = isComponentName(el);

      // >>> document.createElement("div")
      if (isComponent) {
        createEl = createComponentCode(t, el, props, instanceParamId, genDynamicIdentifiers(null, null, el, props));
      } else {
        createEl = t.callExpression(t.memberExpression(t.identifier("document"), t.identifier("createElement")), [t.stringLiteral(el)]);
      }

      if (hasProps || hasChildren) {
        if (!tmpNodeId) {
          tmpNodeId = genUidIdentifier();
          acc.variableDeclarators.push(t.variableDeclarator(tmpNodeId));
        }

        acc.statements.push(
        // >>> _n = document.createElement("div")
        t.expressionStatement(t.assignmentExpression("=", tmpNodeId, createEl)));
        createEl = tmpNodeId;

        if (!isComponent && hasProps) {
          var _acc$statements;

          (_acc$statements = acc.statements).push.apply(_acc$statements, _toConsumableArray(createPropsStatements(t, instanceParamId, genDynamicIdentifiers, tmpNodeId, props)));
        }

        if (hasChildren) {
          var _acc$variableDeclarat, _acc$statements2;

          var _createElementsCode = createElementsCode(t, instanceParamId, genUidIdentifier, genDynamicIdentifiers, tmpNodeId, _children);

          var variableDeclarators = _createElementsCode.variableDeclarators;
          var statements = _createElementsCode.statements;

          (_acc$variableDeclarat = acc.variableDeclarators).push.apply(_acc$variableDeclarat, _toConsumableArray(variableDeclarators));
          (_acc$statements2 = acc.statements).push.apply(_acc$statements2, _toConsumableArray(statements));
        }
      }
    } else if (isDynamic(t, child)) {
      var _genDynamicIdentifier = genDynamicIdentifiers(child);

      var valueId = _genDynamicIdentifier.valueId;
      var rerenderId = _genDynamicIdentifier.rerenderId;
      var contextId = _genDynamicIdentifier.contextId;

      // >>> xvdom.createDynamic(inst.v0, inst, "r0", "c0");

      createEl = t.callExpression(t.memberExpression(t.identifier("xvdom"), t.identifier("createDynamic")), [children.length === 1 ? parentId : t.identifier("null"), t.memberExpression(instanceParamId, valueId), instanceParamId, t.stringLiteral(rerenderId.name), t.stringLiteral(contextId.name)]);
    } else {
      createEl = t.callExpression(t.memberExpression(t.identifier("document"), t.identifier("createTextNode")), [t.logicalExpression("||", t.sequenceExpression([child]), t.stringLiteral(""))]);
    }

    acc.statements.push(
    // >>> parentNode.appendChild(...);
    t.expressionStatement(t.callExpression(t.memberExpression(parentId, t.identifier("appendChild")), [createEl])));
    return acc;
  }, result);
}

function createRootElementCode(t, instanceParamId, genUidIdentifier, genDynamicIdentifiers, _ref) {
  var el = _ref.el;
  var props = _ref.props;
  var children = _ref.children;

  var nodeId = genUidIdentifier();
  var isComponent = isComponentName(el);

  // >>> document.createElement("div")
  var createEl = isComponent ? createComponentCode(t, el, props, instanceParamId, genDynamicIdentifiers(null, null, el, props)) : t.callExpression(t.memberExpression(t.identifier("document"), t.identifier("createElement")), [t.stringLiteral(el)]);
{'fraction_non_alphanumeric': 0.1383047652706603, 'fraction_numerical': 0.002532838546268481, 'mean_word_length': 3.567662093085822, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 57, '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': '2038098', 'n_tokens_mistral': 5161, 'n_tokens_neox': 5081, 'n_words': 986}