Skip to content

Commit 815da82

Browse files
committed
Fix for empty segments
1 parent ec62845 commit 815da82

File tree

3 files changed

+35
-3
lines changed

3 files changed

+35
-3
lines changed

lib/format-code-path.js

+2
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,8 @@ function formatCodePath(codePath, { segmentNames } = { })
4545
const lines = segment.internal.nodes || [];
4646
if (segmentNames)
4747
lines.unshift(`【${id}】`);
48+
else if (!lines.length)
49+
lines.push(id);
4850
const label = lines.join('\n');
4951
text += `state ${JSON.stringify(label)} as ${id}\n`;
5052
}

test/create-graph-map.js

+17-3
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@ const { strict: assert } = require('assert');
66
const { promises: { readFile } } = require('fs');
77
const { join } = require('path');
88

9-
async function test({ code, detail, expectedMappings })
9+
async function test({ code, detail, expectedMappings, sourceType })
1010
{
11-
const graphMap = createGraphMap(code, { detail });
11+
const graphMap = createGraphMap(code, { detail, sourceType });
1212
const actualKeys = [...graphMap.keys()];
1313
const expectedKeys = Object.keys(expectedMappings);
1414
assert.deepEqual(actualKeys, expectedKeys);
@@ -39,7 +39,7 @@ describe
3939
{
4040
foo();
4141
}
42-
bar();
42+
bar();
4343
`,
4444
expectedMappings: { 's1': 'graph-1' },
4545
},
@@ -87,6 +87,20 @@ describe
8787
),
8888
);
8989

90+
it
91+
(
92+
'conditional return statement with segments detail',
93+
async () =>
94+
await test
95+
(
96+
{
97+
code: 'if (foo) return;',
98+
sourceType: 'commonjs',
99+
expectedMappings: { 's1': 'graph-8' },
100+
},
101+
),
102+
);
103+
90104
it
91105
(
92106
'function declaration',

test/expected/graph-8.mermaid

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
stateDiagram-v2
2+
classDef common fill: white, stroke: black
3+
class s1_1, s1_2, s1_4, s1_3 common
4+
classDef unreachable fill: #FF9800, stroke-dasharray: 5 5
5+
class s1_3 unreachable
6+
state "Program:enter\nIfStatement:enter\nIdentifier (foo)" as s1_1
7+
state "ReturnStatement" as s1_2
8+
state "IfStatement:exit\nProgram:exit" as s1_4
9+
state "s1_3" as s1_3
10+
[*] --> s1_1
11+
s1_1 --> s1_2
12+
s1_1 --> s1_4
13+
s1_2 --> s1_3
14+
s1_3 --> s1_4
15+
s1_2 --> [*]
16+
s1_4 --> [*]

0 commit comments

Comments
 (0)