Skip to content

Commit

Permalink
Fix backwards compat shim for custom AST plugins.
Browse files Browse the repository at this point in the history
  • Loading branch information
rwjblue committed Jan 25, 2018
1 parent e6cbc39 commit eeecb9b
Show file tree
Hide file tree
Showing 3 changed files with 87 additions and 16 deletions.
3 changes: 2 additions & 1 deletion packages/ember-template-compiler/lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ export { default as precompile } from './system/precompile';
export { default as compile } from './system/compile';
export {
default as compileOptions,
registerPlugin
registerPlugin,
unregisterPlugin
} from './system/compile-options';
export { default as defaultPlugins } from './plugins';

Expand Down
29 changes: 17 additions & 12 deletions packages/ember-template-compiler/lib/system/compile-options.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,27 +16,24 @@ export default function compileOptions(_options) {
options.plugins = { ast: [...USER_PLUGINS, ...PLUGINS] };
} else {
let potententialPugins = [...USER_PLUGINS, ...PLUGINS];
let providedPlugins = options.plugins.ast.map(plugin => wrapLegacyPluginIfNeeded(plugin));
let pluginsToAdd = potententialPugins.filter((plugin) => {
return options.plugins.ast.indexOf(plugin) === -1;
});
options.plugins.ast = options.plugins.ast.slice().concat(pluginsToAdd);
options.plugins.ast = providedPlugins.concat(pluginsToAdd);
}

return options;
}

export function registerPlugin(type, _plugin) {
if (type !== 'ast') {
throw new Error(`Attempting to register ${_plugin} as "${type}" which is not a valid Glimmer plugin type.`);
}

let plugin;
function wrapLegacyPluginIfNeeded(_plugin) {
let plugin = _plugin;
if (_plugin.prototype && _plugin.prototype.transform) {
plugin = (env) => {
return {
name: _plugin.constructor && _plugin.constructor.name,

visitors: {
visitor: {
Program(node) {
let plugin = new _plugin(env);

Expand All @@ -45,16 +42,24 @@ export function registerPlugin(type, _plugin) {
return plugin.transform(node);
}
}
};
};
};
} else {
plugin = _plugin;
}

return plugin;
}

export function registerPlugin(type, _plugin) {
if (type !== 'ast') {
throw new Error(`Attempting to register ${_plugin} as "${type}" which is not a valid Glimmer plugin type.`);
}

let plugin = wrapLegacyPluginIfNeeded(_plugin);

USER_PLUGINS = [plugin, ...USER_PLUGINS];
}

export function removePlugin(type, PluginClass) {
export function unregisterPlugin(type, PluginClass) {
if (type !== 'ast') {
throw new Error(`Attempting to unregister ${PluginClass} as "${type}" which is not a valid Glimmer plugin type.`);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { compileOptions } from '../../index';
import { defaultPlugins } from '../../index';
import { moduleFor, AbstractTestCase } from 'internal-test-helpers';
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) {
Expand All @@ -18,3 +17,69 @@ moduleFor('ember-template-compiler: default compile options', class extends Abst
}
}
});

class CustomTransform {
constructor(options) {
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;
}
}

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

afterEach() {
unregisterPlugin('ast', CustomTransform);
}

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

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]
}
});
}

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

0 comments on commit eeecb9b

Please sign in to comment.