fp docs - Remove console.log() from examples.

This commit is contained in:
Jeroen Engels
2016-02-01 21:17:59 +01:00
committed by John-David Dalton
parent e397707dc9
commit 5062f22839
2 changed files with 284 additions and 25 deletions

View File

@@ -40,13 +40,9 @@ function getBaseName(entry) {
* @return {number} Ary of the function as an integer
*/
function getMethodAry(mapping, name) {
var ary = _.reduce(mapping.aryMethod, function(res, value, key) {
if (res) {
return res;
}
return _.includes(value, name) && key;
}, '');
return _.parseInt(ary);
return _.find(mapping.caps, function(cap) {
return _.includes(mapping.aryMethod[cap], name) && cap;
});
}
/**
@@ -62,7 +58,7 @@ function reorderParams(mapping, name, params) {
if (!mapping || mapping.skipRearg[name]) {
return params;
}
var reargOrder = mapping.methodRearg[name] || mapping.aryRearg[params.length];
var reargOrder = mapping.methodRearg[name] || mapping.aryRearg[getMethodAry(mapping, name)];
if (!reargOrder) {
return params;
}
@@ -78,6 +74,17 @@ var dotsRegex = /^\.\.\./;
var parensRegex = /^\((.*)\)$/;
var arrayRegex = /\[\]$/;
function wrapInParensIfMultiple(types) {
if (types.length > 1) {
return '(' + types.join('|') + ')';
}
return types;
}
function singleItemOrArrayOf(types) {
return types + '|' + types + '[]';
}
/**
* Replace parameter type from something like `...number` to `number|number[]`.
*
@@ -98,15 +105,8 @@ function removeDotsFromType(param) {
return s.replace(arrayRegex, '');
})
.uniq()
.thru(function(array) {
if (array.length > 1) {
return '(' + array.join('|') + ')';
}
return array;
})
.thru(function(subtypes) {
return subtypes + '|' + subtypes + '[]';
})
.thru(wrapInParensIfMultiple)
.thru(singleItemOrArrayOf)
.value();
return [newType].concat(_.tail(param));
@@ -157,16 +157,17 @@ function concatExtraArgs(j, mapping, name, args) {
}
/**
* Updates a code sample so that the arguments in the call are reordered according to `mapping`.
* Reorder the args in the example if needed, and eventually merges them when
* the method is called with more args than the method's ary.
*
* @param {object} j JSCodeShift object.
* @param {ASTObject} root AST representation of the example
* @param {Object} mapping Mapping object that defines if and how the arguments will be reordered.
* @param {string} codeSample Code sample to update.
* @returns {string} Updated code sample.
* @return {ASTObject} AST object where the arguments are reordered/merged
*/
function reorderParamsInExample(mapping, codeSample) {
return j(codeSample)
.find(j.CallExpression, { callee: { object: {name: '_' }}})
.replaceWith(function(callExpr) {
function reorderMethodArgs(j, root, mapping) {
root.find(j.CallExpression, { callee: { object: {name: '_' }}})
.replaceWith(function(callExpr, i) {
var value = callExpr.value;
var name = value.callee.property.name;
var args = concatExtraArgs(j, mapping, name, value.arguments);
@@ -174,8 +175,29 @@ function reorderParamsInExample(mapping, codeSample) {
value.callee,
reorderParams(mapping, name, args)
);
});
}
function removeConsoleLogs(codeSample) {
return codeSample
.split('\n')
.filter(function(line) {
return !line.startsWith('console.log');
})
.toSource();
.join('\n');
}
/**
* Updates a code sample so that the arguments in the call are reordered according to `mapping`.
*
* @param {Object} mapping Mapping object that defines if and how the arguments will be reordered.
* @param {string} codeSample Code sample to update.
* @returns {string} Updated code sample.
*/
function reorderParamsInExample(mapping, codeSample) {
var root = j(removeConsoleLogs(codeSample));
reorderMethodArgs(j, root, mapping);
return root.toSource();
}
/**