UNPKG

typedraft

Version:

TypeDraft is a superset of typescript with built-in support for DSL extension and literate programming.

46 lines (45 loc) 1.49 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const core_1 = require("@babel/core"); const traverse_1 = require("@babel/traverse"); const binding_1 = require("@babel/traverse/lib/scope/binding"); const parser_1 = require("@babel/parser"); const generator_1 = require("@babel/generator"); function ToAst(code) { return core_1.template.ast(code, { plugins: ["typescript", "jsx"] }); } exports.ToAst = ToAst; function ToNodePath(code) { const path = new traverse_1.NodePath(null, null); path.node = ToAst(code); return path; } exports.ToNodePath = ToNodePath; /** * only used in test to construct required binding param */ function ToBinding(code) { const binding = new binding_1.default({ path: ToNodePath(code), existing: null, identifier: null, scope: null, kind: "let", }); return binding; } exports.ToBinding = ToBinding; function ToFile(raw) { // babel bug: https://github.com/babel/babel/issues/8837 // refert to test case "transcriber.interface-no-parse-error" const code = raw.replace(new RegExp("^<", "gm"), ";<"); return parser_1.parse(code, { sourceType: "module", plugins: ["typescript", "jsx", ["decorators", { decoratorsBeforeExport: true }]], }); } exports.ToFile = ToFile; function ToString(node, options) { return generator_1.default(node, options || {}).code; } exports.ToString = ToString;