Skip to content

Commit

Permalink
Add support for using $ as a separator in addition to ::.
Browse files Browse the repository at this point in the history
Since Ember has decided to repurpose the `::` sigil to mean "nested
folder invocation" (see emberjs/rfcs#457), this introduces `$` as a
replacement sigil. Ultimately, usage of `::` will be deprecated by this
addon (though that will be in a future commit).
  • Loading branch information
rwjblue committed Aug 15, 2019
1 parent d4efbc8 commit 4fba465
Show file tree
Hide file tree
Showing 5 changed files with 252 additions and 83 deletions.
2 changes: 1 addition & 1 deletion addon/helpers/-translate-dynamic.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { helper } from '@ember/component/helper';

export default helper(function(args) {
if (args && typeof args[0] === 'string') {
return args[0].replace('::', '@');
return args[0].replace('::', '@').replace('$', '@');
}
return args && args[0];
});
31 changes: 25 additions & 6 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,20 +14,39 @@ module.exports = {
},

setupPreprocessorRegistry(type, registry) {
let pluginObj = this._buildPlugin();
pluginObj.parallelBabel = {
let dollarPluginObj = this._buildDollarPlugin();
dollarPluginObj.parallelBabel = {
requireFile: __filename,
buildUsing: '_buildPlugin',
buildUsing: '_buildDollarPlugin',
params: {}
}

registry.add("htmlbars-ast-plugin", pluginObj);
registry.add("htmlbars-ast-plugin", dollarPluginObj);

let colonPluginObj = this._buildColonPlugin();
colonPluginObj.parallelBabel = {
requireFile: __filename,
buildUsing: '_buildColonPlugin',
params: {}
}

registry.add("htmlbars-ast-plugin", colonPluginObj);
},

_buildDollarPlugin() {
return {
name: 'holy-futuristic-template-namespacing-batman',
plugin: require("./lib/namespacing-transform").DollarNamespacingTransform,
baseDir: function() {
return __dirname;
}
};
},

_buildPlugin() {
_buildColonPlugin() {
return {
name: 'holy-futuristic-template-namespacing-batman',
plugin: require("./lib/namespacing-transform"),
plugin: require("./lib/namespacing-transform").ColonNamespacingTransform,
baseDir: function() {
return __dirname;
}
Expand Down
43 changes: 33 additions & 10 deletions lib/namespacing-transform.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@

const TRANSLATE_HELPER = 'ember-holy-futuristic-template-namespacing-batman@-translate-dynamic';

function rewriteOrWrapComponentParam(node, b) {
function rewriteOrWrapComponentParam(node, b, sigil) {
if (!node.params.length) {
return;
}
let firstParam = node.params[0];
if (firstParam.type === 'StringLiteral') {
// handle string constant
node.params[0] = b.string(firstParam.original.replace('::', '@'));
node.params[0] = b.string(firstParam.original.replace(sigil, '@'));
return;
}
if (firstParam.type === 'PathExpression' || firstParam.type === 'SubExpression') {
Expand All @@ -18,18 +18,20 @@ function rewriteOrWrapComponentParam(node, b) {
}
}

module.exports = class NamespacingTransform {
class NamespacingTransform {
constructor(options) {
this.syntax = null;
this.options = options;
this.sigil = undefined;
}

transform(ast) {
const b = this.syntax.builders;
let sigil = this.sigil;

this.syntax.traverse(ast, {
PathExpression(node) {
if (node.parts.length > 1 || !node.original.includes('::')) {
if (node.parts.length > 1 || !node.original.includes(sigil)) {
return;
}

Expand All @@ -38,36 +40,57 @@ module.exports = class NamespacingTransform {
if (loc === null) {
loc = undefined;
}
return b.path(node.original.replace('::', '@'), loc);
return b.path(node.original.replace(sigil, '@'), loc);
},
ElementNode(node) {
if (node.tag.indexOf('::') > -1) {
node.tag = node.tag.replace('::', '@');
if (node.tag.indexOf(sigil) > -1) {
node.tag = node.tag.replace(sigil, '@');
}
},
MustacheStatement(node) {
if (node.path.original !== 'component') {
// we don't care about non-component expressions
return;
}
rewriteOrWrapComponentParam(node, b);
rewriteOrWrapComponentParam(node, b, sigil);
},
SubExpression(node) {
if (node.path.original !== 'component') {
// we don't care about non-component expressions
return;
}
rewriteOrWrapComponentParam(node, b);
rewriteOrWrapComponentParam(node, b, sigil);
},
BlockStatement(node) {
if (node.path.original !== 'component') {
// we don't care about blocks not using component
return;
}
rewriteOrWrapComponentParam(node, b);
rewriteOrWrapComponentParam(node, b, sigil);
}
});

return ast;
}
}

class DollarNamespacingTransform extends NamespacingTransform {
constructor(options) {
super(options);

this.sigil = '$';
}
}

class ColonNamespacingTransform extends NamespacingTransform {
constructor(options) {
super(options);

this.sigil = '::';
}
}

module.exports = {
DollarNamespacingTransform,
ColonNamespacingTransform,
};
Loading

0 comments on commit 4fba465

Please sign in to comment.