@@ -68,6 +68,174 @@ const collectDeclaration = (declarations, pattern) => { |
68 | 68 | } |
69 | 69 | }; |
70 | 70 |
|
| 71 | +/** |
| 72 | + * `collectDeclaration` for a pattern a node reader reads, by id. |
| 73 | + * @param {JavascriptNodeReader} path the reader |
| 74 | + * @param {Declarations} declarations set of declarations |
| 75 | + * @param {number} pattern the pattern to collect declarations from |
| 76 | + */ |
| 77 | +const collectDeclarationByPath = (path, declarations, pattern) => { |
| 78 | + const stack = [pattern]; |
| 79 | + while (stack.length > 0) { |
| 80 | + const node = /** @type {number} */ (stack.pop()); |
| 81 | + switch (path.type(node)) { |
| 82 | + case NodeType.Identifier: |
| 83 | + declarations.add(path.name(node)); |
| 84 | + break; |
| 85 | + case NodeType.ArrayPattern: { |
| 86 | + const count = path.fieldCount(Field.elements, node); |
| 87 | + for (let i = 0; i < count; i++) { |
| 88 | + const element = path.field(i, Field.elements, node); |
| 89 | + if (element !== 0) stack.push(element); |
| 90 | + } |
| 91 | + break; |
| 92 | + } |
| 93 | + case NodeType.AssignmentPattern: |
| 94 | + stack.push(path.field(0, Field.left, node)); |
| 95 | + break; |
| 96 | + case NodeType.ObjectPattern: { |
| 97 | + const count = path.fieldCount(Field.properties, node); |
| 98 | + for (let i = 0; i < count; i++) { |
| 99 | + const property = path.field(i, Field.properties, node); |
| 100 | + stack.push( |
| 101 | + path.type(property) === NodeType.RestElement |
| 102 | + ? property |
| 103 | + : path.field(0, Field.value, property) |
| 104 | + ); |
| 105 | + } |
| 106 | + break; |
| 107 | + } |
| 108 | + case NodeType.RestElement: |
| 109 | + stack.push(path.field(0, Field.argument, node)); |
| 110 | + break; |
| 111 | + } |
| 112 | + } |
| 113 | +}; |
| 114 | + |
| 115 | +/** |
| 116 | + * `getHoistedDeclarations` for a statement a node reader reads, by id. |
| 117 | + * @param {JavascriptNodeReader} path the reader |
| 118 | + * @param {number} branch branch to get hoisted declarations from |
| 119 | + * @param {boolean} includeFunctionDeclarations whether to include function declarations |
| 120 | + * @returns {string[]} hoisted declarations |
| 121 | + */ |
| 122 | +const getHoistedDeclarationsByPath = ( |
| 123 | + path, |
| 124 | + branch, |
| 125 | + includeFunctionDeclarations |
| 126 | +) => { |
| 127 | + /** @type {Declarations} */ |
| 128 | + const declarations = new Set(); |
| 129 | + const stack = [branch]; |
| 130 | + while (stack.length > 0) { |
| 131 | + const node = /** @type {number} */ (stack.pop()); |
| 132 | + if (node === 0) continue; |
| 133 | + switch (path.type(node)) { |
| 134 | + case NodeType.BlockStatement: { |
| 135 | + const count = path.fieldCount(Field.body, node); |
| 136 | + for (let i = 0; i < count; i++) { |
| 137 | + stack.push(path.field(i, Field.body, node)); |
| 138 | + } |
| 139 | + break; |
| 140 | + } |
| 141 | + case NodeType.IfStatement: |
| 142 | + stack.push(path.field(0, Field.consequent, node)); |
| 143 | + stack.push(path.field(0, Field.alternate, node)); |
| 144 | + break; |
| 145 | + case NodeType.ForStatement: |
| 146 | + stack.push(path.field(0, Field.init, node)); |
| 147 | + stack.push(path.field(0, Field.body, node)); |
| 148 | + break; |
| 149 | + case NodeType.ForInStatement: |
| 150 | + case NodeType.ForOfStatement: |
| 151 | + stack.push(path.field(0, Field.left, node)); |
| 152 | + stack.push(path.field(0, Field.body, node)); |
| 153 | + break; |
| 154 | + case NodeType.DoWhileStatement: |
| 155 | + case NodeType.WhileStatement: |
| 156 | + case NodeType.LabeledStatement: |
| 157 | + stack.push(path.field(0, Field.body, node)); |
| 158 | + break; |
| 159 | + case NodeType.SwitchStatement: { |
| 160 | + const count = path.fieldCount(Field.cases, node); |
| 161 | + for (let i = 0; i < count; i++) { |
| 162 | + const switchCase = path.field(i, Field.cases, node); |
| 163 | + const consequentCount = path.fieldCount(Field.consequent, switchCase); |
| 164 | + for (let j = 0; j < consequentCount; j++) { |
| 165 | + stack.push(path.field(j, Field.consequent, switchCase)); |
| 166 | + } |
| 167 | + } |
| 168 | + break; |
| 169 | + } |
| 170 | + case NodeType.TryStatement: { |
| 171 | + stack.push(path.field(0, Field.block, node)); |
| 172 | + const handler = path.field(0, Field.handler, node); |
| 173 | + if (handler !== 0) stack.push(path.field(0, Field.body, handler)); |
| 174 | + stack.push(path.field(0, Field.finalizer, node)); |
| 175 | + break; |
| 176 | + } |
| 177 | + case NodeType.FunctionDeclaration: |
| 178 | + if (includeFunctionDeclarations) { |
| 179 | + collectDeclarationByPath( |
| 180 | + path, |
| 181 | + declarations, |
| 182 | + path.field(0, Field.id, node) |
| 183 | + ); |
| 184 | + } |
| 185 | + break; |
| 186 | + case NodeType.VariableDeclaration: |
| 187 | + if (path.textOf(Part.kind, node) === "var") { |
| 188 | + const count = path.fieldCount(Field.declarations, node); |
| 189 | + for (let i = 0; i < count; i++) { |
| 190 | + collectDeclarationByPath( |
| 191 | + path, |
| 192 | + declarations, |
| 193 | + path.field(0, Field.id, path.field(i, Field.declarations, node)) |
| 194 | + ); |
| 195 | + } |
| 196 | + } |
| 197 | + break; |
| 198 | + } |
| 199 | + } |
| 200 | + return [...declarations]; |
| 201 | +}; |
| 202 | + |
| 203 | +/** |
| 204 | + * `ConstPlugin.eliminateUnusedStatement` for a statement a node reader reads, by id. |
| 205 | + * @param {JavascriptParser} parser the parser |
| 206 | + * @param {JavascriptNodeReader} path the reader |
| 207 | + * @param {number} statement the statement to remove |
| 208 | + * @param {boolean} alwaysInBlock whether to always generate curly brackets |
| 209 | + * @returns {void} |
| 210 | + */ |
| 211 | +const eliminateUnusedStatementByPath = ( |
| 212 | + parser, |
| 213 | + path, |
| 214 | + statement, |
| 215 | + alwaysInBlock |
| 216 | +) => { |
| 217 | + // as in `eliminateUnusedStatement`, strict mode hoists `var` but not `function` |
| 218 | + const declarations = getHoistedDeclarationsByPath( |
| 219 | + path, |
| 220 | + statement, |
| 221 | + !parser.scope.isStrict |
| 222 | + ); |
| 223 | + const inBlock = |
| 224 | + alwaysInBlock || path.type(statement) === NodeType.BlockStatement; |
| 225 | + |
| 226 | + let replacement = inBlock ? "{" : ""; |
| 227 | + replacement += |
| 228 | + declarations.length > 0 ? ` var ${declarations.join(", ")}; ` : ""; |
| 229 | + replacement += inBlock ? "}" : ""; |
| 230 | + |
| 231 | + const dep = new ConstDependency( |
| 232 | + `// removed by dead control flow\n${replacement}`, |
| 233 | + path.range(statement) |
| 234 | + ); |
| 235 | + dep.loc = parser.getLocation(parser.nodeOfPath(path, statement)); |
| 236 | + parser.state.module.addPresentationalDependency(dep); |
| 237 | +}; |
| 238 | + |
71 | 239 | /** |
72 | 240 | * Gets hoisted declarations. |
73 | 241 | * @param {Statement} branch branch to get hoisted declarations from |
@@ -299,9 +467,10 @@ class ConstPlugin { |
299 | 467 | statement |
300 | 468 | ); |
301 | 469 | if (branchToRemove !== 0) { |
302 | | - this.eliminateUnusedStatement( |
| 470 | + eliminateUnusedStatementByPath( |
303 | 471 | parser, |
304 | | - /** @type {Statement} */ (path.object(branchToRemove)), |
| 472 | + path, |
| 473 | + branchToRemove, |
305 | 474 | true |
306 | 475 | ); |
307 | 476 | } |
@@ -319,12 +488,7 @@ class ConstPlugin { |
319 | 488 | ) { |
320 | 489 | return; |
321 | 490 | } |
322 | | - // dead code after a jump is rare, so it reads the statement's object |
323 | | - this.eliminateUnusedStatement( |
324 | | - parser, |
325 | | - /** @type {Statement} */ (path.object(statement)), |
326 | | - false |
327 | | - ); |
| 491 | + eliminateUnusedStatementByPath(parser, path, statement, false); |
328 | 492 | return true; |
329 | 493 | } |
330 | 494 | ); |
|