Spaces:
Sleeping
Sleeping
File size: 5,622 Bytes
61d39e2 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 |
/*
* Copyright (C) 2024-present Puter Technologies Inc.
*
* This file is part of Puter.
*
* Puter is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published
* by the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
const processors = [];
processors.push({
title: 'track all require calls',
match () { return true; },
traverse: {
CallExpression (path, context) {
const callee = path.get('callee');
if ( ! callee.isIdentifier() ) return;
if ( callee.node.name === 'require' ) {
context.doc_module.requires.push(path.node.arguments[0].value);
}
}
}
});
processors.push({
title: 'get leading comment',
match () { return true; },
traverse: {
ClassDeclaration (path, context) {
const node = path.node;
const comment = (node.leadingComments && (
node.leadingComments.length < 1 ? '' :
node.leadingComments[node.leadingComments.length - 1]
)) ?? '';
context.comment = comment;
}
}
});
processors.push({
title: 'provide name and comment for modules and services',
match (context) {
return context.type === 'module' || context.type === 'service';
},
traverse: {
ClassDeclaration (path, context) {
context.doc_item = context.doc_module;
if ( context.type === 'service' ) {
// Skip if class name doesn't end with 'Service'
if ( ! path.node.id.name.endsWith('Service') ) {
context.skip = true;
return;
}
context.doc_item = context.doc_module.add_service();
}
context.doc_item.name = path.node.id.name;
if ( context.comment === '' ) return;
context.doc_item.provide_comment(context.comment);
}
}
});
processors.push({
title: 'provide methods and listeners for services',
match (context) {
return context.type === 'service';
},
traverse: {
ClassDeclaration (path, context) {
path.node.body.body.forEach(member => {
if ( member.type !== 'ClassMethod' ) return;
const key = member.key.name ?? member.key.value;
const comment = member.leadingComments?.[0]?.value ?? '';
if ( key.startsWith('__on_') ) {
// 2nd argument is always an object destructuring;
// we want the list of keys in the object:
const params = member.params?.[1]?.properties ?? [];
context.doc_item.provide_listener({
key: key.slice(5),
comment,
params,
});
} else {
// Method overrides
if ( key.startsWith('_') ) return;
// Private methods
if ( key.endsWith('_') ) return;
const params = member.params ?? [];
context.doc_item.provide_method({
key,
comment,
params,
});
}
});
}
}
});
processors.push({
title: 'provide library function documentation',
match (context) {
return context.type === 'lib';
},
traverse: {
VariableDeclaration (path, context) {
// skip non-const declarations
if ( path.node.kind !== 'const' ) return;
// skip declarations with multiple declarators
if ( path.node.declarations.length !== 1 ) return;
// skip declarations without an initializer
if ( ! path.node.declarations[0].init ) return;
// skip declarations that aren't in the root scope
if ( path.scope.parent ) return;
console.log('path.node', path.node.declarations);
// is it a function?
if ( ! ['FunctionExpression', 'ArrowFunctionExpression'].includes(
path.node.declarations[0].init.type
) ) return;
// get the name of the function
const name = path.node.declarations[0].id.name;
// get the comment
const comment = path.node.leadingComments?.[0]?.value ?? '';
// get the parameters
const params = path.node.declarations[0].init.params ?? [];
context.doc_item.provide_function({
key: name,
comment,
params,
});
}
}
});
module.exports = processors;
|