How to support vue template parse with recast?

Viewed 170

I need to print the vue source code non-destructively after modifying the vue template ast. The intent of the recast meets my needs, The react project has been completed, but I can’t see the way forward in the vue project.

  const parser = require("@vue/compiler-sfc")

  const ast = recast.parse(content, {
    parser: parser 
  });
  const output = recast.print(ast)

or

  const parser = require("@vue/compiler-sfc")

  const result = parser.parse(content)
  const { template, script, styles } = result.descriptor

  // template.ast.loc.source is vue <tamplate> code
  const ast = recast.parse(template.ast.loc.source, {
    parser: parser
  });
  const output = recast.print(ast)

got the same error:

node_modules\esprima\dist\esprima.js:5008
                    throw error;
                    ^
Error: Line 1: Invalid regular expression: missing /
    at ErrorHandler.constructError (node_modules\esprima\dist\esprima.js:5012:22)
    at ErrorHandler.createError (node_modules\esprima\dist\esprima.js:5028:27)
    at ErrorHandler.throwError (node_modules\esprima\dist\esprima.js:5035:21)
    at Scanner.throwUnexpectedToken (node_modules\esprima\dist\esprima.js:5164:35)
    at Scanner.scanRegExpBody (node_modules\esprima\dist\esprima.js:6157:19)
    at Scanner.scanRegExp (node_modules\esprima\dist\esprima.js:6204:29)
    at Tokenizer.getNextToken (node_modules\esprima\dist\esprima.js:6675:56)
    at Object.tokenize (node_modules\esprima\dist\esprima.js:154:36)
    at Object.parse (node_modules\recast\lib\parser.js:40:30)
    at compiler (compiler.js:32:27) {
  index: 14,
  lineNumber: 1,
  description: 'Invalid regular expression: missing /'
}

template.ast looks like:

{
  "type": 1,
  "ns": 0,
  "tag": "template",
  "tagType": 0,
  "props": [],
  "isSelfClosing": false,
  "children": [
    {
      "type": 2,
      "content": "  ",
      "loc": {
        "start": { "column": 11, "line": 1, "offset": 10 },
        "end": { "column": 3, "line": 2, "offset": 13 },
        "source": "\n  "
      }
    },
...
0 Answers
Related