DEV: Run prettier. (#6420)

This commit is contained in:
Bianca Nenciu 2018-09-21 14:02:23 +03:00 committed by Régis Hanol
parent 2e02a28ab4
commit 5407036ef9
2 changed files with 72 additions and 42 deletions

View File

@ -1,5 +1,5 @@
function resolve(path) {
if (path.indexOf('settings') === 0 || path.indexOf('transformed') === 0) {
if (path.indexOf("settings") === 0 || path.indexOf("transformed") === 0) {
return `this.${path}`;
}
return path;
@ -7,7 +7,6 @@ function resolve(path) {
function sexp(value) {
if (value.path.original === "hash") {
let result = [];
value.hash.pairs.forEach(p => {
@ -37,8 +36,8 @@ function argValue(arg) {
function mustacheValue(node, state) {
let path = node.path.original;
switch(path) {
case 'attach':
switch (path) {
case "attach":
let widgetName = argValue(node.hash.pairs.find(p => p.key === "widget"));
let attrs = node.hash.pairs.find(p => p.key === "attrs");
@ -48,10 +47,10 @@ function mustacheValue(node, state) {
return `this.attach(${widgetName}, attrs)`;
break;
case 'yield':
case "yield":
return `this.attrs.contents()`;
break;
case 'i18n':
case "i18n":
let value;
if (node.params[0].type === "StringLiteral") {
value = `"${node.params[0].value}"`;
@ -64,8 +63,8 @@ function mustacheValue(node, state) {
}
break;
case 'fa-icon':
case 'd-icon':
case "fa-icon":
case "d-icon":
state.helpersUsed.iconNode = true;
let icon = node.params[0].value;
return `__iN("${icon}")`;
@ -99,10 +98,12 @@ class Compiler {
let instructions = [];
let innerAcc;
switch(node.type) {
switch (node.type) {
case "Program":
node.body.forEach(bodyNode => {
instructions = instructions.concat(this.processNode(parentAcc, bodyNode));
instructions = instructions.concat(
this.processNode(parentAcc, bodyNode)
);
});
break;
case "ElementNode":
@ -113,21 +114,28 @@ class Compiler {
});
if (node.attributes.length) {
let attributes = [];
node.attributes.forEach(a => {
const name = a.name === 'class' ? 'className' : a.name;
const name = a.name === "class" ? "className" : a.name;
if (a.value.type === "MustacheStatement") {
attributes.push(`"${name}":${mustacheValue(a.value, this.state)}`);
attributes.push(
`"${name}":${mustacheValue(a.value, this.state)}`
);
} else {
attributes.push(`"${name}":"${a.value.chars}"`);
}
});
const attrString = `{${attributes.join(', ')}}`;
instructions.push(`${parentAcc}.push(virtualDom.h('${node.tag}', ${attrString}, ${innerAcc}));`);
const attrString = `{${attributes.join(", ")}}`;
instructions.push(
`${parentAcc}.push(virtualDom.h('${
node.tag
}', ${attrString}, ${innerAcc}));`
);
} else {
instructions.push(`${parentAcc}.push(virtualDom.h('${node.tag}', ${innerAcc}));`);
instructions.push(
`${parentAcc}.push(virtualDom.h('${node.tag}', ${innerAcc}));`
);
}
break;
@ -142,34 +150,44 @@ class Compiler {
}
break;
case "BlockStatement":
let negate = '';
let negate = "";
switch(node.path.original) {
case 'unless':
negate = '!';
case 'if':
instructions.push(`if (${negate}${resolve(node.params[0].original)}) {`);
switch (node.path.original) {
case "unless":
negate = "!";
case "if":
instructions.push(
`if (${negate}${resolve(node.params[0].original)}) {`
);
node.program.body.forEach(child => {
instructions = instructions.concat(this.processNode(parentAcc, child));
instructions = instructions.concat(
this.processNode(parentAcc, child)
);
});
if (node.inverse) {
instructions.push(`} else {`);
node.inverse.body.forEach(child => {
instructions = instructions.concat(this.processNode(parentAcc, child));
instructions = instructions.concat(
this.processNode(parentAcc, child)
);
});
}
instructions.push(`}`);
break;
case 'each':
case "each":
const collection = resolve(node.params[0].original);
instructions.push(`if (${collection} && ${collection}.length) {`);
instructions.push(` ${collection}.forEach(${node.program.blockParams[0]} => {`);
instructions.push(
` ${collection}.forEach(${node.program.blockParams[0]} => {`
);
node.program.body.forEach(child => {
instructions = instructions.concat(this.processNode(parentAcc, child));
instructions = instructions.concat(
this.processNode(parentAcc, child)
);
});
instructions.push(` });`);
instructions.push('}');
instructions.push("}");
break;
}
@ -182,19 +200,18 @@ class Compiler {
}
compile() {
return this.processNode('_r', this.ast);
return this.processNode("_r", this.ast);
}
}
function compile(template) {
const preprocessor = Ember.__loader.require('@glimmer/syntax');
const preprocessor = Ember.__loader.require("@glimmer/syntax");
const compiled = preprocessor.preprocess(template);
const compiler = new Compiler(compiled);
let code = compiler.compile();
let imports = '';
let imports = "";
if (compiler.state.helpersUsed.iconNode) {
imports += "var __iN = Discourse.__widget_helpers.iconNode; ";
}
@ -209,7 +226,9 @@ exports.compile = compile;
function error(path, state, msg) {
const filename = state.file.opts.filename;
return path.replaceWithSourceString(`function() { console.error("${filename}: ${msg}"); }`);
return path.replaceWithSourceString(
`function() { console.error("${filename}: ${msg}"); }`
);
}
exports.WidgetHbsCompiler = function(babel) {
@ -218,7 +237,9 @@ exports.WidgetHbsCompiler = function(babel) {
visitor: {
ImportDeclaration(path, state) {
let node = path.node;
if (t.isLiteral(node.source, { value: "discourse/widgets/hbs-compiler" })) {
if (
t.isLiteral(node.source, { value: "discourse/widgets/hbs-compiler" })
) {
let first = node.specifiers && node.specifiers[0];
if (!t.isImportDefaultSpecifier(first)) {
let input = state.file.code;
@ -227,32 +248,41 @@ exports.WidgetHbsCompiler = function(babel) {
throw path.buildCodeFrameError(msg);
}
state.importId = state.importId || path.scope.generateUidIdentifierBasedOnNode(path.node.id);
state.importId =
state.importId ||
path.scope.generateUidIdentifierBasedOnNode(path.node.id);
path.scope.rename(first.local.name, state.importId.name);
path.remove();
}
},
TaggedTemplateExpression(path, state) {
if (!state.importId) { return; }
if (!state.importId) {
return;
}
let tagPath = path.get('tag');
let tagPath = path.get("tag");
if (tagPath.node.name !== state.importId.name) {
return;
}
if (path.node.quasi.expressions.length) {
return error(path, state, "placeholders inside a tagged template string are not supported");
return error(
path,
state,
"placeholders inside a tagged template string are not supported"
);
}
let template = path.node.quasi.quasis.map(quasi => quasi.value.cooked).join('');
let template = path.node.quasi.quasis
.map(quasi => quasi.value.cooked)
.join("");
try {
path.replaceWithSourceString(compile(template));
} catch(e) {
} catch (e) {
return error(path, state, e.toString());
}
}
}
};

View File

@ -1,3 +1,3 @@
.my-plugin-thing {
color: blue
color: blue;
}