File : ClassDocBuilder.js

1
/*
2
Copyright - 2021 - wwwouaiebe - Contact: https://www.ouaie.be/
3
4
This  program is free software;
5
you can redistribute it and/or modify it under the terms of the
6
GNU General Public License as published by the Free Software Foundation;
7
either version 3 of the License, or any later version.
8
9
This program is distributed in the hope that it will be useful,
10
but WITHOUT ANY WARRANTY; without even the implied warranty of
11
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
GNU General Public License for more details.
13
14
You should have received a copy of the GNU General Public License
15
along with this program; if not, write to the Free Software
16
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
17
*/
18
/*
19
Changes:
20
    - v1.0.0:
21
        - created
22
Doc reviewed 20211111
23
*/
24
/* ------------------------------------------------------------------------------------------------------------------------- */
25
26
import CommentsDocBuilder from './CommentsDocBuilder.js';
27
import { MethodOrPropertyDoc, ClassDoc } from './Docs.js';
28
29
/* ------------------------------------------------------------------------------------------------------------------------- */
30
/**
31
Build a ClassDoc object for a class
32
*/
33
/* ------------------------------------------------------------------------------------------------------------------------- */
34
35
class ClassDocBuilder {
36
37
    /**
38
    The path between the html file and theConfig.destDir ( something like '../../../', depending of the folders tree )
39
    @type {String}
40
    */
41
42
    #rootPath;
43
44
    /**
45
    The file name ( with the path since theConfig.srcDir ) in witch the current class is declared
46
    @type {String}
47
    */
48
49
    #fileName;
50
51
    /**
52
    A CommentsDocBuilder object
53
    @type {CommentsDocBuilder}
54
    */
55
56
    #commentsDocBuilder;
57
58
    /**
59
    The constructor
60
    */
61
62
    constructor ( ) {
63
        Object.freeze ( this );
64
        this.#commentsDocBuilder = new CommentsDocBuilder ( );
65
    }
66
67
    /**
68
    Build a MethodOrPropertyDoc object from an
69
    [ast node](https://github.com/babel/babel/blob/main/packages/babel-parser/ast/spec.md)
70
    @param {Object} methodOrPropertyNode An ast node of type **ClassPrivateProperty**, **ClassProperty**,
71
    **ClassPrivateMethod** or **ClassMethod**
72
    @return {MethodOrPropertyDoc} The created object
73
    */
74
75
    #buildMethodOrPropertyDoc ( methodOrPropertyNode ) {
76
77
        const methodOrPropertyDoc = new MethodOrPropertyDoc ( );
78
79
        methodOrPropertyDoc.name = methodOrPropertyNode?.key?.name || methodOrPropertyNode?.key?.id?.name;
80
        methodOrPropertyDoc.static = methodOrPropertyNode?.static;
81
        methodOrPropertyDoc.async = methodOrPropertyNode?.async;
82
        methodOrPropertyDoc.kind = methodOrPropertyNode?.kind;
83
        methodOrPropertyDoc.file = this.#fileName;
84
        methodOrPropertyDoc.rootPath = this.#rootPath;
85
        methodOrPropertyDoc.line = methodOrPropertyNode?.loc?.start?.line;
86
        methodOrPropertyDoc.commentsDoc = this.#commentsDocBuilder.build ( methodOrPropertyNode?.leadingComments );
87
88
        if ( methodOrPropertyNode?.params?.length ) {
89
            methodOrPropertyDoc.params = [];
90
            methodOrPropertyNode.params.forEach (
91
                param => { methodOrPropertyDoc.params.push ( param?.name ); }
92
            );
93
        }
94
95
        switch ( methodOrPropertyNode.type ) {
96
        case 'ClassPrivateProperty' :
97
            methodOrPropertyDoc.private = true;
98
            methodOrPropertyDoc.isA = 'property';
99
            break;
100
        case 'ClassProperty' :
101
            methodOrPropertyDoc.private = false;
102
            methodOrPropertyDoc.isA = 'property';
103
            break;
104
        case 'ClassPrivateMethod' :
105
            methodOrPropertyDoc.private = true;
106
            methodOrPropertyDoc.isA = 'method';
107
            break;
108
        case 'ClassMethod' :
109
            methodOrPropertyDoc.private = false;
110
            methodOrPropertyDoc.isA = 'method';
111
            break;
112
        default :
113
            break;
114
        }
115
116
        return Object.freeze ( methodOrPropertyDoc );
117
    }
118
119
    /**
120
    Build a ClassDoc object from an [ast node](https://github.com/babel/babel/blob/main/packages/babel-parser/ast/spec.md)
121
    @param {Object} classDeclarationNode An ast node of type classDeclarationNode
122
    @param {String} fileName The file name with the path since theConfig.srcDir
123
    @return {ClassDoc} The created object
124
    */
125
126
    build ( classDeclarationNode, fileName ) {
127
128
        // Saving the fileName for others methods
129
        this.#fileName = fileName;
130
131
        // Computing rootPath
132
        this.#rootPath = '';
133
        let rootPathCounter = this.#fileName.split ( '/' ).length - 1;
134
        while ( 0 < rootPathCounter ) {
135
            this.#rootPath += '../';
136
            rootPathCounter --;
137
        }
138
139
        // Creating the ClassDoc object
140
        const classDoc = new ClassDoc;
141
        classDoc.name = classDeclarationNode.id.name;
142
        classDoc.file = this.#fileName;
143
        classDoc.rootPath = this.#rootPath;
144
        classDoc.line = classDeclarationNode.loc.start.line;
145
146
        if ( classDeclarationNode?.superClass?.name ) {
147
            classDoc.superClass = classDeclarationNode.superClass.name;
148
        }
149
150
        classDoc.commentsDoc = this.#commentsDocBuilder.build ( classDeclarationNode.leadingComments );
151
152
        // Adding methods and properties
153
        classDeclarationNode.body.body.forEach (
154
            methodOrPropertyNode => {
155
                const methodOrPropertyDoc = this.#buildMethodOrPropertyDoc ( methodOrPropertyNode, this.#fileName );
156
                if ( methodOrPropertyDoc.isA && ! methodOrPropertyDoc?.commentsDoc?.ignore ) {
157
                    classDoc.methodsAndProperties = ( classDoc.methodsAndProperties ?? [] );
158
                    classDoc.methodsAndProperties.push ( methodOrPropertyDoc );
159
                }
160
            }
161
        );
162
163
        Object.freeze ( classDoc.methodsAndProperties );
164
        Object.freeze ( classDoc );
165
166
        return classDoc;
167
    }
168
}
169
170
export default ClassDocBuilder;
171
172
/* --- End of file --------------------------------------------------------------------------------------------------------- */
173