Skip to content

Commit 13dbd34

Browse files
author
Robert Jackson
committed
Add more basic AST plugin tests.
Tests for both inline precompilation and precompiled standalone templates in both AST plugin formats (function and class).
1 parent e3f2311 commit 13dbd34

5 files changed

Lines changed: 93 additions & 0 deletions

File tree

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{{module-name-inliner}}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{{module-name-reverser}}

tests/dummy/lib/module-name-inliner/index.js

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ module.exports = {
1515
if (checker.forEmber().gte('3.1.0')) {
1616
registry.add('htmlbars-ast-plugin', this.buildPlugin());
1717
}
18+
19+
registry.add('htmlbars-ast-plugin', this.buildLegacyPlugin());
1820
},
1921

2022
buildPlugin() {
@@ -38,6 +40,8 @@ module.exports = {
3840
visitor: {
3941
PathExpression(node) {
4042
if (node.original === 'module-name-inliner') {
43+
// replacing the path with a string literal, like this
44+
// {{"the-module-name-here"}}
4145
return builders.string(env.moduleName);
4246
}
4347
},
@@ -46,4 +50,47 @@ module.exports = {
4650
},
4751
};
4852
},
53+
54+
// this type of plugin has worked since at least Ember 2.4+
55+
buildLegacyPlugin() {
56+
return {
57+
name: 'module-name-inliner',
58+
baseDir() {
59+
return __dirname;
60+
},
61+
parallelBabel: {
62+
requireFile: __filename,
63+
buildUsing: 'buildPlugin',
64+
params: {},
65+
},
66+
67+
plugin: class LegacyPlugin {
68+
constructor(options) {
69+
this.options = options;
70+
}
71+
72+
transform(ast) {
73+
let { meta } = this.options;
74+
let b = this.syntax.builders;
75+
76+
this.syntax.traverse(ast, {
77+
// replacing the mustache with text, like this
78+
// {{module-name-reverser}} -> `some-module-name`
79+
MustacheStatement(node) {
80+
if (node.path.original === 'module-name-reverser') {
81+
return b.text(
82+
meta.moduleName
83+
.split('')
84+
.reverse()
85+
.join('')
86+
);
87+
}
88+
},
89+
});
90+
91+
return ast;
92+
}
93+
},
94+
};
95+
},
4996
};
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import { module, test } from 'qunit';
2+
import { setupRenderingTest } from 'ember-qunit';
3+
import { render } from '@ember/test-helpers';
4+
import { hbs } from 'ember-cli-htmlbars';
5+
import hasEmberVersion from '@ember/test-helpers/has-ember-version';
6+
7+
module('tests/integration/components/ast-plugins-test', function(hooks) {
8+
setupRenderingTest(hooks);
9+
10+
test('stand alone templates have "legacy" AST plugins ran', async function(assert) {
11+
await render(hbs`{{x-module-name-reversed-component}}`);
12+
13+
assert.equal(
14+
this.element.textContent.trim(),
15+
'sbh.tnenopmoc-desrever-eman-eludom-x/stnenopmoc/setalpmet/ymmud'
16+
);
17+
});
18+
19+
if (hasEmberVersion(3, 1)) {
20+
test('stand alone templates have AST plugins ran', async function(assert) {
21+
await render(hbs`{{x-module-name-inlined-component}}`);
22+
23+
assert.equal(
24+
this.element.textContent.trim(),
25+
'dummy/templates/components/x-module-name-inlined-component.hbs'
26+
);
27+
});
28+
}
29+
});

tests/integration/components/test-inline-precompile-test.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { render } from '@ember/test-helpers';
44
import hbsOne from 'htmlbars-inline-precompile';
55
import hbsTwo from 'ember-cli-htmlbars-inline-precompile';
66
import { hbs as hbsThree } from 'ember-cli-htmlbars';
7+
import hasEmberVersion from '@ember/test-helpers/has-ember-version';
78

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

2627
assert.equal(this.element.textContent.trim(), 'Wheeeee');
2728
});
29+
30+
test('inline templates have "legacy" AST plugins ran', async function(assert) {
31+
await render(hbsThree('{{module-name-reverser}}', { moduleName: 'hello-template.hbs' }));
32+
33+
assert.equal(this.element.textContent.trim(), 'sbh.etalpmet-olleh');
34+
});
35+
36+
if (hasEmberVersion(3, 1)) {
37+
test('inline templates have AST plugins ran', async function(assert) {
38+
await render(hbsThree('{{module-name-inliner}}', { moduleName: 'hello-template.hbs' }));
39+
40+
assert.equal(this.element.textContent.trim(), 'hello-template.hbs');
41+
});
42+
}
2843
});

0 commit comments

Comments
 (0)