aboutsummaryrefslogtreecommitdiff
path: root/test/mocha/arguments.js
blob: 998978eb6153584edd7171d3b2f6393a63964205 (about) (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
var UglifyJS = require("../node");
var assert = require("assert");

describe("arguments", function() {
    it("Should known that arguments in functions are local scoped", function() {
        var ast = UglifyJS.parse("var arguments; var f = function() {arguments.length}");
        ast.figure_out_scope();

        // Test scope of `var arguments`
        assert.strictEqual(ast.find_variable("arguments").global, true);

        // Select arguments symbol in function
        var symbol = ast.body[1].definitions[0].value.find_variable("arguments");

        assert.strictEqual(symbol.global, false);
        assert.strictEqual(symbol.scope, ast. // From ast
            body[1]. // Select 2nd statement (equals to `var f ...`)
            definitions[0]. // First definition of selected statement
            value // Select function as scope
        );
    });

    it("Should recognize when a function uses arguments", function() {
        var ast = UglifyJS.parse("function a(){function b(){function c(){}; return arguments[0];}}");
        ast.figure_out_scope();
        assert.strictEqual(ast.body[0].uses_arguments, false);
        assert.strictEqual(ast.body[0].body[0].uses_arguments, true);
        assert.strictEqual(ast.body[0].body[0].body[0].uses_arguments, false);
    });
});
mappings: map.mappings.split(/;/).map(function(line) { indices[0] = 0; return line.split(/,/).map(function(segment) { return indices.slice(0, vlq_decode(indices, segment)); }); }), sources: map.sources, }; if (!sources_content || !map.sourcesContent) return; for (var i = 0; i < map.sources.length; i++) { var content = map.sourcesContent[i]; if (content) sources_content[map.sources[i]] = content; } }); var prev_source; var generated_line = 1; var generated_column = 0; var source_index = 0; var original_line = 1; var original_column = 0; var name_index = 0; return { add: options.orig ? function(source, gen_line, gen_col, orig_line, orig_col, name) { var map = options.orig[source]; if (map) { var segments = map.mappings[orig_line - 1]; if (!segments) return; var indices; for (var i = 0; i < segments.length; i++) { var col = segments[i][0]; if (orig_col >= col) indices = segments[i]; if (orig_col <= col) break; } if (!indices || indices.length < 4) { source = null; } else { source = map.sources[indices[1]]; orig_line = indices[2]; orig_col = indices[3]; if (indices.length > 4) name = map.names[indices[4]]; } } add(source, gen_line, gen_col, orig_line, orig_col, name); } : add, setSourceContent: sources_content ? function(source, content) { if (!(source in sources_content)) { sources_content[source] = content; } } : noop, toString: function() { return JSON.stringify({ version: 3, file: options.filename || undefined, sourceRoot: options.root || undefined, sources: sources, sourcesContent: sources_content ? sources.map(function(source) { return sources_content[source] || null; }) : undefined, names: names, mappings: mappings, }); } }; function add(source, gen_line, gen_col, orig_line, orig_col, name) { if (prev_source == null && source == null) return; prev_source = source; if (generated_line < gen_line) { generated_column = 0; do { mappings += ";"; } while (++generated_line < gen_line); } else if (mappings) { mappings += ","; } mappings += vlq_encode(gen_col - generated_column); generated_column = gen_col; if (source == null) return; var src_idx = sources.index(source); mappings += vlq_encode(src_idx - source_index); source_index = src_idx; mappings += vlq_encode(orig_line - original_line); original_line = orig_line; mappings += vlq_encode(orig_col - original_column); original_column = orig_col; if (options.names && name != null) { var name_idx = names.index(name); mappings += vlq_encode(name_idx - name_index); name_index = name_idx; } } }