Skip to content

Commit

Permalink
Do not move let and const out of for initializers. Closes #997
Browse files Browse the repository at this point in the history
  • Loading branch information
fabiosantoscode committed Jan 27, 2023
1 parent c14d512 commit 79d7613
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 4 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -758,7 +758,7 @@ If you happen to need the source map as a raw object, set `sourceMap.asObject` t
- `3` -- inline functions with arguments and variables
- `true` -- same as `3`

- `join_vars` (default: `true`) -- join consecutive `var` statements
- `join_vars` (default: `true`) -- join consecutive `var`, `let` and `const` statements

- `keep_classnames` (default: `false`) -- Pass `true` to prevent the compressor from
discarding class names. Pass a regular expression to only keep class names matching
Expand Down
1 change: 1 addition & 0 deletions lib/ast.js
Original file line number Diff line number Diff line change
Expand Up @@ -994,6 +994,7 @@ var AST_Jump = DEFNODE("Jump", null, function AST_Jump(props) {
$documentation: "Base class for “jumps” (for now that's `return`, `throw`, `break` and `continue`)"
}, AST_Statement);

/** Base class for “exits” (`return` and `throw`) */
var AST_Exit = DEFNODE("Exit", "value", function AST_Exit(props) {
if (props) {
this.value = props.value;
Expand Down
11 changes: 9 additions & 2 deletions lib/compress/tighten-body.js
Original file line number Diff line number Diff line change
Expand Up @@ -1404,14 +1404,21 @@ export function tighten_body(statements, compressor) {
CHANGED = true;
stat.init = exprs.length ? make_sequence(stat.init, exprs) : null;
statements[++j] = stat;
} else if (prev instanceof AST_Var && (!stat.init || stat.init.TYPE == prev.TYPE)) {
} else if (
prev instanceof AST_Var
&& (!stat.init || stat.init.TYPE == prev.TYPE)
) {
if (stat.init) {
prev.definitions = prev.definitions.concat(stat.init.definitions);
}
stat.init = prev;
statements[j] = stat;
CHANGED = true;
} else if (defs && stat.init && defs.TYPE == stat.init.TYPE && declarations_only(stat.init)) {
} else if (
defs instanceof AST_Var
&& stat.init instanceof AST_Var
&& declarations_only(stat.init)
) {
defs.definitions = defs.definitions.concat(stat.init.definitions);
stat.init = null;
statements[++j] = stat;
Expand Down
4 changes: 3 additions & 1 deletion lib/scope.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ import {
AST_Export,
AST_For,
AST_ForIn,
AST_ForOf,
AST_Function,
AST_Import,
AST_IterationStatement,
Expand Down Expand Up @@ -230,8 +231,9 @@ AST_Scope.DEFMETHOD("figure_out_scope", function(options, { parent_scope = null,
scope.init_scope_vars(parent_scope);
scope.uses_with = save_scope.uses_with;
scope.uses_eval = save_scope.uses_eval;

if (options.safari10) {
if (node instanceof AST_For || node instanceof AST_ForIn) {
if (node instanceof AST_For || node instanceof AST_ForIn || node instanceof AST_ForOf) {
for_scopes.push(scope);
}
}
Expand Down
16 changes: 16 additions & 0 deletions test/compress/collapse_vars.js
Original file line number Diff line number Diff line change
Expand Up @@ -4420,6 +4420,22 @@ issue_2497: {
}
}

issue_997: {
options = {
defaults: true,
}
input: {
function main(e) {
console.log(e.length);
let other = "12";
for (let e; e = 0;)
console.log(e);
}
main("foo");
}
expect_stdout: ["3"]
}

issue_2506: {
options = {
collapse_vars: true,
Expand Down

0 comments on commit 79d7613

Please sign in to comment.