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

[BUGFIX lts] Avoid excessively calling Glimmer AST transforms. #16241

Merged
merged 1 commit into from
Feb 13, 2018
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
23 changes: 19 additions & 4 deletions packages/ember-template-compiler/lib/system/compile-options.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,20 +30,28 @@ function wrapLegacyPluginIfNeeded(_plugin) {
let plugin = _plugin;
if (_plugin.prototype && _plugin.prototype.transform) {
plugin = (env) => {
let pluginInstantiated = false;

return {
name: _plugin.constructor && _plugin.constructor.name,

visitor: {
Program(node) {
let plugin = new _plugin(env);
if (!pluginInstantiated) {

pluginInstantiated = true;
let plugin = new _plugin(env);

plugin.syntax = env.syntax;
plugin.syntax = env.syntax;

return plugin.transform(node);
return plugin.transform(node);
}
}
}
};
};

plugin.__raw = _plugin;
}

return plugin;
Expand All @@ -54,6 +62,13 @@ export function registerPlugin(type, _plugin) {
throw new Error(`Attempting to register ${_plugin} as "${type}" which is not a valid Glimmer plugin type.`);
}

for (let i = 0; i < USER_PLUGINS.length; i++) {
let PLUGIN = USER_PLUGINS[i];
if (PLUGIN === _plugin || PLUGIN.__raw === _plugin) {
return;
}
}

let plugin = wrapLegacyPluginIfNeeded(_plugin);

USER_PLUGINS = [plugin, ...USER_PLUGINS];
Expand All @@ -64,5 +79,5 @@ export function unregisterPlugin(type, PluginClass) {
throw new Error(`Attempting to unregister ${PluginClass} as "${type}" which is not a valid Glimmer plugin type.`);
}

USER_PLUGINS = USER_PLUGINS.filter((plugin) => plugin !== PluginClass);
USER_PLUGINS = USER_PLUGINS.filter((plugin) => plugin !== PluginClass && plugin.__raw !== PluginClass);
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,10 @@ moduleFor('ember-template-compiler: default compile options', class extends Abst
}
});

let customTransformCounter = 0;
class CustomTransform {
constructor(options) {
customTransformCounter++;
this.options = options;
this.syntax = null;
}
Expand All @@ -45,13 +47,10 @@ class CustomTransform {
}
}

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

class CustomPluginsTests extends RenderingTestCase {
afterEach() {
unregisterPlugin('ast', CustomTransform);
customTransformCounter = 0;
return super.afterEach();
}

['@test custom plugins can be used']() {
Expand All @@ -62,6 +61,28 @@ moduleFor('ember-template-compiler: registerPlugin with a custom plugins', class
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 {
Expand All @@ -73,13 +94,4 @@ moduleFor('ember-template-compiler: custom plugins passed to compile', class ext
}
});
}

['@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: ''
});
}
});