Skip to content

Commit 2233b38

Browse files
committed
fix(gen): minor parser & type improvements & fixes
1 parent 86c657f commit 2233b38

File tree

14 files changed

+1325
-1370
lines changed

14 files changed

+1325
-1370
lines changed

ipld-schema.pegjs

Lines changed: 33 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,13 +44,42 @@
4444
while (pcl.length && !pcl[0].trim()) {
4545
pcl.shift()
4646
}
47-
let lastempty = pcl.findLastIndex((l) => /^\s*$/.test(l))
48-
if (lastempty !== -1) {
49-
pcl = pcl.slice(lastempty + 1)
47+
48+
// Check if there's a blank line followed by comments
49+
// If so, those comments should be linecomments for the previous field
50+
let firstCommentAfterBlank = -1
51+
let hasBlankLine = false
52+
for (let i = 0; i < pcl.length; i++) {
53+
if (/^\s*$/.test(pcl[i])) {
54+
hasBlankLine = true
55+
} else if (hasBlankLine && /^\s*#/.test(pcl[i])) {
56+
firstCommentAfterBlank = i
57+
break
58+
}
59+
}
60+
61+
if (firstCommentAfterBlank !== -1 && !linecomment) {
62+
// Move comments after blank line to linecomment
63+
const commentLines = pcl.slice(firstCommentAfterBlank)
64+
linecomment = commentLines.join('\n')
65+
pcl = pcl.slice(0, firstCommentAfterBlank)
66+
// Remove any trailing empty lines from pcl
67+
while (pcl.length && !pcl[pcl.length - 1].trim()) {
68+
pcl.pop()
69+
}
70+
} else {
71+
// Original behavior: only keep comments after the last empty line
72+
let lastempty = pcl.findLastIndex((l) => /^\s*$/.test(l))
73+
if (lastempty !== -1) {
74+
pcl = pcl.slice(lastempty + 1)
75+
}
5076
}
5177
// trim leading space and # on each line
5278
pcl = pcl.map((l) => l.replace(/^[ \t]*#[ \t]?/gm, ''))
53-
linecomment = linecomment ? flattenArray(linecomment).join('').replace(/^[ \t]*#[ \t]?/, '') : null
79+
if (linecomment && typeof linecomment !== 'string') {
80+
linecomment = flattenArray(linecomment).join('')
81+
}
82+
linecomment = linecomment ? linecomment.replace(/^[ \t]*#[ \t]?/gm, '') : null
5483
const comments = (pcl.length || linecomment) ? {} : null
5584
if (pcl.length) {
5685
comments.precomments = pcl.join('\n')

lib/gen/go.js

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -182,9 +182,7 @@ export function generateGo (schema, options = {}) {
182182
// Check for type override annotation
183183
/** @type { { [k in string]: string }[]} */
184184
let annotations = []
185-
// @ts-ignore - annotations exist in the parsed data even if not in type definition
186185
if (typeof typeDefn.enum.annotations === 'object' && typeof typeDefn.enum.annotations.type === 'object') {
187-
// @ts-ignore
188186
annotations = typeDefn.enum.annotations.type
189187
}
190188

@@ -271,9 +269,7 @@ export function generateGo (schema, options = {}) {
271269
// Check for type-level annotations
272270
/** @type { { [k in string]: string }[]} */
273271
let annotations = []
274-
// @ts-ignore - annotations exist in the parsed data even if not in type definition
275272
if (typeof typeDefn.copy.annotations === 'object' && typeof typeDefn.copy.annotations.type === 'object') {
276-
// @ts-ignore
277273
annotations = typeDefn.copy.annotations.type
278274
}
279275

lib/gen/rust.js

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -272,9 +272,7 @@ export function generateRust (schema) {
272272
// Check for type override annotation
273273
/** @type { { [k in string]: string }[]} */
274274
let annotations = []
275-
// @ts-ignore - annotations exist in the parsed data even if not in type definition
276275
if (typeof typeDefn.enum.annotations === 'object' && typeof typeDefn.enum.annotations.type === 'object') {
277-
// @ts-ignore
278276
annotations = typeDefn.enum.annotations.type
279277
}
280278

@@ -377,9 +375,7 @@ export function generateRust (schema) {
377375
/** @type { { [k in string]: string }[]} */
378376
let annotations = []
379377
if (['Bool', 'Int', 'Float', 'String', 'Bytes', 'Link'].includes(fromType)) {
380-
// @ts-ignore - annotations exist in the parsed data even if not in type definition
381378
if (typeof typeDefn.copy.annotations === 'object' && typeof typeDefn.copy.annotations.type === 'object') {
382-
// @ts-ignore
383379
// For copy types, only use @unsigned annotation, ignore @rusttype
384380
annotations = typeDefn.copy.annotations.type.filter(a => !('rusttype' in a))
385381
}

0 commit comments

Comments
 (0)