Enteee / plantuml-parser

Parse PlantUML with JavaScript or TypeScript
https://duckpond.ch/category/plantuml-parser
Apache License 2.0
138 stars 33 forks source link

Is there any type guards? #92

Open hache9669 opened 1 year ago

hache9669 commented 1 year ago

Is your feature request related to a problem? Please describe. I imported plantuml-parser in my Next.js app, and I need some of type guards. Now I just written a piece of type guards(only for what I need), and they are in my app project.

eg. When I render UML in browser,

Describe the solution you'd like

  1. As far as I have been able to find in the repository, it does not appear that type guards are provided. If I am mistaken and type guards already exists in the project, please let me know.

  2. May I create a pull request to add the type guard I coded? It might help other users. (although it may not be necessary) Currently I have only created type guards for a few classes I need, but I consider that I can create type guards for all types defined in types.d.ts.

Here is an example of what I have created;

const isMethod = (maybeMethod: Object): maybeMethod is Method => {
  return (
    hasPrimitive(maybeMethod, 'name', 'string') &&
    hasPrimitive(maybeMethod, 'isStatic', 'boolean') &&
    hasStringLiteral(maybeMethod, 'accessor', isAccessor) &&
    hasPrimitive(maybeMethod, 'returnType', 'string') &&
    hasPrimitive(maybeMethod, '_arguments', 'string')
  );
};

const isMemberVariable = (maybeMemberVariable: Object): maybeMemberVariable is MemberVariable => {
  return (
    hasPrimitive(maybeMemberVariable, 'name', 'string') &&
    hasPrimitive(maybeMemberVariable, 'isStatic', 'boolean') &&
    hasStringLiteral(maybeMemberVariable, 'accessor', isAccessor) &&
    hasPrimitive(maybeMemberVariable, 'type', 'string')
  );
};

const isMember = (maybeMember: Object): maybeMember is Member => {
  return isMethod(maybeMember) || isMemberVariable(maybeMember);
};

// how to use
const renderClass = (c: Class) => {
  const variables = c.members.filter(member => isMemberVariable(member));
  const methods = c.members.filter(member => isMethod(member));

  return <div className="container">
    <h1>{c.title}</h1>
    {variables.length > 0 &&<div className="properties">
      <h2>properties</h2>
      <ul>{variables.map(v => <li>{v.name}</li>)}</ul>
    </div>}
    {methods.length > 0 &&<div className="methods">
      <h2>methods</h2>
      <ul>{methods.map(m => <li>{m.name}</li>)}</ul>
    </div>}
  </div>
}
Enteee commented 1 year ago

Hello @hache9669 , and thank you for this issue. You are right. There are no such type guards provided for any types in this project.

If you have a set of helpful typeguards and are willing to extend those to cover all types in this project. I am more than happy to review and merge a pull request for this :+1: . I think the only point to discuss would be how we can easily integrate testing for those guards.