demike / TsUML2

Generates UML diagrams from TypeScript source code
MIT License
250 stars 33 forks source link

Is it possible to model Typescript `type`s not just interface and Classes #12

Closed rpocklin closed 1 year ago

rpocklin commented 2 years ago

I am using types instead of interfaces and classes eg.

type Something = {
   name: string
   value: number
   user: User
}```

with many types containing other types (eg. a hierarchy similar to the demo) but when running tsuml I get the following (as though it can't parse it, but it is valid Typescript):

render to svg Error: Parse error on line 3:

^ Expecting 'IDENT', '[', got 'EOF' Error: Parse error on line 3:

^ Expecting 'IDENT', '[', got 'EOF' at Parser.parseError (/Users/.../node_modules/nomnoml/dist/nomnoml.js:806:25) at Parser.parse (/Users/.../node_modules/nomnoml/dist/nomnoml.js:867:26) at intermediateParse (/Users/.../node_modules/nomnoml/dist/nomnoml.js:1392:34) at parse (/Users/.../node_modules/nomnoml/dist/nomnoml.js:1312:25) at parseAndRender (/Users/.../node_modules/nomnoml/dist/nomnoml.js:2159:29) at Object.renderSvg (/Users/.../node_modules/nomnoml/dist/nomnoml.js:2175:34) at renderNomnomlSVG (/Users/.../node_modules/tsuml2/dist/core/io.js:30:20) at createNomnomlSVG (/Users/.../node_modules/tsuml2/dist/core/index.js:93:41) at /Users/.../node_modules/tsuml2/dist/bin/index.js:18:41 at Object. (/Users/.../node_modules/tsuml2/dist/bin/index.js:24:3) { hash: { text: '', token: 'EOF', line: 2, loc: { first_line: 1, last_line: 3, first_column: 0, last_column: 0 }, expected: [ "'IDENT'", "'['" ] } }```

demike commented 2 years ago

I can take a look. Of course it totally makes sense to support types (similarily to interfaces)

demike commented 2 years ago

@rpocklin I implemented type parsing. But I want to be shure the above error is gone. Can you send me a minimal reproduction of your problem?

rpocklin commented 2 years ago

Sure, use the file test.ts with the contents:

export type X =
  | "a"
  | "b"

Then I run it with node ./node_modules/tsuml2/dist/bin/index.js -g ./test.ts

and it gives:

parsing source files:
/Users/.../test.ts

emitting declarations:
/Users/.../test.ts

render to svg
Error: Parse error on line 3:

^
Expecting 'IDENT', '[', got 'EOF' Error: Parse error on line 3:

^
Expecting 'IDENT', '[', got 'EOF'
demike commented 2 years ago

That's what I call a minimal reproduction 👍 In this case the problem was that not a single entity could be emitted and therefore nomnoml complained about that.

Now an error message will be shown in such a case.

By the way

export type X =
  | "a"
  | "b"

will not produce an entity now (open for suggestions). For now only types like

export type T = {
  a: number;
  doSomething(): void;
}

will produce output.

Can you give 0.5.1 a try

rpocklin commented 2 years ago

Yep, that works, in terms of generating the separate boxes, one for each object type, nice! Is it possible to have types link to dependent types in the SVG eg.

export type X = {
  a: string;
  b: number;
}

export type Y = {
a: X;
c: number;
demike commented 2 years ago

Adding this functionality is on my list. I have to investigate the possible relations: https://d3n817fwly711g.cloudfront.net/blog/wp-content/uploads/2012/03/Class-Diagram-Relationships.png Multiplicity ... (open for suggestions)

demike commented 2 years ago

@rpocklin member associations can be rendered now in v0.6.1 If the member is an array "0..*" is added in addition to the assocation line. Would be nice if you can try it out use the -m command line parameter

And thanks for the constructive comments

demike commented 1 year ago

Implemented v0.6.1