Security News
ESLint is Now Language-Agnostic: Linting JSON, Markdown, and Beyond
ESLint has added JSON and Markdown linting support with new officially-supported plugins, expanding its versatility beyond JavaScript.
Analyze ES module and extract information about imports, exports, and dynamic imports.
Analyze ES module and extract information about imports, exports, and dynamic imports.
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);
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"]
}
This module exports following members.
analyze
: A function which can analyze an AST and extract imports, exports information.const analyzeResult: {
import: Object<moduleId : importInfo>,
export: exportInfo,
dynamicImport: Array<String>
} = analyze(ast);
import
is an object map. The key is the module ID and the value is an info object with these members:
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 the following shape:
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.
0.2.1 (Jun 6, 2019)
.scope
to ._esInfoScope
to avoid the conflict.0.2.0 (Jun 6, 2019)
options.dynamicImport
. Now the module always analyze the entire tree.importInfo.used
. Use it to check which names are used.0.1.1 (Apr 29, 2018)
0.1.0 (Apr 28, 2018)
FAQs
Analyze ES module and extract information about imports, exports, and dynamic imports.
We found that es-info demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
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.
Security News
ESLint has added JSON and Markdown linting support with new officially-supported plugins, expanding its versatility beyond JavaScript.
Security News
Members Hub is conducting large-scale campaigns to artificially boost Discord server metrics, undermining community trust and platform integrity.
Security News
NIST has failed to meet its self-imposed deadline of clearing the NVD's backlog by the end of the fiscal year. Meanwhile, CVE's awaiting analysis have increased by 33% since June.