@spec2ts/core
Version:
Core module for @spec2ts modules, includes codegen helpers and common parsing methods
122 lines (121 loc) • 5.42 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.addComment = exports.upsertPropertyValue = exports.changePropertyValue = exports.createObjectBinding = exports.createArrowFunction = exports.createObjectLiteral = exports.createTemplateString = exports.createMethodCall = exports.createCall = exports.isIdentifier = exports.isValidIdentifier = exports.toPropertyName = exports.toLiteral = exports.toIdentifier = exports.toExpression = void 0;
const ts = require("typescript");
const common_1 = require("./common");
const declaration_1 = require("./declaration");
function toExpression(ex) {
if (typeof ex === "string")
return ts.factory.createIdentifier(ex);
return ex;
}
exports.toExpression = toExpression;
function toIdentifier(ex) {
if (typeof ex === "string")
return ts.factory.createIdentifier(ex);
return ex;
}
exports.toIdentifier = toIdentifier;
function toLiteral(ex) {
if (ex === "true")
return ts.factory.createTrue();
if (ex === "false")
return ts.factory.createFalse();
if (typeof ex === "string")
return ts.factory.createStringLiteral(ex);
if (typeof ex === "number")
return ts.factory.createNumericLiteral(ex);
if (typeof ex === "bigint")
return ts.factory.createBigIntLiteral(ex.toString());
return ex;
}
exports.toLiteral = toLiteral;
function toPropertyName(name) {
if (typeof name === "string") {
return isValidIdentifier(name)
? ts.factory.createIdentifier(name)
: ts.factory.createStringLiteral(name);
}
return name;
}
exports.toPropertyName = toPropertyName;
function isValidIdentifier(str) {
if (!str.length || str.trim() !== str)
return false;
const node = ts.parseIsolatedEntityName(str, ts.ScriptTarget.Latest);
return (!!node &&
node.kind === ts.SyntaxKind.Identifier &&
!ts.identifierToKeywordKind(node));
}
exports.isValidIdentifier = isValidIdentifier;
function isIdentifier(n) {
return !!n && ts.isIdentifier(n);
}
exports.isIdentifier = isIdentifier;
function createCall(expression, { typeArgs, args } = {}) {
return ts.factory.createCallExpression(toExpression(expression), typeArgs, args);
}
exports.createCall = createCall;
function createMethodCall(method, opts) {
return createCall(ts.factory.createPropertyAccessExpression(ts.factory.createThis(), method), opts);
}
exports.createMethodCall = createMethodCall;
function createTemplateString(head, spans) {
if (!spans.length) {
return ts.factory.createStringLiteral(head);
}
return ts.factory.createTemplateExpression(ts.factory.createTemplateHead(head), spans.map(({ expression, literal }, i) => ts.factory.createTemplateSpan(expression, i === spans.length - 1
? ts.factory.createTemplateTail(literal)
: ts.factory.createTemplateMiddle(literal))));
}
exports.createTemplateString = createTemplateString;
function createObjectLiteral(props) {
return ts.factory.createObjectLiteralExpression(props.map(([name, identifier]) => (0, declaration_1.createPropertyAssignment)(name, toExpression(identifier))), true);
}
exports.createObjectLiteral = createObjectLiteral;
function createArrowFunction(parameters, body, { modifiers, typeParameters, type, equalsGreaterThanToken } = {}) {
return ts.factory.createArrowFunction(modifiers, typeParameters, parameters, type, equalsGreaterThanToken, body);
}
exports.createArrowFunction = createArrowFunction;
function createObjectBinding(elements) {
return ts.factory.createObjectBindingPattern(elements.map(({ dotDotDotToken, propertyName, name, initializer }) => ts.factory.createBindingElement(dotDotDotToken, propertyName, name, initializer)));
}
exports.createObjectBinding = createObjectBinding;
function changePropertyValue(o, property, value) {
const i = o.properties.findIndex(p => ts.isPropertyAssignment(p) && (0, common_1.getName)(p.name) === property);
if (i === -1) {
throw new Error(`No such property: ${property}`);
}
const p = o.properties[i];
if (!ts.isPropertyAssignment(p)) {
throw new Error(`Invalid node: ${property}`);
}
return ts.factory.updateObjectLiteralExpression(o, [
...o.properties.slice(0, i),
ts.factory.updatePropertyAssignment(p, p.name, value),
...o.properties.slice(i + 1)
]);
}
exports.changePropertyValue = changePropertyValue;
function upsertPropertyValue(o, property, value) {
const i = o.properties.findIndex(p => ts.isPropertyAssignment(p) && (0, common_1.getName)(p.name) === property);
if (i === -1) {
return ts.factory.updateObjectLiteralExpression(o, (0, common_1.appendNodes)(o.properties, ts.factory.createPropertyAssignment(property, value)));
}
const p = o.properties[i];
if (!ts.isPropertyAssignment(p)) {
throw new Error(`Invalid node: ${property}`);
}
return ts.factory.updateObjectLiteralExpression(o, [
...o.properties.slice(0, i),
ts.factory.updatePropertyAssignment(p, p.name, value),
...o.properties.slice(i + 1)
]);
}
exports.upsertPropertyValue = upsertPropertyValue;
function addComment(node, comment) {
if (!comment)
return node;
return ts.addSyntheticLeadingComment(node, ts.SyntaxKind.MultiLineCommentTrivia, `*\n * ${comment.replace(/\n/g, "\n * ")}\n `, true);
}
exports.addComment = addComment;