Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

tplant

Package Overview
Dependencies
Maintainers
1
Versions
31
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

tplant - npm Package Compare versions

Comparing version 2.1.4 to 2.1.5

146

dist/convertToPlant.js

@@ -10,13 +10,10 @@ "use strict";

Object.defineProperty(exports, "__esModule", { value: true });
var _a;
var os = __importStar(require("os"));
var ISerializeSymbol_1 = require("./ISerializeSymbol");
var TYPES = (_a = {},
_a[ISerializeSymbol_1.MODIFIER_TYPE.PRIVATE] = '-',
_a[ISerializeSymbol_1.MODIFIER_TYPE.PROTECTED] = '#',
_a[ISerializeSymbol_1.MODIFIER_TYPE.PUBLIC] = '+',
_a);
var Class_1 = require("./components/Class");
var ComponentKind_1 = require("./components/ComponentKind");
var Interface_1 = require("./components/Interface");
var Method_1 = require("./components/Method");
var COMPOSITION_LINE = '*--';
var REGEX_ONLY_TYPE_NAMES = /\w+/g;
function convertToPlant(tsjs, options) {
function convertToPlant(files, options) {
if (options === void 0) { options = {

@@ -28,93 +25,66 @@ compositions: false,

var compositions = [];
var listOfSerializeSymbols = tsjs;
if (options.onlyInterfaces) {
listOfSerializeSymbols = listOfSerializeSymbols.filter(function (serializedSymbol) { return serializedSymbol.structure === ISerializeSymbol_1.STRUCTURE.INTERFACE; });
for (var _i = 0, files_1 = files; _i < files_1.length; _i++) {
var file = files_1[_i];
if (file.componentKind === ComponentKind_1.ComponentKind.FILE) {
file.parts = file.parts
.filter(function (part) { return part.componentKind === ComponentKind_1.ComponentKind.INTERFACE; });
}
}
}
lines.push('@startuml');
listOfSerializeSymbols.forEach(function (serializedSymbol) {
var keyword = '';
if (serializedSymbol.keyword !== undefined) {
keyword = serializedSymbol.keyword + " ";
files.forEach(function (file) {
var conversion = file.toPUML();
if (conversion !== '') {
lines.push(conversion);
}
var heritage = '';
if (serializedSymbol.extends !== undefined) {
heritage += " extends " + serializedSymbol.extends;
}
var serializedClass = serializedSymbol;
if (serializedClass.implements !== undefined) {
heritage += " implements " + serializedClass.implements.join(', ');
}
var parameters = '';
if (serializedClass.parameters !== undefined && serializedClass.parameters.length > 0) {
parameters = "<" + serializedClass.parameters.map(function (parameter) {
if (parameter.constraint === undefined) {
return parameter.name;
}
return parameter.name + " extends " + parameter.constraint;
})
.join(', ') + ">";
}
var openingBrace = '';
if (serializedSymbol.members.length > 0) {
openingBrace = ' {';
}
lines.push("" + keyword + serializedSymbol.structure + " " + serializedSymbol.name + parameters + heritage + openingBrace);
serializedSymbol.members.forEach(function (serializedMember) {
checkCompositions(serializedMember, serializedSymbol.name);
lines.push(memberToString(serializedMember, serializedSymbol.name));
});
if (serializedSymbol.members.length > 0) {
lines.push('}');
}
});
if (compositions.length > 0 && options.compositions) {
var uniqueCompositions = compositions.filter(function (value, index, array) { return array.indexOf(value) === index; });
uniqueCompositions.forEach(function (composition) { return lines.push(composition); });
}
lines.push('@enduml');
return lines.join(os.EOL);
function memberToString(member, parentName) {
var line = ' ';
if (member.type === ISerializeSymbol_1.MEMBER_TYPE.ENUM) {
return "" + line + member.name;
}
line += TYPES[member.modifierType];
if (member.keyword !== undefined) {
line += "{" + member.keyword + "} ";
}
line += "" + member.name + (member.questionToken !== undefined && member.questionToken ? '?' : '');
if (member.type === ISerializeSymbol_1.MEMBER_TYPE.METHOD) {
line += "(" + member.parameters.map(function (parameter) {
return "" + parameter.name + (parameter.questionToken !== undefined && parameter.questionToken ? '?' : '') + ": " + parameter.type;
})
.join(', ') + ")";
}
if (member.returnType !== undefined) {
line += ": " + member.returnType;
}
return line;
}
function checkCompositions(member, parentName) {
listOfSerializeSymbols.forEach(function (serializedSymbolToSearch) {
if (parentName === serializedSymbolToSearch.name) {
return;
}
var memberTypes = [];
var onlyReturnTypeNames = member.returnType.match(REGEX_ONLY_TYPE_NAMES);
if (onlyReturnTypeNames !== null) {
memberTypes.push.apply(memberTypes, onlyReturnTypeNames);
}
member.parameters.forEach(function (parameter) {
var onlyTypeNames = parameter.type.match(REGEX_ONLY_TYPE_NAMES);
if (onlyTypeNames !== null) {
memberTypes.push.apply(memberTypes, onlyTypeNames);
if (options.compositions) {
var mappedTypes_1 = {};
var outputConstraints_1 = {};
files.forEach(function (file) {
file.parts.forEach(function (part) {
if (part.componentKind === ComponentKind_1.ComponentKind.CLASS ||
part.componentKind === ComponentKind_1.ComponentKind.INTERFACE ||
part.componentKind === ComponentKind_1.ComponentKind.ENUM) {
mappedTypes_1[part.name] = true;
}
});
if (memberTypes.indexOf(serializedSymbolToSearch.name) < 0) {
return;
});
files.forEach(function (file) {
if (file.componentKind === ComponentKind_1.ComponentKind.FILE) {
file.parts.forEach(function (part) {
if (part instanceof Class_1.Class || part instanceof Interface_1.Interface) {
part.members.forEach(function (member) {
var checks = [];
if (member instanceof Method_1.Method) {
member.parameters.forEach(function (p) {
var parameters = p.type.match(REGEX_ONLY_TYPE_NAMES);
if (parameters !== null) {
checks = checks.concat(parameters);
}
});
}
var returnTypes = member.returnType.match(REGEX_ONLY_TYPE_NAMES);
if (returnTypes !== null) {
checks = checks.concat(returnTypes);
}
for (var _i = 0, checks_1 = checks; _i < checks_1.length; _i++) {
var allTypeName = checks_1[_i];
var key = part.name + " " + COMPOSITION_LINE + " " + allTypeName;
if (allTypeName !== part.name &&
!outputConstraints_1.hasOwnProperty(key) && mappedTypes_1.hasOwnProperty(allTypeName)) {
lines.push(key);
outputConstraints_1[key] = true;
}
}
});
}
});
}
compositions.push(parentName + " " + COMPOSITION_LINE + " " + serializedSymbolToSearch.name);
});
}
lines.push('@enduml');
return lines.join(os.EOL);
}
exports.convertToPlant = convertToPlant;
"use strict";
var __assign = (this && this.__assign) || function () {
__assign = Object.assign || function(t) {
for (var s, i = 1, n = arguments.length; i < n; i++) {
s = arguments[i];
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
t[p] = s[p];
}
return t;
};
return __assign.apply(this, arguments);
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (Object.hasOwnProperty.call(mod, k)) result[k] = mod[k];
result["default"] = mod;
return result;
};
Object.defineProperty(exports, "__esModule", { value: true });
var typescript_1 = __importDefault(require("typescript"));
var ISerializeSymbol_1 = require("./ISerializeSymbol");
var FileFactory = __importStar(require("./components/FileFactory"));
function generateDocumentation(fileNames, options) {

@@ -23,267 +19,14 @@ if (options === void 0) { options = typescript_1.default.getDefaultCompilerOptions(); }

var checker = program.getTypeChecker();
var output = [];
var result = [];
program.getSourceFiles()
.forEach(function (sourceFile) {
if (!sourceFile.isDeclarationFile) {
typescript_1.default.forEachChild(sourceFile, visit);
var file = FileFactory.create(sourceFile, checker);
if (file !== undefined) {
result.push(file);
}
}
});
return output;
function visit(node) {
if (!isNodeExported(node)) {
return;
}
if (node.kind === typescript_1.default.SyntaxKind.ClassDeclaration) {
var currentNode = node;
if (currentNode.name === undefined) {
return;
}
var symbol = checker.getSymbolAtLocation(currentNode.name);
if (symbol === undefined) {
return;
}
output.push(serializeClass(symbol));
return;
}
if (node.kind === typescript_1.default.SyntaxKind.InterfaceDeclaration) {
var currentNode = node;
var symbol = checker.getSymbolAtLocation(currentNode.name);
if (symbol === undefined) {
return;
}
output.push(serializeInterface(symbol));
return;
}
if (node.kind === typescript_1.default.SyntaxKind.ModuleDeclaration) {
typescript_1.default.forEachChild(node, visit);
return;
}
if (node.kind === typescript_1.default.SyntaxKind.EnumDeclaration) {
var currentNode = node;
var symbol = checker.getSymbolAtLocation(currentNode.name);
if (symbol === undefined) {
return;
}
output.push(serializeEnum(symbol));
return;
}
}
function serializeSymbol(symbol) {
var declarations = symbol.getDeclarations();
var questionToken = false;
if (declarations !== undefined) {
questionToken = declarations.some(function (declaration) {
if (declaration.questionToken === undefined &&
declaration.initializer === undefined) {
return false;
}
return true;
});
}
return {
name: symbol.getName(),
type: checker.typeToString(checker.getTypeOfSymbolAtLocation(symbol, symbol.valueDeclaration)),
questionToken: questionToken
};
}
function serializeMember(memberSymbol, memberDeclaration) {
var result = {
keyword: getMemberKeyword(memberDeclaration),
modifierType: getMemberModifierType(memberDeclaration),
name: memberSymbol.getName(),
parameters: [],
returnType: getMemberReturnType(memberSymbol, memberDeclaration),
type: getMemberType(memberDeclaration),
questionToken: getQuestionToken(memberDeclaration),
constraint: getConstraint(memberDeclaration)
};
if (result.type === ISerializeSymbol_1.MEMBER_TYPE.CONSTRUCTOR) {
return;
}
if (result.type === ISerializeSymbol_1.MEMBER_TYPE.METHOD) {
var methodSignature = checker.getSignatureFromDeclaration(memberDeclaration);
if (methodSignature === undefined) {
return result;
}
return __assign({}, result, serializeSignature(methodSignature));
}
return result;
}
function getMemberReturnType(memberSymbol, memberDeclaration) {
var memberType = getMemberType(memberDeclaration);
if (memberType === ISerializeSymbol_1.MEMBER_TYPE.ENUM || memberType === ISerializeSymbol_1.MEMBER_TYPE.CONSTRUCTOR) {
return '';
}
return checker.typeToString(checker.getTypeOfSymbolAtLocation(memberSymbol, memberDeclaration));
}
function getQuestionToken(memberDeclaration) {
var propertyDeclaration = memberDeclaration;
return propertyDeclaration.questionToken !== undefined;
}
function getConstraint(memberDeclaration) {
var effectiveConstraint = typescript_1.default.getEffectiveConstraintOfTypeParameter(memberDeclaration);
if (effectiveConstraint === undefined) {
return;
}
return checker.typeToString(checker.getTypeFromTypeNode(effectiveConstraint));
}
function getMemberType(memberDeclaration) {
switch (memberDeclaration.kind) {
case typescript_1.default.SyntaxKind.PropertyDeclaration:
case typescript_1.default.SyntaxKind.PropertySignature:
case typescript_1.default.SyntaxKind.Parameter:
case typescript_1.default.SyntaxKind.GetAccessor:
case typescript_1.default.SyntaxKind.SetAccessor:
return ISerializeSymbol_1.MEMBER_TYPE.PROPERTY;
case typescript_1.default.SyntaxKind.MethodDeclaration:
case typescript_1.default.SyntaxKind.MethodSignature:
return ISerializeSymbol_1.MEMBER_TYPE.METHOD;
case typescript_1.default.SyntaxKind.Constructor:
case typescript_1.default.SyntaxKind.ConstructSignature:
return ISerializeSymbol_1.MEMBER_TYPE.CONSTRUCTOR;
case typescript_1.default.SyntaxKind.IndexSignature:
return ISerializeSymbol_1.MEMBER_TYPE.INDEX;
case typescript_1.default.SyntaxKind.TypeParameter:
return ISerializeSymbol_1.MEMBER_TYPE.PARAMETER;
case typescript_1.default.SyntaxKind.EnumMember:
case typescript_1.default.SyntaxKind.EnumDeclaration:
return ISerializeSymbol_1.MEMBER_TYPE.ENUM;
default:
throw new Error('unable to determine member type');
}
}
function getModifierType(modifiers) {
var modifierType = ISerializeSymbol_1.MODIFIER_TYPE.PUBLIC;
modifiers.some(function (modifier) {
if (modifier.kind === typescript_1.default.SyntaxKind.PrivateKeyword) {
modifierType = ISerializeSymbol_1.MODIFIER_TYPE.PRIVATE;
return true;
}
if (modifier.kind === typescript_1.default.SyntaxKind.PublicKeyword) {
modifierType = ISerializeSymbol_1.MODIFIER_TYPE.PUBLIC;
return true;
}
if (modifier.kind === typescript_1.default.SyntaxKind.ProtectedKeyword) {
modifierType = ISerializeSymbol_1.MODIFIER_TYPE.PROTECTED;
return true;
}
return false;
});
return modifierType;
}
function getMemberModifierType(memberDeclaration) {
var memberModifiers = memberDeclaration.modifiers;
if (memberModifiers === undefined ||
memberModifiers.length === 0) {
return ISerializeSymbol_1.MODIFIER_TYPE.PUBLIC;
}
return getModifierType(memberModifiers);
}
function getMemberKeyword(memberDeclaration) {
var memberKeyword;
var memberModifiers = memberDeclaration.modifiers;
if (memberModifiers === undefined ||
memberModifiers.length === 0) {
return memberKeyword;
}
memberModifiers.some(function (memberModifier) {
if (memberModifier.kind === typescript_1.default.SyntaxKind.AbstractKeyword) {
memberKeyword = ISerializeSymbol_1.CLASS_MEMBER_KEYWORD.ABSTRACT;
return true;
}
if (memberModifier.kind === typescript_1.default.SyntaxKind.StaticKeyword) {
memberKeyword = ISerializeSymbol_1.CLASS_MEMBER_KEYWORD.STATIC;
return true;
}
return false;
});
return memberKeyword;
}
function getExtendsHeritageClauseName(heritageClause) {
return heritageClause.types[0].expression.text;
}
function getInterfaceName(nodeObject) {
return nodeObject.expression.text;
}
function getImplementsHeritageClauseNames(heritageClause) {
return heritageClause.types.map(getInterfaceName);
}
function serializeEnum(enumSymbol) {
return __assign({}, serializeInterface(enumSymbol), { structure: ISerializeSymbol_1.STRUCTURE.ENUM });
}
function serializeInterface(interfaceSymbol) {
var serializedInterface = __assign({}, serializeSymbol(interfaceSymbol), { members: [], structure: ISerializeSymbol_1.STRUCTURE.INTERFACE });
if (interfaceSymbol.members !== undefined) {
interfaceSymbol.members.forEach(handleInterfaceMemberSymbols);
}
if (interfaceSymbol.exports !== undefined) {
interfaceSymbol.exports.forEach(handleInterfaceMemberSymbols);
}
if (interfaceSymbol.globalExports !== undefined) {
interfaceSymbol.globalExports.forEach(handleInterfaceMemberSymbols);
}
var interfaceDeclarations = interfaceSymbol.getDeclarations();
if (interfaceDeclarations === undefined) {
return serializedInterface;
}
interfaceDeclarations.forEach(function (interfaceDeclaration) {
var heritageClauses = interfaceDeclaration.heritageClauses;
if (heritageClauses === undefined) {
return;
}
heritageClauses.forEach(function (heritageClause) {
if (heritageClause.token === typescript_1.default.SyntaxKind.ExtendsKeyword) {
serializedInterface.extends = getExtendsHeritageClauseName(heritageClause);
return;
}
if (heritageClause.token === typescript_1.default.SyntaxKind.ImplementsKeyword) {
serializedInterface.implements = getImplementsHeritageClauseNames(heritageClause);
return;
}
throw new Error('unsupported heritage clause');
});
});
return serializedInterface;
function handleInterfaceMemberSymbols(memberSymbol) {
var memberDeclarations = memberSymbol.getDeclarations();
if (memberDeclarations === undefined) {
return;
}
memberDeclarations.forEach(function (memberDeclaration) {
var serializedMember = serializeMember(memberSymbol, memberDeclaration);
if (serializedMember === undefined) {
return;
}
if (serializedMember.type === ISerializeSymbol_1.MEMBER_TYPE.PARAMETER) {
if (serializedInterface.parameters === undefined) {
serializedInterface.parameters = [];
}
serializedInterface.parameters.push(serializedMember);
return;
}
serializedInterface.members.push(serializedMember);
});
}
}
function serializeClass(classSymbol) {
var classKeyword;
var classDeclaration = classSymbol.getDeclarations();
if (classDeclaration !== undefined && classDeclaration.length > 0) {
classKeyword = getMemberKeyword(classDeclaration[classDeclaration.length - 1]);
}
var constructorType = checker.getTypeOfSymbolAtLocation(classSymbol, classSymbol.valueDeclaration);
return __assign({}, serializeInterface(classSymbol), { structure: ISerializeSymbol_1.STRUCTURE.CLASS, keyword: classKeyword, constructors: constructorType.getConstructSignatures()
.map(serializeSignature) });
}
function serializeSignature(signature) {
return {
parameters: signature.parameters.map(serializeSymbol),
returnType: checker.typeToString(signature.getReturnType())
};
}
function isNodeExported(node) {
return (node.flags & typescript_1.default.ModifierFlags.Export) !== 0 ||
node.parent.kind === typescript_1.default.SyntaxKind.SourceFile;
}
return result;
}
exports.generateDocumentation = generateDocumentation;

@@ -16,3 +16,3 @@ #!/usr/bin/env node

commander_1.default
.version('2.1.4')
.version('2.1.5')
.usage('[options]')

@@ -19,0 +19,0 @@ .option('-i, --input <path>', 'Define the path of the Typescript file')

{
"name": "tplant",
"version": "2.1.4",
"version": "2.1.5",
"description": "Typescript to PlantUML",

@@ -39,3 +39,3 @@ "keywords": [

"prepublishOnly": "npm test && npm run lint",
"test": "npm run build && mocha"
"test": "npm run build && mocha --timeout 0"
},

@@ -42,0 +42,0 @@ "dependencies": {

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc