2017-07-28 08:09:26 +00:00
|
|
|
/**
|
|
|
|
* Copyright 2017 Google Inc. All rights reserved.
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
2017-07-12 18:42:36 +00:00
|
|
|
const jsBuilder = require('./JSBuilder');
|
|
|
|
const mdBuilder = require('./MDBuilder');
|
|
|
|
const Documentation = require('./Documentation');
|
2017-07-31 04:49:04 +00:00
|
|
|
const Message = require('../Message');
|
2017-07-12 18:42:36 +00:00
|
|
|
|
2017-07-21 17:27:53 +00:00
|
|
|
const EXCLUDE_CLASSES = new Set([
|
2018-01-04 02:21:40 +00:00
|
|
|
'CSSCoverage',
|
2017-07-12 18:42:36 +00:00
|
|
|
'Connection',
|
2018-08-09 23:51:12 +00:00
|
|
|
'CustomError',
|
2017-07-18 01:13:04 +00:00
|
|
|
'EmulationManager',
|
2017-07-12 18:42:36 +00:00
|
|
|
'FrameManager',
|
2018-01-03 03:53:53 +00:00
|
|
|
'JSCoverage',
|
2017-07-12 18:42:36 +00:00
|
|
|
'Helper',
|
2017-08-15 01:08:06 +00:00
|
|
|
'Launcher',
|
2017-08-12 00:24:31 +00:00
|
|
|
'Multimap',
|
2017-07-19 01:54:24 +00:00
|
|
|
'NavigatorWatcher',
|
2017-07-12 18:42:36 +00:00
|
|
|
'NetworkManager',
|
2018-09-07 20:36:16 +00:00
|
|
|
'PipeTransport',
|
2017-07-19 05:10:38 +00:00
|
|
|
'TaskQueue',
|
2017-07-24 16:58:51 +00:00
|
|
|
'WaitTask',
|
2018-09-07 20:36:16 +00:00
|
|
|
'WebSocketTransport',
|
2017-07-12 18:42:36 +00:00
|
|
|
]);
|
|
|
|
|
2018-08-09 23:51:12 +00:00
|
|
|
const EXCLUDE_PROPERTIES = new Set([
|
2017-10-18 02:14:57 +00:00
|
|
|
'Browser.create',
|
2017-07-12 18:42:36 +00:00
|
|
|
'Headers.fromPayload',
|
|
|
|
'Page.create',
|
2017-10-10 17:54:20 +00:00
|
|
|
'JSHandle.toString',
|
2018-08-09 23:51:12 +00:00
|
|
|
'TimeoutError.name',
|
2017-07-12 18:42:36 +00:00
|
|
|
]);
|
|
|
|
|
2017-07-13 08:25:32 +00:00
|
|
|
/**
|
|
|
|
* @param {!Page} page
|
2017-07-31 04:49:04 +00:00
|
|
|
* @param {!Array<!Source>} mdSources
|
|
|
|
* @param {!Array<!Source>} jsSources
|
|
|
|
* @return {!Promise<!Array<!Message>>}
|
2017-07-13 08:25:32 +00:00
|
|
|
*/
|
2017-07-31 04:49:04 +00:00
|
|
|
module.exports = async function lint(page, mdSources, jsSources) {
|
2017-08-21 23:39:04 +00:00
|
|
|
const mdResult = await mdBuilder(page, mdSources);
|
|
|
|
const jsResult = await jsBuilder(jsSources);
|
|
|
|
const jsDocumentation = filterJSDocumentation(jsResult.documentation);
|
|
|
|
const mdDocumentation = mdResult.documentation;
|
2017-07-12 18:42:36 +00:00
|
|
|
|
2017-08-21 23:39:04 +00:00
|
|
|
const jsErrors = jsResult.errors;
|
2017-07-21 21:17:21 +00:00
|
|
|
jsErrors.push(...checkDuplicates(jsDocumentation));
|
2017-07-21 17:27:53 +00:00
|
|
|
|
2017-08-21 23:39:04 +00:00
|
|
|
const mdErrors = mdResult.errors;
|
2017-07-21 21:17:21 +00:00
|
|
|
mdErrors.push(...compareDocumentations(mdDocumentation, jsDocumentation));
|
|
|
|
mdErrors.push(...checkDuplicates(mdDocumentation));
|
|
|
|
mdErrors.push(...checkSorting(mdDocumentation));
|
2017-07-13 22:15:31 +00:00
|
|
|
|
|
|
|
// Push all errors with proper prefixes
|
2017-08-21 23:39:04 +00:00
|
|
|
const errors = jsErrors.map(error => '[JavaScript] ' + error);
|
2017-07-13 22:15:31 +00:00
|
|
|
errors.push(...mdErrors.map(error => '[MarkDown] ' + error));
|
2017-07-31 04:49:04 +00:00
|
|
|
return errors.map(error => Message.error(error));
|
2017-07-21 21:17:21 +00:00
|
|
|
};
|
2017-07-13 22:15:31 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {!Documentation} doc
|
|
|
|
* @return {!Array<string>}
|
|
|
|
*/
|
2017-07-21 21:17:21 +00:00
|
|
|
function checkSorting(doc) {
|
2017-07-13 22:15:31 +00:00
|
|
|
const errors = [];
|
2017-08-21 23:39:04 +00:00
|
|
|
for (const cls of doc.classesArray) {
|
|
|
|
const members = cls.membersArray;
|
2017-07-14 20:03:21 +00:00
|
|
|
|
|
|
|
// 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.
|
2017-08-21 23:39:04 +00:00
|
|
|
const constructorIndex = members.findIndex(member => member.type === 'method' && member.name === 'constructor');
|
2017-07-14 20:03:21 +00:00
|
|
|
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) {
|
2017-08-21 23:39:04 +00:00
|
|
|
const member1 = cls.membersArray[i];
|
|
|
|
const member2 = cls.membersArray[i + 1];
|
2017-07-14 20:03:21 +00:00
|
|
|
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-08-21 23:39:04 +00:00
|
|
|
const member1 = cls.membersArray[i];
|
|
|
|
const 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-21 19:41:49 +00:00
|
|
|
let memberName1 = `${cls.name}.${member1.name}`;
|
2017-07-14 20:03:21 +00:00
|
|
|
if (member1.type === 'method')
|
2017-07-21 19:41:49 +00:00
|
|
|
memberName1 += '()';
|
|
|
|
let memberName2 = `${cls.name}.${member2.name}`;
|
|
|
|
if (member2.type === 'method')
|
|
|
|
memberName2 += '()';
|
|
|
|
errors.push(`Bad alphabetic ordering of ${cls.name} members: ${memberName1} should go after ${memberName2}`);
|
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.
|
2017-08-21 23:39:04 +00:00
|
|
|
const classes = [];
|
|
|
|
for (const cls of jsDocumentation.classesArray) {
|
2017-07-12 18:42:36 +00:00
|
|
|
if (EXCLUDE_CLASSES.has(cls.name))
|
|
|
|
continue;
|
2017-08-21 23:39:04 +00:00
|
|
|
const members = cls.membersArray.filter(member => {
|
2017-07-14 05:52:02 +00:00
|
|
|
if (member.name.startsWith('_'))
|
2017-07-12 18:42:36 +00:00
|
|
|
return false;
|
2017-10-02 21:28:51 +00:00
|
|
|
// Exclude all constructors by default.
|
|
|
|
if (member.name === 'constructor' && member.type === 'method')
|
|
|
|
return false;
|
2018-08-09 23:51:12 +00:00
|
|
|
return !EXCLUDE_PROPERTIES.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
|
|
|
|
2017-07-21 21:17:21 +00:00
|
|
|
/**
|
|
|
|
* @param {!Documentation} doc
|
|
|
|
* @return {!Array<string>}
|
|
|
|
*/
|
|
|
|
function checkDuplicates(doc) {
|
|
|
|
const errors = [];
|
2017-08-21 23:39:04 +00:00
|
|
|
const classes = new Set();
|
2017-07-21 21:17:21 +00:00
|
|
|
// Report duplicates.
|
2017-08-21 23:39:04 +00:00
|
|
|
for (const cls of doc.classesArray) {
|
2017-07-21 21:17:21 +00:00
|
|
|
if (classes.has(cls.name))
|
|
|
|
errors.push(`Duplicate declaration of class ${cls.name}`);
|
|
|
|
classes.add(cls.name);
|
2017-08-21 23:39:04 +00:00
|
|
|
const members = new Set();
|
|
|
|
for (const member of cls.membersArray) {
|
2017-09-14 04:27:14 +00:00
|
|
|
if (members.has(member.type + ' ' + member.name))
|
|
|
|
errors.push(`Duplicate declaration of ${member.type} ${cls.name}.${member.name}()`);
|
|
|
|
members.add(member.type + ' ' + member.name);
|
2017-08-21 23:39:04 +00:00
|
|
|
const args = new Set();
|
|
|
|
for (const arg of member.argsArray) {
|
2017-07-21 21:17:21 +00:00
|
|
|
if (args.has(arg.name))
|
|
|
|
errors.push(`Duplicate declaration of argument ${cls.name}.${member.name} "${arg.name}"`);
|
|
|
|
args.add(arg.name);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return errors;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {!Documentation} actual
|
|
|
|
* @param {!Documentation} expected
|
|
|
|
* @return {!Array<string>}
|
|
|
|
*/
|
|
|
|
function compareDocumentations(actual, expected) {
|
|
|
|
const errors = [];
|
|
|
|
|
|
|
|
const actualClasses = Array.from(actual.classes.keys()).sort();
|
|
|
|
const expectedClasses = Array.from(expected.classes.keys()).sort();
|
2017-08-21 23:39:04 +00:00
|
|
|
const classesDiff = diff(actualClasses, expectedClasses);
|
|
|
|
for (const className of classesDiff.extra)
|
2017-07-21 21:17:21 +00:00
|
|
|
errors.push(`Non-existing class found: ${className}`);
|
2017-08-21 23:39:04 +00:00
|
|
|
for (const className of classesDiff.missing)
|
2017-07-21 21:17:21 +00:00
|
|
|
errors.push(`Class not found: ${className}`);
|
|
|
|
|
2017-08-21 23:39:04 +00:00
|
|
|
for (const className of classesDiff.equal) {
|
2017-07-21 21:17:21 +00:00
|
|
|
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-08-21 23:39:04 +00:00
|
|
|
for (const methodName of methodDiff.extra)
|
2017-07-21 21:17:21 +00:00
|
|
|
errors.push(`Non-existing method found: ${className}.${methodName}()`);
|
2017-08-21 23:39:04 +00:00
|
|
|
for (const methodName of methodDiff.missing)
|
2017-07-21 21:17:21 +00:00
|
|
|
errors.push(`Method not found: ${className}.${methodName}()`);
|
|
|
|
|
2017-08-21 23:39:04 +00:00
|
|
|
for (const methodName of methodDiff.equal) {
|
2017-07-21 21:17:21 +00:00
|
|
|
const actualMethod = actualClass.methods.get(methodName);
|
|
|
|
const expectedMethod = expectedClass.methods.get(methodName);
|
|
|
|
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`);
|
|
|
|
}
|
|
|
|
const actualArgs = Array.from(actualMethod.args.keys());
|
|
|
|
const expectedArgs = Array.from(expectedMethod.args.keys());
|
|
|
|
const argDiff = diff(actualArgs, expectedArgs);
|
|
|
|
if (argDiff.extra.length || argDiff.missing.length) {
|
2017-08-21 23:39:04 +00:00
|
|
|
const text = [`Method ${className}.${methodName}() fails to describe its parameters:`];
|
|
|
|
for (const arg of argDiff.missing)
|
2017-07-21 21:17:21 +00:00
|
|
|
text.push(`- Argument not found: ${arg}`);
|
2017-08-21 23:39:04 +00:00
|
|
|
for (const arg of argDiff.extra)
|
2017-07-21 21:17:21 +00:00
|
|
|
text.push(`- Non-existing argument found: ${arg}`);
|
|
|
|
errors.push(text.join('\n'));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
const actualProperties = Array.from(actualClass.properties.keys()).sort();
|
|
|
|
const expectedProperties = Array.from(expectedClass.properties.keys()).sort();
|
|
|
|
const propertyDiff = diff(actualProperties, expectedProperties);
|
2017-08-21 23:39:04 +00:00
|
|
|
for (const propertyName of propertyDiff.extra)
|
2017-07-21 21:17:21 +00:00
|
|
|
errors.push(`Non-existing property found: ${className}.${propertyName}`);
|
2017-08-21 23:39:04 +00:00
|
|
|
for (const propertyName of propertyDiff.missing)
|
2017-07-21 21:17:21 +00:00
|
|
|
errors.push(`Property not found: ${className}.${propertyName}`);
|
|
|
|
|
|
|
|
const actualEvents = Array.from(actualClass.events.keys()).sort();
|
|
|
|
const expectedEvents = Array.from(expectedClass.events.keys()).sort();
|
|
|
|
const eventsDiff = diff(actualEvents, expectedEvents);
|
2017-08-21 23:39:04 +00:00
|
|
|
for (const eventName of eventsDiff.extra)
|
2017-07-21 21:17:21 +00:00
|
|
|
errors.push(`Non-existing event found in class ${className}: '${eventName}'`);
|
2017-08-21 23:39:04 +00:00
|
|
|
for (const eventName of eventsDiff.missing)
|
2017-07-21 21:17:21 +00:00
|
|
|
errors.push(`Event not found in class ${className}: '${eventName}'`);
|
|
|
|
}
|
|
|
|
return errors;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @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: []};
|
2017-08-21 23:39:04 +00:00
|
|
|
const d = new Array(N);
|
|
|
|
const bt = new Array(N);
|
2017-07-21 21:17:21 +00:00
|
|
|
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';
|
|
|
|
}
|
2017-08-21 23:39:04 +00:00
|
|
|
const diag = val(i - 1, j - 1);
|
2017-07-21 21:17:21 +00:00
|
|
|
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;
|
2017-08-21 23:39:04 +00:00
|
|
|
const missing = [];
|
|
|
|
const extra = [];
|
|
|
|
const equal = [];
|
2017-07-21 21:17:21 +00:00
|
|
|
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];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|