<commit_before>import {
  compile,
  compileOptions,
  defaultPlugins,
  registerPlugin,
  unregisterPlugin,
} from '../../index';
import { moduleFor, AbstractTestCase, RenderingTestCase } from 'internal-test-helpers';

moduleFor(
  'ember-template-compiler: default compile options',
  class extends AbstractTestCase {
    ['@test default options are a new copy'](assert) {
      assert.notEqual(compileOptions(), compileOptions());
    }

    ['@test has default AST plugins'](assert) {
      assert.expect(defaultPlugins.length);

      let plugins = compileOptions().plugins.ast;

      for (let i = 0; i < defaultPlugins.length; i++) {
        let plugin = defaultPlugins[i];
        assert.ok(plugins.indexOf(plugin) > -1, `includes ${plugin}`);
      }
    }
  }
);

let customTransformCounter = 0;
class CustomTransform {
  constructor(options) {
    customTransformCounter++;
    this.options = options;
    this.syntax = null;
  }

  transform(ast) {
    let walker = new this.syntax.Walker();

    walker.visit(ast, node => {
      if (node.type !== 'ElementNode') {
        return;
      }

      for (var i = 0; i < node.attributes.length; i++) {
        let attribute = node.attributes[i];

        if (attribute.name === 'data-test') {
          node.attributes.splice(i, 1);
        }
      }
    });

    return ast;
  }
}

class CustomPluginsTests extends RenderingTestCase {
  afterEach() {
    customTransformCounter = 0;
    return super.afterEach();
  }

  ['@test custom plugins can be used']() {
    this.render('<div data-test="foo" data-blah="derp" class="hahaha"></div>');
    this.assertElement(this.firstChild, {
      tagName: 'div',
      attrs: { class: 'hahaha', 'data-blah': 'derp' },
      content: '',
    });
  }

  ['@test wrapped plugins are only invoked once per template'](assert) {
    this.render('<div>{{#if falsey}}nope{{/if}}</div>');
    assert.equal(customTransformCounter, 1, 'transform should only be instantiated once');
  }
}

moduleFor(
  'ember-template-compiler: registerPlugin with a custom plugins',
  class extends CustomPluginsTests {
    beforeEach() {
      registerPlugin('ast', CustomTransform);
    }

    afterEach() {
      unregisterPlugin('ast', CustomTransform);
      return super.afterEach();
    }

    ['@test custom registered plugins are deduplicated'](assert) {
      registerPlugin('ast', CustomTransform);
      this.registerTemplate(
        'application',
        '<div data-test="foo" data-blah="derp" class="hahaha"></div>'
      );
      assert.equal(customTransformCounter, 1, 'transform should only be instantiated once');
    }
  }
);

moduleFor(
  'ember-template-compiler: custom plugins passed to compile',
  class extends RenderingTestCase {
    // override so that we can provide custom AST plugins to compile
    compile(templateString) {
      return compile(templateString, {
        plugins: {
          ast: [CustomTransform],
        },
      });
    }
  }
);<commit_msg>Add tests for current AST plugin format.<commit_after>import {
  compile,
  compileOptions,
  defaultPlugins,
  registerPlugin,
  unregisterPlugin,
} from '../../index';
import { moduleFor, AbstractTestCase, RenderingTestCase } from 'internal-test-helpers';

moduleFor(
  'ember-template-compiler: default compile options',
  class extends AbstractTestCase {
    ['@test default options are a new copy'](assert) {
      assert.notEqual(compileOptions(), compileOptions());
    }

    ['@test has default AST plugins'](assert) {
      assert.expect(defaultPlugins.length);

      let plugins = compileOptions().plugins.ast;

      for (let i = 0; i < defaultPlugins.length; i++) {
        let plugin = defaultPlugins[i];
        assert.ok(plugins.indexOf(plugin) > -1, `includes ${plugin}`);
      }
    }
  }
);

let customTransformCounter = 0;
class LegacyCustomTransform {
  constructor(options) {
    customTransformCounter++;
    this.options = options;
    this.syntax = null;
  }

  transform(ast) {
    let walker = new this.syntax.Walker();

    walker.visit(ast, node => {
      if (node.type !== 'ElementNode') {
        return;
      }

      for (var i = 0; i < node.attributes.length; i++) {
        let attribute = node.attributes[i];

        if (attribute.name === 'data-test') {
          node.attributes.splice(i, 1);
        }
      }
    });

    return ast;
  }
}

function customTransform() {
  customTransformCounter++;

  return {
    name: 'remove-data-test',

    visitor: {
      ElementNode(node) {
        for (var i = 0; i < node.attributes.length; i++) {
          let attribute = node.attributes[i];

          if (attribute.name === 'data-test') {
            node.attributes.splice(i, 1);
          }
        }
      },
    },
  };
}

class CustomPluginsTests extends RenderingTestCase {
  afterEach() {
    customTransformCounter = 0;
    return super.afterEach();
  }

  ['@test custom plugins can be used']() {
    this.render('<div data-test="foo" data-blah="derp" class="hahaha"></div>');
    this.assertElement(this.firstChild, {
      tagName: 'div',
      attrs: { class: 'hahaha', 'data-blah': 'derp' },
      content: '',
    });
  }

  ['@test wrapped plugins are only invoked once per template'](assert) {
    this.render('<div>{{#if falsey}}nope{{/if}}</div>');
    assert.equal(customTransformCounter, 1, 'transform should only be instantiated once');
  }
}

moduleFor(
  'ember-template-compiler: registerPlugin with a custom plugins in legacy format',
  class extends CustomPluginsTests {
    beforeEach() {
      registerPlugin('ast', LegacyCustomTransform);
    }

    afterEach() {
      unregisterPlugin('ast', LegacyCustomTransform);
      return super.afterEach();
    }

    ['@test custom registered plugins are deduplicated'](assert) {
      registerPlugin('ast', LegacyCustomTransform);
      this.registerTemplate(
        'application',
        '<div data-test="foo" data-blah="derp" class="hahaha"></div>'
      );
      assert.equal(customTransformCounter, 1, 'transform should only be instantiated once');
    }
  }
);

moduleFor(
  'ember-template-compiler: registerPlugin with a custom plugins',
  class extends CustomPluginsTests {
    beforeEach() {
      registerPlugin('ast', customTransform);
    }

    afterEach() {
      unregisterPlugin('ast', customTransform);
      return super.afterEach();
    }

    ['@test custom registered plugins are deduplicated'](assert) {
      registerPlugin('ast', customTransform);
      this.registerTemplate(
        'application',
        '<div data-test="foo" data-blah="derp" class="hahaha"></div>'
      );
      assert.equal(customTransformCounter, 1, 'transform should only be instantiated once');
    }
  }
);

moduleFor(
  'ember-template-compiler: custom plugins in legacy format passed to compile',
  class extends RenderingTestCase {
    // override so that we can provide custom AST plugins to compile
    compile(templateString) {
      return compile(templateString, {
        plugins: {
          ast: [LegacyCustomTransform],
        },
      });
    }
  }
);

moduleFor(
  'ember-template-compiler: custom plugins passed to compile',
  class extends RenderingTestCase {
    // override so that we can provide custom AST plugins to compile
    compile(templateString) {
      return compile(templateString, {
        plugins: {
          ast: [customTransform],
        },
      });
    }
  }
);
{'fraction_non_alphanumeric': 0.13560698335363378, 'fraction_numerical': 0.002571389903911219, 'mean_word_length': 2.941333333333333, 'pattern_counts': {'":': 0, '<': 22, '<?xml version=': 0, '>': 21, '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': '139838', 'n_tokens_mistral': 2189, 'n_tokens_neox': 2117, 'n_words': 516}