/* This Source Code Form is subject to the terms of the Mozilla Public * License, v. 2.0. If a copy of the MPL was not distributed with this * file, You can obtain one at . */ import { parseScriptTags } from "./parse-script-tags"; import * as babelParser from "@babel/parser"; import * as t from "@babel/types"; import { getSource } from "../sources"; const ASTs = new Map(); function _parse(code, opts) { return babelParser.parse(code, { ...opts, tokens: true, }); } const sourceOptions = { generated: { sourceType: "unambiguous", tokens: true, plugins: [ "classStaticBlock", "classPrivateProperties", "classPrivateMethods", "classProperties", "explicitResourceManagement", "importAttributes", "objectRestSpread", "optionalChaining", "privateIn", "nullishCoalescingOperator", "regexpUnicodeSets", ], }, original: { sourceType: "unambiguous", tokens: true, plugins: [ "asyncGenerators", "classPrivateMethods", "classPrivateProperties", "classProperties", "classStaticBlock", "decorators-legacy", "doExpressions", "dynamicImport", "explicitResourceManagement", "exportDefaultFrom", "exportNamespaceFrom", "flow", "functionBind", "functionSent", "importAttributes", "jsx", "nullishCoalescingOperator", "objectRestSpread", "optionalChaining", "react-jsx", "regexpUnicodeSets", ], }, }; export function parse(text, opts) { let ast = {}; if (!text) { return ast; } try { ast = _parse(text, opts); } catch (error) { console.error(error); } return ast; } // Custom parser for parse-script-tags that adapts its input structure to // our parser's signature function htmlParser({ source, line }) { return parse(source, { startLine: line, ...sourceOptions.generated }); } const VUE_COMPONENT_START = /^\s* p !== "flow" && p !== "decorators" && p !== "decorators2" && (p !== "jsx" || contentType.match(/typescript-jsx/)) ), "decorators-legacy", "typescript", ], }; ast = parse(source.text, options); } ASTs.set(source.id, ast); return ast; } export function clearASTs(sourceIds) { for (const sourceId of sourceIds) { ASTs.delete(sourceId); } } export function replaceNode(ancestors, node) { const parent = ancestors[ancestors.length - 1]; if (typeof parent.index === "number") { if (Array.isArray(node)) { parent.node[parent.key].splice(parent.index, 1, ...node); } else { parent.node[parent.key][parent.index] = node; } } else { parent.node[parent.key] = node; } }