Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
function exportsAsESModule(modulePath) {
const contents = fs.readFileSync(modulePath, 'utf8')
try {
const ast = babylon.parse(contents, {
sourceType: 'module',
})
let hasExportSpecifier = false
babel.traverse(ast, {
ExportSpecifier(path) {
hasExportSpecifier = true
path.stop()
},
})
return hasExportSpecifier
} catch (error) {
// eslint-disable-next-line no-console
console.warn(`unable to parse "${modulePath}"`, error)
return false
}
}
function pushDependency(nodeArgs, parentType) {
const arg = nodeArgs[0];
if (nodeArgs.length != 1 || arg.type !== 'StringLiteral') {
// Dynamic requires directly inside of a try statement are considered optional dependencies
if (parentType === 'TryStatement') {
return;
}
throw new Error(
`require() must have a single string literal argument: ${filename}:${arg.loc.start.line - 1}`);
}
dependencyOffsets.push(arg.start);
dependencies.add(arg.value);
}
babel.traverse(ast, {
CallExpression(path) {
const node = path.node;
const callee = node.callee;
const parent = path.scope.parentBlock;
if (callee.type === 'Identifier' && callee.name === 'require') {
pushDependency(node.arguments, parent.type);
}
if (callee.type !== 'MemberExpression') {
return;
}
const obj = callee.object;
const prop = callee.property;
if (
obj.type === 'Identifier' &&
obj.name === 'require' &&
!callee.computed &&
export function compile(txt, options, wpack) {
let opt = Object.assign({}, DEFAULT_OPTIONS, options)
if(typeof opt.additional_class == 'function') {
opt.additional_class = opt.additional_class(wpack)
}
opt.always_add_class = new Set(opt.always_add_class || []);
let parse_tree = parser.parse(txt, opt);
let ast = T.file(T.program([]));
babel.traverse(ast, {
Program: path => {
parse_tree
.filter(block => block[0] == 'view')
.map(([_block, name]) => {
// Bind all views in scope, so they can call each other
// (including recursively)
set_var(path, name, T.identifier(name))
})
parse_tree.map(block => compile_block(block, path, opt))
},
});
return ast;
}
function findFunctionNodeInAst(ast, selectionCoords) {
let matchedScope = null;
let lastScope = null;
babelCore.traverse(ast, {
enter: function(nodePath) {
lastScope = /(Function|MethodDefinition)/.test(nodePath.type) ? nodePath.node : lastScope;
matchedScope = coordsWithin(selectionCoords, nodePath.node) ? lastScope : matchedScope;
}
});
return matchedScope;
}
public static getNodes(ast: babel.types.File): NodeWithParent[] {
const nodes: NodeWithParent[] = [];
babel.traverse(ast, {
enter(path: NodePath) {
const node: NodeWithParent = path.node;
node.parent = path.parent;
Object.freeze(node);
nodes.push(node);
}
});
return nodes;
}
function collectDependencies(
ast,
replacement,
dependencyMapIdentifier,
): {
dependencies: $ReadOnlyArray,
dependencyMapName: string,
} {
const visited = new WeakSet();
const traversalState = {dependencyMapIdentifier};
traverse(
ast,
{
Program(path, state) {
if (!state.dependencyMapIdentifier) {
state.dependencyMapIdentifier = path.scope.generateUidIdentifier(
'dependencyMap',
);
}
},
CallExpression(path, state) {
const node = path.node;
if (replacement.replaceImports && node.callee.type === 'Import') {
const reqNode = processImportCall(path, node, replacement, state);
visited.add(reqNode);
return;
exports.toAST = function (ast) {
ast.sourceType = "module";
ast.range = [ast.start, ast.end];
traverse(ast, astTransformVisitor);
};