113 lines
19 KiB
JavaScript
113 lines
19 KiB
JavaScript
|
(function() {
|
||
|
'use strict';
|
||
|
var escope, esprima, expect;
|
||
|
|
||
|
expect = require('chai').expect;
|
||
|
|
||
|
escope = require('..');
|
||
|
|
||
|
esprima = require('esprima');
|
||
|
|
||
|
describe('implicit global reference', function() {
|
||
|
it('assignments global scope', function() {
|
||
|
var ast, scopes;
|
||
|
ast = esprima.parse("var x = 20;\nx = 300;");
|
||
|
scopes = escope.analyze(ast).scopes;
|
||
|
expect(scopes.map(function(scope) {
|
||
|
return scope.variables.map(function(variable) {
|
||
|
return variable.defs.map(function(def) {
|
||
|
return def.type;
|
||
|
});
|
||
|
});
|
||
|
})).to.be.eql([[['Variable']]]);
|
||
|
return expect(scopes[0].implicit.variables.map(function(variable) {
|
||
|
return variable.name;
|
||
|
})).to.be.eql([]);
|
||
|
});
|
||
|
it('assignments global scope without definition', function() {
|
||
|
var ast, scopes;
|
||
|
ast = esprima.parse("x = 300;\nx = 300;");
|
||
|
scopes = escope.analyze(ast).scopes;
|
||
|
expect(scopes.map(function(scope) {
|
||
|
return scope.variables.map(function(variable) {
|
||
|
return variable.defs.map(function(def) {
|
||
|
return def.type;
|
||
|
});
|
||
|
});
|
||
|
})).to.be.eql([[]]);
|
||
|
return expect(scopes[0].implicit.variables.map(function(variable) {
|
||
|
return variable.name;
|
||
|
})).to.be.eql(['x']);
|
||
|
});
|
||
|
it('assignments global scope without definition eval', function() {
|
||
|
var ast, scopes;
|
||
|
ast = esprima.parse("function inner() {\n eval(str);\n x = 300;\n}");
|
||
|
scopes = escope.analyze(ast).scopes;
|
||
|
expect(scopes.map(function(scope) {
|
||
|
return scope.variables.map(function(variable) {
|
||
|
return variable.defs.map(function(def) {
|
||
|
return def.type;
|
||
|
});
|
||
|
});
|
||
|
})).to.be.eql([[['FunctionName']], [[]]]);
|
||
|
return expect(scopes[0].implicit.variables.map(function(variable) {
|
||
|
return variable.name;
|
||
|
})).to.be.eql([]);
|
||
|
});
|
||
|
it('assignment leaks', function() {
|
||
|
var ast, scopes;
|
||
|
ast = esprima.parse("function outer() {\n x = 20;\n}");
|
||
|
scopes = escope.analyze(ast).scopes;
|
||
|
expect(scopes.map(function(scope) {
|
||
|
return scope.variables.map(function(variable) {
|
||
|
return variable.name;
|
||
|
});
|
||
|
})).to.be.eql([['outer'], ['arguments']]);
|
||
|
return expect(scopes[0].implicit.variables.map(function(variable) {
|
||
|
return variable.name;
|
||
|
})).to.be.eql(['x']);
|
||
|
});
|
||
|
it('assignment doesn\'t leak', function() {
|
||
|
var ast, scopes;
|
||
|
ast = esprima.parse("function outer() {\n function inner() {\n x = 20;\n }\n var x;\n}");
|
||
|
scopes = escope.analyze(ast).scopes;
|
||
|
expect(scopes.map(function(scope) {
|
||
|
return scope.variables.map(function(variable) {
|
||
|
return variable.name;
|
||
|
});
|
||
|
})).to.be.eql([['outer'], ['arguments', 'inner', 'x'], ['arguments']]);
|
||
|
return expect(scopes[0].implicit.variables.map(function(variable) {
|
||
|
return variable.name;
|
||
|
})).to.be.eql([]);
|
||
|
});
|
||
|
it('for-in-statement leaks', function() {
|
||
|
var ast, scopes;
|
||
|
ast = esprima.parse("function outer() {\n for (x in y) { }\n}");
|
||
|
scopes = escope.analyze(ast).scopes;
|
||
|
expect(scopes.map(function(scope) {
|
||
|
return scope.variables.map(function(variable) {
|
||
|
return variable.name;
|
||
|
});
|
||
|
})).to.be.eql([['outer'], ['arguments']]);
|
||
|
return expect(scopes[0].implicit.variables.map(function(variable) {
|
||
|
return variable.name;
|
||
|
})).to.be.eql(['x']);
|
||
|
});
|
||
|
return it('for-in-statement doesn\'t leaks', function() {
|
||
|
var ast, scopes;
|
||
|
ast = esprima.parse("function outer() {\n function inner() {\n for (x in y) { }\n }\n var x;\n}");
|
||
|
scopes = escope.analyze(ast).scopes;
|
||
|
expect(scopes.map(function(scope) {
|
||
|
return scope.variables.map(function(variable) {
|
||
|
return variable.name;
|
||
|
});
|
||
|
})).to.be.eql([['outer'], ['arguments', 'inner', 'x'], ['arguments']]);
|
||
|
return expect(scopes[0].implicit.variables.map(function(variable) {
|
||
|
return variable.name;
|
||
|
})).to.be.eql([]);
|
||
|
});
|
||
|
});
|
||
|
|
||
|
}).call(this);
|
||
|
|
||
|
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJzb3VyY2VzIjpbImltcGxpY2l0LWdsb2JhbC1yZWZlcmVuY2UuY29mZmVlIl0sIm5hbWVzIjpbXSwibWFwcGluZ3MiOiJBQXNCQztBQUFBLEVBQUEsWUFBQSxDQUFBO0FBQUEsTUFBQSx1QkFBQTs7QUFBQSxFQUVELE1BQUEsR0FBUyxPQUFBLENBQVMsTUFBVCxDQUFlLENBQUMsTUFGeEIsQ0FBQTs7QUFBQSxFQUdELE1BQUEsR0FBUyxPQUFBLENBQVMsSUFBVCxDQUhSLENBQUE7O0FBQUEsRUFJRCxPQUFBLEdBQVUsT0FBQSxDQUFTLFNBQVQsQ0FKVCxDQUFBOztBQUFBLEVBTUQsUUFBQSxDQUFVLDJCQUFWLEVBQXNDLFNBQUEsR0FBQTtBQUNsQyxJQUFBLEVBQUEsQ0FBSSwwQkFBSixFQUErQixTQUFBLEdBQUE7QUFDM0IsVUFBQSxXQUFBO0FBQUEsTUFBQSxHQUFBLEdBQU0sT0FBTyxDQUFDLEtBQVIsQ0FBaUIsdUJBQWpCLENBQU4sQ0FBQTtBQUFBLE1BS0EsTUFBQSxHQUFTLE1BQU0sQ0FBQyxPQUFQLENBQWUsR0FBZixDQUFtQixDQUFDLE1BTDdCLENBQUE7QUFBQSxNQU9BLE1BQUEsQ0FBTyxNQUFNLENBQUMsR0FBUCxDQUFXLFNBQUMsS0FBRCxHQUFBO2VBQ2QsS0FBSyxDQUFDLFNBQVMsQ0FBQyxHQUFoQixDQUFvQixTQUFDLFFBQUQsR0FBQTtpQkFDaEIsUUFBUSxDQUFDLElBQUksQ0FBQyxHQUFkLENBQWtCLFNBQUMsR0FBRCxHQUFBO21CQUFTLEd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