tools: dry utility function in tools/doc/json.js

Also, move a declaration of unrelated variable
closer to its only context.

PR-URL: https://github.com/nodejs/node/pull/19692
Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de>
Reviewed-By: Tobias Nießen <tniessen@tnie.de>
This commit is contained in:
Vse Mozhet Byt 2018-03-30 12:28:34 +03:00
parent b88477ef4d
commit 83d44bee01

View File

@ -323,6 +323,8 @@ function processList(section) {
delete section.list; delete section.list;
} }
const paramExpr = /\((.*)\);?$/;
// textRaw = "someobject.someMethod(a[, b=100][, c])" // textRaw = "someobject.someMethod(a[, b=100][, c])"
function parseSignature(text, sig) { function parseSignature(text, sig) {
var params = text.match(paramExpr); var params = text.match(paramExpr);
@ -556,42 +558,23 @@ function deepCopy_(src) {
// These parse out the contents of an H# tag. // These parse out the contents of an H# tag.
const eventExpr = /^Event(?::|\s)+['"]?([^"']+).*$/i; const headingExpressions = [
const classExpr = /^Class:\s*([^ ]+).*$/i; { type: 'event', re: /^Event(?::|\s)+['"]?([^"']+).*$/i },
const propExpr = /^[^.]+\.([^ .()]+)\s*$/; { type: 'class', re: /^Class:\s*([^ ]+).*$/i },
const braceExpr = /^[^.[]+(\[[^\]]+\])\s*$/; { type: 'property', re: /^[^.[]+(\[[^\]]+\])\s*$/ },
const classMethExpr = /^class\s*method\s*:?[^.]+\.([^ .()]+)\([^)]*\)\s*$/i; { type: 'property', re: /^[^.]+\.([^ .()]+)\s*$/ },
const methExpr = /^(?:[^.]+\.)?([^ .()]+)\([^)]*\)\s*$/; { type: 'classMethod', re: /^class\s*method\s*:?[^.]+\.([^ .()]+)\([^)]*\)\s*$/i },
const newExpr = /^new ([A-Z][a-zA-Z]+)\([^)]*\)\s*$/; { type: 'method', re: /^(?:[^.]+\.)?([^ .()]+)\([^)]*\)\s*$/ },
var paramExpr = /\((.*)\);?$/; { type: 'ctor', re: /^new ([A-Z][a-zA-Z]+)\([^)]*\)\s*$/ },
];
function newSection(tok) { function newSection({ text }) {
const section = {};
// Infer the type from the text. // Infer the type from the text.
const text = section.textRaw = tok.text; for (const { type, re } of headingExpressions) {
if (text.match(eventExpr)) { const [, name] = text.match(re) || [];
section.type = 'event'; if (name) {
section.name = text.replace(eventExpr, '$1'); return { textRaw: text, type, name };
} else if (text.match(classExpr)) {
section.type = 'class';
section.name = text.replace(classExpr, '$1');
} else if (text.match(braceExpr)) {
section.type = 'property';
section.name = text.replace(braceExpr, '$1');
} else if (text.match(propExpr)) {
section.type = 'property';
section.name = text.replace(propExpr, '$1');
} else if (text.match(classMethExpr)) {
section.type = 'classMethod';
section.name = text.replace(classMethExpr, '$1');
} else if (text.match(methExpr)) {
section.type = 'method';
section.name = text.replace(methExpr, '$1');
} else if (text.match(newExpr)) {
section.type = 'ctor';
section.name = text.replace(newExpr, '$1');
} else {
section.name = text;
} }
return section; }
return { textRaw: text, name: text };
} }