UNPKG

@ethan-jones-vizio/sveld

Version:

Generate TypeScript definitions for your Svelte components.

124 lines (123 loc) 5.44 kB
"use strict"; var __assign = (this && this.__assign) || function () { __assign = Object.assign || function(t) { for (var s, i = 1, n = arguments.length; i < n; i++) { s = arguments[i]; for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p]; } return t; }; return __assign.apply(this, arguments); }; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; var desc = Object.getOwnPropertyDescriptor(m, k); if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { desc = { enumerable: true, get: function() { return m[k]; } }; } Object.defineProperty(o, k2, desc); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k); __setModuleDefault(result, mod); return result; }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; exports.__esModule = true; exports.parseExports = void 0; var acorn = __importStar(require("acorn")); var fs = __importStar(require("fs")); var path_1 = __importDefault(require("path")); var path_2 = require("./path"); function parseExports(source, dir) { var ast = acorn.parse(source, { ecmaVersion: "latest", sourceType: "module" }); var exports_by_identifier = {}; // @ts-ignore ast.body.forEach(function (node) { var _a, _b, _c, _d; if (node.type === "ExportDefaultDeclaration") { var id = node.declaration.name; if (id in exports_by_identifier) { exports_by_identifier[id]["default"] = true; } else { exports_by_identifier[id] = { source: "", "default": true }; } } else if (node.type === "ExportAllDeclaration") { if (!node.source) return; var file_path = path_1["default"].resolve(dir, node.source.value); if (!fs.lstatSync(file_path).isFile()) { var files = fs.readdirSync(file_path); for (var _i = 0, files_1 = files; _i < files_1.length; _i++) { var file = files_1[_i]; if (file.includes("index")) { file_path = path_1["default"].join(file_path, file); break; } } } var export_file = fs.readFileSync(file_path, "utf-8"); var exports_1 = parseExports(export_file, path_1["default"].dirname(file_path)); for (var _e = 0, _f = Object.entries(exports_1); _e < _f.length; _e++) { var _g = _f[_e], key = _g[0], value = _g[1]; var source_1 = (0, path_2.normalizeSeparators)("./" + path_1["default"].join(node.source.value, value.source)); exports_by_identifier[key] = __assign(__assign({}, value), { source: source_1 }); } } else if (node.type === "ExportNamedDeclaration") { node.specifiers.forEach(function (specifier) { var _a, _b, _c, _d; var exported_name = specifier.exported.name; var id = exported_name || specifier.local.name; if (id in exports_by_identifier) { if (node.type === "ExportNamedDeclaration") { exports_by_identifier[id].mixed = true; } if (!exports_by_identifier[id].source) { exports_by_identifier[id].source = (_b = (_a = node.source) === null || _a === void 0 ? void 0 : _a.value) !== null && _b !== void 0 ? _b : ""; } } else { exports_by_identifier[id] = { source: (_d = (_c = node.source) === null || _c === void 0 ? void 0 : _c.value) !== null && _d !== void 0 ? _d : "", "default": id === "default" }; } }); } else if (node.type === "ImportDeclaration") { var id = node.specifiers[0].local.name; if (id in exports_by_identifier) { if (!exports_by_identifier[id].source) { exports_by_identifier[id].source = (_b = (_a = node.source) === null || _a === void 0 ? void 0 : _a.value) !== null && _b !== void 0 ? _b : ""; } } else { exports_by_identifier[id] = { source: (_d = (_c = node.source) === null || _c === void 0 ? void 0 : _c.value) !== null && _d !== void 0 ? _d : "", "default": id === "default" }; } } }); return exports_by_identifier; } exports.parseExports = parseExports;