1 | /*
|
---|
2 | MIT License http://www.opensource.org/licenses/mit-license.php
|
---|
3 | Author Tobias Koppers @sokra
|
---|
4 | */
|
---|
5 |
|
---|
6 | "use strict";
|
---|
7 |
|
---|
8 | const InnerGraph = require("../optimize/InnerGraph");
|
---|
9 | const ConstDependency = require("./ConstDependency");
|
---|
10 | const HarmonyExportExpressionDependency = require("./HarmonyExportExpressionDependency");
|
---|
11 | const HarmonyExportHeaderDependency = require("./HarmonyExportHeaderDependency");
|
---|
12 | const HarmonyExportImportedSpecifierDependency = require("./HarmonyExportImportedSpecifierDependency");
|
---|
13 | const HarmonyExportSpecifierDependency = require("./HarmonyExportSpecifierDependency");
|
---|
14 | const {
|
---|
15 | harmonySpecifierTag,
|
---|
16 | getAssertions
|
---|
17 | } = require("./HarmonyImportDependencyParserPlugin");
|
---|
18 | const HarmonyImportSideEffectDependency = require("./HarmonyImportSideEffectDependency");
|
---|
19 |
|
---|
20 | const { HarmonyStarExportsList } = HarmonyExportImportedSpecifierDependency;
|
---|
21 |
|
---|
22 | module.exports = class HarmonyExportDependencyParserPlugin {
|
---|
23 | constructor(options) {
|
---|
24 | this.strictExportPresence = options.strictExportPresence;
|
---|
25 | }
|
---|
26 |
|
---|
27 | apply(parser) {
|
---|
28 | parser.hooks.export.tap(
|
---|
29 | "HarmonyExportDependencyParserPlugin",
|
---|
30 | statement => {
|
---|
31 | const dep = new HarmonyExportHeaderDependency(
|
---|
32 | statement.declaration && statement.declaration.range,
|
---|
33 | statement.range
|
---|
34 | );
|
---|
35 | dep.loc = Object.create(statement.loc);
|
---|
36 | dep.loc.index = -1;
|
---|
37 | parser.state.module.addPresentationalDependency(dep);
|
---|
38 | return true;
|
---|
39 | }
|
---|
40 | );
|
---|
41 | parser.hooks.exportImport.tap(
|
---|
42 | "HarmonyExportDependencyParserPlugin",
|
---|
43 | (statement, source) => {
|
---|
44 | parser.state.lastHarmonyImportOrder =
|
---|
45 | (parser.state.lastHarmonyImportOrder || 0) + 1;
|
---|
46 | const clearDep = new ConstDependency("", statement.range);
|
---|
47 | clearDep.loc = Object.create(statement.loc);
|
---|
48 | clearDep.loc.index = -1;
|
---|
49 | parser.state.module.addPresentationalDependency(clearDep);
|
---|
50 | const sideEffectDep = new HarmonyImportSideEffectDependency(
|
---|
51 | source,
|
---|
52 | parser.state.lastHarmonyImportOrder,
|
---|
53 | getAssertions(statement)
|
---|
54 | );
|
---|
55 | sideEffectDep.loc = Object.create(statement.loc);
|
---|
56 | sideEffectDep.loc.index = -1;
|
---|
57 | parser.state.current.addDependency(sideEffectDep);
|
---|
58 | return true;
|
---|
59 | }
|
---|
60 | );
|
---|
61 | parser.hooks.exportExpression.tap(
|
---|
62 | "HarmonyExportDependencyParserPlugin",
|
---|
63 | (statement, expr) => {
|
---|
64 | const isFunctionDeclaration = expr.type === "FunctionDeclaration";
|
---|
65 | const comments = parser.getComments([
|
---|
66 | statement.range[0],
|
---|
67 | expr.range[0]
|
---|
68 | ]);
|
---|
69 | const dep = new HarmonyExportExpressionDependency(
|
---|
70 | expr.range,
|
---|
71 | statement.range,
|
---|
72 | comments
|
---|
73 | .map(c => {
|
---|
74 | switch (c.type) {
|
---|
75 | case "Block":
|
---|
76 | return `/*${c.value}*/`;
|
---|
77 | case "Line":
|
---|
78 | return `//${c.value}\n`;
|
---|
79 | }
|
---|
80 | return "";
|
---|
81 | })
|
---|
82 | .join(""),
|
---|
83 | expr.type.endsWith("Declaration") && expr.id
|
---|
84 | ? expr.id.name
|
---|
85 | : isFunctionDeclaration
|
---|
86 | ? {
|
---|
87 | id: expr.id ? expr.id.name : undefined,
|
---|
88 | range: [
|
---|
89 | expr.range[0],
|
---|
90 | expr.params.length > 0
|
---|
91 | ? expr.params[0].range[0]
|
---|
92 | : expr.body.range[0]
|
---|
93 | ],
|
---|
94 | prefix: `${expr.async ? "async " : ""}function${
|
---|
95 | expr.generator ? "*" : ""
|
---|
96 | } `,
|
---|
97 | suffix: `(${expr.params.length > 0 ? "" : ") "}`
|
---|
98 | }
|
---|
99 | : undefined
|
---|
100 | );
|
---|
101 | dep.loc = Object.create(statement.loc);
|
---|
102 | dep.loc.index = -1;
|
---|
103 | parser.state.current.addDependency(dep);
|
---|
104 | InnerGraph.addVariableUsage(
|
---|
105 | parser,
|
---|
106 | expr.type.endsWith("Declaration") && expr.id
|
---|
107 | ? expr.id.name
|
---|
108 | : "*default*",
|
---|
109 | "default"
|
---|
110 | );
|
---|
111 | return true;
|
---|
112 | }
|
---|
113 | );
|
---|
114 | parser.hooks.exportSpecifier.tap(
|
---|
115 | "HarmonyExportDependencyParserPlugin",
|
---|
116 | (statement, id, name, idx) => {
|
---|
117 | const settings = parser.getTagData(id, harmonySpecifierTag);
|
---|
118 | let dep;
|
---|
119 | const harmonyNamedExports = (parser.state.harmonyNamedExports =
|
---|
120 | parser.state.harmonyNamedExports || new Set());
|
---|
121 | harmonyNamedExports.add(name);
|
---|
122 | InnerGraph.addVariableUsage(parser, id, name);
|
---|
123 | if (settings) {
|
---|
124 | dep = new HarmonyExportImportedSpecifierDependency(
|
---|
125 | settings.source,
|
---|
126 | settings.sourceOrder,
|
---|
127 | settings.ids,
|
---|
128 | name,
|
---|
129 | harmonyNamedExports,
|
---|
130 | null,
|
---|
131 | this.strictExportPresence,
|
---|
132 | null,
|
---|
133 | settings.assertions
|
---|
134 | );
|
---|
135 | } else {
|
---|
136 | dep = new HarmonyExportSpecifierDependency(id, name);
|
---|
137 | }
|
---|
138 | dep.loc = Object.create(statement.loc);
|
---|
139 | dep.loc.index = idx;
|
---|
140 | parser.state.current.addDependency(dep);
|
---|
141 | return true;
|
---|
142 | }
|
---|
143 | );
|
---|
144 | parser.hooks.exportImportSpecifier.tap(
|
---|
145 | "HarmonyExportDependencyParserPlugin",
|
---|
146 | (statement, source, id, name, idx) => {
|
---|
147 | const harmonyNamedExports = (parser.state.harmonyNamedExports =
|
---|
148 | parser.state.harmonyNamedExports || new Set());
|
---|
149 | let harmonyStarExports = null;
|
---|
150 | if (name) {
|
---|
151 | harmonyNamedExports.add(name);
|
---|
152 | } else {
|
---|
153 | harmonyStarExports = parser.state.harmonyStarExports =
|
---|
154 | parser.state.harmonyStarExports || new HarmonyStarExportsList();
|
---|
155 | }
|
---|
156 | const dep = new HarmonyExportImportedSpecifierDependency(
|
---|
157 | source,
|
---|
158 | parser.state.lastHarmonyImportOrder,
|
---|
159 | id ? [id] : [],
|
---|
160 | name,
|
---|
161 | harmonyNamedExports,
|
---|
162 | harmonyStarExports && harmonyStarExports.slice(),
|
---|
163 | this.strictExportPresence,
|
---|
164 | harmonyStarExports
|
---|
165 | );
|
---|
166 | if (harmonyStarExports) {
|
---|
167 | harmonyStarExports.push(dep);
|
---|
168 | }
|
---|
169 | dep.loc = Object.create(statement.loc);
|
---|
170 | dep.loc.index = idx;
|
---|
171 | parser.state.current.addDependency(dep);
|
---|
172 | return true;
|
---|
173 | }
|
---|
174 | );
|
---|
175 | }
|
---|
176 | };
|
---|