Skip to content

Commit 49364b0

Browse files
TrottMylesBorins
authored andcommitted
tools: update JSON header parsing for backticks
Methods, events, and so on in headers in our documentation may (and should) be set off with backticks in the raw markdown. When that happens, the headers is misinterpreted by tools/json.js as not being a method or event. Update the JSON tool generator to accommodate backticks in this situation and add a test for this situation. Fixes: #31290 PR-URL: #31294 Reviewed-By: Richard Lau <riclau@uk.ibm.com> Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
1 parent 2285936 commit 49364b0

File tree

3 files changed

+92
-7
lines changed

3 files changed

+92
-7
lines changed

test/doctool/test-doctool-json.js

+68
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,74 @@ const testData = [
157157
}
158158
]
159159
}
160+
},
161+
{
162+
file: fixtures.path('doc_with_backticks_in_headings.md'),
163+
json: {
164+
type: 'module',
165+
source: 'foo',
166+
modules: [
167+
{
168+
textRaw: 'Fhqwhgads',
169+
name: 'fhqwhgads',
170+
properties: [
171+
{
172+
name: 'fullName',
173+
textRaw: '`Fqhqwhgads.fullName`'
174+
}
175+
],
176+
classMethods: [
177+
{
178+
name: 'again',
179+
signatures: [
180+
{
181+
params: []
182+
}
183+
],
184+
textRaw: 'Class Method: `Fhqwhgads.again()`',
185+
type: 'classMethod'
186+
}
187+
],
188+
classes: [
189+
{
190+
textRaw: 'Class: `ComeOn`',
191+
type: 'class',
192+
name: 'ComeOn'
193+
}
194+
],
195+
ctors: [
196+
{
197+
name: 'Fhqwhgads',
198+
signatures: [
199+
{
200+
params: []
201+
}
202+
],
203+
textRaw: 'Constructor: `new Fhqwhgads()`',
204+
type: 'ctor'
205+
}
206+
],
207+
methods: [
208+
{
209+
textRaw: '`everybody.to(limit)`',
210+
type: 'method',
211+
name: 'to',
212+
signatures: [{ params: [] }]
213+
}
214+
],
215+
events: [
216+
{
217+
textRaw: "Event: `'FHQWHfest'`",
218+
type: 'event',
219+
name: 'FHQWHfest',
220+
params: []
221+
}
222+
],
223+
type: 'module',
224+
displayName: 'Fhqwhgads'
225+
}
226+
]
227+
}
160228
}
161229
];
162230

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# Fhqwhgads
2+
3+
## Class: `ComeOn`
4+
5+
## `everybody.to(limit)`
6+
7+
## Event: `'FHQWHfest'`
8+
9+
## Constructor: `new Fhqwhgads()`
10+
11+
## Class Method: `Fhqwhgads.again()`
12+
13+
## `Fqhqwhgads.fullName`

tools/doc/json.js

+11-7
Original file line numberDiff line numberDiff line change
@@ -435,13 +435,15 @@ const r = String.raw;
435435

436436
const eventPrefix = '^Event: +';
437437
const classPrefix = '^[Cc]lass: +';
438-
const ctorPrefix = '^(?:[Cc]onstructor: +)?new +';
438+
const ctorPrefix = '^(?:[Cc]onstructor: +)?`?new +';
439439
const classMethodPrefix = '^Class Method: +';
440440
const maybeClassPropertyPrefix = '(?:Class Property: +)?';
441441

442442
const maybeQuote = '[\'"]?';
443443
const notQuotes = '[^\'"]+';
444444

445+
const maybeBacktick = '`?';
446+
445447
// To include constructs like `readable\[Symbol.asyncIterator\]()`
446448
// or `readable.\_read(size)` (with Markdown escapes).
447449
const simpleId = r`(?:(?:\\?_)+|\b)\w+\b`;
@@ -458,25 +460,27 @@ const noCallOrProp = '(?![.[(])';
458460

459461
const maybeExtends = `(?: +extends +${maybeAncestors}${classId})?`;
460462

463+
/* eslint-disable max-len */
461464
const headingExpressions = [
462465
{ type: 'event', re: RegExp(
463-
`${eventPrefix}${maybeQuote}(${notQuotes})${maybeQuote}$`, 'i') },
466+
`${eventPrefix}${maybeBacktick}${maybeQuote}(${notQuotes})${maybeQuote}${maybeBacktick}$`, 'i') },
464467

465468
{ type: 'class', re: RegExp(
466-
`${classPrefix}(${maybeAncestors}${classId})${maybeExtends}$`, '') },
469+
`${classPrefix}${maybeBacktick}(${maybeAncestors}${classId})${maybeExtends}${maybeBacktick}$`, '') },
467470

468471
{ type: 'ctor', re: RegExp(
469-
`${ctorPrefix}(${maybeAncestors}${classId})${callWithParams}$`, '') },
472+
`${ctorPrefix}(${maybeAncestors}${classId})${callWithParams}${maybeBacktick}$`, '') },
470473

471474
{ type: 'classMethod', re: RegExp(
472-
`${classMethodPrefix}${maybeAncestors}(${id})${callWithParams}$`, 'i') },
475+
`${classMethodPrefix}${maybeBacktick}${maybeAncestors}(${id})${callWithParams}${maybeBacktick}$`, 'i') },
473476

474477
{ type: 'method', re: RegExp(
475-
`^${maybeAncestors}(${id})${callWithParams}$`, 'i') },
478+
`^${maybeBacktick}${maybeAncestors}(${id})${callWithParams}${maybeBacktick}$`, 'i') },
476479

477480
{ type: 'property', re: RegExp(
478-
`^${maybeClassPropertyPrefix}${ancestors}(${id})${noCallOrProp}$`, 'i') },
481+
`^${maybeClassPropertyPrefix}${maybeBacktick}${ancestors}(${id})${maybeBacktick}${noCallOrProp}$`, 'i') },
479482
];
483+
/* eslint-enable max-len */
480484

481485
function newSection(header, file) {
482486
const text = textJoin(header.children, file);

0 commit comments

Comments
 (0)