Refactor and inline handlebars' postprocess file in parser file (#9021)

This commit is contained in:
Cyrille David authored and GitHub committed 2020-08-22 11:35:51 +08:00
1 parent 8013e510cc
commit ca68cbe731
2 files changed
+19 -38

No files matched your search

+19 -5
View File
@@ -1,13 +1,27 @@
"use strict";
const createError = require("../common/parser-create-error");
const postprocess = require("./postprocess");
function parse(text, parsers, options) {
const { preprocess: glimmer, traverse } = require("@glimmer/syntax");
/* from the following template: `non-escaped mustache \\{{helper}}`
* glimmer parser will produce an AST missing a backslash
* so here we add it back
* */
function addBackslash(/* options*/) {
return {
name: "addBackslash",
visitor: {
TextNode(node) {
node.chars = node.chars.replace(/\\/, "\\\\");
},
},
};
}
function parse(text) {
const { preprocess: glimmer } = require("@glimmer/syntax");
let ast;
try {
ast = glimmer(text, { mode: "codemod" });
ast = glimmer(text, { mode: "codemod", plugins: { ast: [addBackslash] } });
} catch (error) {
const matches = error.message.match(/on line (\d+)/);
/* istanbul ignore else */
@@ -20,7 +34,7 @@ function parse(text, parsers, options) {
}
}
return postprocess(ast, options, traverse);
return ast;
}
module.exports = {
-33
View File
@@ -1,33 +0,0 @@
"use strict";
/** @type {Array<(any) => any>} */
const PIPELINE = [addBackslash];
/* VISITORS */
/*
*/
function addBackslash(/* options*/) {
return {
TextNode(node) {
/* from the following template: `non-escaped mustache \\{{helper}}`
* glimmer parser will produce an AST missing a backslash
* so here we add it back
* */
if (node.chars.includes("\\")) {
node.chars = node.chars.replace(/\\/, "\\\\");
}
},
};
}
function postprocess(ast, options, traverse) {
for (const fn of PIPELINE) {
const visitor = fn(options);
traverse(ast, visitor);
}
return ast;
}
module.exports = postprocess;