(JS/TS) Parse Imports

For a given JavaScript file, the parser should return all imported symbols and their fully qualified names (FQNs). For example:

ES Module Imports

import X from 'pkg'; // pkg (default export)
import { Y } from 'pkg'; // pkg.Y
import { Y as Z } from 'pkg'; // pkg.Y (aliased as Z)
import * as utils from 'pkg'; // pkg.* (namespace import)
import 'pkg'; // pkg (side-effect only)

CommonJS Import

const X = require('pkg'); // pkg (entire module)
const { Y } = require('pkg'); // pkg.Y
const { Y: Z } = require('pkg'); // pkg.Y (aliased as Z)
require('pkg'); // pkg (side-effect only)

Relative Imports

// In src/components/Button.js
import { helper } from './utils'; // src.components.utils.helper
import { Component } from '../base'; // src.base.Component
import config from '../../config'; // config (default export)

Mixed Import Patterns

import React, { useState, useEffect as useEff } from 'react';
// react (default export)
// react.useState  
// react.useEffect (aliased as useEff)

const { readFile, writeFile } = require('fs');
// fs.readFile
// fs.writeFile

Also some special cases to handle, like conditional imports ex. const X = condition ? require('a') : require('b'), and chained access to members like require('pkg').submodule.function

Edited by Michael Usachenko