From 1598462014fedfad4c0bb81b86ce329ea15fc29f Mon Sep 17 00:00:00 2001 From: Aryaman Dhingra Date: Thu, 30 May 2024 14:51:48 -0400 Subject: [PATCH 1/2] feat: fix module span parsing to parse comments for openapi spec DX-433 --- packages/openapi-generator/src/sourceFile.ts | 16 ++++++++-------- packages/openapi-generator/test/codec.test.ts | 12 ++++++------ 2 files changed, 14 insertions(+), 14 deletions(-) diff --git a/packages/openapi-generator/src/sourceFile.ts b/packages/openapi-generator/src/sourceFile.ts index 3b375c16..f32b7b6f 100644 --- a/packages/openapi-generator/src/sourceFile.ts +++ b/packages/openapi-generator/src/sourceFile.ts @@ -19,18 +19,18 @@ export async function parseSource( src: string, ): Promise { try { - const module = await swc.parse(src, { + const lastSpan = swc.parseSync(''); + lastSpanEnd = lastSpan.span.end; + + 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; - } + + module.span.start = lastSpan.span.start; + 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: '', }, From e66f5ddf3664f9a5733df3d225615387fc2cccba Mon Sep 17 00:00:00 2001 From: Aryaman Dhingra Date: Thu, 30 May 2024 14:54:21 -0400 Subject: [PATCH 2/2] docs: add comments about why span changes work DX-433 --- packages/openapi-generator/src/sourceFile.ts | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/packages/openapi-generator/src/sourceFile.ts b/packages/openapi-generator/src/sourceFile.ts index f32b7b6f..9a6c78d5 100644 --- a/packages/openapi-generator/src/sourceFile.ts +++ b/packages/openapi-generator/src/sourceFile.ts @@ -19,8 +19,8 @@ export async function parseSource( src: string, ): Promise { try { + // Parse an empty string to get the last span const lastSpan = swc.parseSync(''); - lastSpanEnd = lastSpan.span.end; const module = swc.parseSync(src, { syntax: 'typescript', @@ -28,7 +28,10 @@ export async function parseSource( comments: true, }); + // 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); return {