<commit_before>import {forEach} from '../';

const BIND = 'bind';

function compileExpression(expression, scope) {
  expression = expression.replace(/([^\.a-z\$_])([a-z\$_]+)/ig, (match, char, propName) => {
    return `${char}scope.${propName}`;
  });

  let value;

  try {
    value = eval(`scope.${expression}`);
    if (typeof value === 'undefined') value = '';
  } catch (e) {
    value = '';
  }

  return value;
}

function compileElementBinding(element, scope) {
  let expression = element.getAttribute(BIND);
  element.innerHTML = compileExpression(expression, scope);
}

function bindElement(element, scope) {
  let listener = () => compileElementBinding(element, scope);
  scope.on('update', listener);
  return () => scope.removeListener('update', listener);
}

function compileAttributeBinding(element, attributeName, attributeValue, scope) {
  let attributeToReplace = attributeName.substr(6);
  element.setAttribute(attributeToReplace, compileExpression(attributeValue, scope));
}

function bindAttribute(element, attributeName, attributeValue, scope) {
  let listener = () => compileAttributeBinding(element, attributeName, attributeValue, scope);
  scope.on('update', listener);
  return () => scope.removeListener('update', listener);
}

function bindFactory(element, scope) {
  let unbinds = [];

  if (element.hasAttribute('bind')) {
    unbinds.push(bindElement(element, scope));
  }

  forEach(element.attributes, (attribute) => {
    let {name, value} = attribute;
    if (name.substr(0, 6) === 'scomp-') {
      unbinds.push(bindAttribute(element, name, value, scope));
    }
  });

  return unbinds;
}

function inChildScope(childScopes, element) {
  let isInChildScope = false;
  forEach(childScopes, (childScope) => {
    if (childScope.contains(element)) {
      isInChildScope = true;
      return false;
    }
  });
  return isInChildScope;
}

export default function (root, scope) {
  let unbinds = bindFactory(root, scope);
  let elements = root.querySelectorAll('*');
  forEach(elements, (element) => {
    let childScopes = root.querySelectorAll('[data-scope]');
    if (!inChildScope(childScopes, element)) {
      unbinds = unbinds.concat(bindFactory(element, scope));
    }
  });
  return () => unbinds.forEach((unbind) => unbind());
}
<commit_msg>Removing reserved word.<commit_after>import {forEach} from '../';

const BIND = 'bind';

function compileExpression(expression, scope) {
  expression = expression.replace(/([^\.a-z\$_])([a-z\$_]+)/ig, (match, character, propName) => {
    return `${character}scope.${propName}`;
  });

  let value;

  try {
    value = eval(`scope.${expression}`);
    if (typeof value === 'undefined') value = '';
  } catch (e) {
    value = '';
  }

  return value;
}

function compileElementBinding(element, scope) {
  let expression = element.getAttribute(BIND);
  element.innerHTML = compileExpression(expression, scope);
}

function bindElement(element, scope) {
  let listener = () => compileElementBinding(element, scope);
  scope.on('update', listener);
  return () => scope.removeListener('update', listener);
}

function compileAttributeBinding(element, attributeName, attributeValue, scope) {
  let attributeToReplace = attributeName.substr(6);
  element.setAttribute(attributeToReplace, compileExpression(attributeValue, scope));
}

function bindAttribute(element, attributeName, attributeValue, scope) {
  let listener = () => compileAttributeBinding(element, attributeName, attributeValue, scope);
  scope.on('update', listener);
  return () => scope.removeListener('update', listener);
}

function bindFactory(element, scope) {
  let unbinds = [];

  if (element.hasAttribute('bind')) {
    unbinds.push(bindElement(element, scope));
  }

  forEach(element.attributes, (attribute) => {
    let {name, value} = attribute;
    if (name.substr(0, 6) === 'scomp-') {
      unbinds.push(bindAttribute(element, name, value, scope));
    }
  });

  return unbinds;
}

function inChildScope(childScopes, element) {
  let isInChildScope = false;
  forEach(childScopes, (childScope) => {
    if (childScope.contains(element)) {
      isInChildScope = true;
      return false;
    }
  });
  return isInChildScope;
}

export default function (root, scope) {
  let unbinds = bindFactory(root, scope);
  let elements = root.querySelectorAll('*');
  forEach(elements, (element) => {
    let childScopes = root.querySelectorAll('[data-scope]');
    if (!inChildScope(childScopes, element)) {
      unbinds = unbinds.concat(bindFactory(element, scope));
    }
  });
  return () => unbinds.forEach((unbind) => unbind());
}

{'fraction_non_alphanumeric': 0.15788327526132404, 'fraction_numerical': 0.0013066202090592336, 'mean_word_length': 4.574029126213592, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 23, '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': '7193222', 'n_tokens_mistral': 1394, 'n_tokens_neox': 1391, 'n_words': 294}