Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add more tests using AST plugins (inline and standalone) #125

Merged
merged 1 commit into from
Oct 4, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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');
});
}
});