Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
files.forEach((file: {src: string[]; dest: string}) => {
var jsFileContent = fs.readFileSync(file.src[0]).toString(),
ast = esprima.parse(jsFileContent, {loc: true, range: true});
// Ensure destination folder exists
if (!fs.existsSync(path.dirname(file.dest))) {
grunt.file.mkdir(path.dirname(file.dest));
}
var processedAst = estraverse.replace(ast, {
enter: function(node: any) {
if (node.type === 'ExpressionStatement' &&
node.expression.type === 'CallExpression' &&
remove.indexOf(node.expression.callee.name) > -1) {
return {type:'EmptyStatement'};
}
}
}), output = escodegen.generate(processedAst, {sourceMap: path.relative(path.dirname(file.dest), file.src[0]), sourceMapWithCode: true});
var mapDest = file.dest + '.map';
fs.writeFileSync(mapDest, output.map);
fs.writeFileSync(file.dest, `${output.code}\n//# sourceMappingURL=${path.basename(file.dest)}.map`);
});
});
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
if (isRoot && contextHasProperty(variable.name)) {
return;
}
if (variable.defs.length > 0) {
if (variable.defs.every(def => def.type !== "CatchClause")) {
scope[variable.name] = {
type: variable.defs[0].type
};
export function compile(code)
{
let pattern = esprima.parse(code, {tolerant: true});
delete pattern.errors;
estraverse.replace(pattern, {
leave(node)
{
if (node.type == "Identifier")
{
let match = /^((expression|statement)\d+)(?:_(\w+))?$/.exec(node.name);
if (match)
{
let result = new (match[2] == "expression" ? ExpressionPlaceholder : StatementPlaceholder)(match[1]);
if (match[3])
for (let modifier of match[3].split("_"))
result.modifier(modifier);
return result;
}
}
else if (node.type == "ExpressionStatement" && node.expression.type == "StatementPlaceholder")
return node.expression;
value: function parentize(node) {
var isRootNode = true;
estraverse.replace(node, {
enter: function enter(node, parentNode) {
Object.defineProperty(node, 'parentNode', {
configurable: true,
enumerable: true,
value: isRootNode ? NodeUtils.getProgramNode([node]) : parentNode || node,
writable: true
});
isRootNode = false;
}
});
}
}, {
function postcondition(ast, options, labels, func) {
OBLIGATIONS.precondition(ast.type === 'LabeledStatement');
OBLIGATIONS.precondition(ast.body.type === 'BlockStatement');
OBLIGATIONS.precondition(options && typeof options === 'object');
var __result;
var effects = sideEffects(ast);
if (effects.length) {
throw new ContractError('Postcondition contains side-effects! ', effects[0]);
}
__result = removeLabel(labels, func, options, estraverse.replace(ast, { enter: enter.bind(null, options) }));
OBLIGATIONS.postcondition(Array.isArray(__result));
return __result;
}
;
gatherStatmentSplitInfo: function(node){
this.currentStatementTmpUsed = [];
this.assignmentsToBePrepended = [];
this.onGatherSplitInfo();
return walk.replace(node, {
enter: this.statementSplitEnter.bind(this),
leave: this.statementSplitExit.bind(this)
});
},
statementSplitEnter: function(nodeParent){
function removeByNode (tree, compare, options) {
let count = 0
estraverse.replace(tree, {
enter (current) {
if (options.first && count === 1) {
return this.break()
}
if (compare(current)) {
count += 1
return this.remove()
}
},
leave (current) {
if (isNodeEmpty(current)) {
return this.remove()
}
}
})
}
ContextReplacer.prototype.run = function run(src) {
var ast = esprima.parse(src);
var self = this;
return escodegen.generate(estraverse.replace(ast, {
enter: function(node, parent) {
self.estraverse = this;
return self.enterNode(node, parent);
},
leave: function(node) {
return self.leaveNode(node);
}
}));
};
if(isCommonJS && hasExportsAssignment) {
callbackFunc.body.body.push({
'type': 'ReturnStatement',
'argument': {
'type': 'Identifier',
'name': 'exports',
'range': defaultRange,
'loc': defaultLOC
},
'range': defaultRange,
'loc': defaultLOC
});
}
estraverse.replace(callbackFunc, {
'enter': function(node, parent) {
var normalizedModuleName,
newName;
if(utils.isRequireExpression(node)) {
if(node['arguments'] && node['arguments'][0] && node['arguments'][0].value) {
normalizedModuleName = normalizeModuleName.call(kclean, utils.normalizeDependencyName(moduleId, node['arguments'][0].value, moduleId));
if(_.contains(ignoreModules, normalizedModuleName)) {
return node;
}
if(_.where(matchingRequireExpressionNames, {
'originalName': normalizedModuleName
replaceFunctionInvocations: function(functionBodyAast){
var self = this;
walk.replace(functionBodyAast, {
enter: function (node, parent) {
if(node.type == Syntax.CallExpression){
if(node.callee.type == Syntax.Identifier && self.functionSpaceInfo[node.callee.name]){
var paramTransitions = self.functionSpaceInfo[node.callee.name];
var oldArgs = node.arguments, newArgs = [];
for(var i = 0; i < paramTransitions.length; ++i){
var paramT = paramTransitions[i];
if(!paramT.space)
oldArgs[paramT.idx] !== undefined && newArgs.push(oldArgs[paramT.idx]);
else{
newArgs.push(SpaceTransformTools.getSpaceTransformCall(oldArgs[paramT.idx], paramT.space));
}
}
node.arguments = newArgs;
}
}