<commit_before>import isEnabled from 'ember-metal/features';

function TransformTopLevelComponents() {
  // set later within HTMLBars to the syntax package
  this.syntax = null;
}

/**
  @private
  @method transform
  @param {AST} The AST to be transformed.
*/
TransformTopLevelComponents.prototype.transform = function TransformTopLevelComponents_transform(ast) {
  let b = this.syntax.builders;

  hasSingleComponentNode(ast.body, component => {
    if (component.type === 'ComponentNode') {
      component.tag = `@${component.tag}`;
      component.isStatic = true;
    }
  }, element => {
    let hasTripleCurlies = element.attributes.some(attr => attr.value.escaped === false);

    if (element.modifiers.length || hasTripleCurlies) {
      return element;
    } else {
      // TODO: Properly copy loc from children
      let program = b.program(element.children);
      let component = b.component(`@<${element.tag}>`, element.attributes, program, element.loc);
      component.isStatic = true;
      return component;
    }
  });

  return ast;
};

function hasSingleComponentNode(body, componentCallback, elementCallback) {
  let lastComponentNode;
  let lastIndex;
  let nodeCount = 0;

  for (let i = 0, l = body.length; i < l; i++) {
    let curr = body[i];<commit_msg>Optimize the AST transform for top-level elements<commit_after>import isEnabled from 'ember-metal/features';

function TransformTopLevelComponents() {
  // set later within HTMLBars to the syntax package
  this.syntax = null;
}

/**
  @private
  @method transform
  @param {AST} The AST to be transformed.
*/
TransformTopLevelComponents.prototype.transform = function TransformTopLevelComponents_transform(ast) {
  let b = this.syntax.builders;

  hasSingleComponentNode(ast, component => {
    if (component.type === 'ComponentNode') {
      component.tag = `@${component.tag}`;
      component.isStatic = true;
    }
  }, element => {
    let hasTripleCurlies = element.attributes.some(attr => attr.value.escaped === false);

    if (element.modifiers.length || hasTripleCurlies) {
      return element;
    } else {
      // TODO: Properly copy loc from children
      let program = b.program(element.children);
      let component = b.component(`@<${element.tag}>`, element.attributes, program, element.loc);
      component.isStatic = true;
      return component;
    }
  });

  return ast;
};

function hasSingleComponentNode(program, componentCallback, elementCallback) {
  let { loc, body } = program;
  if (!loc || loc.start.line !== 1 || loc.start.column !== 0) { return; }

  let lastComponentNode;
  let lastIndex;
  let nodeCount = 0;

  for (let i = 0, l = body.length; i < l; i++) {
    let curr = body[i];
{'fraction_non_alphanumeric': 0.1268491124260355, 'fraction_numerical': 0.0022189349112426036, 'mean_word_length': 3.5385906040268456, 'pattern_counts': {'":': 0, '<': 7, '<?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': '2656628', 'n_tokens_mistral': 824, 'n_tokens_neox': 796, 'n_words': 210}