UNPKG

@cosmology/ast

Version:
174 lines (173 loc) 7.02 kB
"use strict"; 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; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.newExpression = exports.makeCommentLineWithBlocks = exports.objectProperty = exports.objectMethod = exports.objectPattern = exports.tsTypeParameterDeclaration = exports.arrowFunctionExpression = exports.classProperty = exports.classDeclaration = exports.identifier = exports.callExpression = exports.functionDeclaration = exports.tsPropertySignature = exports.tsEnumMember = exports.classMethod = exports.tsMethodSignature = exports.commentLine = exports.commentBlock = void 0; const t = __importStar(require("@babel/types")); const utils_1 = require("./utils"); // TODO move to @cosmology/utils package const commentBlock = (value) => { return { type: 'CommentBlock', value, start: null, end: null, loc: null }; }; exports.commentBlock = commentBlock; const commentLine = (value) => { return { type: 'CommentLine', value, start: null, end: null, loc: null }; }; exports.commentLine = commentLine; function tsMethodSignature(key, typeParameters = null, parameters, typeAnnotation = null, trailingComments, leadingComments) { const obj = t.tsMethodSignature(key, typeParameters, parameters, typeAnnotation); obj.kind = 'method'; if (trailingComments && trailingComments.length) { obj.trailingComments = trailingComments; } if (leadingComments && leadingComments.length) { obj.leadingComments = leadingComments; } return obj; } exports.tsMethodSignature = tsMethodSignature; const classMethod = (kind, key, params, body, returnType, leadingComments = [], computed = false, _static = false, generator = false, async = false) => { const obj = t.classMethod(kind, key, params, body, computed, _static, generator, async); if (returnType) { obj.returnType = returnType; } if (leadingComments) { obj.leadingComments = leadingComments; } return obj; }; exports.classMethod = classMethod; const tsEnumMember = (id, initializer, leadingComments) => { const obj = t.tsEnumMember(id, initializer); obj.leadingComments = leadingComments; return obj; }; exports.tsEnumMember = tsEnumMember; const tsPropertySignature = (key, typeAnnotation, optional) => { const obj = t.tsPropertySignature(key, typeAnnotation); obj.optional = optional; return obj; }; exports.tsPropertySignature = tsPropertySignature; const functionDeclaration = (id, params, body, generator, async, returnType) => { const func = t.functionDeclaration(id, params, body, generator, async); func.returnType = returnType; return func; }; exports.functionDeclaration = functionDeclaration; const callExpression = (callee, _arguments, typeParameters) => { const callExpr = t.callExpression(callee, _arguments); callExpr.typeParameters = typeParameters; return callExpr; }; exports.callExpression = callExpression; const identifier = (name, typeAnnotation, optional = false) => { const type = t.identifier(name); type.typeAnnotation = typeAnnotation; type.optional = optional; return type; }; exports.identifier = identifier; const classDeclaration = (id, superClass = null, body, decorators = null, vImplements, superTypeParameters) => { const obj = t.classDeclaration(id, superClass, body, decorators); if (superTypeParameters) { obj.superTypeParameters = superTypeParameters; } if (vImplements) { obj.implements = vImplements; } return obj; }; exports.classDeclaration = classDeclaration; function classProperty(key, value = null, typeAnnotation = null, decorators = null, computed = false, _static = false, _readonly = false, accessibility, leadingComments = []) { const obj = t.classProperty(key, value, typeAnnotation, decorators, computed, _static); if (accessibility) obj.accessibility = accessibility; if (_readonly) obj.readonly = _readonly; if (leadingComments.length) obj.leadingComments = leadingComments; return obj; } exports.classProperty = classProperty; ; const arrowFunctionExpression = (params, body, returnType, isAsync = false, typeParameters) => { const func = t.arrowFunctionExpression(params, body, isAsync); func.returnType = returnType; func.typeParameters = typeParameters; return func; }; exports.arrowFunctionExpression = arrowFunctionExpression; const tsTypeParameterDeclaration = (params) => { const obj = t.tsTypeParameterDeclaration(params); delete obj.extra; return obj; }; exports.tsTypeParameterDeclaration = tsTypeParameterDeclaration; const objectPattern = (properties, typeAnnotation) => { const obj = t.objectPattern(properties); obj.typeAnnotation = typeAnnotation; return obj; }; exports.objectPattern = objectPattern; const objectMethod = (kind, key, params, body, computed, generator, async, returnType, typeParameters) => { const obj = t.objectMethod(kind, key, params, body, computed, generator, async); obj.returnType = returnType; obj.typeParameters = typeParameters; return obj; }; exports.objectMethod = objectMethod; const objectProperty = (key, value, computed, shorthand, decorators, leadingComments = []) => { const obj = t.objectProperty(key, value, computed, shorthand, decorators); if (leadingComments.length) obj.leadingComments = leadingComments; return obj; }; exports.objectProperty = objectProperty; const makeCommentLineWithBlocks = (comment) => { if (!comment) return []; // NOTE using blocks instead of lines here... // @ts-ignore return [(0, utils_1.makeCommentBlock)(comment)]; }; exports.makeCommentLineWithBlocks = makeCommentLineWithBlocks; const newExpression = (callee, _arguments, typeParameters) => { const expr = t.newExpression(callee, _arguments); expr.typeParameters = typeParameters; return expr; }; exports.newExpression = newExpression;