Skip to content

Instantly share code, notes, and snippets.

@maclover7
Created October 24, 2017 18:33
Show Gist options
  • Save maclover7/a5c8c83e9ee4c6749fccacae9bb8fa92 to your computer and use it in GitHub Desktop.
Save maclover7/a5c8c83e9ee4c6749fccacae9bb8fa92 to your computer and use it in GitHub Desktop.
const { createInterface } = require('readline');
const { createReadStream } = require ('fs');
// Reads lib/internal/errors.js, and returns one list of errors:
// 1) definedErrors
// --> all errors defined by calling the `E` function
function readDefinedErrors() {
return new Promise(function(resolve, reject) {
var definedErrors = [];
var regex = /E\('ERR_(.*?)'/;
var lineReader = _readFile('lib/internal/errors.js');
lineReader.on('line', function(line) {
if (regex.test(line)) {
for(var match of regex.exec(line)) {
if (match.startsWith('E(')) continue;
definedErrors.push(`ERR_${match}`);
}
}
});
lineReader.on('close', function() {
resolve({ definedErrors });
});
});
};
// Reads doc/api/errors.md, and returns two lists of errors:
// 1) documentedErrorsByHeading
// --> error has an <a> tag
// 2) documentedErrorsByDescription
// --> error has a ### (<h3>) tag, meaningly it likely has a description
function readDocumentedErrors(state) {
return new Promise(function(resolve, reject) {
var documentedErrorsByHeading = [];
var documentedErrorsByDescription = [];
var regexHeading = /<a id="ERR_(.*?)"><\/a>/;
var regexDescription = /### ERR_(.*?$)/;
var lineReader = _readFile('doc/api/errors.md');
lineReader.on('line', function(line) {
if (regexHeading.test(line)) {
for(var match of regexHeading.exec(line)) {
if (match === line) continue;
documentedErrorsByHeading.push(`ERR_${match}`);
}
}
if (regexDescription.test(line)) {
for(var match of regexDescription.exec(line)) {
if (match === line) continue;
documentedErrorsByDescription.push(`ERR_${match}`);
}
}
});
lineReader.on('close', function() {
resolve(
Object.assign({},
state,
{ documentedErrorsByHeading, documentedErrorsByDescription }
)
);
});
});
}
// Creates a master list of all errors known to the system
function organizeState(state) {
return new Promise(function(resolve, reject) {
var errors = new Set();
for (var errorGroup in state) {
for (var error of state[errorGroup]) {
errors.add(error);
}
}
resolve(
Object.assign({}, state, { errors })
);
});
}
function performAnalysis(state) {
return new Promise(function(resolve, reject) {
var errorsNoDefinitions = [];
var errorsNoDocs = [];
var errorsNoHeading = [];
var errorsNoDescription = [];
state.errors.forEach(function(err) {
var noDefinition = !state.definedErrors.includes(err);
var noHeading = !state.documentedErrorsByHeading.includes(err);
var noDescription = !state.documentedErrorsByDescription.includes(err);
if (noDefinition) {
errorsNoDefinitions.push(err);
}
if (noHeading && noDescription) {
errorsNoDocs.push(err);
} else if (noHeading) {
errorsNoHeading.push(err);
} else if (noDescription) {
errorsNoDescription.push(err);
}
});
resolve([
{ name: 'No definition', errors: errorsNoDefinitions },
{ name: 'No documentation', errors: errorsNoDocs },
{ name: 'No heading', errors: errorsNoHeading },
{ name: 'No description', errors: errorsNoDescription }
]);
});
}
// Runs through each dataset and lists:
// --> the number of datapoints in the dataset
// --> each individual datapoint in the dataset
function outputAnalysis(datasets) {
for (var dataset of datasets) {
console.log(`${dataset.name}: ${dataset.errors.length}`);
for (var datapoint of dataset.errors) {
console.log(`- ${datapoint}`);
}
console.log('');
}
}
// Utility function to create a readline interface
function _readFile(file) {
return createInterface({
input: createReadStream(file)
});
}
readDefinedErrors()
.then(readDocumentedErrors)
.then(organizeState)
.then(performAnalysis)
.then(outputAnalysis);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment