UNPKG

@brusalk/react-wow-addon

Version:

React-style UI Framework for World of Warcraft AddOns

63 lines (62 loc) 3.27 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.WowJsxTransformer = void 0; /* eslint-disable @typescript-eslint/no-object-literal-type-assertion */ const ts = require("typescript"); const typescript_to_lua_1 = require("typescript-to-lua"); const literal_1 = require("typescript-to-lua/dist/transformation/visitors/literal"); const transformObjectLiteral = literal_1.literalVisitors[ts.SyntaxKind.ObjectLiteralExpression]; const transformArrayLiteral = literal_1.literalVisitors[ts.SyntaxKind.ArrayLiteralExpression]; function transformJsxAttributesExpression(expression, context) { if (expression.properties.find(element => element.kind === ts.SyntaxKind.JsxSpreadAttribute)) { throw new Error("Unsupported: JsxSpreadAttribute"); } const properties = expression.properties .filter((element) => element.kind !== ts.SyntaxKind.JsxSpreadAttribute) .map(element => { const valueOrExpression = element.initializer ? element.initializer : ts.createLiteral(true); return ts.createPropertyAssignment(element.name, valueOrExpression); }); return transformObjectLiteral(ts.createObjectLiteral(properties), context); } function transformJsxOpeningElement(expression, context, children) { // <Something a="b" /> // React.createElement(Something, {a = 'b'}) const [library, create] = context.options.jsxFactory ? context.options.jsxFactory.split(".") : ["React", "createElement"]; const createElement = typescript_to_lua_1.createTableIndexExpression(typescript_to_lua_1.createIdentifier(library), typescript_to_lua_1.createStringLiteral(create)); const tagName = expression.tagName.getText(); const tag = tagName.toLowerCase() === tagName ? typescript_to_lua_1.createStringLiteral(tagName) : typescript_to_lua_1.createIdentifier(tagName); const props = transformJsxAttributesExpression(expression.attributes, context); if (children) { const childrenOrStringLiterals = children .filter(child => !ts.isJsxText(child) || child.text.trim() !== "") .map(child => ts.isJsxText(child) ? ts.createStringLiteral(child.text.trim()) : child); const arrayLiteral = ts.createArrayLiteral(childrenOrStringLiterals, true); return typescript_to_lua_1.createCallExpression(createElement, [tag, props, transformArrayLiteral(arrayLiteral, context)], expression); } return typescript_to_lua_1.createCallExpression(createElement, [tag, props], expression); } function transformJsxElement(expression, context) { if (ts.isJsxSelfClosingElement(expression)) { return transformJsxOpeningElement(expression, context); } return transformJsxOpeningElement(expression.openingElement, context, expression.children); } exports.WowJsxTransformer = { visitors: { [ts.SyntaxKind.JsxSelfClosingElement]: transformJsxElement, [ts.SyntaxKind.JsxElement]: transformJsxElement, [ts.SyntaxKind.JsxExpression]: (node, context) => { if (node.expression) { return context.transformExpression(node.expression); } return typescript_to_lua_1.createNilLiteral(); }, } };