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) ); } } }; }
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 { 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'), [ t.objectExpression( path.get('declaration').get('declarations').map(d => t.objectProperty( path.replaceWith(t.callExpression(t.identifier('setExport'), [path.get('declaration').node]));
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); }
function wrapTryCatch(node) { return babelTypes.tryStatement( babelTypes.blockStatement([node]), babelTypes.catchClause( babelTypes.identifier('e'), babelTypes.blockStatement([]))); }
function callRandomFunction(path, identifier, seed) { if (seed === undefined) { seed = randomSeed(); } let args = [ identifier, _numericLiteral(seed) ]; args = args.map(_unwrapExpressionStatement); args = args.concat(randomArguments(path)); return babelTypes.callExpression( babelTypes.identifier('__callRandomFunction'), args); }
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, { ObjectExpression(astPath) { const properties = astPath.node.properties; properties.forEach(element => { if (element.value && element.value.name === self.argv.parentName) { astPath.pushContainer('properties', t.objectProperty(t.identifier('children'), t.arrayExpression([self.generateEl(true, true)]))); astPath.skip(); } }); } });
function _identifier(identifier) { return babelTypes.identifier(identifier); }
const memberExpressionFromFqn = (fqn) => { const fqnArr = fqn.split('.').map(part => t.identifier(part)); return fqnArr.reduce((acc, next) => t.memberExpression(acc, next)); }
/** * Create statements for extra printing at the end of a section. We print * the number of caught exceptions, a generic hash of all observed values * and the contents of all variables in scope. */ getSectionFooter(path) { const variables = common.availableVariables(path); const statements = variables.map(prettyPrintStatement); statements.unshift(printCaught()); statements.unshift(printHash()); const statement = babelTypes.tryStatement( babelTypes.blockStatement(statements), babelTypes.catchClause( babelTypes.identifier('e'), babelTypes.blockStatement([]))); this.annotate(statement, 'Print variables and exceptions from section'); return statement; }
function maybeAddImportStatement(path) { if (this.validFunctions.length || this.atLeastOneRecorderWrapperUsed) { const recorderImportStatement = buildRequire({ SOURCE: t.stringLiteral(this.importPath), IDENTIFIER: t.identifier('recorderWrapper'), }); path.unshiftContainer('body', recorderImportStatement); } if (this.atLeastOneMockUsed) { const recorderImportStatement = buildRequire({ SOURCE: t.stringLiteral(this.importPath), IDENTIFIER: t.identifier('mockRecorderWrapper'), }); path.unshiftContainer('body', recorderImportStatement); } const recorderImportStatement = buildRequire({ SOURCE: t.stringLiteral(this.importPath), IDENTIFIER: t.identifier('recordFileMeta'), }); path.unshiftContainer('body', recorderImportStatement); }
describe('Mutate variables', () => { beforeEach(() => { sandbox.stub( common, 'randomVariable').callsFake( () => { return babelTypes.identifier('REPLACED') }); }); afterEach(() => { sandbox.restore(); }); it('test', () => { const source = helpers.loadTestData('mutate_variables.js'); const settings = scriptMutator.defaultSettings(); settings['MUTATE_VARIABLES'] = 1.0; const mutator = new variableMutator.VariableMutator(settings); mutator.mutate(source); const mutated = sourceHelpers.generateCode(source); helpers.assertExpectedResult( 'mutate_variables_expected.js', mutated); }); });
/** * Filter out disallowed V8 runtime functions. */ function neuterDisallowedV8Natives(ast) { babelTraverse(ast, { CallExpression(path) { if (!babelTypes.isIdentifier(path.node.callee) || !path.node.callee.name.startsWith(V8_BUILTIN_PREFIX)) { return; } const functionName = path.node.callee.name.substr( V8_BUILTIN_PREFIX.length); if (!exceptions.isAllowedRuntimeFunction(functionName)) { path.replaceWith(babelTypes.callExpression( babelTypes.identifier('nop'), [])); } } }); }
const metaGenerator = (path, funObj) => { const { name, isAsync, paramIds, isDefault, isEcmaDefault, injectionWhitelist, isObject, } = funObj; return t.objectExpression([ t.objectProperty(t.identifier('path'), t.stringLiteral(path)), t.objectProperty(t.identifier('name'), t.stringLiteral(name)), // t.objectProperty(t.identifier('localName'), t.stringLiteral(localName)), t.objectProperty(t.identifier('paramIds'), t.arrayExpression(paramIds.map(pid => t.stringLiteral(pid)))), t.objectProperty(t.identifier('injectionWhitelist'), t.arrayExpression(injectionWhitelist.map(wl => t.stringLiteral(wl)))), t.objectProperty(t.identifier('isDefault'), t.booleanLiteral(isDefault)), t.objectProperty(t.identifier('isEcmaDefault'), t.booleanLiteral(isEcmaDefault)), t.objectProperty(t.identifier('isAsync'), t.booleanLiteral(isAsync)), t.objectProperty(t.identifier('isObject'), t.booleanLiteral(isObject)), ]); }
const mockInjectorGenerator = ( moduleId, moduleName, importId, importedAs, newFunctionName, fileName, isObjectLike, ) => { if (isObjectLike) { return mockInjectorObjLike({ FP_ID: t.identifier(importedAs), NEW_FP_ID: t.identifier(newFunctionName), FP_STRING_LITERAL: t.stringLiteral(importedAs), MODULE_ID: t.identifier(moduleId), MODULE_STRING_LITERAL: t.stringLiteral(moduleName), FILE_NAME: t.stringLiteral(fileName), }); } return mockInjector({ FP_ID: t.identifier(importedAs), NEW_FP_ID: t.identifier(newFunctionName), FP_STRING_LITERAL: t.stringLiteral(importId), // MODULE_ID: t.identifier(moduleId), MODULE_STRING_LITERAL: t.stringLiteral(moduleName), FILE_NAME: t.stringLiteral(fileName), }); }