Skip to content

Commit ff4c30e

Browse files
vsemozhetbytMylesBorins
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 <ruben@bridgewater.de> Reviewed-By: Tobias Nießen <tniessen@tnie.de>
1 parent 59b99e8 commit ff4c30e

1 file changed

Lines changed: 19 additions & 36 deletions

File tree

‎tools/doc/json.js‎

Lines changed: 19 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -323,6 +323,8 @@ function processList(section) {
323323
deletesection.list;
324324
}
325325

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

557559

558560
// These parse out the contents of an H# tag.
559-
consteventExpr=/^Event(?::|\s)+['"]?([^"']+).*$/i;
560-
constclassExpr=/^Class:\s*([^]+).*$/i;
561-
constpropExpr=/^[^.]+\.([^.()]+)\s*$/;
562-
constbraceExpr=/^[^.[]+(\[[^\]]+\])\s*$/;
563-
constclassMethExpr=/^class\s*method\s*:?[^.]+\.([^.()]+)\([^)]*\)\s*$/i;
564-
constmethExpr=/^(?:[^.]+\.)?([^.()]+)\([^)]*\)\s*$/;
565-
constnewExpr=/^new([A-Z][a-zA-Z]+)\([^)]*\)\s*$/;
566-
varparamExpr=/\((.*)\);?$/;
567-
568-
functionnewSection(tok){
569-
constsection={};
561+
constheadingExpressions=[
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+
functionnewSection({ text }){
570572
// Infer the type from the text.
571-
consttext=section.textRaw=tok.text;
572-
if(text.match(eventExpr)){
573-
section.type='event';
574-
section.name=text.replace(eventExpr,'$1');
575-
}elseif(text.match(classExpr)){
576-
section.type='class';
577-
section.name=text.replace(classExpr,'$1');
578-
}elseif(text.match(braceExpr)){
579-
section.type='property';
580-
section.name=text.replace(braceExpr,'$1');
581-
}elseif(text.match(propExpr)){
582-
section.type='property';
583-
section.name=text.replace(propExpr,'$1');
584-
}elseif(text.match(classMethExpr)){
585-
section.type='classMethod';
586-
section.name=text.replace(classMethExpr,'$1');
587-
}elseif(text.match(methExpr)){
588-
section.type='method';
589-
section.name=text.replace(methExpr,'$1');
590-
}elseif(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 }ofheadingExpressions){
574+
const[,name]=text.match(re)||[];
575+
if(name){
576+
return{textRaw: text, type, name };
577+
}
595578
}
596-
returnsection;
579+
return{textRaw: text,name: text};
597580
}

0 commit comments

Comments
 (0)