transpileFile(file, errorsReport) { try { const ast = parse( file.content, { sourceFilename: file.fileName, sourceType: 'module', plugins: ['objectRestSpread'] }, ); this.transpilers.forEach((t) => babelTraverse(ast, t.traverseObject())); const content = babelGenerator(ast, {}, file.content).code; return Object.assign({}, file, { content }); } catch (e) { if (e.toString().indexOf('SyntaxError') !== -1) { const line = file.content.split('\n')[e.loc.line - 1]; const spaces = Array(e.loc.column).fill(' ').join(''); errorsReport.error(`Syntax error during '${file.fileName}' parsing: ${e.message}:\n${line}\n${spaces}^`); } else { errorsReport.error(e); } } return undefined; }
/** * Remove existing comments. */ function removeComments(ast) { babelTraverse(ast, { enter(path) { babelTypes.removeComments(path.node); } }); }
_traverse(ast, visitor) { let oldEnter = null; if (Object.prototype.hasOwnProperty.call(visitor, 'enter')) { oldEnter = visitor['enter']; } // Transparently skip nodes that are marked. visitor['enter'] = (path) => { if (this.shouldSkip(path.node)) { path.skip(); return; } if (oldEnter) { oldEnter(path); } } babelTraverse(ast, visitor); }
describe('Available variables and functions', () => { it('test', () => { const source = helpers.loadTestData('available_variables.js'); const result = new Array(); babelTraverse(source.ast, { CallExpression(path) { result.push({ variables: common.availableVariables(path), functions: common.availableFunctions(path), }); } }); helpers.assertExpectedResult( 'available_variables_expected.js', JSON.stringify(result, null, 2)); }); });
/** * Removes "Assert" from strings in spidermonkey shells or from older * crash tests: https://crbug.com/1068268 */ function cleanAsserts(ast) { function replace(string) { return string.replace(/Assert/g, 'A****t'); } babelTraverse(ast, { StringLiteral(path) { path.node.value = replace(path.node.value); path.node.extra.raw = replace(path.node.extra.raw); path.node.extra.rawValue = replace(path.node.extra.rawValue); }, TemplateElement(path) { path.node.value.cooked = replace(path.node.value.cooked); path.node.value.raw = replace(path.node.value.raw); }, }); }
/** * 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 dependencies = []; babelTraverse(ast, { CallExpression(path) { const isV8OrSpiderMonkeyLoad = _isV8OrSpiderMonkeyLoad(path);