2019-11-19 05:18:28 +03: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.
|
|
|
|
*/
|
|
|
|
|
2020-12-27 01:31:41 +03:00
|
|
|
// @ts-check
|
|
|
|
|
|
|
|
/** @typedef {{
|
|
|
|
* type: 'text' | 'li' | 'code' | 'gen' | 'h1' | 'h2' | 'h3' | 'h4',
|
|
|
|
* text?: string,
|
|
|
|
* codeLang?: string,
|
|
|
|
* lines?: string[],
|
|
|
|
* liType?: 'default' | 'bullet' | 'ordinal',
|
|
|
|
* children?: MarkdownNode[]
|
|
|
|
* }} MarkdownNode */
|
|
|
|
|
2019-11-19 05:18:28 +03:00
|
|
|
class Documentation {
|
|
|
|
/**
|
|
|
|
* @param {!Array<!Documentation.Class>} classesArray
|
|
|
|
*/
|
|
|
|
constructor(classesArray) {
|
|
|
|
this.classesArray = classesArray;
|
|
|
|
/** @type {!Map<string, !Documentation.Class>} */
|
|
|
|
this.classes = new Map();
|
|
|
|
for (const cls of classesArray)
|
|
|
|
this.classes.set(cls.name, cls);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Documentation.Class = class {
|
|
|
|
/**
|
|
|
|
* @param {string} name
|
|
|
|
* @param {!Array<!Documentation.Member>} membersArray
|
|
|
|
* @param {?string=} extendsName
|
2020-12-27 01:31:41 +03:00
|
|
|
* @param {MarkdownNode[]=} spec
|
2020-03-20 11:30:35 +03:00
|
|
|
* @param {string[]=} templates
|
2019-11-19 05:18:28 +03:00
|
|
|
*/
|
2020-12-27 01:31:41 +03:00
|
|
|
constructor(name, membersArray, extendsName = null, spec = undefined, templates = []) {
|
2019-11-19 05:18:28 +03:00
|
|
|
this.name = name;
|
|
|
|
this.membersArray = membersArray;
|
2020-12-27 01:31:41 +03:00
|
|
|
this.spec = spec;
|
2019-12-21 00:07:14 +03:00
|
|
|
this.extends = extendsName;
|
2020-03-20 11:30:35 +03:00
|
|
|
this.templates = templates;
|
2020-12-27 01:31:41 +03:00
|
|
|
this.comment = '';
|
2019-12-21 00:07:14 +03:00
|
|
|
this.index();
|
|
|
|
}
|
|
|
|
|
|
|
|
index() {
|
2019-11-19 05:18:28 +03:00
|
|
|
/** @type {!Map<string, !Documentation.Member>} */
|
|
|
|
this.members = new Map();
|
|
|
|
/** @type {!Map<string, !Documentation.Member>} */
|
|
|
|
this.properties = new Map();
|
|
|
|
/** @type {!Array<!Documentation.Member>} */
|
|
|
|
this.propertiesArray = [];
|
|
|
|
/** @type {!Map<string, !Documentation.Member>} */
|
2020-12-27 01:31:41 +03:00
|
|
|
this.options = new Map();
|
|
|
|
/** @type {!Array<!Documentation.Member>} */
|
|
|
|
this.optionsArray = [];
|
|
|
|
/** @type {!Map<string, !Documentation.Member>} */
|
2019-11-19 05:18:28 +03:00
|
|
|
this.methods = new Map();
|
|
|
|
/** @type {!Array<!Documentation.Member>} */
|
|
|
|
this.methodsArray = [];
|
|
|
|
/** @type {!Map<string, !Documentation.Member>} */
|
|
|
|
this.events = new Map();
|
|
|
|
/** @type {!Array<!Documentation.Member>} */
|
|
|
|
this.eventsArray = [];
|
2019-12-21 00:07:14 +03:00
|
|
|
|
|
|
|
for (const member of this.membersArray) {
|
2019-11-19 05:18:28 +03:00
|
|
|
this.members.set(member.name, member);
|
|
|
|
if (member.kind === 'method') {
|
|
|
|
this.methods.set(member.name, member);
|
|
|
|
this.methodsArray.push(member);
|
|
|
|
} else if (member.kind === 'property') {
|
|
|
|
this.properties.set(member.name, member);
|
|
|
|
this.propertiesArray.push(member);
|
|
|
|
} else if (member.kind === 'event') {
|
|
|
|
this.events.set(member.name, member);
|
|
|
|
this.eventsArray.push(member);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-12-21 03:57:21 +03:00
|
|
|
|
2020-02-25 05:24:02 +03:00
|
|
|
validateOrder(errors, cls) {
|
2019-12-21 03:57:21 +03:00
|
|
|
const members = this.membersArray;
|
|
|
|
// Events should go first.
|
|
|
|
let eventIndex = 0;
|
|
|
|
for (; eventIndex < members.length && members[eventIndex].kind === 'event'; ++eventIndex);
|
|
|
|
for (; eventIndex < members.length && members[eventIndex].kind !== '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.
|
|
|
|
const constructorIndex = members.findIndex(member => member.kind === 'method' && member.name === 'constructor');
|
|
|
|
if (constructorIndex > 0 && members[constructorIndex - 1].kind !== '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) {
|
|
|
|
const member1 = this.membersArray[i];
|
|
|
|
const member2 = this.membersArray[i + 1];
|
|
|
|
if (member1.kind !== 'event' || member2.kind !== 'event')
|
|
|
|
continue;
|
|
|
|
if (member1.name > member2.name)
|
|
|
|
errors.push(`Event '${member1.name}' in class ${this.name} breaks alphabetic ordering of events`);
|
|
|
|
}
|
|
|
|
|
|
|
|
// All other members should be sorted alphabetically.
|
|
|
|
for (let i = 0; i < members.length - 1; ++i) {
|
|
|
|
const member1 = this.membersArray[i];
|
|
|
|
const member2 = this.membersArray[i + 1];
|
|
|
|
if (member1.kind === 'event' || member2.kind === 'event')
|
|
|
|
continue;
|
|
|
|
if (member1.kind === 'method' && member1.name === 'constructor')
|
|
|
|
continue;
|
2020-04-01 23:09:24 +03:00
|
|
|
if (member1.name.replace(/^\$+/, '$') > member2.name.replace(/^\$+/, '$')) {
|
2019-12-21 03:57:21 +03:00
|
|
|
let memberName1 = `${this.name}.${member1.name}`;
|
|
|
|
if (member1.kind === 'method')
|
|
|
|
memberName1 += '()';
|
|
|
|
let memberName2 = `${this.name}.${member2.name}`;
|
|
|
|
if (member2.kind === 'method')
|
|
|
|
memberName2 += '()';
|
|
|
|
errors.push(`Bad alphabetic ordering of ${this.name} members: ${memberName1} should go after ${memberName2}`);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-12-27 01:31:41 +03:00
|
|
|
|
|
|
|
visit(visitor) {
|
|
|
|
visitor(this);
|
|
|
|
for (const p of this.propertiesArray)
|
|
|
|
p.visit(visitor);
|
|
|
|
for (const m of this.methodsArray)
|
|
|
|
m.visit(visitor);
|
|
|
|
for (const e of this.eventsArray)
|
|
|
|
e.visit(visitor);
|
|
|
|
}
|
2019-11-19 05:18:28 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
Documentation.Member = class {
|
|
|
|
/**
|
|
|
|
* @param {string} kind
|
|
|
|
* @param {string} name
|
|
|
|
* @param {?Documentation.Type} type
|
|
|
|
* @param {!Array<!Documentation.Member>} argsArray
|
2020-12-27 01:31:41 +03:00
|
|
|
* @param {MarkdownNode[]=} spec
|
2020-03-20 11:30:35 +03:00
|
|
|
* @param {boolean=} required
|
|
|
|
* @param {string[]=} templates
|
2019-11-19 05:18:28 +03:00
|
|
|
*/
|
2020-12-27 01:31:41 +03:00
|
|
|
constructor(kind, name, type, argsArray, spec = undefined, required = true, templates = []) {
|
2019-11-19 05:18:28 +03:00
|
|
|
this.kind = kind;
|
|
|
|
this.name = name;
|
|
|
|
this.type = type;
|
2020-12-27 01:31:41 +03:00
|
|
|
this.spec = spec;
|
2019-11-19 05:18:28 +03:00
|
|
|
this.argsArray = argsArray;
|
|
|
|
this.required = required;
|
2020-03-20 11:30:35 +03:00
|
|
|
this.templates = templates;
|
2020-12-27 01:31:41 +03:00
|
|
|
this.comment = '';
|
2019-11-19 05:18:28 +03:00
|
|
|
/** @type {!Map<string, !Documentation.Member>} */
|
|
|
|
this.args = new Map();
|
|
|
|
for (const arg of argsArray)
|
|
|
|
this.args.set(arg.name, arg);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {string} name
|
|
|
|
* @param {!Array<!Documentation.Member>} argsArray
|
|
|
|
* @param {?Documentation.Type} returnType
|
2020-12-27 01:31:41 +03:00
|
|
|
* @param {MarkdownNode[]=} spec
|
2020-03-20 11:30:35 +03:00
|
|
|
* @param {string[]=} templates
|
2019-11-19 05:18:28 +03:00
|
|
|
* @return {!Documentation.Member}
|
|
|
|
*/
|
2020-12-27 01:31:41 +03:00
|
|
|
static createMethod(name, argsArray, returnType, spec, templates) {
|
|
|
|
return new Documentation.Member('method', name, returnType, argsArray, spec, undefined, templates);
|
2019-11-19 05:18:28 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {string} name
|
|
|
|
* @param {!Documentation.Type} type
|
2020-12-27 01:31:41 +03:00
|
|
|
* @param {MarkdownNode[]=} spec
|
2019-11-19 05:18:28 +03:00
|
|
|
* @param {boolean=} required
|
|
|
|
* @return {!Documentation.Member}
|
|
|
|
*/
|
2020-12-27 01:31:41 +03:00
|
|
|
static createProperty(name, type, spec, required) {
|
|
|
|
return new Documentation.Member('property', name, type, [], spec, required);
|
2019-11-19 05:18:28 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {string} name
|
|
|
|
* @param {?Documentation.Type=} type
|
2020-12-27 01:31:41 +03:00
|
|
|
* @param {MarkdownNode[]=} spec
|
2019-11-19 05:18:28 +03:00
|
|
|
* @return {!Documentation.Member}
|
|
|
|
*/
|
2020-12-27 01:31:41 +03:00
|
|
|
static createEvent(name, type = null, spec) {
|
|
|
|
return new Documentation.Member('event', name, type, [], spec);
|
|
|
|
}
|
|
|
|
|
|
|
|
visit(visitor) {
|
|
|
|
visitor(this);
|
|
|
|
if (this.type)
|
|
|
|
this.type.visit(visitor);
|
|
|
|
for (const arg of this.argsArray)
|
|
|
|
arg.visit(visitor);
|
2019-11-19 05:18:28 +03:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
Documentation.Type = class {
|
|
|
|
/**
|
|
|
|
* @param {string} name
|
|
|
|
* @param {!Array<!Documentation.Member>=} properties
|
|
|
|
*/
|
|
|
|
constructor(name, properties = []) {
|
|
|
|
this.name = name;
|
|
|
|
this.properties = properties;
|
|
|
|
}
|
2020-12-27 01:31:41 +03:00
|
|
|
|
|
|
|
visit(visitor) {
|
|
|
|
for (const p of this.properties || [])
|
|
|
|
p.visit(visitor);
|
|
|
|
}
|
2019-11-19 05:18:28 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
module.exports = Documentation;
|