MCPcopy
hub / github.com/webpack/webpack / handler

Method handler

lib/ConstPlugin.js:166–497  ·  view source on GitHub ↗
(parser)

Source from the content-addressed store, hash-verified

164 * @param {JavascriptParser} parser the parser
165 */
166 const handler = (parser) => {
167 parser.hooks.terminate.tap(PLUGIN_NAME, (_statement) => true);
168 parser.hooks.statementIf.tap(PLUGIN_NAME, (statement) => {
169 if (parser.scope.isAsmJs) return;
170 const param = parser.evaluateExpression(statement.test);
171 const bool = param.asBool();
172 if (typeof bool === "boolean") {
173 if (!param.couldHaveSideEffects()) {
174 const dep = new ConstDependency(
175 `${bool}`,
176 /** @type {Range} */ (param.range)
177 );
178 dep.loc = /** @type {SourceLocation} */ (statement.loc);
179 parser.state.module.addPresentationalDependency(dep);
180 } else {
181 parser.walkExpression(statement.test);
182 }
183 const branchToRemove = bool
184 ? statement.alternate
185 : statement.consequent;
186 if (branchToRemove) {
187 this.eliminateUnusedStatement(parser, branchToRemove, true);
188 }
189 return bool;
190 }
191 });
192 parser.hooks.unusedStatement.tap(PLUGIN_NAME, (statement) => {
193 if (
194 parser.scope.isAsmJs ||
195 // Check top level scope here again
196 parser.scope.topLevelScope === true
197 ) {
198 return;
199 }
200 this.eliminateUnusedStatement(parser, statement, false);
201 return true;
202 });
203 parser.hooks.expressionConditionalOperator.tap(
204 PLUGIN_NAME,
205 (expression) => {
206 if (parser.scope.isAsmJs) return;
207 const param = parser.evaluateExpression(expression.test);
208 const bool = param.asBool();
209 if (typeof bool === "boolean") {
210 if (!param.couldHaveSideEffects()) {
211 const dep = new ConstDependency(
212 ` ${bool}`,
213 /** @type {Range} */ (param.range)
214 );
215 dep.loc = /** @type {SourceLocation} */ (expression.loc);
216 parser.state.module.addPresentationalDependency(dep);
217 } else {
218 parser.walkExpression(expression.test);
219 }
220 // Expressions do not hoist.
221 // It is safe to remove the dead branch.
222 //
223 // Given the following code:

Callers

nothing calls this directly

Calls 11

tapMethod · 0.80
evaluateExpressionMethod · 0.80
asBoolMethod · 0.80
couldHaveSideEffectsMethod · 0.80
walkExpressionMethod · 0.80
isBooleanMethod · 0.80
asNullishMethod · 0.80
popMethod · 0.80
pushMethod · 0.45

Tested by

no test coverage detected