Skip to content

Commit 1bdd3b0

Browse files
vsemozhetbytjasnell
authored andcommitted
tools: improve heading type detection in json.js
PR-URL: #20074 Reviewed-By: Luigi Pinca <[email protected]>
1 parent 4125a9f commit 1bdd3b0

File tree

1 file changed

+47
-8
lines changed

1 file changed

+47
-8
lines changed

tools/doc/json.js

+47-8
Original file line numberDiff line numberDiff line change
@@ -553,15 +553,54 @@ function cloneValue(src) {
553553
}
554554

555555

556-
// These parse out the contents of an H# tag.
556+
// This section parse out the contents of an H# tag.
557+
558+
// To reduse escape slashes in RegExp string components.
559+
const r = String.raw;
560+
561+
const eventPrefix = '^Event: +';
562+
const classPrefix = '^[Cc]lass: +';
563+
const ctorPrefix = '^(?:[Cc]onstructor: +)?new +';
564+
const classMethodPrefix = '^Class Method: +';
565+
const maybeClassPropertyPrefix = '(?:Class Property: +)?';
566+
567+
const maybeQuote = '[\'"]?';
568+
const notQuotes = '[^\'"]+';
569+
570+
// To include constructs like `readable\[Symbol.asyncIterator\]()`
571+
// or `readable.\_read(size)` (with Markdown escapes).
572+
const simpleId = r`(?:(?:\\?_)+|\b)\w+\b`;
573+
const computedId = r`\\?\[[\w\.]+\\?\]`;
574+
const id = `(?:${simpleId}|${computedId})`;
575+
const classId = r`[A-Z]\w+`;
576+
577+
const ancestors = r`(?:${id}\.?)+`;
578+
const maybeAncestors = r`(?:${id}\.?)*`;
579+
580+
const callWithParams = r`\([^)]*\)`;
581+
582+
const noCallOrProp = '(?![.[(])';
583+
584+
const maybeExtends = `(?: +extends +${maybeAncestors}${classId})?`;
585+
557586
const headingExpressions = [
558-
{ type: 'event', re: /^Event(?::|\s)+['"]?([^"']+).*$/i },
559-
{ type: 'class', re: /^Class:\s*([^ ]+).*$/i },
560-
{ type: 'property', re: /^[^.[]+(\[[^\]]+\])\s*$/ },
561-
{ type: 'property', re: /^[^.]+\.([^ .()]+)\s*$/ },
562-
{ type: 'classMethod', re: /^class\s*method\s*:?[^.]+\.([^ .()]+)\([^)]*\)\s*$/i },
563-
{ type: 'method', re: /^(?:[^.]+\.)?([^ .()]+)\([^)]*\)\s*$/ },
564-
{ type: 'ctor', re: /^new ([A-Z][a-zA-Z]+)\([^)]*\)\s*$/ },
587+
{ type: 'event', re: RegExp(
588+
`${eventPrefix}${maybeQuote}(${notQuotes})${maybeQuote}$`, 'i') },
589+
590+
{ type: 'class', re: RegExp(
591+
`${classPrefix}(${maybeAncestors}${classId})${maybeExtends}$`, '') },
592+
593+
{ type: 'ctor', re: RegExp(
594+
`${ctorPrefix}(${maybeAncestors}${classId})${callWithParams}$`, '') },
595+
596+
{ type: 'classMethod', re: RegExp(
597+
`${classMethodPrefix}${maybeAncestors}(${id})${callWithParams}$`, 'i') },
598+
599+
{ type: 'method', re: RegExp(
600+
`^${maybeAncestors}(${id})${callWithParams}$`, 'i') },
601+
602+
{ type: 'property', re: RegExp(
603+
`^${maybeClassPropertyPrefix}${ancestors}(${id})${noCallOrProp}$`, 'i') },
565604
];
566605

567606
function newSection({ text }) {

0 commit comments

Comments
 (0)