knownIdentifiersInjectVisitor(field, resolveSymbol) { const self = this; return { ObjectProperty(path) { if (path.node.key.type === 'Identifier' && path.node.key.name.match(field)) { const knownIds = self.collectKnownIdentifiers( resolveSymbol, path.get('value') ); path.get('value').replaceWith( t.arrowFunctionExpression(knownIds.map(i => t.identifier(i)), path.node.value, false) ); } } }; }
const declarations = specifiers.map(specifier => { if (specifier.node.type === 'ImportSpecifier') { return t.variableDeclarator( specifier.get('local').node, t.memberExpression( t.callExpression(t.identifier('require'), [path.get('source').node]), specifier.get('imported').node return t.variableDeclarator( specifier.get('local').node, t.callExpression(t.identifier('require'), [path.get('source').node]) ); } else { path.replaceWith(t.variableDeclaration('const', declarations)); }, ExportNamedDeclaration(path) { const declarations = specifiers.map(specifier => { if (specifier.node.type === 'ExportSpecifier') { return t.objectProperty( specifier.get('exported').node, specifier.get('local').node const addExportCall = t.callExpression(t.identifier('addExport'), [t.objectExpression(declarations)]); if (path.get('declaration')) { path.replaceWithMultiple([ path.get('declaration').node, t.callExpression(t.identifier('addExport'), [
function setSourceLoc(source, index, total) { const noop = babelTypes.noop(); noop.__loc = index / total; noop.__self = noop; source.ast.program.body.unshift(noop); }
generateEl(isChildren = true, isFirst = false) { const propertyArray = [t.objectProperty(t.identifier('path'), t.stringLiteral(`${isChildren ? (isFirst ? '' : this.toLowerLine(this.argv.componentName)) : (isFirst ? '/' : '/' + this.toLowerLine(this.argv.componentName))}`)), t.objectProperty(t.identifier('component'), t.identifier(this.argv.ComponentName))]; if (this.argv.metaParam) { propertyArray.push(t.objectProperty(t.identifier('meta'), t.stringLiteral(this.argv.metaParam)), t.objectProperty(t.identifier('name'), t.stringLiteral(this.argv.componentName))); } return t.objectExpression(propertyArray); }
/** * Annotate code with original file path. */ function annotateWithOriginalPath(ast, relPath) { if (ast.program && ast.program.body && ast.program.body.length > 0) { babelTypes.addComment( ast.program.body[0], 'leading', ' Original: ' + relPath, true); } }
function isInWhileLoop(path) { // Return whether if we're in a while loop. const whileStatement = path.find(p => babelTypes.isWhileStatement(p)); return Boolean(whileStatement); }
function wrapTryCatch(node) { return babelTypes.tryStatement( babelTypes.blockStatement([node]), babelTypes.catchClause( babelTypes.identifier('e'), babelTypes.blockStatement([]))); }
/** * Create print statements for printing the magic section prefix that's * expected by v8_foozzie.py to differentiate different source files. */ getSectionHeader(path) { const orig = common.getOriginalPath(path.node); return printValue({ VALUE: babelTypes.stringLiteral(SECTION_PREFIX + orig), }); }
function createRandomNumber(value) { // TODO(ochang): Maybe replace with variable. const probability = random.random(); if (probability < 0.01) { return babelTypes.numericLiteral( random.randInt(MIN_SAFE_INTEGER, MAX_SAFE_INTEGER)); } else if (probability < 0.06) { return common.randomInterestingNumber(); } else { return common.nearbyRandomNumber(value); } }
const fileMetaGenerator = (meta) => { const { path, mocks, } = meta; return t.objectExpression([ t.objectProperty(t.identifier('path'), t.stringLiteral(path)), t.objectProperty(t.identifier('mocks'), t.arrayExpression(mocks.map(mock => t.stringLiteral(mock)))), ]); }
traverse(ast, { ObjectProperty(astPath) { if (astPath.node.value.name === self.argv.parentName) { if (astPath.parent.properties.some(element => { return element.key.name === 'children'; })) { isChildren = true; } astPath.skip(); } } });