UNPKG

bit-bin

Version:

<a href="https://opensource.org/licenses/Apache-2.0"><img alt="apache" src="https://img.shields.io/badge/License-Apache%202.0-blue.svg"></a> <a href="https://github.com/teambit/bit/blob/master/CONTRIBUTING.md"><img alt="prs" src="https://img.shields.io/b

127 lines (106 loc) 3.53 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.default = _default; function _parserHelper() { const data = require("../parser-helper"); _parserHelper = function () { return data; }; return data; } /** * this file had been forked (and changed since then) from https://github.com/dependents/node-detective-es6 */ const Walker = require('node-source-walk'); /** * Extracts the dependencies of the supplied es6 module * * @param {String|Object} src - File's content or AST * @return {String[]} */ function _default(src) { const walker = new Walker(); const dependencies = {}; const addDependency = dependency => { if (!dependencies[dependency]) { dependencies[dependency] = {}; } }; const addImportSpecifier = (dependency, importSpecifier) => { if (dependencies[dependency].importSpecifiers) { dependencies[dependency].importSpecifiers.push(importSpecifier); } else { dependencies[dependency].importSpecifiers = [importSpecifier]; } }; const addExportedToImportSpecifier = name => { Object.keys(dependencies).forEach(dependency => { if (!dependencies[dependency].importSpecifiers) return; const specifier = dependencies[dependency].importSpecifiers.find(i => i.name === name); if (specifier) specifier.exported = true; }); }; if (typeof src === 'undefined') { throw new Error('src not given'); } if (src === '') { return dependencies; } walker.walk(src, function (node) { switch (node.type) { case 'ImportDeclaration': if (node.source && node.source.value) { const dependency = node.source.value; addDependency(dependency); node.specifiers.forEach(specifier => { const specifierValue = (0, _parserHelper().getSpecifierValueForImportDeclaration)(specifier); addImportSpecifier(dependency, specifierValue); }); } break; case 'ExportNamedDeclaration': case 'ExportAllDeclaration': if (node.source && node.source.value) { const dependency = node.source.value; addDependency(dependency); if (node.specifiers) { // in case of "export * from" there are no node.specifiers node.specifiers.forEach(specifier => { const specifierValue = { isDefault: !specifier.local || specifier.local.name === 'default', // e.g. export { default as isArray } from './is-array'; name: specifier.exported.name, exported: true }; addImportSpecifier(dependency, specifierValue); }); } } else if (node.specifiers && node.specifiers.length) { node.specifiers.forEach(exportSpecifier => { addExportedToImportSpecifier(exportSpecifier.exported.name); }); } break; case 'ExportDefaultDeclaration': addExportedToImportSpecifier(node.declaration.name); break; case 'CallExpression': { const value = (0, _parserHelper().getDependenciesFromCallExpression)(node); if (value) addDependency(value); } break; case 'MemberExpression': { const value = (0, _parserHelper().getDependenciesFromMemberExpression)(node); if (value) addDependency(value); } break; default: break; } }); return dependencies; }