Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
const generate = require('@babel/generator').default
const ast = {
type: 'ExpressionStatement',
expression: {
type: 'BinaryExpression',
operator: '+',
left: {type: 'NumericLiteral', value: 1},
right: {type: 'NumericLiteral', value: 2}
}
}
const {code, map} = generate(ast)
console.log(code)
// 1 + 2;
const errorReport = (e, path, file) => {
const nodeCode = generator_1.default(path.node).code;
console.log("FILE: ", file.filename);
console.log("PART: ", nodeCode);
console.error("ERROR: ", e);
debugger;
};
function setAttr(elem, name, value) {
transpileFile(file, errorsReport) {
try {
const ast = parse(
file.content,
{
sourceFilename: file.fileName,
sourceType: 'module',
plugins: ['objectRestSpread']
},
);
this.transpilers.forEach((t) => babelTraverse(ast, t.traverseObject()));
const content = babelGenerator(ast, {}, file.content).code;
return Object.assign({}, file, { content });
} catch (e) {
if (e.toString().indexOf('SyntaxError') !== -1) {
const line = file.content.split('\n')[e.loc.line - 1];
const spaces = Array(e.loc.column).fill(' ').join('');
errorsReport.error(`Syntax error during '${file.fileName}' parsing: ${e.message}:\n${line}\n${spaces}^`);
} else {
errorsReport.error(e);
}
}
return undefined;
}
if (packager.options.minify) {
mangleScope(path.scope);
}
},
},
});
let opts = {
sourceMaps: packager.options.sourceMaps,
sourceFileName: packager.bundle.name,
minified: packager.options.minify,
comments: !packager.options.minify,
};
return generate(ast, opts);
};
)
];
return {
nodes: [thisNode, ...consequentNodes, ...alternateNodes],
edges: [...thisEdges, ...consequentEdges, ...alternateEdges],
entryNodes: [thisNode],
exitNodes: [...consequentExitNodes, ...alternateExitNodes],
breakNodes: [...consequentBreakNodes, ...alternateBreakNodes],
subGraphs: []
};
}
case "SwitchStatement":
{
const thisNode = cleanGraphNode({
id: makeIdFromAstNode(statement),
name: `switch ${generate(statement.discriminant).code} `,
shape: "rhombus"
});
const scopeGraph = fp.reduce(
(
{ nodes, edges, entryNodes, exitNodes, breakNodes },
caseAstElement
) => {
const {
nodes: caseNodes,
edges: caseEdges,
entryNodes: caseEntryNodes,
exitNodes: caseExitNodes,
breakNodes: caseBreakNodes
} = transformGeneralAstToGraph(caseAstElement.consequent);
const caseEntryEdges = fp.map(
const generateFn = node => {
return generate(node, {
comments: false
}).code
}
];
return {
nodes: [thisNode, ...bodyNodes],
edges: [...thisEdges, ...bodyEdges],
entryNodes: [thisNode],
exitNodes: [...bodyBreakNodes, thisNode],
breakNodes: [],
subGraphs: []
};
}
case "ForOfStatement":
case "ForStatement": {
const thisNode = cleanGraphNode({
id: makeIdFromAstNode(statement),
name: `for ${generate(statement.init).code} ; ${
generate(statement.test).code
} ; ${generate(statement.update).code}`,
shape: "rhombus"
});
const {
nodes: bodyNodes,
edges: bodyEdges,
entryNodes: bodyEntryNodes,
exitNodes: bodyExitNodes,
breakNodes: bodyBreakNodes
} = transformGeneralAstToGraph(statement.body);
const thisEdges = [
...fp.map(
node => ({
from: thisNode.id,
to: node.id,
//递归的`{foo+1+3+4}px`=>>"{{foo+1+3+4}}px"
if (t.isBinaryExpression(exp)) {
let small = ''
small = ConvertBinaryExpression(small, exp)
value += `{{${small}}}`
} else {
//正常的`{foo}px`=>>"{{foo}}px"
value += `{{${exp.name}}}`
}
}
})
return value
}
return isNumber ? generate(node).code + 'px' : node.value
}
function genTemplate(fn, opts) {
const ast = fn(opts);
return generate(ast).code;
}
async renderChunk(source, options) {
const ast = parse(source, {
sourceType: 'module'
});
traverse(ast, babelPlugin().visitor);
traverse(ast, handleGlobalReference().visitor);
const result = generate(ast);
return `/** Original source code: https://github.com/Modernizr/Modernizr/blob/v${version}/src/${options.fileName} **/\n${result.code}`;
}
}