return stat instanceof AST_Const || stat instanceof AST_DefClass || stat instanceof AST_Let;
}
+ function safe_to_trim(stat) {
+ if (stat instanceof AST_LambdaDefinition) {
+ var def = stat.name.definition();
+ return def.scope === stat.name.scope || all(def.references, function(ref) {
+ var scope = ref.scope;
+ do {
+ if (scope === stat.name.scope) return true;
+ } while (scope = scope.parent_scope);
+ });
+ }
+ return !is_lexical_definition(stat);
+ }
+
function as_statement_array(thing) {
if (thing === null) return [];
- if (thing instanceof AST_BlockStatement) return all(thing.body, function(stat) {
- return !is_lexical_definition(stat);
- }) ? thing.body : [ thing ];
+ if (thing instanceof AST_BlockStatement) return all(thing.body, safe_to_trim) ? thing.body : [ thing ];
if (thing instanceof AST_EmptyStatement) return [];
if (is_statement(thing)) return [ thing ];
throw new Error("Can't convert thing to statement array");
for (var i = 0; i < statements.length;) {
var stat = statements[i];
if (stat instanceof AST_BlockStatement) {
- if (all(stat.body, function(stat) {
- return !is_lexical_definition(stat);
- })) {
+ if (all(stat.body, safe_to_trim)) {
CHANGED = true;
eliminate_spurious_blocks(stat.body);
[].splice.apply(statements, [i, 1].concat(stat.body));
}
function extract_functions() {
- var tail = statements.slice(i + 1);
- statements.length = i + 1;
- return tail.filter(function(stat) {
- if (stat instanceof AST_Defun) {
- statements.push(stat);
+ var defuns = [];
+ var tail = statements.splice(i + 1).filter(function(stat) {
+ if (stat instanceof AST_LambdaDefinition) {
+ defuns.push(stat);
return false;
}
return true;
});
+ [].push.apply(all(tail, safe_to_trim) ? statements : tail, defuns);
+ return tail;
}
function as_statement_array_with_return(node, ab) {
function push(node) {
if (block) {
block.push(node);
- if (is_lexical_definition(node)) block.required = true;
+ if (!safe_to_trim(node)) block.required = true;
} else {
target.push(node);
}
return in_list ? List.skip : make_node(AST_EmptyStatement, node);
case 1:
var stat = node.body[0];
- if (is_lexical_definition(stat)) return node;
+ if (!safe_to_trim(stat)) return node;
if (parent instanceof AST_IterationStatement && stat instanceof AST_LambdaDefinition) return node;
return stat;
}
try {
throw a;
} catch {
- console.log(a, a, a);
+ function g() {
+ return a;
+ }
+ console.log(a, a, g());
}
- console.log(a, a, a);
+ console.log(a, a, g());
}
expect_stdout: [
"PASS PASS PASS",
}
expect_stdout: "1"
}
+
+block_scope_1: {
+ input: {
+ console.log(typeof f);
+ function f() {}
+ }
+ expect: {
+ console.log(typeof f);
+ function f() {}
+ }
+ expect_stdout: "function"
+}
+
+block_scope_1_compress: {
+ options = {
+ evaluate: true,
+ reduce_vars: true,
+ toplevel: true,
+ typeofs: true,
+ unused: true,
+ }
+ input: {
+ console.log(typeof f);
+ function f() {}
+ }
+ expect: {
+ console.log("function");
+ }
+ expect_stdout: "function"
+}
+
+block_scope_2: {
+ input: {
+ {
+ console.log(typeof f);
+ }
+ function f() {}
+ }
+ expect: {
+ console.log(typeof f);
+ function f() {}
+ }
+ expect_stdout: "function"
+}
+
+block_scope_2_compress: {
+ options = {
+ evaluate: true,
+ reduce_vars: true,
+ toplevel: true,
+ typeofs: true,
+ unused: true,
+ }
+ input: {
+ {
+ console.log(typeof f);
+ }
+ function f() {}
+ }
+ expect: {
+ console.log("function");
+ }
+ expect_stdout: "function"
+}
+
+block_scope_3: {
+ input: {
+ console.log(typeof f);
+ {
+ function f() {}
+ }
+ }
+ expect: {
+ console.log(typeof f);
+ {
+ function f() {}
+ }
+ }
+ expect_stdout: true
+}
+
+block_scope_3_compress: {
+ options = {
+ evaluate: true,
+ reduce_vars: true,
+ toplevel: true,
+ typeofs: true,
+ unused: true,
+ }
+ input: {
+ console.log(typeof f);
+ {
+ function f() {}
+ }
+ }
+ expect: {
+ console.log(typeof f);
+ {
+ function f() {}
+ }
+ }
+ expect_stdout: true
+}
+
+block_scope_4: {
+ input: {
+ {
+ console.log(typeof f);
+ function f() {}
+ }
+ }
+ expect: {
+ console.log(typeof f);
+ function f() {}
+ }
+ expect_stdout: "function"
+}
+
+block_scope_4_compress: {
+ options = {
+ evaluate: true,
+ reduce_vars: true,
+ toplevel: true,
+ typeofs: true,
+ unused: true,
+ }
+ input: {
+ {
+ console.log(typeof f);
+ function f() {}
+ }
+ }
+ expect: {
+ console.log("function");
+ }
+ expect_stdout: "function"
+}