lint: low hanging bin/doc/*.js

pull/4694/head
John McLear 2021-02-01 09:47:42 +00:00 committed by Richard Hansen
parent 5b701b97c3
commit 9987fab574
4 changed files with 67 additions and 64 deletions

View File

@ -1,4 +1,7 @@
#!/usr/bin/env node #!/usr/bin/env node
'use strict';
// Copyright Joyent, Inc. and other Node contributors. // Copyright Joyent, Inc. and other Node contributors.
// //
// Permission is hereby granted, free of charge, to any person obtaining a // Permission is hereby granted, free of charge, to any person obtaining a
@ -20,7 +23,6 @@
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE // OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
// USE OR OTHER DEALINGS IN THE SOFTWARE. // USE OR OTHER DEALINGS IN THE SOFTWARE.
const marked = require('marked');
const fs = require('fs'); const fs = require('fs');
const path = require('path'); const path = require('path');
@ -33,12 +35,12 @@ let template = null;
let inputFile = null; let inputFile = null;
args.forEach((arg) => { args.forEach((arg) => {
if (!arg.match(/^\-\-/)) { if (!arg.match(/^--/)) {
inputFile = arg; inputFile = arg;
} else if (arg.match(/^\-\-format=/)) { } else if (arg.match(/^--format=/)) {
format = arg.replace(/^\-\-format=/, ''); format = arg.replace(/^--format=/, '');
} else if (arg.match(/^\-\-template=/)) { } else if (arg.match(/^--template=/)) {
template = arg.replace(/^\-\-template=/, ''); template = arg.replace(/^--template=/, '');
} }
}); });
@ -56,11 +58,11 @@ fs.readFile(inputFile, 'utf8', (er, input) => {
}); });
const includeExpr = /^@include\s+([A-Za-z0-9-_\/]+)(?:\.)?([a-zA-Z]*)$/gmi; const includeExpr = /^@include\s+([A-Za-z0-9-_/]+)(?:\.)?([a-zA-Z]*)$/gmi;
const includeData = {}; const includeData = {};
function processIncludes(inputFile, input, cb) { const processIncludes = (inputFile, input, cb) => {
const includes = input.match(includeExpr); const includes = input.match(includeExpr);
if (includes === null) return cb(null, input); if (includes == null) return cb(null, input);
let errState = null; let errState = null;
console.error(includes); console.error(includes);
let incCount = includes.length; let incCount = includes.length;
@ -70,7 +72,7 @@ function processIncludes(inputFile, input, cb) {
let fname = include.replace(/^@include\s+/, ''); let fname = include.replace(/^@include\s+/, '');
if (!fname.match(/\.md$/)) fname += '.md'; if (!fname.match(/\.md$/)) fname += '.md';
if (includeData.hasOwnProperty(fname)) { if (Object.prototype.hasOwnProperty.call(includeData, fname)) {
input = input.split(include).join(includeData[fname]); input = input.split(include).join(includeData[fname]);
incCount--; incCount--;
if (incCount === 0) { if (incCount === 0) {
@ -94,10 +96,10 @@ function processIncludes(inputFile, input, cb) {
}); });
}); });
}); });
} };
function next(er, input) { const next = (er, input) => {
if (er) throw er; if (er) throw er;
switch (format) { switch (format) {
case 'json': case 'json':
@ -117,4 +119,4 @@ function next(er, input) {
default: default:
throw new Error(`Invalid format: ${format}`); throw new Error(`Invalid format: ${format}`);
} }
} };

View File

@ -1,3 +1,5 @@
'use strict';
// Copyright Joyent, Inc. and other Node contributors. // Copyright Joyent, Inc. and other Node contributors.
// //
// Permission is hereby granted, free of charge, to any person obtaining a // Permission is hereby granted, free of charge, to any person obtaining a
@ -23,17 +25,17 @@ const fs = require('fs');
const marked = require('marked'); const marked = require('marked');
const path = require('path'); const path = require('path');
module.exports = toHTML;
function toHTML(input, filename, template, cb) { const toHTML = (input, filename, template, cb) => {
const lexed = marked.lexer(input); const lexed = marked.lexer(input);
fs.readFile(template, 'utf8', (er, template) => { fs.readFile(template, 'utf8', (er, template) => {
if (er) return cb(er); if (er) return cb(er);
render(lexed, filename, template, cb); render(lexed, filename, template, cb);
}); });
} };
module.exports = toHTML;
function render(lexed, filename, template, cb) { const render = (lexed, filename, template, cb) => {
// get the section // get the section
const section = getSection(lexed); const section = getSection(lexed);
@ -52,23 +54,23 @@ function render(lexed, filename, template, cb) {
// content has to be the last thing we do with // content has to be the last thing we do with
// the lexed tokens, because it's destructive. // the lexed tokens, because it's destructive.
content = marked.parser(lexed); const content = marked.parser(lexed);
template = template.replace(/__CONTENT__/g, content); template = template.replace(/__CONTENT__/g, content);
cb(null, template); cb(null, template);
}); });
} };
// just update the list item text in-place. // just update the list item text in-place.
// lists that come right after a heading are what we're after. // lists that come right after a heading are what we're after.
function parseLists(input) { const parseLists = (input) => {
let state = null; let state = null;
let depth = 0; let depth = 0;
const output = []; const output = [];
output.links = input.links; output.links = input.links;
input.forEach((tok) => { input.forEach((tok) => {
if (state === null) { if (state == null) {
if (tok.type === 'heading') { if (tok.type === 'heading') {
state = 'AFTERHEADING'; state = 'AFTERHEADING';
} }
@ -112,29 +114,27 @@ function parseLists(input) {
}); });
return output; return output;
} };
function parseListItem(text) { const parseListItem = (text) => {
text = text.replace(/\{([^\}]+)\}/, '<span class="type">$1</span>'); text = text.replace(/\{([^}]+)\}/, '<span class="type">$1</span>');
// XXX maybe put more stuff here? // XXX maybe put more stuff here?
return text; return text;
} };
// section is just the first heading // section is just the first heading
function getSection(lexed) { const getSection = (lexed) => {
const section = '';
for (let i = 0, l = lexed.length; i < l; i++) { for (let i = 0, l = lexed.length; i < l; i++) {
const tok = lexed[i]; const tok = lexed[i];
if (tok.type === 'heading') return tok.text; if (tok.type === 'heading') return tok.text;
} }
return ''; return '';
} };
function buildToc(lexed, filename, cb) { const buildToc = (lexed, filename, cb) => {
const indent = 0;
let toc = []; let toc = [];
let depth = 0; let depth = 0;
lexed.forEach((tok) => { lexed.forEach((tok) => {
@ -155,18 +155,18 @@ function buildToc(lexed, filename, cb) {
toc = marked.parse(toc.join('\n')); toc = marked.parse(toc.join('\n'));
cb(null, toc); cb(null, toc);
} };
const idCounters = {}; const idCounters = {};
function getId(text) { const getId = (text) => {
text = text.toLowerCase(); text = text.toLowerCase();
text = text.replace(/[^a-z0-9]+/g, '_'); text = text.replace(/[^a-z0-9]+/g, '_');
text = text.replace(/^_+|_+$/, ''); text = text.replace(/^_+|_+$/, '');
text = text.replace(/^([^a-z])/, '_$1'); text = text.replace(/^([^a-z])/, '_$1');
if (idCounters.hasOwnProperty(text)) { if (Object.prototype.hasOwnProperty.call(idCounters, text)) {
text += `_${++idCounters[text]}`; text += `_${++idCounters[text]}`;
} else { } else {
idCounters[text] = 0; idCounters[text] = 0;
} }
return text; return text;
} };

View File

@ -1,3 +1,4 @@
'use strict';
// Copyright Joyent, Inc. and other Node contributors. // Copyright Joyent, Inc. and other Node contributors.
// //
// Permission is hereby granted, free of charge, to any person obtaining a // Permission is hereby granted, free of charge, to any person obtaining a
@ -26,7 +27,7 @@ module.exports = doJSON;
const marked = require('marked'); const marked = require('marked');
function doJSON(input, filename, cb) { const doJSON = (input, filename, cb) => {
const root = {source: filename}; const root = {source: filename};
const stack = [root]; const stack = [root];
let depth = 0; let depth = 0;
@ -40,7 +41,7 @@ function doJSON(input, filename, cb) {
// <!-- type = module --> // <!-- type = module -->
// This is for cases where the markdown semantic structure is lacking. // This is for cases where the markdown semantic structure is lacking.
if (type === 'paragraph' || type === 'html') { if (type === 'paragraph' || type === 'html') {
const metaExpr = /<!--([^=]+)=([^\-]+)-->\n*/g; const metaExpr = /<!--([^=]+)=([^-]+)-->\n*/g;
text = text.replace(metaExpr, (_0, k, v) => { text = text.replace(metaExpr, (_0, k, v) => {
current[k.trim()] = v.trim(); current[k.trim()] = v.trim();
return ''; return '';
@ -146,7 +147,7 @@ function doJSON(input, filename, cb) {
} }
return cb(null, root); return cb(null, root);
} };
// go from something like this: // go from something like this:
@ -191,7 +192,7 @@ function doJSON(input, filename, cb) {
// desc: 'whether or not to send output to parent\'s stdio.', // desc: 'whether or not to send output to parent\'s stdio.',
// default: 'false' } ] } ] // default: 'false' } ] } ]
function processList(section) { const processList = (section) => {
const list = section.list; const list = section.list;
const values = []; const values = [];
let current; let current;
@ -203,13 +204,13 @@ function processList(section) {
if (type === 'space') return; if (type === 'space') return;
if (type === 'list_item_start') { if (type === 'list_item_start') {
if (!current) { if (!current) {
var n = {}; const n = {};
values.push(n); values.push(n);
current = n; current = n;
} else { } else {
current.options = current.options || []; current.options = current.options || [];
stack.push(current); stack.push(current);
var n = {}; const n = {};
current.options.push(n); current.options.push(n);
current = n; current = n;
} }
@ -283,11 +284,11 @@ function processList(section) {
// section.listParsed = values; // section.listParsed = values;
delete section.list; delete section.list;
} };
// textRaw = "someobject.someMethod(a, [b=100], [c])" // textRaw = "someobject.someMethod(a, [b=100], [c])"
function parseSignature(text, sig) { const parseSignature = (text, sig) => {
let params = text.match(paramExpr); let params = text.match(paramExpr);
if (!params) return; if (!params) return;
params = params[1]; params = params[1];
@ -322,10 +323,10 @@ function parseSignature(text, sig) {
if (optional) param.optional = true; if (optional) param.optional = true;
if (def !== undefined) param.default = def; if (def !== undefined) param.default = def;
}); });
} };
function parseListItem(item) { const parseListItem = (item) => {
if (item.options) item.options.forEach(parseListItem); if (item.options) item.options.forEach(parseListItem);
if (!item.textRaw) return; if (!item.textRaw) return;
@ -341,7 +342,7 @@ function parseListItem(item) {
item.name = 'return'; item.name = 'return';
text = text.replace(retExpr, ''); text = text.replace(retExpr, '');
} else { } else {
const nameExpr = /^['`"]?([^'`": \{]+)['`"]?\s*:?\s*/; const nameExpr = /^['`"]?([^'`": {]+)['`"]?\s*:?\s*/;
const name = text.match(nameExpr); const name = text.match(nameExpr);
if (name) { if (name) {
item.name = name[1]; item.name = name[1];
@ -358,7 +359,7 @@ function parseListItem(item) {
} }
text = text.trim(); text = text.trim();
const typeExpr = /^\{([^\}]+)\}/; const typeExpr = /^\{([^}]+)\}/;
const type = text.match(typeExpr); const type = text.match(typeExpr);
if (type) { if (type) {
item.type = type[1]; item.type = type[1];
@ -376,10 +377,10 @@ function parseListItem(item) {
text = text.replace(/^\s*-\s*/, ''); text = text.replace(/^\s*-\s*/, '');
text = text.trim(); text = text.trim();
if (text) item.desc = text; if (text) item.desc = text;
} };
function finishSection(section, parent) { const finishSection = (section, parent) => {
if (!section || !parent) { if (!section || !parent) {
throw new Error(`Invalid finishSection call\n${ throw new Error(`Invalid finishSection call\n${
JSON.stringify(section)}\n${ JSON.stringify(section)}\n${
@ -479,50 +480,50 @@ function finishSection(section, parent) {
parent[plur] = parent[plur] || []; parent[plur] = parent[plur] || [];
parent[plur].push(section); parent[plur].push(section);
} };
// Not a general purpose deep copy. // Not a general purpose deep copy.
// But sufficient for these basic things. // But sufficient for these basic things.
function deepCopy(src, dest) { const deepCopy = (src, dest) => {
Object.keys(src).filter((k) => !dest.hasOwnProperty(k)).forEach((k) => { Object.keys(src).filter((k) => !dest.hasOwnProperty(k)).forEach((k) => {
dest[k] = deepCopy_(src[k]); dest[k] = deepCopy_(src[k]);
}); });
} };
function deepCopy_(src) { const deepCopy_ = (src) => {
if (!src) return src; if (!src) return src;
if (Array.isArray(src)) { if (Array.isArray(src)) {
var c = new Array(src.length); const c = new Array(src.length);
src.forEach((v, i) => { src.forEach((v, i) => {
c[i] = deepCopy_(v); c[i] = deepCopy_(v);
}); });
return c; return c;
} }
if (typeof src === 'object') { if (typeof src === 'object') {
var c = {}; const c = {};
Object.keys(src).forEach((k) => { Object.keys(src).forEach((k) => {
c[k] = deepCopy_(src[k]); c[k] = deepCopy_(src[k]);
}); });
return c; return c;
} }
return src; return 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 eventExpr = /^Event(?::|\s)+['"]?([^"']+).*$/i;
const classExpr = /^Class:\s*([^ ]+).*?$/i; const classExpr = /^Class:\s*([^ ]+).*?$/i;
const propExpr = /^(?:property:?\s*)?[^\.]+\.([^ \.\(\)]+)\s*?$/i; const propExpr = /^(?:property:?\s*)?[^.]+\.([^ .()]+)\s*?$/i;
const braceExpr = /^(?:property:?\s*)?[^\.\[]+(\[[^\]]+\])\s*?$/i; const braceExpr = /^(?:property:?\s*)?[^.[]+(\[[^\]]+\])\s*?$/i;
const classMethExpr = const classMethExpr =
/^class\s*method\s*:?[^\.]+\.([^ \.\(\)]+)\([^\)]*\)\s*?$/i; /^class\s*method\s*:?[^.]+\.([^ .()]+)\([^)]*\)\s*?$/i;
const methExpr = const methExpr =
/^(?:method:?\s*)?(?:[^\.]+\.)?([^ \.\(\)]+)\([^\)]*\)\s*?$/i; /^(?:method:?\s*)?(?:[^.]+\.)?([^ .()]+)\([^)]*\)\s*?$/i;
const newExpr = /^new ([A-Z][a-z]+)\([^\)]*\)\s*?$/; const newExpr = /^new ([A-Z][a-z]+)\([^)]*\)\s*?$/;
var paramExpr = /\((.*)\);?$/; const paramExpr = /\((.*)\);?$/;
function newSection(tok) { const newSection = (tok) => {
const section = {}; const section = {};
// infer the type from the text. // infer the type from the text.
const text = section.textRaw = tok.text; const text = section.textRaw = tok.text;
@ -551,4 +552,4 @@ function newSection(tok) {
section.name = text; section.name = text;
} }
return section; return section;
} };

View File

@ -4,7 +4,7 @@
"description": "Internal tool for generating Node.js API docs", "description": "Internal tool for generating Node.js API docs",
"version": "0.0.0", "version": "0.0.0",
"engines": { "engines": {
"node": ">=0.6.10" "node": ">=10.17.0"
}, },
"dependencies": { "dependencies": {
"marked": "0.8.2" "marked": "0.8.2"