var defs = [];
vars.each(function(def, name){
if (self instanceof AST_Lambda
- && find_if(function(x){ return x.name == def.name.name },
- self.argnames)) {
+ && !all(self.argnames, function(argname) {
+ return argname.name != name;
+ })) {
vars.del(name);
} else {
def = def.clone();
FROM_MOZ_STACK = [];
var ast = from_moz(node);
FROM_MOZ_STACK = save_stack;
+ ast.walk(new TreeWalker(function(node) {
+ if (node instanceof AST_LabelRef) {
+ for (var level = 0, parent; parent = this.parent(level); level++) {
+ if (parent instanceof AST_Scope) break;
+ if (parent instanceof AST_LabeledStatement && parent.label.name == node.name) {
+ node.thedef = parent.label;
+ break;
+ }
+ }
+ if (!node.thedef) {
+ var s = node.start;
+ js_error("Undefined label " + node.name, s.file, s.line, s.col, s.pos);
+ }
+ }
+ }));
return ast;
};
function labeled_statement() {
var label = as_symbol(AST_Label);
- if (find_if(function(l){ return l.name == label.name }, S.labels)) {
+ if (!all(S.labels, function(l) {
+ return l.name != label.name;
+ })) {
// ECMA-262, 12.12: An ECMAScript program is considered
// syntactically incorrect if it contains a
// LabelledStatement that is enclosed by a
// pass 1: setup scope chaining and handle definitions
var self = this;
var scope = self.parent_scope = null;
- var labels = new Dictionary();
var defun = null;
var tw = new TreeWalker(function(node, descend){
if (node instanceof AST_Catch) {
node.init_scope_vars(scope);
var save_scope = scope;
var save_defun = defun;
- var save_labels = labels;
defun = scope = node;
- labels = new Dictionary();
descend();
scope = save_scope;
defun = save_defun;
- labels = save_labels;
return true; // don't descend again in TreeWalker
}
- if (node instanceof AST_LabeledStatement) {
- var l = node.label;
- if (labels.has(l.name)) {
- throw new Error(string_template("Label {name} defined twice", l));
- }
- labels.set(l.name, l);
- descend();
- labels.del(l.name);
- return true; // no descend again
- }
if (node instanceof AST_With) {
for (var s = scope; s; s = s.parent_scope)
s.uses_with = true;
else if (node instanceof AST_SymbolCatch) {
scope.def_variable(node).defun = defun;
}
- else if (node instanceof AST_LabelRef) {
- var sym = labels.get(node.name);
- if (!sym) throw new Error(string_template("Undefined label {name} [{line},{col}]", {
- name: node.name,
- line: node.start.line,
- col: node.start.col
- }));
- node.thedef = sym;
- }
});
self.walk(tw);
assert.strictEqual(err.line, 1);
assert.strictEqual(err.col, 12);
});
+ it("should reject duplicated label name", function() {
+ var result = Uglify.minify("L:{L:{}}");
+ var err = result.error;
+ assert.ok(err instanceof Error);
+ assert.strictEqual(err.stack.split(/\n/)[0], "SyntaxError: Label L defined twice");
+ assert.strictEqual(err.filename, "0");
+ assert.strictEqual(err.line, 1);
+ assert.strictEqual(err.col, 4);
+ });
});
describe("global_defs", function() {