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-07 16:36:45 +00:00
|
|
|
const esprima = require('esprima');
|
2017-08-24 19:20:05 +00:00
|
|
|
const ESTreeWalker = require('../../ESTreeWalker');
|
2017-07-07 16:36:45 +00:00
|
|
|
const Documentation = require('./Documentation');
|
|
|
|
|
|
|
|
class JSOutline {
|
|
|
|
constructor(text) {
|
|
|
|
this.classes = [];
|
2017-07-14 20:03:21 +00:00
|
|
|
this.errors = [];
|
|
|
|
this._eventsByClassName = new Map();
|
2017-07-07 16:36:45 +00:00
|
|
|
this._currentClassName = null;
|
2017-07-14 05:52:02 +00:00
|
|
|
this._currentClassMembers = [];
|
2017-07-07 16:36:45 +00:00
|
|
|
|
|
|
|
this._text = text;
|
2017-08-21 23:39:04 +00:00
|
|
|
const ast = esprima.parseScript(this._text, {loc: true, range: true});
|
|
|
|
const walker = new ESTreeWalker(node => {
|
2017-07-07 16:36:45 +00:00
|
|
|
if (node.type === 'ClassDeclaration')
|
|
|
|
this._onClassDeclaration(node);
|
|
|
|
else if (node.type === 'MethodDefinition')
|
|
|
|
this._onMethodDefinition(node);
|
2017-07-14 20:03:21 +00:00
|
|
|
else if (node.type === 'AssignmentExpression')
|
|
|
|
this._onAssignmentExpression(node);
|
2017-07-07 16:36:45 +00:00
|
|
|
});
|
|
|
|
walker.walk(ast);
|
|
|
|
this._flushClassIfNeeded();
|
2017-07-14 20:03:21 +00:00
|
|
|
this._recreateClassesWithEvents();
|
2017-07-07 16:36:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
_onClassDeclaration(node) {
|
|
|
|
this._flushClassIfNeeded();
|
2017-07-11 15:57:26 +00:00
|
|
|
this._currentClassName = this._extractText(node.id);
|
2017-07-07 16:36:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
_onMethodDefinition(node) {
|
|
|
|
console.assert(this._currentClassName !== null);
|
2017-07-11 15:57:26 +00:00
|
|
|
console.assert(node.value.type === 'FunctionExpression');
|
2017-08-21 23:39:04 +00:00
|
|
|
const methodName = this._extractText(node.key);
|
2017-07-14 06:11:39 +00:00
|
|
|
if (node.kind === 'get') {
|
2017-08-21 23:39:04 +00:00
|
|
|
const property = Documentation.Member.createProperty(methodName);
|
2017-07-14 06:11:39 +00:00
|
|
|
this._currentClassMembers.push(property);
|
|
|
|
return;
|
|
|
|
}
|
2017-07-13 22:54:37 +00:00
|
|
|
// Async functions have return value.
|
|
|
|
let hasReturn = node.value.async;
|
2017-07-12 15:01:21 +00:00
|
|
|
// Extract properties from constructor.
|
|
|
|
if (node.kind === 'constructor') {
|
2017-07-13 22:54:37 +00:00
|
|
|
// Extract properties from constructor.
|
2017-08-21 23:39:04 +00:00
|
|
|
const walker = new ESTreeWalker(node => {
|
2017-07-12 15:01:21 +00:00
|
|
|
if (node.type !== 'AssignmentExpression')
|
|
|
|
return;
|
|
|
|
node = node.left;
|
|
|
|
if (node.type === 'MemberExpression' && node.object &&
|
|
|
|
node.object.type === 'ThisExpression' && node.property &&
|
|
|
|
node.property.type === 'Identifier')
|
2017-07-14 05:52:02 +00:00
|
|
|
this._currentClassMembers.push(Documentation.Member.createProperty(node.property.name));
|
2017-07-12 15:01:21 +00:00
|
|
|
});
|
|
|
|
walker.walk(node);
|
2017-07-13 22:54:37 +00:00
|
|
|
} else if (!hasReturn) {
|
2017-08-21 23:39:04 +00:00
|
|
|
const walker = new ESTreeWalker(node => {
|
2017-07-13 22:54:37 +00:00
|
|
|
if (node.type === 'FunctionExpression' || node.type === 'FunctionDeclaration' || node.type === 'ArrowFunctionExpression')
|
|
|
|
return ESTreeWalker.SkipSubtree;
|
|
|
|
if (node.type === 'ReturnStatement')
|
|
|
|
hasReturn = hasReturn || !!node.argument;
|
|
|
|
});
|
|
|
|
walker.walk(node.value.body);
|
2017-07-12 15:01:21 +00:00
|
|
|
}
|
2017-07-13 22:54:37 +00:00
|
|
|
const args = [];
|
2017-08-21 23:39:04 +00:00
|
|
|
for (const param of node.value.params) {
|
2017-07-21 07:58:38 +00:00
|
|
|
if (param.type === 'AssignmentPattern')
|
|
|
|
args.push(new Documentation.Argument(param.left.name));
|
|
|
|
else if (param.type === 'RestElement')
|
|
|
|
args.push(new Documentation.Argument('...' + param.argument.name));
|
|
|
|
else if (param.type === 'Identifier')
|
|
|
|
args.push(new Documentation.Argument(param.name));
|
2017-08-15 21:29:42 +00:00
|
|
|
else if (param.type === 'ObjectPattern')
|
|
|
|
args.push(new Documentation.Argument('options'));
|
2017-07-21 07:58:38 +00:00
|
|
|
else
|
2017-07-21 17:27:53 +00:00
|
|
|
this.errors.push(`JS Parsing issue: unsupported syntax to define parameter in ${this._currentClassName}.${methodName}(): ${this._extractText(param)}`);
|
2017-07-21 07:58:38 +00:00
|
|
|
}
|
2017-08-21 23:39:04 +00:00
|
|
|
const method = Documentation.Member.createMethod(methodName, args, hasReturn, node.value.async);
|
2017-07-13 22:54:37 +00:00
|
|
|
this._currentClassMembers.push(method);
|
2017-07-12 15:01:21 +00:00
|
|
|
return ESTreeWalker.SkipSubtree;
|
|
|
|
}
|
|
|
|
|
2017-07-14 20:03:21 +00:00
|
|
|
_onAssignmentExpression(node) {
|
|
|
|
if (node.left.type !== 'MemberExpression' || node.right.type !== 'ObjectExpression')
|
|
|
|
return;
|
|
|
|
if (node.left.object.type !== 'Identifier' || node.left.property.type !== 'Identifier' || node.left.property.name !== 'Events')
|
|
|
|
return;
|
|
|
|
const className = node.left.object.name;
|
|
|
|
let events = this._eventsByClassName.get(className);
|
|
|
|
if (!events) {
|
|
|
|
events = [];
|
|
|
|
this._eventsByClassName.set(className, events);
|
|
|
|
}
|
2017-08-21 23:39:04 +00:00
|
|
|
for (const property of node.right.properties) {
|
2017-07-14 20:03:21 +00:00
|
|
|
if (property.type !== 'Property' || property.key.type !== 'Identifier' || property.value.type !== 'Literal')
|
|
|
|
continue;
|
|
|
|
events.push(Documentation.Member.createEvent(property.value.value));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-07-07 16:36:45 +00:00
|
|
|
_flushClassIfNeeded() {
|
|
|
|
if (this._currentClassName === null)
|
|
|
|
return;
|
2017-08-21 23:39:04 +00:00
|
|
|
const jsClass = new Documentation.Class(this._currentClassName, this._currentClassMembers);
|
2017-07-07 16:36:45 +00:00
|
|
|
this.classes.push(jsClass);
|
|
|
|
this._currentClassName = null;
|
2017-07-14 05:52:02 +00:00
|
|
|
this._currentClassMembers = [];
|
2017-07-07 16:36:45 +00:00
|
|
|
}
|
|
|
|
|
2017-07-14 20:03:21 +00:00
|
|
|
_recreateClassesWithEvents() {
|
|
|
|
this.classes = this.classes.map(cls => {
|
2017-08-21 23:39:04 +00:00
|
|
|
const events = this._eventsByClassName.get(cls.name) || [];
|
|
|
|
const members = cls.membersArray.concat(events);
|
2017-07-14 20:03:21 +00:00
|
|
|
return new Documentation.Class(cls.name, members);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2017-07-11 15:57:26 +00:00
|
|
|
_extractText(node) {
|
2017-07-07 16:36:45 +00:00
|
|
|
if (!node)
|
|
|
|
return null;
|
2017-08-21 23:39:04 +00:00
|
|
|
const text = this._text.substring(node.range[0], node.range[1]).trim();
|
2017-07-11 15:57:26 +00:00
|
|
|
return text;
|
2017-07-07 16:36:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-07-11 15:57:26 +00:00
|
|
|
/**
|
2017-07-31 04:49:04 +00:00
|
|
|
* @param {!Array<!Source>} sources
|
2017-07-21 17:27:53 +00:00
|
|
|
* @return {!Promise<{documentation: !Documentation, errors: !Array<string>}>}
|
2017-07-11 15:57:26 +00:00
|
|
|
*/
|
2017-07-31 04:49:04 +00:00
|
|
|
module.exports = async function(sources) {
|
2017-08-21 23:39:04 +00:00
|
|
|
const classes = [];
|
|
|
|
const errors = [];
|
|
|
|
for (const source of sources) {
|
|
|
|
const outline = new JSOutline(source.text());
|
2017-07-11 15:57:26 +00:00
|
|
|
classes.push(...outline.classes);
|
2017-07-21 17:27:53 +00:00
|
|
|
errors.push(...outline.errors);
|
2017-07-11 15:57:26 +00:00
|
|
|
}
|
2017-07-21 17:27:53 +00:00
|
|
|
const documentation = new Documentation(classes);
|
|
|
|
return { documentation, errors };
|
2017-07-11 15:57:26 +00:00
|
|
|
};
|
|
|
|
|