Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 11 additions & 8 deletions packages/openapi-generator/src/sourceFile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,18 +19,21 @@ export async function parseSource(
src: string,
): Promise<SourceFile | undefined> {
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,
Expand Down
12 changes: 6 additions & 6 deletions packages/openapi-generator/test/codec.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 ',
Expand All @@ -650,7 +650,7 @@ testCase('first property comment is parsed', FIRST_PROPERTY_COMMENT, {
postName: '',
postTag: '',
postType: '',
start: ' ',
start: '',
tag: '',
type: '',
},
Expand Down Expand Up @@ -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 ',
Expand All @@ -697,7 +697,7 @@ testCase('second property comment is parsed', SECOND_PROPERTY_COMMENT, {
postName: '',
postTag: '',
postType: '',
start: ' ',
start: '',
tag: '',
type: '',
},
Expand Down