UNPKG

@rightcapital/phpdoc-parser

Version:

TypeScript version of PHPDoc parser with support for intersection types and generics

74 lines (73 loc) 4.48 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.createSourceFileRoot = createSourceFileRoot; exports.addStatementsToNode = addStatementsToNode; exports.createImportDeclarationNode = createImportDeclarationNode; exports.createExportDeclarationNode = createExportDeclarationNode; exports.createInterfaceNode = createInterfaceNode; exports.createCommentNode = createCommentNode; exports.createEnumNode = createEnumNode; exports.renderTsNodeToString = renderTsNodeToString; exports.renderTsSourceFileToString = renderTsSourceFileToString; const typescript_1 = require("typescript"); function createSourceFileRoot(fileName, sourceText = '', scriptTarget = typescript_1.ScriptTarget.Latest, scriptKind = typescript_1.ScriptKind.TS) { const sourceFile = (0, typescript_1.createSourceFile)(fileName, sourceText, scriptTarget, true, scriptKind); return sourceFile; } function addStatementsToNode(sourceFile, newStatements) { const updatedStatements = typescript_1.factory.createNodeArray([ ...sourceFile.statements, ...newStatements, ]); return typescript_1.factory.updateSourceFile(sourceFile, updatedStatements); } function createImportDeclarationNode(symbols, filePath) { const importClause = typescript_1.factory.createImportClause(true, undefined, typescript_1.factory.createNamedImports(symbols.map((symbol) => typescript_1.factory.createImportSpecifier(false, undefined, typescript_1.factory.createIdentifier(symbol))))); const moduleSpecifier = typescript_1.factory.createStringLiteral(filePath); const importDeclaration = typescript_1.factory.createImportDeclaration(undefined, importClause, moduleSpecifier); return importDeclaration; } function createExportDeclarationNode(symbols, moduleSpecifierString) { const exportSpecifiers = symbols.map((symbol) => typescript_1.factory.createExportSpecifier(false, undefined, typescript_1.factory.createIdentifier(symbol))); const namedExports = typescript_1.factory.createNamedExports(exportSpecifiers); const moduleSpecifier = moduleSpecifierString ? typescript_1.factory.createStringLiteral(moduleSpecifierString) : undefined; const exportDeclaration = typescript_1.factory.createExportDeclaration(undefined, false, namedExports, moduleSpecifier); return exportDeclaration; } function createInterfaceNode(interfaceName, members = [], typeParameters = [], heritageClauses = []) { const interfaceDeclaration = typescript_1.factory.createInterfaceDeclaration([typescript_1.factory.createModifier(typescript_1.SyntaxKind.ExportKeyword)], typescript_1.factory.createIdentifier(interfaceName), typeParameters, heritageClauses, members); return interfaceDeclaration; } function createCommentNode(commentText, isMultiLine = true, hasTrailingNewLine = true) { const emptyStatement = typescript_1.factory.createEmptyStatement(); let formattedCommentText; if (isMultiLine) { formattedCommentText = `*\n * ${commentText.split('\n').join('\n * ')}\n `; } else { formattedCommentText = commentText; } const emptyStatementWithComment = (0, typescript_1.addSyntheticLeadingComment)(emptyStatement, isMultiLine ? typescript_1.SyntaxKind.MultiLineCommentTrivia : typescript_1.SyntaxKind.SingleLineCommentTrivia, formattedCommentText, hasTrailingNewLine); return emptyStatementWithComment; } function createEnumNode(enumName, members, modifiers) { const memberNodes = members.map(([key, value]) => typescript_1.factory.createEnumMember(key, typeof value === 'number' ? typescript_1.factory.createNumericLiteral(value) : typescript_1.factory.createStringLiteral(value))); const enumDeclaration = typescript_1.factory.createEnumDeclaration(modifiers.map((modifier) => typescript_1.factory.createToken(modifier)), typescript_1.factory.createIdentifier(enumName), memberNodes); return enumDeclaration; } function renderTsNodeToString(tsNode) { const printer = (0, typescript_1.createPrinter)({ newLine: typescript_1.NewLineKind.LineFeed }); const resultFile = createSourceFileRoot('temp.ts'); return printer.printNode(typescript_1.EmitHint.Unspecified, tsNode, resultFile); } function renderTsSourceFileToString(sourceFile) { const printer = (0, typescript_1.createPrinter)({ newLine: typescript_1.NewLineKind.LineFeed }); const sourceFileContent = printer.printFile(sourceFile); return sourceFileContent; }