Skip to content

Commit 7043e95

Browse files
vsemozhetbyttargos
authored andcommitted
tools: dry utility function in tools/doc/json.js
Also, move a declaration of unrelated variable closer to its only context. PR-URL: #19692 Reviewed-By: Ruben Bridgewater <[email protected]> Reviewed-By: Tobias Nießen <[email protected]>
1 parent 140611b commit 7043e95

File tree

1 file changed

+19
-36
lines changed

1 file changed

+19
-36
lines changed

tools/doc/json.js

+19-36
Original file line numberDiff line numberDiff line change
@@ -323,6 +323,8 @@ function processList(section) {
323323
delete section.list;
324324
}
325325

326+
const paramExpr = /\((.*)\);?$/;
327+
326328
// textRaw = "someobject.someMethod(a[, b=100][, c])"
327329
function parseSignature(text, sig) {
328330
var params = text.match(paramExpr);
@@ -556,42 +558,23 @@ function deepCopy_(src) {
556558

557559

558560
// These parse out the contents of an H# tag.
559-
const eventExpr = /^Event(?::|\s)+['"]?([^"']+).*$/i;
560-
const classExpr = /^Class:\s*([^ ]+).*$/i;
561-
const propExpr = /^[^.]+\.([^ .()]+)\s*$/;
562-
const braceExpr = /^[^.[]+(\[[^\]]+\])\s*$/;
563-
const classMethExpr = /^class\s*method\s*:?[^.]+\.([^ .()]+)\([^)]*\)\s*$/i;
564-
const methExpr = /^(?:[^.]+\.)?([^ .()]+)\([^)]*\)\s*$/;
565-
const newExpr = /^new ([A-Z][a-zA-Z]+)\([^)]*\)\s*$/;
566-
var paramExpr = /\((.*)\);?$/;
567-
568-
function newSection(tok) {
569-
const section = {};
561+
const headingExpressions = [
562+
{ type: 'event', re: /^Event(?::|\s)+['"]?([^"']+).*$/i },
563+
{ type: 'class', re: /^Class:\s*([^ ]+).*$/i },
564+
{ type: 'property', re: /^[^.[]+(\[[^\]]+\])\s*$/ },
565+
{ type: 'property', re: /^[^.]+\.([^ .()]+)\s*$/ },
566+
{ type: 'classMethod', re: /^class\s*method\s*:?[^.]+\.([^ .()]+)\([^)]*\)\s*$/i },
567+
{ type: 'method', re: /^(?:[^.]+\.)?([^ .()]+)\([^)]*\)\s*$/ },
568+
{ type: 'ctor', re: /^new ([A-Z][a-zA-Z]+)\([^)]*\)\s*$/ },
569+
];
570+
571+
function newSection({ text }) {
570572
// Infer the type from the text.
571-
const text = section.textRaw = tok.text;
572-
if (text.match(eventExpr)) {
573-
section.type = 'event';
574-
section.name = text.replace(eventExpr, '$1');
575-
} else if (text.match(classExpr)) {
576-
section.type = 'class';
577-
section.name = text.replace(classExpr, '$1');
578-
} else if (text.match(braceExpr)) {
579-
section.type = 'property';
580-
section.name = text.replace(braceExpr, '$1');
581-
} else if (text.match(propExpr)) {
582-
section.type = 'property';
583-
section.name = text.replace(propExpr, '$1');
584-
} else if (text.match(classMethExpr)) {
585-
section.type = 'classMethod';
586-
section.name = text.replace(classMethExpr, '$1');
587-
} else if (text.match(methExpr)) {
588-
section.type = 'method';
589-
section.name = text.replace(methExpr, '$1');
590-
} else if (text.match(newExpr)) {
591-
section.type = 'ctor';
592-
section.name = text.replace(newExpr, '$1');
593-
} else {
594-
section.name = text;
573+
for (const { type, re } of headingExpressions) {
574+
const [, name] = text.match(re) || [];
575+
if (name) {
576+
return { textRaw: text, type, name };
577+
}
595578
}
596-
return section;
579+
return { textRaw: text, name: text };
597580
}

0 commit comments

Comments
 (0)