2017-06-29 16:22:19 -04:00
|
|
|
function resolve(path) {
|
2018-09-21 07:02:23 -04:00
|
|
|
if (path.indexOf("settings") === 0 || path.indexOf("transformed") === 0) {
|
2017-09-28 15:19:31 -04:00
|
|
|
return `this.${path}`;
|
|
|
|
}
|
|
|
|
return path;
|
2017-06-29 16:22:19 -04:00
|
|
|
}
|
|
|
|
|
2017-09-28 16:08:14 -04:00
|
|
|
function sexp(value) {
|
|
|
|
if (value.path.original === "hash") {
|
|
|
|
let result = [];
|
|
|
|
|
|
|
|
value.hash.pairs.forEach(p => {
|
2017-10-04 12:48:14 -04:00
|
|
|
let pValue = p.value.original;
|
|
|
|
if (p.value.type === "StringLiteral") {
|
|
|
|
pValue = JSON.stringify(pValue);
|
|
|
|
}
|
|
|
|
|
|
|
|
result.push(`"${p.key}": ${pValue}`);
|
2017-09-28 16:08:14 -04:00
|
|
|
});
|
|
|
|
|
|
|
|
return `{ ${result.join(", ")} }`;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function argValue(arg) {
|
|
|
|
let value = arg.value;
|
|
|
|
if (value.type === "SubExpression") {
|
|
|
|
return sexp(arg.value);
|
|
|
|
} else if (value.type === "PathExpression") {
|
|
|
|
return value.original;
|
2017-10-05 16:13:00 -04:00
|
|
|
} else if (value.type === "StringLiteral") {
|
|
|
|
return JSON.stringify(value.value);
|
2017-09-28 16:08:14 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-09-01 11:20:14 -04:00
|
|
|
function mustacheValue(node, state) {
|
2017-06-29 16:22:19 -04:00
|
|
|
let path = node.path.original;
|
|
|
|
|
2018-09-21 07:02:23 -04:00
|
|
|
switch (path) {
|
|
|
|
case "attach":
|
2017-10-05 16:13:00 -04:00
|
|
|
let widgetName = argValue(node.hash.pairs.find(p => p.key === "widget"));
|
2017-09-28 16:08:14 -04:00
|
|
|
|
|
|
|
let attrs = node.hash.pairs.find(p => p.key === "attrs");
|
|
|
|
if (attrs) {
|
2017-10-05 16:13:00 -04:00
|
|
|
return `this.attach(${widgetName}, ${argValue(attrs)})`;
|
2017-09-28 16:08:14 -04:00
|
|
|
}
|
2017-10-05 16:13:00 -04:00
|
|
|
return `this.attach(${widgetName}, attrs)`;
|
2017-09-28 16:08:14 -04:00
|
|
|
|
2017-06-29 16:22:19 -04:00
|
|
|
break;
|
2018-09-21 07:02:23 -04:00
|
|
|
case "yield":
|
2017-06-29 16:22:19 -04:00
|
|
|
return `this.attrs.contents()`;
|
|
|
|
break;
|
2018-09-21 07:02:23 -04:00
|
|
|
case "i18n":
|
2017-06-29 16:22:19 -04:00
|
|
|
let value;
|
|
|
|
if (node.params[0].type === "StringLiteral") {
|
|
|
|
value = `"${node.params[0].value}"`;
|
|
|
|
} else if (node.params[0].type === "PathExpression") {
|
2017-10-05 16:13:00 -04:00
|
|
|
value = resolve(node.params[0].original);
|
2017-06-29 16:22:19 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if (value) {
|
|
|
|
return `I18n.t(${value})`;
|
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
2018-09-21 07:02:23 -04:00
|
|
|
case "fa-icon":
|
|
|
|
case "d-icon":
|
2017-09-01 11:20:14 -04:00
|
|
|
state.helpersUsed.iconNode = true;
|
2017-06-29 16:22:19 -04:00
|
|
|
let icon = node.params[0].value;
|
2017-09-01 11:20:14 -04:00
|
|
|
return `__iN("${icon}")`;
|
2017-06-29 16:22:19 -04:00
|
|
|
break;
|
|
|
|
default:
|
2017-09-29 09:55:53 -04:00
|
|
|
if (node.escaped) {
|
|
|
|
return `${resolve(path)}`;
|
|
|
|
} else {
|
|
|
|
state.helpersUsed.rawHtml = true;
|
|
|
|
return `new __rH({ html: '<span>' + ${resolve(path)} + '</span>'})`;
|
|
|
|
}
|
2017-06-29 16:22:19 -04:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class Compiler {
|
|
|
|
constructor(ast) {
|
|
|
|
this.idx = 0;
|
|
|
|
this.ast = ast;
|
2017-09-01 11:20:14 -04:00
|
|
|
|
|
|
|
this.state = {
|
|
|
|
helpersUsed: {}
|
|
|
|
};
|
2017-06-29 16:22:19 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
newAcc() {
|
|
|
|
return `_a${this.idx++}`;
|
|
|
|
}
|
|
|
|
|
|
|
|
processNode(parentAcc, node) {
|
|
|
|
let instructions = [];
|
|
|
|
let innerAcc;
|
|
|
|
|
2018-09-21 07:02:23 -04:00
|
|
|
switch (node.type) {
|
2017-06-29 16:22:19 -04:00
|
|
|
case "Program":
|
|
|
|
node.body.forEach(bodyNode => {
|
2018-09-21 07:02:23 -04:00
|
|
|
instructions = instructions.concat(
|
|
|
|
this.processNode(parentAcc, bodyNode)
|
|
|
|
);
|
2017-06-29 16:22:19 -04:00
|
|
|
});
|
|
|
|
break;
|
|
|
|
case "ElementNode":
|
|
|
|
innerAcc = this.newAcc();
|
|
|
|
instructions.push(`var ${innerAcc} = [];`);
|
|
|
|
node.children.forEach(child => {
|
|
|
|
instructions = instructions.concat(this.processNode(innerAcc, child));
|
|
|
|
});
|
|
|
|
|
|
|
|
if (node.attributes.length) {
|
|
|
|
let attributes = [];
|
|
|
|
node.attributes.forEach(a => {
|
2018-09-21 07:02:23 -04:00
|
|
|
const name = a.name === "class" ? "className" : a.name;
|
2017-06-29 16:22:19 -04:00
|
|
|
if (a.value.type === "MustacheStatement") {
|
2018-09-21 07:02:23 -04:00
|
|
|
attributes.push(
|
|
|
|
`"${name}":${mustacheValue(a.value, this.state)}`
|
|
|
|
);
|
2017-06-29 16:22:19 -04:00
|
|
|
} else {
|
|
|
|
attributes.push(`"${name}":"${a.value.chars}"`);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2018-09-21 07:02:23 -04:00
|
|
|
const attrString = `{${attributes.join(", ")}}`;
|
|
|
|
instructions.push(
|
|
|
|
`${parentAcc}.push(virtualDom.h('${
|
|
|
|
node.tag
|
|
|
|
}', ${attrString}, ${innerAcc}));`
|
|
|
|
);
|
2017-06-29 16:22:19 -04:00
|
|
|
} else {
|
2018-09-21 07:02:23 -04:00
|
|
|
instructions.push(
|
|
|
|
`${parentAcc}.push(virtualDom.h('${node.tag}', ${innerAcc}));`
|
|
|
|
);
|
2017-06-29 16:22:19 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
case "TextNode":
|
|
|
|
return `${parentAcc}.push(${JSON.stringify(node.chars)});`;
|
|
|
|
|
|
|
|
case "MustacheStatement":
|
2017-09-01 11:20:14 -04:00
|
|
|
const value = mustacheValue(node, this.state);
|
2017-06-29 16:22:19 -04:00
|
|
|
if (value) {
|
|
|
|
instructions.push(`${parentAcc}.push(${value})`);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case "BlockStatement":
|
2018-09-21 07:02:23 -04:00
|
|
|
let negate = "";
|
|
|
|
|
|
|
|
switch (node.path.original) {
|
|
|
|
case "unless":
|
|
|
|
negate = "!";
|
|
|
|
case "if":
|
|
|
|
instructions.push(
|
|
|
|
`if (${negate}${resolve(node.params[0].original)}) {`
|
|
|
|
);
|
2017-06-29 16:22:19 -04:00
|
|
|
node.program.body.forEach(child => {
|
2018-09-21 07:02:23 -04:00
|
|
|
instructions = instructions.concat(
|
|
|
|
this.processNode(parentAcc, child)
|
|
|
|
);
|
2017-06-29 16:22:19 -04:00
|
|
|
});
|
|
|
|
|
|
|
|
if (node.inverse) {
|
|
|
|
instructions.push(`} else {`);
|
|
|
|
node.inverse.body.forEach(child => {
|
2018-09-21 07:02:23 -04:00
|
|
|
instructions = instructions.concat(
|
|
|
|
this.processNode(parentAcc, child)
|
|
|
|
);
|
2017-06-29 16:22:19 -04:00
|
|
|
});
|
|
|
|
}
|
|
|
|
instructions.push(`}`);
|
|
|
|
break;
|
2018-09-21 07:02:23 -04:00
|
|
|
case "each":
|
2017-10-05 16:13:00 -04:00
|
|
|
const collection = resolve(node.params[0].original);
|
2017-06-29 16:22:19 -04:00
|
|
|
instructions.push(`if (${collection} && ${collection}.length) {`);
|
2018-09-21 07:02:23 -04:00
|
|
|
instructions.push(
|
|
|
|
` ${collection}.forEach(${node.program.blockParams[0]} => {`
|
|
|
|
);
|
2017-06-29 16:22:19 -04:00
|
|
|
node.program.body.forEach(child => {
|
2018-09-21 07:02:23 -04:00
|
|
|
instructions = instructions.concat(
|
|
|
|
this.processNode(parentAcc, child)
|
|
|
|
);
|
2017-06-29 16:22:19 -04:00
|
|
|
});
|
|
|
|
instructions.push(` });`);
|
2018-09-21 07:02:23 -04:00
|
|
|
instructions.push("}");
|
2017-06-29 16:22:19 -04:00
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return instructions.join("\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
compile() {
|
2018-09-21 07:02:23 -04:00
|
|
|
return this.processNode("_r", this.ast);
|
2017-06-29 16:22:19 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function compile(template) {
|
2018-09-21 07:02:23 -04:00
|
|
|
const preprocessor = Ember.__loader.require("@glimmer/syntax");
|
2017-06-29 16:22:19 -04:00
|
|
|
const compiled = preprocessor.preprocess(template);
|
|
|
|
const compiler = new Compiler(compiled);
|
|
|
|
|
2017-09-01 11:20:14 -04:00
|
|
|
let code = compiler.compile();
|
|
|
|
|
2018-09-21 07:02:23 -04:00
|
|
|
let imports = "";
|
2017-09-01 11:20:14 -04:00
|
|
|
if (compiler.state.helpersUsed.iconNode) {
|
2017-09-29 09:55:53 -04:00
|
|
|
imports += "var __iN = Discourse.__widget_helpers.iconNode; ";
|
|
|
|
}
|
|
|
|
if (compiler.state.helpersUsed.rawHtml) {
|
|
|
|
imports += "var __rH = Discourse.__widget_helpers.rawHtml; ";
|
2017-09-01 11:20:14 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
return `function(attrs, state) { ${imports}var _r = [];\n${code}\nreturn _r; }`;
|
2017-06-29 16:22:19 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
exports.compile = compile;
|
|
|
|
|
|
|
|
function error(path, state, msg) {
|
|
|
|
const filename = state.file.opts.filename;
|
2018-09-21 07:02:23 -04:00
|
|
|
return path.replaceWithSourceString(
|
|
|
|
`function() { console.error("${filename}: ${msg}"); }`
|
|
|
|
);
|
2017-06-29 16:22:19 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
exports.WidgetHbsCompiler = function(babel) {
|
|
|
|
let t = babel.types;
|
|
|
|
return {
|
|
|
|
visitor: {
|
|
|
|
ImportDeclaration(path, state) {
|
|
|
|
let node = path.node;
|
2018-09-21 07:02:23 -04:00
|
|
|
if (
|
|
|
|
t.isLiteral(node.source, { value: "discourse/widgets/hbs-compiler" })
|
|
|
|
) {
|
2017-06-29 16:22:19 -04:00
|
|
|
let first = node.specifiers && node.specifiers[0];
|
|
|
|
if (!t.isImportDefaultSpecifier(first)) {
|
|
|
|
let input = state.file.code;
|
|
|
|
let usedImportStatement = input.slice(node.start, node.end);
|
|
|
|
let msg = `Only \`import hbs from 'discourse/widgets/hbs-compiler'\` is supported. You used: \`${usedImportStatement}\``;
|
|
|
|
throw path.buildCodeFrameError(msg);
|
|
|
|
}
|
|
|
|
|
2018-09-21 07:02:23 -04:00
|
|
|
state.importId =
|
|
|
|
state.importId ||
|
|
|
|
path.scope.generateUidIdentifierBasedOnNode(path.node.id);
|
2017-06-29 16:22:19 -04:00
|
|
|
path.scope.rename(first.local.name, state.importId.name);
|
|
|
|
path.remove();
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
TaggedTemplateExpression(path, state) {
|
2018-09-21 07:02:23 -04:00
|
|
|
if (!state.importId) {
|
|
|
|
return;
|
|
|
|
}
|
2017-06-29 16:22:19 -04:00
|
|
|
|
2018-09-21 07:02:23 -04:00
|
|
|
let tagPath = path.get("tag");
|
2017-06-29 16:22:19 -04:00
|
|
|
if (tagPath.node.name !== state.importId.name) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (path.node.quasi.expressions.length) {
|
2018-09-21 07:02:23 -04:00
|
|
|
return error(
|
|
|
|
path,
|
|
|
|
state,
|
|
|
|
"placeholders inside a tagged template string are not supported"
|
|
|
|
);
|
2017-06-29 16:22:19 -04:00
|
|
|
}
|
|
|
|
|
2018-09-21 07:02:23 -04:00
|
|
|
let template = path.node.quasi.quasis
|
|
|
|
.map(quasi => quasi.value.cooked)
|
|
|
|
.join("");
|
2017-06-29 16:22:19 -04:00
|
|
|
|
|
|
|
try {
|
|
|
|
path.replaceWithSourceString(compile(template));
|
2018-09-21 07:02:23 -04:00
|
|
|
} catch (e) {
|
2017-06-29 16:22:19 -04:00
|
|
|
return error(path, state, e.toString());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
};
|