export function resolveToImport()

in modules/material-parser/src/parse/js/resolver/resolveImport.ts [30:75]


export function resolveToImport(initialPath) {
  const pathBuffer = [initialPath];

  while (pathBuffer.length) {
    let path = pathBuffer.shift();
    const node = path.node;
    switch (node.type) {
      case 'VariableDeclarator':
        if (node.init) {
          pathBuffer.unshift(path.get('init'));
        }
        break;
      case 'CallExpression': {
        if (match(node.callee, { type: 'Identifier', name: 'require' })) {
          return path;
        }
        const paths = [path.get('callee')];
        const argumentsPath = path.get('arguments');
        for (let index = 0; index < argumentsPath.value.length; index++) {
          paths.push(argumentsPath.get(index));
        }
        pathBuffer.unshift(...paths);
      }
      // eslint-disable-next-line no-fallthrough
      case 'Identifier':
      case 'JSXIdentifier': {
        const valuePath = resolveToValue(path);
        if (valuePath !== path) {
          pathBuffer.unshift(valuePath);
        }
        break;
      }
      case 'ImportDeclaration':
        return path;
      case 'MemberExpression':
        while (path && t.MemberExpression.check(path.node)) {
          path = path.get('object');
        }
        if (path) {
          pathBuffer.unshift(path);
        }
    }
  }

  return null;
}