Skip to content

Commit

Permalink
tools: improve heading type detection in json.js
Browse files Browse the repository at this point in the history
PR-URL: #20074
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
  • Loading branch information
vsemozhetbyt committed Apr 22, 2018
1 parent 647954d commit 7fcb52c
Showing 1 changed file with 47 additions and 8 deletions.
55 changes: 47 additions & 8 deletions tools/doc/json.js
Original file line number Diff line number Diff line change
Expand Up @@ -553,15 +553,54 @@ function cloneValue(src) {
}


// These parse out the contents of an H# tag.
// This section parse out the contents of an H# tag.

// To reduse escape slashes in RegExp string components.
const r = String.raw;

const eventPrefix = '^Event: +';
const classPrefix = '^[Cc]lass: +';
const ctorPrefix = '^(?:[Cc]onstructor: +)?new +';
const classMethodPrefix = '^Class Method: +';
const maybeClassPropertyPrefix = '(?:Class Property: +)?';

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

// To include constructs like `readable\[Symbol.asyncIterator\]()`
// or `readable.\_read(size)` (with Markdown escapes).
const simpleId = r`(?:(?:\\?_)+|\b)\w+\b`;
const computedId = r`\\?\[[\w\.]+\\?\]`;
const id = `(?:${simpleId}|${computedId})`;
const classId = r`[A-Z]\w+`;

const ancestors = r`(?:${id}\.?)+`;
const maybeAncestors = r`(?:${id}\.?)*`;

const callWithParams = r`\([^)]*\)`;

const noCallOrProp = '(?![.[(])';

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

const headingExpressions = [
{ type: 'event', re: /^Event(?::|\s)+['"]?([^"']+).*$/i },
{ type: 'class', re: /^Class:\s*([^ ]+).*$/i },
{ type: 'property', re: /^[^.[]+(\[[^\]]+\])\s*$/ },
{ type: 'property', re: /^[^.]+\.([^ .()]+)\s*$/ },
{ type: 'classMethod', re: /^class\s*method\s*:?[^.]+\.([^ .()]+)\([^)]*\)\s*$/i },
{ type: 'method', re: /^(?:[^.]+\.)?([^ .()]+)\([^)]*\)\s*$/ },
{ type: 'ctor', re: /^new ([A-Z][a-zA-Z]+)\([^)]*\)\s*$/ },
{ type: 'event', re: RegExp(
`${eventPrefix}${maybeQuote}(${notQuotes})${maybeQuote}$`, 'i') },

{ type: 'class', re: RegExp(
`${classPrefix}(${maybeAncestors}${classId})${maybeExtends}$`, '') },

{ type: 'ctor', re: RegExp(
`${ctorPrefix}(${maybeAncestors}${classId})${callWithParams}$`, '') },

{ type: 'classMethod', re: RegExp(
`${classMethodPrefix}${maybeAncestors}(${id})${callWithParams}$`, 'i') },

{ type: 'method', re: RegExp(
`^${maybeAncestors}(${id})${callWithParams}$`, 'i') },

{ type: 'property', re: RegExp(
`^${maybeClassPropertyPrefix}${ancestors}(${id})${noCallOrProp}$`, 'i') },
];

function newSection({ text }) {
Expand Down

0 comments on commit 7fcb52c

Please sign in to comment.