Skip to content

Commit 747cf9d

Browse files
fix: improve logic for dead control flow
1 parent df3da48 commit 747cf9d

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

93 files changed

+1019
-289
lines changed

lib/Template.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -250,7 +250,7 @@ class Template {
250250

251251
/**
252252
* @typedef {object} WithId
253-
* @property {string|number} id
253+
* @property {string | number} id
254254
*/
255255

256256
/**

lib/dependencies/ContextDependencyHelpers.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ const splitContextFromPrefix = prefix => {
4242
};
4343

4444
/** @typedef {Partial<Omit<ContextDependencyOptions, "resource">>} PartialContextDependencyOptions */
45-
/** @typedef {{ new(options: ContextDependencyOptions, range: Range, valueRange: [number, number], ...args: any[]): ContextDependency }} ContextDependencyConstructor */
45+
/** @typedef {{ new(options: ContextDependencyOptions, range: Range, valueRange: Range, ...args: any[]): ContextDependency }} ContextDependencyConstructor */
4646

4747
/**
4848
* @param {ContextDependencyConstructor} Dep the Dependency class

lib/dependencies/CssIcssImportDependency.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ const ModuleDependency = require("./ModuleDependency");
1616
/** @typedef {import("../DependencyTemplate").CssDependencyTemplateContext} DependencyTemplateContext */
1717
/** @typedef {import("../Module")} Module */
1818
/** @typedef {import("../ModuleGraph")} ModuleGraph */
19+
/** @typedef {import("../javascript/JavascriptParser").Range} Range */
1920
/** @typedef {import("../serialization/ObjectMiddleware").ObjectDeserializerContext} ObjectDeserializerContext */
2021
/** @typedef {import("../serialization/ObjectMiddleware").ObjectSerializerContext} ObjectSerializerContext */
2122

@@ -26,12 +27,12 @@ class CssIcssImportDependency extends ModuleDependency {
2627
*:import('./style.css') { IMPORTED_NAME: v-primary }
2728
* @param {string} request request request path which needs resolving
2829
* @param {string} exportName export name
29-
* @param {[number, number]} range the range of dependency
30+
* @param {Range} range the range of dependency
3031
*/
3132
constructor(request, exportName, range) {
3233
super(request);
33-
this.range = range;
3434
this.exportName = exportName;
35+
this.range = range;
3536
}
3637

3738
get type() {

lib/dependencies/WorkerPlugin.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ class WorkerPlugin {
119119
/**
120120
* @param {JavascriptParser} parser the parser
121121
* @param {Expression} expr expression
122-
* @returns {[string, [number, number]] | void} parsed
122+
* @returns {[string, Range] | void} parsed
123123
*/
124124
const parseModuleUrl = (parser, expr) => {
125125
if (expr.type !== "NewExpression" || expr.callee.type === "Super")
@@ -259,7 +259,7 @@ class WorkerPlugin {
259259

260260
/** @type {string} */
261261
let url;
262-
/** @type {[number, number]} */
262+
/** @type {Range} */
263263
let range;
264264
/** @type {boolean} */
265265
let needNewUrl = false;

lib/javascript/BasicEvaluatedExpression.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -524,7 +524,7 @@ class BasicEvaluatedExpression {
524524

525525
/**
526526
* Set's the range for the expression.
527-
* @param {[number, number]} range range to set
527+
* @param {Range} range range to set
528528
* @returns {this} this
529529
*/
530530
setRange(range) {

lib/javascript/JavascriptParser.js

Lines changed: 35 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -284,6 +284,9 @@ class VariableInfo {
284284
* @property {TagInfo | undefined} next
285285
*/
286286

287+
const SCOPE_INFO_TERMINATED_RETURN = 1;
288+
const SCOPE_INFO_TERMINATED_THROW = 2;
289+
287290
/**
288291
* @typedef {object} ScopeInfo
289292
* @property {StackedMap<string, VariableInfo | ScopeInfo>} definitions
@@ -293,8 +296,7 @@ class VariableInfo {
293296
* @property {boolean} inTry
294297
* @property {boolean} isStrict
295298
* @property {boolean} isAsmJs
296-
* @property {boolean} inExecutedPath false for unknown state
297-
* @property {undefined|"return"|"throw"} terminated
299+
* @property {undefined | 1 | 2} terminated
298300
*/
299301

300302
/** @typedef {[number, number]} Range */
@@ -310,9 +312,9 @@ class VariableInfo {
310312
* Helper function for joining two ranges into a single range. This is useful
311313
* when working with AST nodes, as it allows you to combine the ranges of child nodes
312314
* to create the range of the _parent node_.
313-
* @param {[number, number]} startRange start range to join
314-
* @param {[number, number]} endRange end range to join
315-
* @returns {[number, number]} joined range
315+
* @param {Range} startRange start range to join
316+
* @param {Range} endRange end range to join
317+
* @returns {Range} joined range
316318
* @example
317319
* ```js
318320
* const startRange = [0, 5];
@@ -2173,7 +2175,7 @@ class JavascriptParser extends Parser {
21732175
this.blockPreWalkStatements(body);
21742176
this.prevStatement = prev;
21752177
this.walkStatements(body);
2176-
}, this.scope.inExecutedPath);
2178+
}, true);
21772179
}
21782180

21792181
/**
@@ -2213,20 +2215,12 @@ class JavascriptParser extends Parser {
22132215

22142216
this.scope.terminated =
22152217
consequentTerminated && alternateTerminated
2216-
? this.scope.terminated
2218+
? alternateTerminated
22172219
: undefined;
2218-
} else {
2219-
const oldState = this.scope.inExecutedPath;
2220-
2221-
this.scope.inExecutedPath = true;
2222-
2223-
if (result) {
2224-
this.walkNestedStatement(statement.consequent);
2225-
} else if (statement.alternate) {
2226-
this.walkNestedStatement(statement.alternate);
2227-
}
2228-
2229-
this.scope.inExecutedPath = oldState;
2220+
} else if (result) {
2221+
this.walkNestedStatement(statement.consequent);
2222+
} else if (statement.alternate) {
2223+
this.walkNestedStatement(statement.alternate);
22302224
}
22312225
}
22322226

@@ -2292,7 +2286,9 @@ class JavascriptParser extends Parser {
22922286
if (this.scope.topLevelScope === true) return;
22932287
if (this.hooks.terminate.call(statement)) {
22942288
this.scope.terminated =
2295-
statement.type === "ReturnStatement" ? "return" : "throw";
2289+
statement.type === "ReturnStatement"
2290+
? SCOPE_INFO_TERMINATED_RETURN
2291+
: SCOPE_INFO_TERMINATED_THROW;
22962292
}
22972293
}
22982294

@@ -2339,14 +2335,20 @@ class JavascriptParser extends Parser {
23392335
const handlerTerminated = this.scope.terminated;
23402336
this.scope.terminated = undefined;
23412337

2342-
if (statement.finalizer) this.walkStatement(statement.finalizer);
2338+
if (statement.finalizer) {
2339+
this.walkStatement(statement.finalizer);
2340+
}
23432341

2344-
if (
2345-
!this.scope.terminated &&
2342+
const finalizerTerminated = this.scope.terminated;
2343+
this.scope.terminated = undefined;
2344+
2345+
if (finalizerTerminated) {
2346+
this.scope.terminated = finalizerTerminated;
2347+
} else if (
23462348
tryTerminated &&
23472349
(statement.handler ? handlerTerminated : true)
23482350
) {
2349-
this.scope.terminated = tryTerminated;
2351+
this.scope.terminated = handlerTerminated || tryTerminated;
23502352
}
23512353
}
23522354

@@ -2414,7 +2416,9 @@ class JavascriptParser extends Parser {
24142416
if (statement.update) {
24152417
this.walkExpression(statement.update);
24162418
}
2419+
24172420
const body = statement.body;
2421+
24182422
if (body.type === "BlockStatement") {
24192423
// no need to add additional scope
24202424
const prev = this.prevStatement;
@@ -2448,8 +2452,11 @@ class JavascriptParser extends Parser {
24482452
} else {
24492453
this.walkPattern(statement.left);
24502454
}
2455+
24512456
this.walkExpression(statement.right);
2457+
24522458
const body = statement.body;
2459+
24532460
if (body.type === "BlockStatement") {
24542461
// no need to add additional scope
24552462
const prev = this.prevStatement;
@@ -2486,8 +2493,11 @@ class JavascriptParser extends Parser {
24862493
} else {
24872494
this.walkPattern(statement.left);
24882495
}
2496+
24892497
this.walkExpression(statement.right);
2498+
24902499
const body = statement.body;
2500+
24912501
if (body.type === "BlockStatement") {
24922502
// no need to add additional scope
24932503
const prev = this.prevStatement;
@@ -4076,7 +4086,6 @@ class JavascriptParser extends Parser {
40764086
inTaggedTemplateTag: false,
40774087
isStrict: oldScope.isStrict,
40784088
isAsmJs: oldScope.isAsmJs,
4079-
inExecutedPath: false,
40804089
terminated: undefined,
40814090
definitions: oldScope.definitions.createChild()
40824091
};
@@ -4105,7 +4114,6 @@ class JavascriptParser extends Parser {
41054114
inTry: false,
41064115
inShorthand: false,
41074116
inTaggedTemplateTag: false,
4108-
inExecutedPath: true,
41094117
isStrict: oldScope.isStrict,
41104118
isAsmJs: oldScope.isAsmJs,
41114119
terminated: undefined,
@@ -4138,7 +4146,6 @@ class JavascriptParser extends Parser {
41384146
inTry: false,
41394147
inShorthand: false,
41404148
inTaggedTemplateTag: false,
4141-
inExecutedPath: true,
41424149
isStrict: oldScope.isStrict,
41434150
isAsmJs: oldScope.isAsmJs,
41444151
terminated: undefined,
@@ -4170,7 +4177,6 @@ class JavascriptParser extends Parser {
41704177
inTry: oldScope.inTry,
41714178
inShorthand: false,
41724179
inTaggedTemplateTag: false,
4173-
inExecutedPath,
41744180
isStrict: oldScope.isStrict,
41754181
isAsmJs: oldScope.isAsmJs,
41764182
terminated: oldScope.terminated,
@@ -4181,10 +4187,7 @@ class JavascriptParser extends Parser {
41814187

41824188
const terminated = this.scope.terminated;
41834189

4184-
if (
4185-
inExecutedPath &&
4186-
((this.scope.inTry && terminated === "throw") || terminated === "return")
4187-
) {
4190+
if (inExecutedPath && terminated) {
41884191
oldScope.terminated = terminated;
41894192
}
41904193

@@ -4504,7 +4507,6 @@ class JavascriptParser extends Parser {
45044507
inTry: false,
45054508
inShorthand: false,
45064509
inTaggedTemplateTag: false,
4507-
inExecutedPath: false,
45084510
isStrict: false,
45094511
isAsmJs: false,
45104512
terminated: undefined,

lib/library/EnableLibraryPlugin.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ const enabledTypes = new WeakMap();
1414

1515
/**
1616
* @typedef {object} EnableLibraryPluginOptions
17-
* @property {() => void} [additionalApply] function that runs when applying the current plugin.
17+
* @property {() => void=} additionalApply function that runs when applying the current plugin.
1818
*/
1919

2020
/**

lib/logging/createConsoleLogger.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,6 @@ module.exports = ({ level = "info", debug = false, console }) => {
8989
.concat(debug)
9090
.map(filterToFunction)
9191
);
92-
/** @type {number} */
9392
const loglevel = LogLevel[`${level}`] || 0;
9493

9594
/**

lib/util/runtime.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -431,6 +431,7 @@ class RuntimeSpecMap {
431431
* @param {RuntimeSpecMap<T, R>=} clone copy form this
432432
*/
433433
constructor(clone) {
434+
/** @type {0 | 1 | 2} */
434435
this._mode = clone ? clone._mode : 0; // 0 = empty, 1 = single entry, 2 = map
435436
/** @type {RuntimeSpec} */
436437
this._singleRuntime = clone ? clone._singleRuntime : undefined;

test/StatsTestCases.basictest.js

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -213,7 +213,16 @@ describe("StatsTestCases", () => {
213213
match => `${match.replace(/[0-9a-f]/g, "X")}`
214214
);
215215
expect(actual).toMatchSnapshot();
216-
if (testConfig.validate) testConfig.validate(stats, stderr.toString());
216+
217+
if (testConfig.validate) {
218+
try {
219+
testConfig.validate(stats, stderr.toString());
220+
} catch (err) {
221+
done(err);
222+
return;
223+
}
224+
}
225+
217226
done();
218227
});
219228
});

0 commit comments

Comments
 (0)