diff --git a/packages/openapi-generator/src/sourceFile.ts b/packages/openapi-generator/src/sourceFile.ts index 3b375c16..9a6c78d5 100644 --- a/packages/openapi-generator/src/sourceFile.ts +++ b/packages/openapi-generator/src/sourceFile.ts @@ -19,18 +19,21 @@ export async function parseSource( src: string, ): Promise { try { - const module = await swc.parse(src, { + // Parse an empty string to get the last span + const lastSpan = swc.parseSync(''); + + const module = swc.parseSync(src, { syntax: 'typescript', target: 'esnext', + comments: true, }); - if (lastSpanEnd === -1) { - // Since the starting offset is seemingly arbitrary, simulate it by subtracting the length of the source file - // from the end of the first module. This probably doesn't matter since the first source file parsed will be - // the apiSpec file. - lastSpanEnd = module.span.end - src.length; - } + + // Set the start of the module to the end of the last span, so that we don't have any + // issues when parsing files that start with comments + module.span.start = lastSpan.span.start; + lastSpanEnd = lastSpan.span.end; + const symbols = parseTopLevelSymbols(src, lastSpanEnd, module.body); - lastSpanEnd = module.span.end; return { path, src, diff --git a/packages/openapi-generator/test/codec.test.ts b/packages/openapi-generator/test/codec.test.ts index b492e380..cf690994 100644 --- a/packages/openapi-generator/test/codec.test.ts +++ b/packages/openapi-generator/test/codec.test.ts @@ -638,8 +638,8 @@ testCase('first property comment is parsed', FIRST_PROPERTY_COMMENT, { problems: [], source: [ { - number: 1, - source: ' /** this is a comment */', + number: 0, + source: '/** this is a comment */', tokens: { delimiter: '/**', description: 'this is a comment ', @@ -650,7 +650,7 @@ testCase('first property comment is parsed', FIRST_PROPERTY_COMMENT, { postName: '', postTag: '', postType: '', - start: ' ', + start: '', tag: '', type: '', }, @@ -685,8 +685,8 @@ testCase('second property comment is parsed', SECOND_PROPERTY_COMMENT, { problems: [], source: [ { - number: 1, - source: ' /** this is a comment */', + number: 0, + source: '/** this is a comment */', tokens: { delimiter: '/**', description: 'this is a comment ', @@ -697,7 +697,7 @@ testCase('second property comment is parsed', SECOND_PROPERTY_COMMENT, { postName: '', postTag: '', postType: '', - start: ' ', + start: '', tag: '', type: '', },