MCPcopy Index your code
hub / github.com/documentationjs/documentation / paramToDoc

Function paramToDoc

src/infer/params.js:140–292  ·  view source on GitHub ↗

* Babel parses JavaScript source code and produces an abstract syntax * tree that includes methods and their arguments. This function takes * that AST and uses it to infer details that would otherwise need * explicit documentation, like the names of comments and their * default values. * * It

(param, prefix, i)

Source from the content-addressed store, hash-verified

138 * @returns {Object} parameter with inference.
139 */
140function paramToDoc(param, prefix, i) {
141 const autoName = '$' + String(i);
142 const prefixedName = prefix + '.' + param.name;
143
144 switch (param.type) {
145 case 'AssignmentPattern': {
146 // (a = b)
147 const newAssignmentParam = paramToDoc(param.left, '', i);
148
149 if (Array.isArray(newAssignmentParam)) {
150 throw new Error('Encountered an unexpected parameter type');
151 }
152
153 return Object.assign(newAssignmentParam, {
154 default: generate(param.right, {
155 compact: true
156 }).code,
157 type: newAssignmentParam.type
158 });
159 }
160 // ObjectPattern <AssignmentProperty | RestElement>
161 case 'ObjectPattern': {
162 // { a }
163 if (prefix === '') {
164 // If this is a root-level param, like f({ x }), then we need to name
165 // it, like $0 or $1, depending on its position.
166 return {
167 title: 'param',
168 name: autoName,
169 anonymous: true,
170 type: (param.typeAnnotation &&
171 typeAnnotation(param.typeAnnotation)) || {
172 type: 'NameExpression',
173 name: 'Object'
174 },
175 properties: _.flatMap(param.properties, prop => {
176 return paramToDoc(prop, prefix + autoName);
177 })
178 };
179 } else if (param.indexed) {
180 // Likewise, if this object pattern sits inside of an ArrayPattern,
181 // like [{ foo }], it shouldn't just look like $0.foo, but like $0.0.foo,
182 // so make sure it isn't indexed first.
183 return {
184 title: 'param',
185 name: prefixedName,
186 anonymous: true,
187 type: (param.typeAnnotation &&
188 typeAnnotation(param.typeAnnotation)) || {
189 type: 'NameExpression',
190 name: 'Object'
191 },
192 properties: _.flatMap(param.properties, prop => {
193 return paramToDoc(prop, prefixedName);
194 })
195 };
196 }
197 // If, otherwise, this is nested, we don't really represent it as

Callers 1

inferAndCombineParamsFunction · 0.85

Calls 2

generateFunction · 0.85
typeAnnotationFunction · 0.85

Tested by

no test coverage detected