Skip to content
Closed
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
104 changes: 52 additions & 52 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions packages/openapi-generator/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
"test:target": "c8 --all --src src node --require @swc-node/register"
},
"dependencies": {
"@swc/core": "1.5.7",
"@swc/core": "1.10.6",
"cmd-ts": "0.13.0",
"comment-parser": "1.4.1",
"fp-ts": "2.16.9",
Expand All @@ -39,8 +39,8 @@
"typescript": "4.7.4"
},
"optionalDependencies": {
"@swc/core-linux-x64-gnu": "1.5.7",
"@swc/core-darwin-arm64": "1.5.7"
"@swc/core-linux-x64-gnu": "1.10.6",
"@swc/core-darwin-arm64": "1.10.6"
},
"publishConfig": {
"access": "public"
Expand Down
35 changes: 34 additions & 1 deletion packages/openapi-generator/src/comments.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,46 @@
import { parse as parseComment, Block } from 'comment-parser';
import { Schema } from './ir';

/**
* Convert a UTF-8 byte offset to a JavaScript string character offset.
* SWC (written in Rust) uses byte offsets, but JavaScript strings use
* UTF-16 code unit offsets. This function handles the conversion by
* iterating through the string and accumulating byte lengths.
*
* @param str The source string
* @param byteOffset The byte offset to convert
* @returns The corresponding character offset
*/
function byteOffsetToCharOffset(str: string, byteOffset: number): number {
let charCount = 0;
let byteCount = 0;

for (const char of str) {
const charBytes = Buffer.byteLength(char, 'utf8');
if (byteCount + charBytes > byteOffset) break;
byteCount += charBytes;
charCount++;
}

return charCount;
}

export function leadingComment(
src: string,
srcSpanStart: number,
start: number,
end: number,
): Block[] {
let commentString = src.slice(start - srcSpanStart, end - srcSpanStart).trim();
// SWC uses byte offsets, but JavaScript strings use character offsets.
// When there are multibyte UTF-8 characters (e.g., À, 日, 😀), we need to
// convert byte offsets to character offsets for correct string slicing.
const startByteOffset = start - srcSpanStart;
const endByteOffset = end - srcSpanStart;

const startCharOffset = byteOffsetToCharOffset(src, startByteOffset);
const endCharOffset = byteOffsetToCharOffset(src, endByteOffset);

let commentString = src.slice(startCharOffset, endCharOffset).trim();

if (commentString.includes(' * ') && !/\/\*\*([\s\S]*?)\*\//.test(commentString)) {
// The comment block seems to be JSDoc but was sliced incorrectly
Expand Down
Loading