tools: apply linting to doc tools

Apply eslint rules to `tools/doc`.

PR-URL: https://github.com/nodejs/node/pull/4973
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Jeremiah Senkpiel <fishrock123@rocketmail.com>
Reviewed-By: Roman Reiss <me@silverwind.io>
This commit is contained in:
Rich Trott 2016-01-30 10:17:57 -08:00
parent 165b33fce2
commit 2d89d3d1e6
8 changed files with 51 additions and 31 deletions

View File

@ -6,3 +6,4 @@ test/fixtures
test/**/node_modules test/**/node_modules
test/disabled test/disabled
test/tmp*/ test/tmp*/
tools/doc/node_modules

View File

@ -517,7 +517,7 @@ bench-idle:
$(NODE) benchmark/idle_clients.js & $(NODE) benchmark/idle_clients.js &
jslint: jslint:
$(NODE) tools/eslint/bin/eslint.js src lib test tools/eslint-rules \ $(NODE) tools/eslint/bin/eslint.js lib src test tools/doc tools/eslint-rules \
--rulesdir tools/eslint-rules --quiet --rulesdir tools/eslint-rules --quiet
CPPLINT_EXCLUDE ?= CPPLINT_EXCLUDE ?=

View File

@ -4,12 +4,13 @@ const fs = require('fs');
const path = require('path'); const path = require('path');
const marked = require('marked'); const marked = require('marked');
const doc = path.resolve(__dirname, '..', '..', 'doc', 'api', 'addons.markdown'); const rootDir = path.resolve(__dirname, '..', '..');
const verifyDir = path.resolve(__dirname, '..', '..', 'test', 'addons'); const doc = path.resolve(rootDir, 'doc', 'api', 'addons.markdown');
const verifyDir = path.resolve(rootDir, 'test', 'addons');
const contents = fs.readFileSync(doc).toString(); const contents = fs.readFileSync(doc).toString();
let tokens = marked.lexer(contents, {}); const tokens = marked.lexer(contents, {});
let files = null; let files = null;
let blockName; let blockName;
let id = 0; let id = 0;
@ -27,7 +28,7 @@ oldDirs = oldDirs.filter(function(dir) {
for (var i = 0; i < tokens.length; i++) { for (var i = 0; i < tokens.length; i++) {
var token = tokens[i]; var token = tokens[i];
if (token.type === 'heading' && token.text) { if (token.type === 'heading' && token.text) {
blockName = token.text blockName = token.text;
if (files && Object.keys(files).length !== 0) { if (files && Object.keys(files).length !== 0) {
verifyFiles(files, verifyFiles(files,
blockName, blockName,
@ -60,8 +61,14 @@ function verifyFiles(files, blockName, onprogress, ondone) {
return; return;
} }
blockName = blockName.toLowerCase().replace(/\s/g, '_').replace(/[^a-z\d_]/g, '') blockName = blockName
let dir = path.resolve(verifyDir, `${(++id < 10 ? '0' : '') + id}_${blockName}`); .toLowerCase()
.replace(/\s/g, '_')
.replace(/[^a-z\d_]/g, '');
const dir = path.resolve(
verifyDir,
`${(++id < 10 ? '0' : '') + id}_${blockName}`
);
files = Object.keys(files).map(function(name) { files = Object.keys(files).map(function(name) {
return { return {

View File

@ -1,7 +1,7 @@
'use strict';
var processIncludes = require('./preprocess.js'); var processIncludes = require('./preprocess.js');
var marked = require('marked');
var fs = require('fs'); var fs = require('fs');
var path = require('path');
// parse the args. // parse the args.
// Don't use nopt or whatever for this. It's simple enough. // Don't use nopt or whatever for this. It's simple enough.
@ -11,7 +11,7 @@ var format = 'json';
var template = null; var template = null;
var inputFile = null; var inputFile = null;
args.forEach(function (arg) { args.forEach(function(arg) {
if (!arg.match(/^\-\-/)) { if (!arg.match(/^\-\-/)) {
inputFile = arg; inputFile = arg;
} else if (arg.match(/^\-\-format=/)) { } else if (arg.match(/^\-\-format=/)) {
@ -19,7 +19,7 @@ args.forEach(function (arg) {
} else if (arg.match(/^\-\-template=/)) { } else if (arg.match(/^\-\-template=/)) {
template = arg.replace(/^\-\-template=/, ''); template = arg.replace(/^\-\-template=/, '');
} }
}) });
if (!inputFile) { if (!inputFile) {
@ -35,8 +35,6 @@ fs.readFile(inputFile, 'utf8', function(er, input) {
}); });
function next(er, input) { function next(er, input) {
if (er) throw er; if (er) throw er;
switch (format) { switch (format) {

View File

@ -1,3 +1,5 @@
'use strict';
var fs = require('fs'); var fs = require('fs');
var marked = require('marked'); var marked = require('marked');
var path = require('path'); var path = require('path');
@ -6,7 +8,14 @@ var preprocess = require('./preprocess.js');
module.exports = toHTML; module.exports = toHTML;
// TODO(chrisdickinson): never stop vomitting / fix this. // TODO(chrisdickinson): never stop vomitting / fix this.
var gtocPath = path.resolve(path.join(__dirname, '..', '..', 'doc', 'api', '_toc.markdown')); var gtocPath = path.resolve(path.join(
__dirname,
'..',
'..',
'doc',
'api',
'_toc.markdown'
));
var gtocLoading = null; var gtocLoading = null;
var gtocData = null; var gtocData = null;
@ -55,7 +64,10 @@ function loadGtoc(cb) {
} }
function toID(filename) { function toID(filename) {
return filename.replace('.html', '').replace(/[^\w\-]/g, '-').replace(/-+/g, '-'); return filename
.replace('.html', '')
.replace(/[^\w\-]/g, '-')
.replace(/-+/g, '-');
} }
function render(lexed, filename, template, cb) { function render(lexed, filename, template, cb) {
@ -85,7 +97,7 @@ 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);
@ -173,7 +185,6 @@ function parseAPIHeader(text) {
// section is just the first heading // section is just the first heading
function getSection(lexed) { function getSection(lexed) {
var section = '';
for (var i = 0, l = lexed.length; i < l; i++) { for (var i = 0, l = lexed.length; i < l; i++) {
var tok = lexed[i]; var tok = lexed[i];
if (tok.type === 'heading') return tok.text; if (tok.type === 'heading') return tok.text;
@ -183,7 +194,6 @@ function getSection(lexed) {
function buildToc(lexed, filename, cb) { function buildToc(lexed, filename, cb) {
var indent = 0;
var toc = []; var toc = [];
var depth = 0; var depth = 0;
lexed.forEach(function(tok) { lexed.forEach(function(tok) {

View File

@ -1,3 +1,5 @@
'use strict';
module.exports = doJSON; module.exports = doJSON;
// Take the lexed input, and return a JSON-encoded object // Take the lexed input, and return a JSON-encoded object
@ -12,7 +14,7 @@ function doJSON(input, filename, cb) {
var current = root; var current = root;
var state = null; var state = null;
var lexed = marked.lexer(input); var lexed = marked.lexer(input);
lexed.forEach(function (tok) { lexed.forEach(function(tok) {
var type = tok.type; var type = tok.type;
var text = tok.text; var text = tok.text;
@ -31,7 +33,7 @@ function doJSON(input, filename, cb) {
if (type === 'heading' && if (type === 'heading' &&
!text.trim().match(/^example/i)) { !text.trim().match(/^example/i)) {
if (tok.depth - depth > 1) { if (tok.depth - depth > 1) {
return cb(new Error('Inappropriate heading level\n'+ return cb(new Error('Inappropriate heading level\n' +
JSON.stringify(tok))); JSON.stringify(tok)));
} }
@ -77,7 +79,7 @@ function doJSON(input, filename, cb) {
// //
// If one of these isnt' found, then anything that comes between // If one of these isnt' found, then anything that comes between
// here and the next heading should be parsed as the desc. // here and the next heading should be parsed as the desc.
var stability var stability;
if (state === 'AFTERHEADING') { if (state === 'AFTERHEADING') {
if (type === 'code' && if (type === 'code' &&
(stability = text.match(/^Stability: ([0-5])(?:\s*-\s*)?(.*)$/))) { (stability = text.match(/^Stability: ([0-5])(?:\s*-\s*)?(.*)$/))) {
@ -125,7 +127,7 @@ function doJSON(input, filename, cb) {
finishSection(current, stack[stack.length - 1]); finishSection(current, stack[stack.length - 1]);
} }
return cb(null, root) return cb(null, root);
} }
@ -146,7 +148,7 @@ function doJSON(input, filename, cb) {
// { type: 'list_item_end' }, // { type: 'list_item_end' },
// { type: 'list_item_start' }, // { type: 'list_item_start' },
// { type: 'text', // { type: 'text',
// text: 'silent: Boolean, whether or not to send output to parent\'s stdio.' }, // text: 'silent: Boolean, whether to send output to parent\'s stdio.' },
// { type: 'text', text: 'Default: `false`' }, // { type: 'text', text: 'Default: `false`' },
// { type: 'space' }, // { type: 'space' },
// { type: 'list_item_end' }, // { type: 'list_item_end' },
@ -168,7 +170,7 @@ function doJSON(input, filename, cb) {
// desc: 'string arguments passed to worker.' }, // desc: 'string arguments passed to worker.' },
// { name: 'silent', // { name: 'silent',
// type: 'boolean', // type: 'boolean',
// desc: 'whether or not to send output to parent\'s stdio.', // desc: 'whether to send output to parent\'s stdio.',
// default: 'false' } ] } ] // default: 'false' } ] } ]
function processList(section) { function processList(section) {
@ -231,7 +233,7 @@ function processList(section) {
// each item is an argument, unless the name is 'return', // each item is an argument, unless the name is 'return',
// in which case it's the return value. // in which case it's the return value.
section.signatures = section.signatures || []; section.signatures = section.signatures || [];
var sig = {} var sig = {};
section.signatures.push(sig); section.signatures.push(sig);
sig.params = values.filter(function(v) { sig.params = values.filter(function(v) {
if (v.name === 'return') { if (v.name === 'return') {
@ -273,7 +275,7 @@ function parseSignature(text, sig) {
params = params[1]; params = params[1];
// the [ is irrelevant. ] indicates optionalness. // the [ is irrelevant. ] indicates optionalness.
params = params.replace(/\[/g, ''); params = params.replace(/\[/g, '');
params = params.split(/,/) params = params.split(/,/);
params.forEach(function(p, i, _) { params.forEach(function(p, i, _) {
p = p.trim(); p = p.trim();
if (!p) return; if (!p) return;
@ -362,7 +364,7 @@ function parseListItem(item) {
function finishSection(section, parent) { function 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' +
JSON.stringify(parent)); JSON.stringify(parent));
} }
@ -405,7 +407,7 @@ function finishSection(section, parent) {
// properties are a bit special. // properties are a bit special.
// their "type" is the type of object, not "property" // their "type" is the type of object, not "property"
if (section.properties) { if (section.properties) {
section.properties.forEach(function (p) { section.properties.forEach(function(p) {
if (p.typeof) p.type = p.typeof; if (p.typeof) p.type = p.typeof;
else delete p.type; else delete p.type;
delete p.typeof; delete p.typeof;

View File

@ -1,3 +1,5 @@
'use strict';
module.exports = preprocess; module.exports = preprocess;
var path = require('path'); var path = require('path');
@ -8,7 +10,7 @@ var includeData = {};
function preprocess(inputFile, input, cb) { function preprocess(inputFile, input, cb) {
input = stripComments(input); input = stripComments(input);
processIncludes(inputFile, input, function (err, data) { processIncludes(inputFile, input, function(err, data) {
if (err) return cb(err); if (err) return cb(err);
cb(null, data); cb(null, data);
@ -47,7 +49,7 @@ function processIncludes(inputFile, input, cb) {
if (er) return cb(errState = er); if (er) return cb(errState = er);
incCount--; incCount--;
includeData[fname] = inc; includeData[fname] = inc;
input = input.split(include+'\n').join(includeData[fname]+'\n'); input = input.split(include + '\n').join(includeData[fname] + '\n');
if (incCount === 0) { if (incCount === 0) {
return cb(null, input); return cb(null, input);
} }

View File

@ -259,7 +259,7 @@ goto jslint
:jslint :jslint
if not defined jslint goto exit if not defined jslint goto exit
echo running jslint echo running jslint
%config%\node tools\eslint\bin\eslint.js src lib test tools\eslint-rules --rulesdir tools\eslint-rules --quiet %config%\node tools\eslint\bin\eslint.js lib src test tools\doc tools\eslint-rules --rulesdir tools\eslint-rules --quiet
goto exit goto exit
:create-msvs-files-failed :create-msvs-files-failed