Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
var recast = require("recast");
var types = recast.types;
var through = require("through");
var esprima = require("esprima-fb");
var visitor = require("./visitor");
// Make sure that this esprima can parse async functions.
esprima.parse("async function test() {}");
function exports() {
var data = [];
return through(write, end);
function write(buf) {
data.push(buf);
}
function end() {
this.queue(compile(data.join("")).code);
this.queue(null);
}
}
function compile(source, options) {
function reindentScript(script) {
// We do another pass with Esprima in order to indent JS code correctly
var outputAst = esprima.parse(script);
return escodegen.generate(outputAst);
}
var transform = function(code, _context, options) {
let ast = esprima.parse(code, { loc: true });
let scopeManager = escope.analyze(ast);
scopeManager.attach();
scopeStack = new Stack();
context = _context;
contextName = "context" + Date.now();
path = [];
estraverse.replace(ast, {
enter: (node, parent) => {
if (node.__$escope$__) {
let scope = {};
let isRoot = scopeStack.size === 0;
node.__$escope$__.variables.forEach(variable => {
// don't include variables from the context in the root scope
Object.keys(annotations).forEach(function(displayName, i) {
var result = annotations[displayName];
var expect = expected[displayName];
assert.deepEqual(
result,
expect,
'\nExpectations for ' + displayName + ': \n' +
JSON.stringify(result) +
' to be: \n' + JSON.stringify(expect)
);
});
};
var ast = estraverse[transformer.type](
esprima.parse(contents),
transformer
);
if (results.length === 0) {
throw new Error(
'No results were parsed. Expected ' + expected.length + ' annotations.'
);
}
};
.map(function (file) {
return graph(esprima.parse(fs.readFileSync(file, 'utf8')));
});
function transform(visitors, source, options) {
options = options || {};
var ast;
try {
var cachedAst = _astCache[source];
ast = cachedAst ||
(_astCache[source] = esprima.parse(source, {
comment: true,
loc: true,
range: true
}));
} catch (e) {
e.message = 'Parse Error: ' + e.message;
throw e;
}
var state = utils.createState(source, ast, options);
state.g.visitors = visitors;
if (options.sourceMap) {
var SourceMapGenerator = require('source-map').SourceMapGenerator;
state.g.sourceMap = new SourceMapGenerator({file: options.filename || 'transformed.js'});
}
function getAstForSource(source, options) {
if (_astCache[source] && !options.disableAstCache) {
return _astCache[source];
}
var ast = esprima.parse(source, {
comment: true,
loc: true,
range: true,
sourceType: options.sourceType
});
if (!options.disableAstCache) {
_astCache[source] = ast;
}
return ast;
}
export default function parseECMA(source){
return prop('body')(
esprima.parse(source, {
tolerant: true,
loc: true
})
);
}