@ethan-jones-vizio/sveld
Version:
Generate TypeScript definitions for your Svelte components.
299 lines (298 loc) • 16.3 kB
JavaScript
;
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 __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
var __generator = (this && this.__generator) || function (thisArg, body) {
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
function verb(n) { return function (v) { return step([n, v]); }; }
function step(op) {
if (f) throw new TypeError("Generator is already executing.");
while (_) try {
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
if (y = 0, t) op = [op[0] & 2, t.value];
switch (op[0]) {
case 0: case 1: t = op; break;
case 4: _.label++; return { value: op[1], done: false };
case 5: _.label++; y = op[1]; op = [0]; continue;
case 7: op = _.ops.pop(); _.trys.pop(); continue;
default:
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
if (t[2]) _.ops.pop();
_.trys.pop(); continue;
}
op = body.call(thisArg, _);
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
}
};
var __rest = (this && this.__rest) || function (s, e) {
var t = {};
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
t[p] = s[p];
if (s != null && typeof Object.getOwnPropertySymbols === "function")
for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
t[p[i]] = s[p[i]];
}
return t;
};
var __asyncValues = (this && this.__asyncValues) || function (o) {
if (!Symbol.asyncIterator) throw new TypeError("Symbol.asyncIterator is not defined.");
var m = o[Symbol.asyncIterator], i;
return m ? m.call(o) : (o = typeof __values === "function" ? __values(o) : o[Symbol.iterator](), i = {}, verb("next"), verb("throw"), verb("return"), i[Symbol.asyncIterator] = function () { return this; }, i);
function verb(n) { i[n] = o[n] && function (v) { return new Promise(function (resolve, reject) { v = o[n](v), settle(resolve, reject, v.done, v.value); }); }; }
function settle(resolve, reject, d, v) { Promise.resolve(v).then(function(v) { resolve({ value: v, done: d }); }, reject); }
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
exports.__esModule = true;
exports.writeTsDefinition = exports.getTypeDefs = exports.formatTsProps = void 0;
var path = __importStar(require("path"));
var create_exports_1 = require("../create-exports");
var Writer_1 = __importDefault(require("./Writer"));
var ANY_TYPE = "any";
var EMPTY_STR = "";
function formatTsProps(props) {
if (props === undefined)
return ANY_TYPE;
return props + "\n";
}
exports.formatTsProps = formatTsProps;
function getTypeDefs(def) {
if (def.typedefs.length === 0)
return EMPTY_STR;
return def.typedefs.map(function (typedef) { return "export ".concat(typedef.ts); }).join("\n\n");
}
exports.getTypeDefs = getTypeDefs;
function clampKey(key) {
if (/(\-|\s+|\:)/.test(key)) {
return /(\"|\')/.test(key) ? key : "[\"".concat(key, "\"]");
}
return key;
}
function addCommentLine(value, returnValue) {
if (!value)
return undefined;
return "* ".concat(returnValue || value, "\n");
}
function genPropDef(def) {
var _a, _b, _c;
var initial_props = def.props
.filter(function (prop) { return !prop.isFunctionDeclaration && prop.kind !== "const"; })
.map(function (prop) {
var _a;
var defaultValue = prop.value;
if (typeof prop.value === "string") {
defaultValue = prop.value.replace(/\s+/g, " ");
}
if (prop.value === undefined) {
defaultValue = "undefined";
}
var prop_comments = [
addCommentLine((_a = prop.description) === null || _a === void 0 ? void 0 : _a.replace(/\n/g, "\n* ")),
addCommentLine(prop.constant, "@constant"),
"* @default ".concat(defaultValue, "\n"),
]
.filter(Boolean)
.join("");
var prop_value = prop.constant && !prop.isFunction ? prop.value : prop.type;
return "\n ".concat(prop_comments.length > 0 ? "/**\n".concat(prop_comments, "*/") : EMPTY_STR, "\n ").concat(prop.name).concat(prop.isRequired ? "" : "?", ": ").concat(prop_value, ";");
});
if (((_a = def.rest_props) === null || _a === void 0 ? void 0 : _a.type) === "Element") {
var elements = (_b = def.rest_props) === null || _b === void 0 ? void 0 : _b.name.split("|").map(function (element) { return element.replace(/\s+/g, ""); });
if (elements.includes("a")) {
initial_props.push([
"\n",
"\n /**\n * SvelteKit attribute to enable data prefetching\n * if a link is hovered over or touched on mobile.\n * @see https://kit.svelte.dev/docs/a-options#sveltekit-prefetch\n * @default false\n */\n \"sveltekit:prefetch\"?: boolean;\n ",
"\n",
"\n /**\n * SvelteKit attribute to trigger a full page\n * reload after the link is clicked.\n * @see https://kit.svelte.dev/docs/a-options#sveltekit-reload\n * @default false\n */\n \"sveltekit:reload\"?: boolean;\n ",
"\n",
"\n /**\n * SvelteKit attribute to prevent scrolling\n * after the link is clicked.\n * @see https://kit.svelte.dev/docs/a-options#sveltekit-noscroll\n * @default false\n */\n \"sveltekit:noscroll\"?: boolean;\n ",
].join("\n"));
}
}
var props = initial_props.join("\n");
var props_name = "".concat(def.moduleName, "Props");
var prop_def = EMPTY_STR;
if (((_c = def.rest_props) === null || _c === void 0 ? void 0 : _c.type) === "Element") {
var extend_tag_map = def.rest_props.name
.split("|")
.map(function (name) {
var element = name.trim();
if (element === "svg") {
return "svelte.JSX.SVGAttributes<SVGSVGElement>";
}
return "svelte.JSX.HTMLAttributes<HTMLElementTagNameMap[\"".concat(element, "\"]>");
})
.join(",");
prop_def = "\n export interface ".concat(props_name, " extends ").concat(def["extends"] !== undefined ? "".concat(def["extends"].interface, ", ") : "").concat(extend_tag_map, " {\n ").concat(props, "\n }\n ");
}
else {
prop_def = "\n export interface ".concat(props_name, " ").concat(def["extends"] !== undefined ? "extends ".concat(def["extends"].interface) : "", " {\n ").concat(props, "\n }\n ");
}
return {
props_name: props_name,
prop_def: prop_def
};
}
function genSlotDef(def) {
return def.slots
.map(function (_a) {
var name = _a.name, slot_props = _a.slot_props, rest = __rest(_a, ["name", "slot_props"]);
var key = rest["default"] ? "default" : clampKey(name);
return "".concat(clampKey(key), ": ").concat(formatTsProps(slot_props), ";");
})
.join("\n");
}
function genEventDef(def) {
var createDispatchedEvent = function (detail) {
if (detail === void 0) { detail = ANY_TYPE; }
if (/CustomEvent/.test(detail))
return detail;
return "CustomEvent<".concat(detail, ">");
};
return def.events
.map(function (event) {
return "".concat(clampKey(event.name), ": ").concat(event.type === "dispatched" ? createDispatchedEvent(event.detail) : "WindowEventMap[\"".concat(event.name, "\"]"), ";");
})
.join("\n");
}
function genAccessors(def) {
return def.props
.filter(function (prop) { return prop.isFunctionDeclaration || prop.kind === "const"; })
.map(function (prop) {
var _a;
var prop_comments = [addCommentLine((_a = prop.description) === null || _a === void 0 ? void 0 : _a.replace(/\n/g, "\n* "))].filter(Boolean).join("");
return "\n ".concat(prop_comments.length > 0 ? "/**\n".concat(prop_comments, "*/") : EMPTY_STR, "\n ").concat(prop.name, ": ").concat(prop.type, ";");
})
.join("\n");
}
function genImports(def) {
if (def["extends"] === undefined)
return "";
return "import type { ".concat(def["extends"].interface, " } from ").concat(def["extends"]["import"], ";");
}
function genComponentComment(def) {
if (!def.componentComment)
return "";
if (!/\n/.test(def.componentComment))
return "/** ".concat(def.componentComment.trim(), " */");
return "/*".concat(def.componentComment
.split("\n")
.map(function (line) { return "* ".concat(line); })
.join("\n"), "\n*/");
}
function genModuleExports(def) {
return def.moduleExports
.map(function (prop) {
var _a;
var prop_comments = [addCommentLine((_a = prop.description) === null || _a === void 0 ? void 0 : _a.replace(/\n/g, "\n* "))].filter(Boolean).join("");
var type_def = "export type ".concat(prop.name, " = ").concat(prop.type || ANY_TYPE, ";");
var is_function = prop.type && /=>/.test(prop.type);
if (is_function) {
var _b = prop.type.split("=>"), first = _b[0], second = _b[1], rest = _b.slice(2);
var rest_type = rest.map(function (item) { return "=>" + item; }).join("");
type_def = "export declare function ".concat(prop.name).concat(first, ":").concat(second).concat(rest_type, ";");
}
return "\n ".concat(prop_comments.length > 0 ? "/**\n".concat(prop_comments, "*/") : EMPTY_STR, "\n ").concat(type_def);
})
.join("\n");
}
function writeTsDefinition(component) {
var moduleName = component.moduleName, typedefs = component.typedefs, props = component.props, moduleExports = component.moduleExports, slots = component.slots, events = component.events, rest_props = component.rest_props, _extends = component["extends"], componentComment = component.componentComment;
var _a = genPropDef({
moduleName: moduleName,
props: props,
rest_props: rest_props,
"extends": _extends
}), props_name = _a.props_name, prop_def = _a.prop_def;
return "\n /// <reference types=\"svelte\" />\n import type { SvelteComponentTyped } from \"svelte\";\n ".concat(genImports({ "extends": _extends }), "\n ").concat(genModuleExports({ moduleExports: moduleExports }), "\n ").concat(getTypeDefs({ typedefs: typedefs }), "\n ").concat(prop_def, "\n ").concat(genComponentComment({ componentComment: componentComment }), "\n export default class ").concat(moduleName === "default" ? "" : moduleName, " extends SvelteComponentTyped<\n ").concat(props_name, ",\n {").concat(genEventDef({ events: events }), "},\n {").concat(genSlotDef({ slots: slots }), "}\n > {\n ").concat(genAccessors({ props: props }), "\n }");
}
exports.writeTsDefinition = writeTsDefinition;
function writeTsDefinitions(components, options) {
var components_1, components_1_1;
var e_1, _a;
return __awaiter(this, void 0, void 0, function () {
var ts_base_path, writer, indexDTs, _b, moduleName, component, ts_filepath, e_1_1;
return __generator(this, function (_c) {
switch (_c.label) {
case 0:
ts_base_path = path.join(process.cwd(), options.outDir, "index.d.ts");
writer = new Writer_1["default"]({ parser: "typescript", printWidth: 80 });
indexDTs = options.preamble + (0, create_exports_1.createExports)(options.exports, components);
_c.label = 1;
case 1:
_c.trys.push([1, 7, 8, 13]);
components_1 = __asyncValues(components);
_c.label = 2;
case 2: return [4 /*yield*/, components_1.next()];
case 3:
if (!(components_1_1 = _c.sent(), !components_1_1.done)) return [3 /*break*/, 6];
_b = components_1_1.value, moduleName = _b[0], component = _b[1];
ts_filepath = (0, create_exports_1.convertSvelteExt)(path.join(options.outDir, component.filePath));
return [4 /*yield*/, writer.write(ts_filepath, writeTsDefinition(component))];
case 4:
_c.sent();
_c.label = 5;
case 5: return [3 /*break*/, 2];
case 6: return [3 /*break*/, 13];
case 7:
e_1_1 = _c.sent();
e_1 = { error: e_1_1 };
return [3 /*break*/, 13];
case 8:
_c.trys.push([8, , 11, 12]);
if (!(components_1_1 && !components_1_1.done && (_a = components_1["return"]))) return [3 /*break*/, 10];
return [4 /*yield*/, _a.call(components_1)];
case 9:
_c.sent();
_c.label = 10;
case 10: return [3 /*break*/, 12];
case 11:
if (e_1) throw e_1.error;
return [7 /*endfinally*/];
case 12: return [7 /*endfinally*/];
case 13: return [4 /*yield*/, writer.write(ts_base_path, indexDTs)];
case 14:
_c.sent();
console.log("created TypeScript definitions.");
return [2 /*return*/];
}
});
});
}
exports["default"] = writeTsDefinitions;