Skip to content

Instantly share code, notes, and snippets.

@typhonrt
Created June 10, 2016 00:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save typhonrt/4ebde611d1517b350830b4a33c2b8267 to your computer and use it in GitHub Desktop.
Save typhonrt/4ebde611d1517b350830b4a33c2b8267 to your computer and use it in GitHub Desktop.
Fixes ESDoc issue #295 as ArrayPattern wasn't being parsed.
static guessParams(params) {
let _params = [];
for (let i = 0; i < params.length; i++) {
let param = params[i];
let result = {};
switch (param.type) {
case 'Identifier':
// e.g. func(a){}
result.name = param.name;
result.types = ['*'];
break;
case 'AssignmentPattern':
if (param.left.type === 'Identifier') {
result.name = param.left.name;
} else if (param.left.type === 'ArrayPattern') {
let arrayPatternName = [];
for (let property of param.left.elements) {
arrayPatternName.push(`${property.name}`);
}
result.name = `[${arrayPatternName.join(', ')}]`;
} else if (param.left.type === 'ObjectPattern') {
result.name = `objectPattern${i === 0 ? '' : i}`;
}
result.optional = true;
if (ParamParser.isLiteral(param.right.type)) {
// e.g. func(a = 10){}
result.types = param.right.value === null ? ['*'] : [typeof param.right.value];
result.defaultRaw = param.right.value;
result.defaultValue = `${result.defaultRaw}`;
} else if (param.right.type === 'ArrayExpression') {
// e.g. func(a = [123]){}
if (param.right.elements.length) {
let arrayType = typeof param.right.elements[0].value;
for (let cntr = 0; cntr < param.right.elements.length; cntr++) {
if (arrayType !== typeof param.right.elements[cntr].value) { arrayType = '*'; break; }
}
result.types = [`${arrayType}[]`];
} else {
result.types = ['*[]'];
}
result.defaultRaw = param.right.elements.map((elm)=> elm.value);
result.defaultValue = `${JSON.stringify(result.defaultRaw)}`;
} else if(param.right.type === 'ObjectExpression'){
let typeMap = {};
for (let prop of param.left.properties || []) {
typeMap[prop.key.name] = '*';
}
// e.g. func(a = {key: 123}){}
let obj = {};
for (let prop of param.right.properties) {
obj[prop.key.name] = prop.value.value;
typeMap[prop.key.name] = typeof prop.value.value;
}
let types = [];
for (let key of Object.keys(typeMap)) {
types.push(`"${key}": ${typeMap[key]}`);
}
result.types = [`{${types.join(', ')}}`];
result.defaultRaw = obj;
result.defaultValue = `${JSON.stringify(result.defaultRaw)}`;
} else if (param.right.type === 'Identifier') {
// e.g. func(a = value){}
result.types = ['*'];
result.defaultRaw = param.right.name;
result.defaultValue = `${param.right.name}`;
} else {
// e.g. func(a = new Foo()){}, func(a = foo()){}
// CallExpression, NewExpression
result.types = ['*'];
}
break;
case 'RestElement':
// e.g. func(...a){}
result.name = `${param.argument.name}`;
result.types = ['...*'];
result.spread = true;
break;
case 'ArrayPattern':
let arrayPatternName = [];
for (let property of param.elements) {
arrayPatternName.push(`${property.name}`);
}
result.name = `[${arrayPatternName.join(', ')}]`;
result.types = ['*[]'];
break;
case 'ObjectPattern':
let objectPattern = [];
let raw = {};
for (let property of param.properties) {
objectPattern.push(`"${property.key.name}": *`);
raw[property.key.name] = null;
}
result.name = `objectPattern${i === 0 ? '' : i}`;
result.types = [`{${objectPattern.join(', ')}}`];
result.defaultRaw = raw;
result.defaultValue = `${JSON.stringify(result.defaultRaw)}`;
break;
default:
logger.w('unknown param.type', param);
}
_params.push(result);
}
return _params;
}
@typhonrt
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment