Skip to content

Commit 6f9543b

Browse files
authored
Merge pull request #775 from BitGo/DX-433-fix-span-parsing
feat: fix module span parsing to parse comments for openapi spec
2 parents 35ee662 + e66f5dd commit 6f9543b

File tree

2 files changed

+17
-14
lines changed

2 files changed

+17
-14
lines changed

packages/openapi-generator/src/sourceFile.ts

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,18 +19,21 @@ export async function parseSource(
1919
src: string,
2020
): Promise<SourceFile | undefined> {
2121
try {
22-
const module = await swc.parse(src, {
22+
// Parse an empty string to get the last span
23+
const lastSpan = swc.parseSync('');
24+
25+
const module = swc.parseSync(src, {
2326
syntax: 'typescript',
2427
target: 'esnext',
28+
comments: true,
2529
});
26-
if (lastSpanEnd === -1) {
27-
// Since the starting offset is seemingly arbitrary, simulate it by subtracting the length of the source file
28-
// from the end of the first module. This probably doesn't matter since the first source file parsed will be
29-
// the apiSpec file.
30-
lastSpanEnd = module.span.end - src.length;
31-
}
30+
31+
// Set the start of the module to the end of the last span, so that we don't have any
32+
// issues when parsing files that start with comments
33+
module.span.start = lastSpan.span.start;
34+
lastSpanEnd = lastSpan.span.end;
35+
3236
const symbols = parseTopLevelSymbols(src, lastSpanEnd, module.body);
33-
lastSpanEnd = module.span.end;
3437
return {
3538
path,
3639
src,

packages/openapi-generator/test/codec.test.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -638,8 +638,8 @@ testCase('first property comment is parsed', FIRST_PROPERTY_COMMENT, {
638638
problems: [],
639639
source: [
640640
{
641-
number: 1,
642-
source: ' /** this is a comment */',
641+
number: 0,
642+
source: '/** this is a comment */',
643643
tokens: {
644644
delimiter: '/**',
645645
description: 'this is a comment ',
@@ -650,7 +650,7 @@ testCase('first property comment is parsed', FIRST_PROPERTY_COMMENT, {
650650
postName: '',
651651
postTag: '',
652652
postType: '',
653-
start: ' ',
653+
start: '',
654654
tag: '',
655655
type: '',
656656
},
@@ -685,8 +685,8 @@ testCase('second property comment is parsed', SECOND_PROPERTY_COMMENT, {
685685
problems: [],
686686
source: [
687687
{
688-
number: 1,
689-
source: ' /** this is a comment */',
688+
number: 0,
689+
source: '/** this is a comment */',
690690
tokens: {
691691
delimiter: '/**',
692692
description: 'this is a comment ',
@@ -697,7 +697,7 @@ testCase('second property comment is parsed', SECOND_PROPERTY_COMMENT, {
697697
postName: '',
698698
postTag: '',
699699
postType: '',
700-
start: ' ',
700+
start: '',
701701
tag: '',
702702
type: '',
703703
},

0 commit comments

Comments
 (0)