2017-07-12 18:42:36 +00:00
|
|
|
const jsBuilder = require('./JSBuilder');
|
|
|
|
const mdBuilder = require('./MDBuilder');
|
|
|
|
const Documentation = require('./Documentation');
|
|
|
|
|
2017-07-21 17:27:53 +00:00
|
|
|
const EXCLUDE_CLASSES = new Set([
|
2017-07-20 02:04:51 +00:00
|
|
|
'AwaitedElement',
|
2017-07-12 18:42:36 +00:00
|
|
|
'Connection',
|
2017-07-18 01:13:04 +00:00
|
|
|
'EmulationManager',
|
2017-07-12 18:42:36 +00:00
|
|
|
'FrameManager',
|
|
|
|
'Helper',
|
2017-07-19 01:54:24 +00:00
|
|
|
'NavigatorWatcher',
|
2017-07-12 18:42:36 +00:00
|
|
|
'NetworkManager',
|
2017-07-19 05:10:38 +00:00
|
|
|
'ProxyStream',
|
|
|
|
'TaskQueue',
|
2017-07-12 18:42:36 +00:00
|
|
|
]);
|
|
|
|
|
2017-07-21 17:27:53 +00:00
|
|
|
const EXCLUDE_METHODS = new Set([
|
2017-07-12 18:42:36 +00:00
|
|
|
'Body.constructor',
|
|
|
|
'Dialog.constructor',
|
|
|
|
'Frame.constructor',
|
|
|
|
'Headers.constructor',
|
|
|
|
'Headers.fromPayload',
|
|
|
|
'InterceptedRequest.constructor',
|
2017-07-19 05:10:38 +00:00
|
|
|
'Keyboard.constructor',
|
2017-07-12 18:42:36 +00:00
|
|
|
'Page.constructor',
|
|
|
|
'Page.create',
|
|
|
|
'Request.constructor',
|
|
|
|
'Response.constructor',
|
|
|
|
]);
|
|
|
|
|
2017-07-13 08:25:32 +00:00
|
|
|
/**
|
|
|
|
* @param {!Page} page
|
|
|
|
* @param {string} docsFolderPath
|
|
|
|
* @param {string} jsFolderPath
|
|
|
|
* @return {!Promise<!Array<string>>}
|
|
|
|
*/
|
|
|
|
async function lint(page, docsFolderPath, jsFolderPath) {
|
|
|
|
let mdResult = await mdBuilder(page, docsFolderPath);
|
|
|
|
let jsResult = await jsBuilder(jsFolderPath);
|
2017-07-21 17:27:53 +00:00
|
|
|
let jsDocumentation = filterJSDocumentation(jsResult.documentation);
|
2017-07-12 18:42:36 +00:00
|
|
|
let mdDocumentation = mdResult.documentation;
|
|
|
|
|
2017-07-21 17:27:53 +00:00
|
|
|
let jsErrors = jsResult.errors;
|
2017-07-13 22:15:31 +00:00
|
|
|
jsErrors.push(...Documentation.validate(jsDocumentation));
|
2017-07-21 17:27:53 +00:00
|
|
|
|
|
|
|
let mdErrors = mdResult.errors;
|
|
|
|
mdErrors.push(...Documentation.diff(mdDocumentation, jsDocumentation));
|
2017-07-13 22:15:31 +00:00
|
|
|
mdErrors.push(...Documentation.validate(mdDocumentation));
|
|
|
|
mdErrors.push(...lintMarkdown(mdDocumentation));
|
|
|
|
|
|
|
|
// Push all errors with proper prefixes
|
|
|
|
let errors = jsErrors.map(error => '[JavaScript] ' + error);
|
|
|
|
errors.push(...mdErrors.map(error => '[MarkDown] ' + error));
|
|
|
|
return errors;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {!Documentation} doc
|
|
|
|
* @return {!Array<string>}
|
|
|
|
*/
|
|
|
|
function lintMarkdown(doc) {
|
|
|
|
const errors = [];
|
|
|
|
for (let cls of doc.classesArray) {
|
2017-07-14 20:03:21 +00:00
|
|
|
let members = cls.membersArray;
|
|
|
|
|
|
|
|
// Events should go first.
|
|
|
|
let eventIndex = 0;
|
|
|
|
for (; eventIndex < members.length && members[eventIndex].type === 'event'; ++eventIndex);
|
|
|
|
for (; eventIndex < members.length && members[eventIndex].type !== 'event'; ++eventIndex);
|
|
|
|
if (eventIndex < members.length)
|
|
|
|
errors.push(`Events should go first. Event '${members[eventIndex].name}' in class ${cls.name} breaks order`);
|
|
|
|
|
|
|
|
// Constructor should be right after events and before all other members.
|
|
|
|
let constructorIndex = members.findIndex(member => member.type === 'method' && member.name === 'constructor');
|
|
|
|
if (constructorIndex > 0 && members[constructorIndex - 1].type !== 'event')
|
|
|
|
errors.push(`Constructor of ${cls.name} should go before other methods`);
|
|
|
|
|
|
|
|
// Events should be sorted alphabetically.
|
|
|
|
for (let i = 0; i < members.length - 1; ++i) {
|
|
|
|
let member1 = cls.membersArray[i];
|
|
|
|
let member2 = cls.membersArray[i + 1];
|
|
|
|
if (member1.type !== 'event' || member2.type !== 'event')
|
2017-07-13 08:25:32 +00:00
|
|
|
continue;
|
2017-07-14 20:03:21 +00:00
|
|
|
if (member1.name > member2.name)
|
|
|
|
errors.push(`Event '${member1.name}' in class ${cls.name} breaks alphabetic ordering of events`);
|
|
|
|
}
|
|
|
|
|
|
|
|
// All other members should be sorted alphabetically.
|
|
|
|
for (let i = 0; i < members.length - 1; ++i) {
|
2017-07-14 05:52:02 +00:00
|
|
|
let member1 = cls.membersArray[i];
|
|
|
|
let member2 = cls.membersArray[i + 1];
|
2017-07-14 20:03:21 +00:00
|
|
|
if (member1.type === 'event' || member2.type === 'event')
|
|
|
|
continue;
|
|
|
|
if (member1.type === 'method' && member1.name === 'constructor')
|
|
|
|
continue;
|
2017-07-14 05:52:02 +00:00
|
|
|
if (member1.name > member2.name) {
|
2017-07-14 20:03:21 +00:00
|
|
|
let memberName = `${cls.name}.${member1.name}`;
|
|
|
|
if (member1.type === 'method')
|
|
|
|
memberName += '()';
|
|
|
|
errors.push(`${memberName} breaks alphabetic ordering of class members.`);
|
2017-07-14 05:52:02 +00:00
|
|
|
}
|
2017-07-13 08:25:32 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return errors;
|
2017-07-12 18:42:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {!Documentation} jsDocumentation
|
|
|
|
* @return {!Documentation}
|
|
|
|
*/
|
|
|
|
function filterJSDocumentation(jsDocumentation) {
|
|
|
|
// Filter classes and methods.
|
|
|
|
let classes = [];
|
|
|
|
for (let cls of jsDocumentation.classesArray) {
|
|
|
|
if (EXCLUDE_CLASSES.has(cls.name))
|
|
|
|
continue;
|
2017-07-14 05:52:02 +00:00
|
|
|
let members = cls.membersArray.filter(member => {
|
|
|
|
if (member.name.startsWith('_'))
|
2017-07-12 18:42:36 +00:00
|
|
|
return false;
|
2017-07-14 05:52:02 +00:00
|
|
|
return !EXCLUDE_METHODS.has(`${cls.name}.${member.name}`);
|
2017-07-12 18:42:36 +00:00
|
|
|
});
|
2017-07-14 05:52:02 +00:00
|
|
|
classes.push(new Documentation.Class(cls.name, members));
|
2017-07-12 18:42:36 +00:00
|
|
|
}
|
|
|
|
return new Documentation(classes);
|
|
|
|
}
|
2017-07-13 08:25:32 +00:00
|
|
|
|
|
|
|
module.exports = lint;
|