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

Ensure AST transforms of this addon runs before the AST transform of the in-element-polyfill #30

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
15 changes: 1 addition & 14 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@ module.exports = {
name: require('./package').name,

setupPreprocessorRegistry(type, registry) {
let plugins = registry.load('htmlbars-ast-plugin');
let inElementPlugin = plugins.find((plugin) => plugin.name === 'ember-in-element-polyfill');
let maybePlugin = {
name: 'ember-maybe-in-element-transform',
plugin: EmberMaybeInElementAstTransform,
Expand All @@ -16,17 +14,6 @@ module.exports = {
}
};

// Yes, this a bit ugly, but it seems for some reason AST plugins are applied in a different order (reversed?) than
// the order they are added to the registry.
// With this dirty hack we make sure there is always a "polyfill" transform running after the "maybe" transform, to make
// sure the AST returned from "maybe" containing `{{in-element}}` gets further transformed by the polyfill!
if (inElementPlugin) {
registry.remove('htmlbars-ast-plugin', inElementPlugin);
registry.add('htmlbars-ast-plugin', inElementPlugin);
registry.add('htmlbars-ast-plugin', maybePlugin);
registry.add('htmlbars-ast-plugin', inElementPlugin);
} else {
registry.add('htmlbars-ast-plugin', maybePlugin);
}
registry.add('htmlbars-ast-plugin', maybePlugin);
},
};
48 changes: 24 additions & 24 deletions lib/ast-transform.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
/* eslint-env node */
'use strict';

function deepClone(src) {
return JSON.parse(JSON.stringify(src));
}

/**
* From:
* ```hbs
Expand All @@ -28,30 +24,34 @@ module.exports = class EmberMaybeInElementAstTransform {
}

transform(ast) {
let walker = new this.syntax.Walker();

let b = this.syntax.builders;

walker.visit(ast, function(node) {
if (node.type === 'BlockStatement' && node.path.original === 'maybe-in-element') {
if (node.params.length < 2) {
throw new Error('{{#maybe-in-element}} requires two arguments. The first is the destination element and the second is the `renderInPlace` boolean flag');
this.syntax.traverse(ast, {
BlockStatement(node) {
if (node.path.original === "maybe-in-element") {
if (node.params.length < 2) {
throw new Error(
"{{#maybe-in-element}} requires two arguments. The first is the destination element and the second is the `renderInPlace` boolean flag"
);
}
let destinationElement = node.params[0];
let renderInPlace = node.params[1];
return b.block(
b.path("if"),
[renderInPlace],
undefined,
node.program,
b.program([
b.block(
b.path("in-element"),
[destinationElement],
node.hash,
node.program
),
])
);
}
let destinationElement = node.params[0];
let renderInPlace = node.params[1];
node.path = b.path('if');
node.params = [renderInPlace];
node.inverse = b.program([
b.block(
b.path('in-element'),
[destinationElement],
node.hash,
deepClone(node.program)
)
]);
}
});

return ast;
}
};
Loading