2017-07-11 15:57:26 +00:00
|
|
|
class Documentation {
|
|
|
|
/**
|
|
|
|
* @param {!Array<!Documentation.Class>} clasesArray
|
|
|
|
*/
|
|
|
|
constructor(classesArray) {
|
|
|
|
this.classesArray = classesArray;
|
|
|
|
this.classes = new Map();
|
|
|
|
for (let cls of classesArray)
|
|
|
|
this.classes.set(cls.name, cls);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {!Documentation} actual
|
|
|
|
* @param {!Documentation} expected
|
2017-07-13 22:15:31 +00:00
|
|
|
* @return {!Array<string>}
|
2017-07-11 15:57:26 +00:00
|
|
|
*/
|
|
|
|
static diff(actual, expected) {
|
2017-07-13 22:15:31 +00:00
|
|
|
const errors = [];
|
|
|
|
|
2017-07-11 15:57:26 +00:00
|
|
|
const actualClasses = Array.from(actual.classes.keys()).sort();
|
|
|
|
const expectedClasses = Array.from(expected.classes.keys()).sort();
|
|
|
|
let classesDiff = diff(actualClasses, expectedClasses);
|
2017-07-13 22:15:31 +00:00
|
|
|
for (let className of classesDiff.extra)
|
|
|
|
errors.push(`Non-existing class found: ${className}`);
|
|
|
|
for (let className of classesDiff.missing)
|
|
|
|
errors.push(`Class not found: ${className}`);
|
2017-07-11 15:57:26 +00:00
|
|
|
|
|
|
|
for (let className of classesDiff.equal) {
|
|
|
|
const actualClass = actual.classes.get(className);
|
|
|
|
const expectedClass = expected.classes.get(className);
|
|
|
|
const actualMethods = Array.from(actualClass.methods.keys()).sort();
|
|
|
|
const expectedMethods = Array.from(expectedClass.methods.keys()).sort();
|
|
|
|
const methodDiff = diff(actualMethods, expectedMethods);
|
2017-07-13 22:15:31 +00:00
|
|
|
for (let methodName of methodDiff.extra)
|
|
|
|
errors.push(`Non-existing method found: ${className}.${methodName}()`);
|
|
|
|
for (let methodName of methodDiff.missing)
|
|
|
|
errors.push(`Method not found: ${className}.${methodName}()`);
|
|
|
|
|
2017-07-11 15:57:26 +00:00
|
|
|
for (let methodName of methodDiff.equal) {
|
|
|
|
const actualMethod = actualClass.methods.get(methodName);
|
|
|
|
const expectedMethod = expectedClass.methods.get(methodName);
|
2017-07-13 22:54:37 +00:00
|
|
|
if (actualMethod.hasReturn !== expectedMethod.hasReturn) {
|
|
|
|
if (actualMethod.hasReturn)
|
|
|
|
errors.push(`Method ${className}.${methodName} has unneeded description of return type`);
|
|
|
|
else if (!expectedMethod.async)
|
|
|
|
errors.push(`Method ${className}.${methodName} is missing return type description`);
|
|
|
|
else
|
|
|
|
errors.push(`Async method ${className}.${methodName} should describe return type Promise`);
|
|
|
|
}
|
2017-07-13 22:15:31 +00:00
|
|
|
const actualArgs = Array.from(actualMethod.args.keys());
|
|
|
|
const expectedArgs = Array.from(expectedMethod.args.keys());
|
2017-07-11 15:57:26 +00:00
|
|
|
const argDiff = diff(actualArgs, expectedArgs);
|
2017-07-12 00:01:58 +00:00
|
|
|
if (argDiff.extra.length || argDiff.missing.length) {
|
2017-07-13 22:15:31 +00:00
|
|
|
let text = [`Method ${className}.${methodName}() fails to describe its parameters:`];
|
|
|
|
for (let arg of argDiff.missing)
|
|
|
|
text.push(`- Argument not found: ${arg}`);
|
|
|
|
for (let arg of argDiff.extra)
|
|
|
|
text.push(`- Non-existing argument found: ${arg}`);
|
|
|
|
errors.push(text.join('\n'));
|
2017-07-11 15:57:26 +00:00
|
|
|
}
|
|
|
|
}
|
2017-07-13 22:15:31 +00:00
|
|
|
const actualProperties = Array.from(actualClass.properties.keys()).sort();
|
|
|
|
const expectedProperties = Array.from(expectedClass.properties.keys()).sort();
|
2017-07-12 15:01:21 +00:00
|
|
|
const propertyDiff = diff(actualProperties, expectedProperties);
|
2017-07-13 22:15:31 +00:00
|
|
|
for (let propertyName of propertyDiff.extra)
|
|
|
|
errors.push(`Non-existing property found: ${className}.${propertyName}`);
|
|
|
|
for (let propertyName of propertyDiff.missing)
|
|
|
|
errors.push(`Property not found: ${className}.${propertyName}`);
|
2017-07-14 20:03:21 +00:00
|
|
|
|
|
|
|
const actualEvents = Array.from(actualClass.events.keys()).sort();
|
|
|
|
const expectedEvents = Array.from(expectedClass.events.keys()).sort();
|
|
|
|
const eventsDiff = diff(actualEvents, expectedEvents);
|
|
|
|
for (let eventName of eventsDiff.extra)
|
|
|
|
errors.push(`Non-existing event found in class ${className}: '${eventName}'`);
|
|
|
|
for (let eventName of eventsDiff.missing)
|
|
|
|
errors.push(`Event not found in class ${className}: '${eventName}'`);
|
2017-07-13 22:15:31 +00:00
|
|
|
}
|
|
|
|
return errors;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {!Documentation} doc
|
|
|
|
* @return {!Array<string>}
|
|
|
|
*/
|
|
|
|
static validate(doc) {
|
|
|
|
const errors = [];
|
|
|
|
let classes = new Set();
|
|
|
|
// Report duplicates.
|
|
|
|
for (let cls of doc.classesArray) {
|
|
|
|
if (classes.has(cls.name))
|
|
|
|
errors.push(`Duplicate declaration of class ${cls.name}`);
|
|
|
|
classes.add(cls.name);
|
2017-07-14 05:52:02 +00:00
|
|
|
let members = new Set();
|
|
|
|
for (let member of cls.membersArray) {
|
|
|
|
if (members.has(member.name))
|
|
|
|
errors.push(`Duplicate declaration of method ${cls.name}.${member.name}()`);
|
|
|
|
members.add(member.name);
|
2017-07-13 22:15:31 +00:00
|
|
|
let args = new Set();
|
2017-07-14 05:52:02 +00:00
|
|
|
for (let arg of member.argsArray) {
|
2017-07-13 22:15:31 +00:00
|
|
|
if (args.has(arg.name))
|
2017-07-14 05:52:02 +00:00
|
|
|
errors.push(`Duplicate declaration of argument ${cls.name}.${member.name} "${arg.name}"`);
|
2017-07-13 22:15:31 +00:00
|
|
|
args.add(arg.name);
|
|
|
|
}
|
|
|
|
}
|
2017-07-11 15:57:26 +00:00
|
|
|
}
|
2017-07-13 22:15:31 +00:00
|
|
|
return errors;
|
2017-07-11 15:57:26 +00:00
|
|
|
}
|
|
|
|
}
|
2017-07-07 16:36:45 +00:00
|
|
|
|
|
|
|
Documentation.Class = class {
|
|
|
|
/**
|
|
|
|
* @param {string} name
|
2017-07-14 05:52:02 +00:00
|
|
|
* @param {!Array<!Documentation.Member>} membersArray
|
2017-07-07 16:36:45 +00:00
|
|
|
*/
|
2017-07-14 05:52:02 +00:00
|
|
|
constructor(name, membersArray) {
|
2017-07-07 16:36:45 +00:00
|
|
|
this.name = name;
|
2017-07-14 05:52:02 +00:00
|
|
|
this.membersArray = membersArray;
|
|
|
|
this.members = new Map();
|
|
|
|
this.properties = new Map();
|
2017-07-07 16:36:45 +00:00
|
|
|
this.methods = new Map();
|
2017-07-14 20:03:21 +00:00
|
|
|
this.events = new Map();
|
2017-07-14 05:52:02 +00:00
|
|
|
for (let member of membersArray) {
|
|
|
|
this.members.set(member.name, member);
|
|
|
|
if (member.type === 'method')
|
|
|
|
this.methods.set(member.name, member);
|
|
|
|
else if (member.type === 'property')
|
|
|
|
this.properties.set(member.name, member);
|
2017-07-14 20:03:21 +00:00
|
|
|
else if (member.type === 'event')
|
|
|
|
this.events.set(member.name, member);
|
2017-07-14 05:52:02 +00:00
|
|
|
}
|
2017-07-07 16:36:45 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2017-07-14 05:52:02 +00:00
|
|
|
Documentation.Member = class {
|
2017-07-11 15:57:26 +00:00
|
|
|
/**
|
2017-07-14 05:52:02 +00:00
|
|
|
* @param {string} type
|
2017-07-11 15:57:26 +00:00
|
|
|
* @param {string} name
|
2017-07-13 22:15:31 +00:00
|
|
|
* @param {!Array<!Documentation.Argument>} argsArray
|
2017-07-13 22:54:37 +00:00
|
|
|
* @param {boolean} hasReturn
|
|
|
|
* @param {boolean} async
|
2017-07-11 15:57:26 +00:00
|
|
|
*/
|
2017-07-13 22:54:37 +00:00
|
|
|
constructor(type, name, argsArray, hasReturn, async) {
|
2017-07-14 05:52:02 +00:00
|
|
|
this.type = type;
|
2017-07-11 15:57:26 +00:00
|
|
|
this.name = name;
|
2017-07-13 22:15:31 +00:00
|
|
|
this.argsArray = argsArray;
|
|
|
|
this.args = new Map();
|
2017-07-13 22:54:37 +00:00
|
|
|
this.hasReturn = hasReturn;
|
|
|
|
this.async = async;
|
2017-07-13 22:15:31 +00:00
|
|
|
for (let arg of argsArray)
|
|
|
|
this.args.set(arg.name, arg);
|
2017-07-11 15:57:26 +00:00
|
|
|
}
|
2017-07-14 05:52:02 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {string} name
|
|
|
|
* @param {!Array<!Documentation.Argument>} argsArray
|
2017-07-13 22:54:37 +00:00
|
|
|
* @param {boolean} hasReturn
|
2017-07-14 05:52:02 +00:00
|
|
|
* @return {!Documentation.Member}
|
|
|
|
*/
|
2017-07-13 22:54:37 +00:00
|
|
|
static createMethod(name, argsArray, hasReturn, async) {
|
|
|
|
return new Documentation.Member('method', name, argsArray, hasReturn, async);
|
2017-07-14 05:52:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {string} name
|
|
|
|
* @return {!Documentation.Member}
|
|
|
|
*/
|
|
|
|
static createProperty(name) {
|
2017-07-13 22:54:37 +00:00
|
|
|
return new Documentation.Member('property', name, [], false, false);
|
2017-07-14 05:52:02 +00:00
|
|
|
}
|
2017-07-14 20:03:21 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {string} name
|
|
|
|
* @return {!Documentation.Member}
|
|
|
|
*/
|
|
|
|
static createEvent(name) {
|
|
|
|
return new Documentation.Member('event', name, [], false, false);
|
|
|
|
}
|
2017-07-11 15:57:26 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
Documentation.Argument = class {
|
2017-07-07 16:36:45 +00:00
|
|
|
/**
|
|
|
|
* @param {string} name
|
|
|
|
*/
|
|
|
|
constructor(name) {
|
|
|
|
this.name = name;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2017-07-11 15:57:26 +00:00
|
|
|
/**
|
|
|
|
* @param {!Array<string>} actual
|
|
|
|
* @param {!Array<string>} expected
|
|
|
|
* @return {{extra: !Array<string>, missing: !Array<string>, equal: !Array<string>}}
|
|
|
|
*/
|
|
|
|
function diff(actual, expected) {
|
|
|
|
const N = actual.length;
|
|
|
|
const M = expected.length;
|
|
|
|
if (N === 0 && M === 0)
|
|
|
|
return { extra: [], missing: [], equal: []};
|
|
|
|
if (N === 0)
|
|
|
|
return {extra: [], missing: expected.slice(), equal: []};
|
|
|
|
if (M === 0)
|
|
|
|
return {extra: actual.slice(), missing: [], equal: []};
|
|
|
|
let d = new Array(N);
|
|
|
|
let bt = new Array(N);
|
|
|
|
for (let i = 0; i < N; ++i) {
|
|
|
|
d[i] = new Array(M);
|
|
|
|
bt[i] = new Array(M);
|
|
|
|
for (let j = 0; j < M; ++j) {
|
|
|
|
const top = val(i - 1, j);
|
|
|
|
const left = val(i, j - 1);
|
|
|
|
if (top > left) {
|
|
|
|
d[i][j] = top;
|
|
|
|
bt[i][j] = 'extra';
|
|
|
|
} else {
|
|
|
|
d[i][j] = left;
|
|
|
|
bt[i][j] = 'missing';
|
|
|
|
}
|
|
|
|
let diag = val(i - 1, j - 1);
|
|
|
|
if (actual[i] === expected[j] && d[i][j] < diag + 1) {
|
|
|
|
d[i][j] = diag + 1;
|
|
|
|
bt[i][j] = 'eq';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Backtrack results.
|
|
|
|
let i = N - 1;
|
|
|
|
let j = M - 1;
|
|
|
|
let missing = [];
|
|
|
|
let extra = [];
|
|
|
|
let equal = [];
|
|
|
|
while (i >= 0 && j >= 0) {
|
|
|
|
switch (bt[i][j]) {
|
|
|
|
case 'extra':
|
|
|
|
extra.push(actual[i]);
|
|
|
|
i -= 1;
|
|
|
|
break;
|
|
|
|
case 'missing':
|
|
|
|
missing.push(expected[j]);
|
|
|
|
j -= 1;
|
|
|
|
break;
|
|
|
|
case 'eq':
|
|
|
|
equal.push(actual[i]);
|
|
|
|
i -= 1;
|
|
|
|
j -= 1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
while (i >= 0)
|
|
|
|
extra.push(actual[i--]);
|
|
|
|
while (j >= 0)
|
|
|
|
missing.push(expected[j--]);
|
|
|
|
extra.reverse();
|
|
|
|
missing.reverse();
|
|
|
|
equal.reverse();
|
|
|
|
return {extra, missing, equal};
|
|
|
|
|
|
|
|
function val(i, j) {
|
|
|
|
return i < 0 || j < 0 ? 0 : d[i][j];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-07-07 16:36:45 +00:00
|
|
|
module.exports = Documentation;
|
2017-07-11 15:57:26 +00:00
|
|
|
|