From c536cde10004563d5931bd31b872656015f89035 Mon Sep 17 00:00:00 2001 From: Vse Mozhet Byt Date: Sat, 14 Apr 2018 12:45:06 +0300 Subject: [PATCH 1/2] tools: improve heading type detection in json.js --- tools/doc/json.js | 55 ++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 47 insertions(+), 8 deletions(-) diff --git a/tools/doc/json.js b/tools/doc/json.js index b1a04c69380dda..ac611534b548ae 100644 --- a/tools/doc/json.js +++ b/tools/doc/json.js @@ -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 = r`^Event:\s+`; +const classPrefix = r`^[Cc]lass:\s+`; +const ctorPrefix = r`^(?:[Cc]onstructor:\s+)?new\s+`; +const classMethodPrefix = r`^Class Method:\s+`; +const maybeClassPropertyPrefix = r`(?:Class Property:\s+)?`; + +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 = r`(?:\s+extends\s+${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 }) { From c6c9709f1108993ff876a39d3e3066b39b12b0a1 Mon Sep 17 00:00:00 2001 From: Vse Mozhet Byt Date: Sun, 22 Apr 2018 14:08:18 +0300 Subject: [PATCH 2/2] fixup: replace `\s` with ` ` for strictness --- tools/doc/json.js | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/tools/doc/json.js b/tools/doc/json.js index ac611534b548ae..44b67e3e284b17 100644 --- a/tools/doc/json.js +++ b/tools/doc/json.js @@ -558,11 +558,11 @@ function cloneValue(src) { // To reduse escape slashes in RegExp string components. const r = String.raw; -const eventPrefix = r`^Event:\s+`; -const classPrefix = r`^[Cc]lass:\s+`; -const ctorPrefix = r`^(?:[Cc]onstructor:\s+)?new\s+`; -const classMethodPrefix = r`^Class Method:\s+`; -const maybeClassPropertyPrefix = r`(?:Class Property:\s+)?`; +const eventPrefix = '^Event: +'; +const classPrefix = '^[Cc]lass: +'; +const ctorPrefix = '^(?:[Cc]onstructor: +)?new +'; +const classMethodPrefix = '^Class Method: +'; +const maybeClassPropertyPrefix = '(?:Class Property: +)?'; const maybeQuote = '[\'"]?'; const notQuotes = '[^\'"]+'; @@ -581,7 +581,7 @@ const callWithParams = r`\([^)]*\)`; const noCallOrProp = '(?![.[(])'; -const maybeExtends = r`(?:\s+extends\s+${maybeAncestors}${classId})?`; +const maybeExtends = `(?: +extends +${maybeAncestors}${classId})?`; const headingExpressions = [ { type: 'event', re: RegExp(