Skip to content

Commit

Permalink
Add more tests using AST plugins (inline and standalone) (#125)
Browse files Browse the repository at this point in the history
Add more tests using AST plugins (inline and standalone)
  • Loading branch information
rwjblue authored Oct 4, 2019
2 parents e3f2311 + 13dbd34 commit fa66e31
Show file tree
Hide file tree
Showing 5 changed files with 93 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{{module-name-inliner}}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{{module-name-reverser}}
47 changes: 47 additions & 0 deletions tests/dummy/lib/module-name-inliner/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ module.exports = {
if (checker.forEmber().gte('3.1.0')) {
registry.add('htmlbars-ast-plugin', this.buildPlugin());
}

registry.add('htmlbars-ast-plugin', this.buildLegacyPlugin());
},

buildPlugin() {
Expand All @@ -38,6 +40,8 @@ module.exports = {
visitor: {
PathExpression(node) {
if (node.original === 'module-name-inliner') {
// replacing the path with a string literal, like this
// {{"the-module-name-here"}}
return builders.string(env.moduleName);
}
},
Expand All @@ -46,4 +50,47 @@ module.exports = {
},
};
},

// this type of plugin has worked since at least Ember 2.4+
buildLegacyPlugin() {
return {
name: 'module-name-inliner',
baseDir() {
return __dirname;
},
parallelBabel: {
requireFile: __filename,
buildUsing: 'buildPlugin',
params: {},
},

plugin: class LegacyPlugin {
constructor(options) {
this.options = options;
}

transform(ast) {
let { meta } = this.options;
let b = this.syntax.builders;

this.syntax.traverse(ast, {
// replacing the mustache with text, like this
// {{module-name-reverser}} -> `some-module-name`
MustacheStatement(node) {
if (node.path.original === 'module-name-reverser') {
return b.text(
meta.moduleName
.split('')
.reverse()
.join('')
);
}
},
});

return ast;
}
},
};
},
};
29 changes: 29 additions & 0 deletions tests/integration/components/ast-plugins-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { module, test } from 'qunit';
import { setupRenderingTest } from 'ember-qunit';
import { render } from '@ember/test-helpers';
import { hbs } from 'ember-cli-htmlbars';
import hasEmberVersion from '@ember/test-helpers/has-ember-version';

module('tests/integration/components/ast-plugins-test', function(hooks) {
setupRenderingTest(hooks);

test('stand alone templates have "legacy" AST plugins ran', async function(assert) {
await render(hbs`{{x-module-name-reversed-component}}`);

assert.equal(
this.element.textContent.trim(),
'sbh.tnenopmoc-desrever-eman-eludom-x/stnenopmoc/setalpmet/ymmud'
);
});

if (hasEmberVersion(3, 1)) {
test('stand alone templates have AST plugins ran', async function(assert) {
await render(hbs`{{x-module-name-inlined-component}}`);

assert.equal(
this.element.textContent.trim(),
'dummy/templates/components/x-module-name-inlined-component.hbs'
);
});
}
});
15 changes: 15 additions & 0 deletions tests/integration/components/test-inline-precompile-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { render } from '@ember/test-helpers';
import hbsOne from 'htmlbars-inline-precompile';
import hbsTwo from 'ember-cli-htmlbars-inline-precompile';
import { hbs as hbsThree } from 'ember-cli-htmlbars';
import hasEmberVersion from '@ember/test-helpers/has-ember-version';

module('tests/integration/components/test-inline-precompile', function(hooks) {
setupRenderingTest(hooks);
Expand All @@ -25,4 +26,18 @@ module('tests/integration/components/test-inline-precompile', function(hooks) {

assert.equal(this.element.textContent.trim(), 'Wheeeee');
});

test('inline templates have "legacy" AST plugins ran', async function(assert) {
await render(hbsThree('{{module-name-reverser}}', { moduleName: 'hello-template.hbs' }));

assert.equal(this.element.textContent.trim(), 'sbh.etalpmet-olleh');
});

if (hasEmberVersion(3, 1)) {
test('inline templates have AST plugins ran', async function(assert) {
await render(hbsThree('{{module-name-inliner}}', { moduleName: 'hello-template.hbs' }));

assert.equal(this.element.textContent.trim(), 'hello-template.hbs');
});
}
});

0 comments on commit fa66e31

Please sign in to comment.