Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
walk(ast, (node) => {
if (node.name === 'script' || node.name === 'style') {
const modifiedBlockNodes = node.block.nodes.map((block) => {
if (block.type === 'Code') {
// eslint-disable-next-line no-param-reassign
block.type = 'Text';
// eslint-disable-next-line no-param-reassign
block.val = `#{${block.val}}`;
}
return block;
});
const newCode = wrap((
generateCode((
{ ...node.block, nodes: modifiedBlockNodes }
))
))();
const replacedCode = node.name === 'script'
? replaceJs(newCode, merge(options.espreeOptions, { sourceFile: opts.sourceFile }))
: replaceCss(newCode, { sourceFile: opts.sourceFile });
// add one tab after each new line
const pugCode = `${node.name}.\n${replacedCode}`.replace(/\n/g, '\n\t');
const astReplaced = parser(lex(pugCode));
const scriptBlock = astReplaced.nodes[0].block;
// do not change entire scriptBlock
// this might be look like the correct ast,
// but the begin and end loc numbers are wrong
// eslint-disable-next-line no-param-reassign
export function compilePugTemplate(str = '', locals = {}) {
try {
let funcStr = generateCode(parse(lex(str)), {
compileDebug: false,
pretty: true,
inlineRuntimeFunctions: false,
templateName: '_parse'
});
let func = wrap(funcStr, '_parse');
return func(locals);
} catch (e) {
return `Compile Error: ${e.message}`;
}
}