UNPKG

1.21 kBJavaScriptView Raw
1"use strict";
2
3const AV = require("./assignment-visitor.js");
4const FastPath = require("./fast-path.js");
5const IEV = require("./import-export-visitor.js");
6
7const assignmentVisitor = new AV;
8const importExportVisitor = new IEV;
9
10module.exports = function (ast, options) {
11 const importExportOptions = Object.assign(Object.create(null), options);
12 const rootPath = new FastPath(ast);
13
14 // Essential so that the AST will be modified.
15 importExportOptions.ast = true;
16
17 importExportVisitor.visit(
18 rootPath,
19 null, // No code to modify.
20 importExportOptions
21 );
22
23 const result = { ast, identical: false };
24
25 if (importExportVisitor.madeChanges) {
26 assignmentVisitor.visit(rootPath, {
27 exportedLocalNames: importExportVisitor.exportedLocalNames,
28 moduleAlias: importExportOptions.moduleAlias,
29 modifyAST: true
30 });
31 } else {
32 // Let the caller know the result is no different from the input.
33 result.identical = true;
34 }
35
36 // Whether or not there were any changes, finalizeHoisting does some
37 // important cleanup work, and should not be computationally expensive
38 // when no changes need to be made.
39 importExportVisitor.finalizeHoisting();
40
41 return result;
42};