Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
export function cg(ast, opts = {}) {
var res;
if (ast == null) {
// console.error("")
return "";
}
try {
if (Array.isArray(ast)) {
ast =
ast.length > 0 && T.isExpression(ast[0])
? T.sequenceExpression(ast)
: T.blockStatement(ast);
}
res = generate(ast, opts, "").code;
} catch (e) {
if (ast.type != null) console.log("ERROR:", e.stack);
}
if (res != null) {
return res;
}
return "!!" + opts.compact
? JSON.stringify(ast, defaultReplacer)
: JSON.stringify(ast, defaultReplacer, 2);
}
export function cg(ast, opts = {}) {
var res;
if (ast == null) {
// console.error("")
return "";
}
try {
if (Array.isArray(ast)) {
ast =
ast.length > 0 && T.isExpression(ast[0])
? T.sequenceExpression(ast)
: T.blockStatement(ast);
}
res = generate(ast, opts).code;
} catch (e) {
if (ast.type != null) console.error(e.stack);
}
if (res != null) return res;
return "!!" + opts.compact
? JSON.stringify(ast)
: JSON.stringify(ast, null, 2);
}
} else {
// x-for={??? in value}
throw new Error('Stynax error of x-for.');
}
} else {
// x-for={value}, x-for={callExp()}, ...
iterValue = expression;
params = [t.identifier('item'), createIndexNode(), createIndexNode()];
}
const parentJSXEl = path.findParent(p => p.isJSXElement());
// Transform x-for iterValue to map function
const properties = [
t.objectProperty(params[0], params[0]),
t.objectProperty(params[1], params[1])
];
const loopFnBody = t.blockStatement([
t.returnStatement(
t.objectExpression(properties)
)
]);
const mapCallExpression = t.callExpression(
t.memberExpression(iterValue, t.identifier('map')),
[
t.arrowFunctionExpression(params, loopFnBody)
]);
const listItem = getListItem(iterValue);
let parentList;
if (listItem) {
parentList = listItem.__listItem.parentList;
if (parentList) {
// Rename index name
if (listItem.__listItem.index === params[1].name) {
function cleanUpClassMethod(node, { identifierName, JSDocCommentValue } = {}) {
if (identifierName) {
node.key = t.identifier(identifierName);
}
// NOTE: ensure TS code is compatible with documentation.js (babel 7 flow preset)
// https://github.com/niieani/typescript-vs-flowtype
// https://github.com/babel/babel/blob/v7.0.0-beta.44/packages/babel-plugin-transform-typescript/src/index.js#L89
node.accessibility = null;
node.params = node.params.map(param =>
param.type === 'TSParameterProperty' ? param.parameter : param,
);
node.decorators = [];
node.body = t.blockStatement([]);
cleanUpNodeJSDoc(node, JSDocCommentValue);
}
),
);
} else {
body.push(node);
}
}
let executed = getName(asset, 'executed');
decls.push(
t.variableDeclarator(t.identifier(executed), t.booleanLiteral(false)),
);
let init = t.functionDeclaration(
getIdentifier(asset, 'init'),
[],
t.blockStatement([
t.ifStatement(t.identifier(executed), t.returnStatement()),
t.expressionStatement(
t.assignmentExpression(
'=',
t.identifier(executed),
t.booleanLiteral(true),
),
),
...body,
]),
);
return [t.variableDeclaration('var', decls), ...fns, init];
}
function serializeBody(
generator: Generator,
context: SerializationContext,
valuesToProcess: Set
): BabelNodeBlockStatement {
let statements = context.serializeGenerator(generator, valuesToProcess);
if (statements.length === 1 && statements[0].type === "BlockStatement") return (statements[0]: any);
return t.blockStatement(statements);
}
)
);
} else {
body.push(node);
}
}
let executed = getName(asset, 'executed');
decls.push(
t.variableDeclarator(t.identifier(executed), t.booleanLiteral(false))
);
let init = t.functionDeclaration(
getIdentifier(asset, 'init'),
[],
t.blockStatement([
t.ifStatement(t.identifier(executed), t.returnStatement()),
t.expressionStatement(
t.assignmentExpression(
'=',
t.identifier(executed),
t.booleanLiteral(true)
)
),
...body
])
);
return [t.variableDeclaration('var', decls), ...fns, init];
}
}
declarations.push(
t.variableDeclaration('const', [
t.variableDeclarator(
t.identifier(name),
t.callExpression(
t.memberExpression(fn, t.identifier('typeParameter')),
args
)
)
])
);
}
return [
t.arrowFunctionExpression([fn], t.blockStatement([
...declarations,
t.returnStatement(t.arrayExpression(invocations))
]))
];
}
else {
return invocations;
}
}
case "if_stmt": {
const children = term.children;
expectLength(children, 2, 3);
let pattern: PatternOutput;
const testTerm = children[0];
if (testTerm.name === "pattern") {
pattern = translatePattern(testTerm.children[0], translateTermToValue(testTerm.children[1], scope, functions), scope, functions);
} else {
pattern = convertToPattern(translateTermToValue(testTerm, scope, functions));
}
const { prefix, test, suffix } = flattenPattern(pattern, scope, term);
const consequent = concat(suffix, translateInNewScope(children[1], scope, functions, "consequent"));
if (isTrueExpression(test)) {
return concat(prefix, consequent);
}
const alternate = children.length === 3 ? blockStatement(translateInNewScope(children[2], scope, functions, "alternate")) : undefined;
return concat(prefix, [annotate(ifStatement(test, blockStatement(consequent), alternate), term)]);
}
case "while_stmt": {
expectLength(term.children, 2);
const testTerm = term.children[0];
const bodyTerm = term.children[1];
return [annotate(whileStatement(read(translateTermToValue(testTerm, scope, functions), scope), blockStatement(translateInNewScope(bodyTerm, scope, functions, "body"))), term)];
}
case "repeat_while_stmt": {
expectLength(term.children, 2);
const bodyTerm = term.children[0];
const testTerm = term.children[1];
return [annotate(doWhileStatement(read(translateTermToValue(testTerm, scope, functions), scope), blockStatement(translateInNewScope(bodyTerm, scope, functions, "body"))), term)];
}
case "for_each_stmt": {
expectLength(term.children, 6);
t.identifier('length')
);
const i = t.identifier('i');
getNodeFromPath(ast, path.slice(forIndex + 1).reverse())[
path[forIndex]
] = t.forStatement(
t.variableDeclaration('let', [
t.variableDeclarator(i, t.numericLiteral(0))
]),
t.binaryExpression(
'<',
i,
t.memberExpression(forStatement.right, t.identifier('length'))
),
t.updateExpression('++', i),
t.blockStatement([
t.variableDeclaration('const', [
t.variableDeclarator(
forStatement.left.declarations[0].id,
t.memberExpression(forStatement.right, i, true)
)
]),
...forStatement.body.body
])
);
return start;
}
}