Socket
Socket
Sign inDemoInstall

es-info

Package Overview
Dependencies
Maintainers
1
Versions
6
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

es-info

Analyze ES module and extract information about imports, exports, and dynamic imports.


Version published
Weekly downloads
1.1K
increased by49.61%
Maintainers
1
Weekly downloads
 
Created
Source

es-info

Build Status codecov

Analyze ES module and extract information about imports, exports, and dynamic imports.

Usage

const {Parser} = require("acorn");
const {default: dynamicImport} = require("acorn-dynamic-import");
const {analyze} = require("es-info");

const DynamicImportParser = Parser.extend(dynamicImport);

const code = `
import foo from "foo";
import {bar} from "bar";
import * as baz from "baz";

export default "bar";
export const boo = "boo";
export {bla} from "bla";

if (foo === "doSomething") {
  import("dynamic-imported")
    .then(module => module.doSomething());
}
`;
const ast = DynamicImportParser.parse(code, {sourceType: "module"});

const result = analyze({ast, subtree: true});

Result:

{
  import: {
    foo: {
      default: true,
      named: [],
      all: false,
      used: ["default"]
    },
    bar: {
      default: false,
      named: ["bar"],
      all: false,
      used: []
    },
    baz: {
      default: false,
      named: [],
      all: true,
      used: []
    },
    bla: {
      default: false,
      named: ["bla"],
      all: false,
      used: []
    }
  },
  export: {
    default: true,
    named: ["boo", "bla"],
    all: false
  },
  dynamicImport: ["dynamic-imported"]
}

API reference

This module exports following members.

  • analyze: A function which can analyze an AST and extract imports, exports information.

analyze

const analyzeResult: {
  import: Object<moduleId : importInfo>,
  export: exportInfo,
  dynamicImport: Array<String>
} = analyze({
  ast,
  subtree?: Boolean
});
options

ast is an estree object.

If subtree is true, the analyzer will analyze the entire tree, otherwise only the top nodes (import/export declarations) are analyzed. You have to set subtree to true if you want to collect the usage of imported names and to collect dynamic imports. Default: false.

analyzeResult

import is an object map. The key is the module ID and the value is an information object with these properties:

const importInfo = {
  default: Boolean,
  named: Array<String>,
  all: Boolean,
  used: Array<String>
};

If importInfo.default is true then the default member is imported from the module.

importInfo.named contains a list of imported names.

If importInfo.all is true then all names are imported from the module (import * from ...).

importInfo.used is an array of imported names. If a name is included in this array, then it is referenced somewhere in the code. You can use this array to determine which names are actually used when importInfo.all is true.

Note that export {foo} from "bar" doesn't use foo.

exportInfo has following properties:

const exportInfo = {
  default: Boolean,
  named: Array<String>,
  all: Boolean
};

If exportInfo.default is true then the module exports a default member.

exportInfo.named is a list of exported names.

If exportInfo.all is true then the module exports all members from another module e.g. export * from "foo".

dynamicImport is a list of module ID which are imported with dynamic import() statement.

Changelog

  • 0.3.0 (Jun 13, 2019)

    • Breaking: the signature of analyze() is changed.
    • Add: subtree option.
  • 0.2.1 (Jun 6, 2019)

    • Fix: rename .scope to ._esInfoScope to avoid the conflict.
  • 0.2.0 (Jun 6, 2019)

    • Breaking: drop options.dynamicImport. Now the module always analyze the entire tree.
    • Add: importInfo.used. Use it to check which names are used.
  • 0.1.1 (Apr 29, 2018)

    • Fix: ClassDeclaration.
  • 0.1.0 (Apr 28, 2018)

    • Initial release.

Keywords

FAQs

Package last updated on 13 Jun 2019

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc