136 lines
30 KiB
JavaScript
136 lines
30 KiB
JavaScript
|
(function() {
|
||
|
var escope, expect, harmony;
|
||
|
|
||
|
expect = require('chai').expect;
|
||
|
|
||
|
harmony = require('../third_party/esprima');
|
||
|
|
||
|
escope = require('..');
|
||
|
|
||
|
describe('ES6 block scope', function() {
|
||
|
it('let is materialized in ES6 block scope#1', function() {
|
||
|
var ast, scope, scopeManager;
|
||
|
ast = harmony.parse("{\n let i = 20;\n i;\n}");
|
||
|
scopeManager = escope.analyze(ast, {
|
||
|
ecmaVersion: 6
|
||
|
});
|
||
|
expect(scopeManager.scopes).to.have.length(2);
|
||
|
scope = scopeManager.scopes[0];
|
||
|
expect(scope.type).to.be.equal('global');
|
||
|
expect(scope.variables).to.have.length(0);
|
||
|
scope = scopeManager.scopes[1];
|
||
|
expect(scope.type).to.be.equal('block');
|
||
|
expect(scope.variables).to.have.length(1);
|
||
|
expect(scope.variables[0].name).to.be.equal('i');
|
||
|
expect(scope.references).to.have.length(2);
|
||
|
expect(scope.references[0].identifier.name).to.be.equal('i');
|
||
|
return expect(scope.references[1].identifier.name).to.be.equal('i');
|
||
|
});
|
||
|
it('let is materialized in ES6 block scope#2', function() {
|
||
|
var ast, scope, scopeManager;
|
||
|
ast = harmony.parse("{\n let i = 20;\n var i = 20;\n i;\n}");
|
||
|
scopeManager = escope.analyze(ast, {
|
||
|
ecmaVersion: 6
|
||
|
});
|
||
|
expect(scopeManager.scopes).to.have.length(2);
|
||
|
scope = scopeManager.scopes[0];
|
||
|
expect(scope.type).to.be.equal('global');
|
||
|
expect(scope.variables).to.have.length(1);
|
||
|
expect(scope.variables[0].name).to.be.equal('i');
|
||
|
scope = scopeManager.scopes[1];
|
||
|
expect(scope.type).to.be.equal('block');
|
||
|
expect(scope.variables).to.have.length(1);
|
||
|
expect(scope.variables[0].name).to.be.equal('i');
|
||
|
expect(scope.references).to.have.length(3);
|
||
|
expect(scope.references[0].identifier.name).to.be.equal('i');
|
||
|
expect(scope.references[1].identifier.name).to.be.equal('i');
|
||
|
return expect(scope.references[2].identifier.name).to.be.equal('i');
|
||
|
});
|
||
|
it('function delaration is materialized in ES6 block scope', function() {
|
||
|
var ast, scope, scopeManager;
|
||
|
ast = harmony.parse("{\n function test() {\n }\n test();\n}");
|
||
|
scopeManager = escope.analyze(ast, {
|
||
|
ecmaVersion: 6
|
||
|
});
|
||
|
expect(scopeManager.scopes).to.have.length(3);
|
||
|
scope = scopeManager.scopes[0];
|
||
|
expect(scope.type).to.be.equal('global');
|
||
|
expect(scope.variables).to.have.length(0);
|
||
|
scope = scopeManager.scopes[1];
|
||
|
expect(scope.type).to.be.equal('block');
|
||
|
expect(scope.variables).to.have.length(1);
|
||
|
expect(scope.variables[0].name).to.be.equal('test');
|
||
|
expect(scope.references).to.have.length(1);
|
||
|
expect(scope.references[0].identifier.name).to.be.equal('test');
|
||
|
scope = scopeManager.scopes[2];
|
||
|
expect(scope.type).to.be.equal('function');
|
||
|
expect(scope.variables).to.have.length(1);
|
||
|
expect(scope.variables[0].name).to.be.equal('arguments');
|
||
|
return expect(scope.references).to.have.length(0);
|
||
|
});
|
||
|
it('let is not hoistable#1', function() {
|
||
|
var ast, globalScope, scope, scopeManager;
|
||
|
ast = harmony.parse("var i = 42; (1)\n{\n i; // (2) ReferenceError at runtime.\n let i = 20; // (2)\n i; // (2)\n}");
|
||
|
scopeManager = escope.analyze(ast, {
|
||
|
ecmaVersion: 6
|
||
|
});
|
||
|
expect(scopeManager.scopes).to.have.length(2);
|
||
|
globalScope = scopeManager.scopes[0];
|
||
|
expect(globalScope.type).to.be.equal('global');
|
||
|
expect(globalScope.variables).to.have.length(1);
|
||
|
expect(globalScope.variables[0].name).to.be.equal('i');
|
||
|
expect(globalScope.references).to.have.length(1);
|
||
|
scope = scopeManager.scopes[1];
|
||
|
expect(scope.type).to.be.equal('block');
|
||
|
expect(scope.variables).to.have.length(1);
|
||
|
expect(scope.variables[0].name).to.be.equal('i');
|
||
|
expect(scope.references).to.have.length(3);
|
||
|
expect(scope.references[0].resolved).to.be.equal(scope.variables[0]);
|
||
|
expect(scope.references[1].resolved).to.be.equal(scope.variables[0]);
|
||
|
return expect(scope.references[2].resolved).to.be.equal(scope.variables[0]);
|
||
|
});
|
||
|
return it('let is not hoistable#2', function() {
|
||
|
var ast, globalScope, scope, scopeManager, v1, v2, v3;
|
||
|
ast = harmony.parse("(function () {\n var i = 42; // (1)\n i; // (1)\n {\n i; // (3)\n {\n i; // (2)\n let i = 20; // (2)\n i; // (2)\n }\n let i = 30; // (3)\n i; // (3)\n }\n i; // (1)\n}());");
|
||
|
scopeManager = escope.analyze(ast, {
|
||
|
ecmaVersion: 6
|
||
|
});
|
||
|
expect(scopeManager.scopes).to.have.length(4);
|
||
|
globalScope = scopeManager.scopes[0];
|
||
|
expect(globalScope.type).to.be.equal('global');
|
||
|
expect(globalScope.variables).to.have.length(0);
|
||
|
expect(globalScope.references).to.have.length(0);
|
||
|
scope = scopeManager.scopes[1];
|
||
|
expect(scope.type).to.be.equal('function');
|
||
|
expect(scope.variables).to.have.length(2);
|
||
|
expect(scope.variables[0].name).to.be.equal('arguments');
|
||
|
expect(scope.variables[1].name).to.be.equal('i');
|
||
|
v1 = scope.variables[1];
|
||
|
expect(scope.references).to.have.length(3);
|
||
|
expect(scope.references[0].resolved).to.be.equal(v1);
|
||
|
expect(scope.references[1].resolved).to.be.equal(v1);
|
||
|
expect(scope.references[2].resolved).to.be.equal(v1);
|
||
|
scope = scopeManager.scopes[2];
|
||
|
expect(scope.type).to.be.equal('block');
|
||
|
expect(scope.variables).to.have.length(1);
|
||
|
expect(scope.variables[0].name).to.be.equal('i');
|
||
|
v3 = scope.variables[0];
|
||
|
expect(scope.references).to.have.length(3);
|
||
|
expect(scope.references[0].resolved).to.be.equal(v3);
|
||
|
expect(scope.references[1].resolved).to.be.equal(v3);
|
||
|
expect(scope.references[2].resolved).to.be.equal(v3);
|
||
|
scope = scopeManager.scopes[3];
|
||
|
expect(scope.type).to.be.equal('block');
|
||
|
expect(scope.variables).to.have.length(1);
|
||
|
expect(scope.variables[0].name).to.be.equal('i');
|
||
|
v2 = scope.variables[0];
|
||
|
expect(scope.references).to.have.length(3);
|
||
|
expect(scope.references[0].resolved).to.be.equal(v2);
|
||
|
expect(scope.references[1].resolved).to.be.equal(v2);
|
||
|
return expect(scope.references[2].resolved).to.be.equal(v2);
|
||
|
});
|
||
|
});
|
||
|
|
||
|
}).call(this);
|
||
|
|
||
|
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJzb3VyY2VzIjpbImVzNi1ibG9jay1zY29wZS5jb2ZmZWUiXSwibmFtZXMiOltdLCJtYXBwaW5ncyI6IkFBdUJBO0FBQUEsTUFBQSx1QkFBQTs7QUFBQSxFQUFBLE1BQUEsR0FBUyxPQUFBLENBQVMsTUFBVCxDQUFlLENBQUMsTUFBekIsQ0FBQTs7QUFBQSxFQUNBLE9BQUEsR0FBVSxPQUFBLENBQVMsd0JBQVQsQ0FEVixDQUFBOztBQUFBLEVBRUEsTUFBQSxHQUFTLE9BQUEsQ0FBUyxJQUFULENBRlQsQ0FBQTs7QUFBQSxFQUlBLFFBQUEsQ0FBVSxpQkFBVixFQUE0QixTQUFBLEdBQUE7QUFDeEIsSUFBQSxFQUFBLENBQUksMENBQUosRUFBK0MsU0FBQSxHQUFBO0FBQzNDLFVBQUEsd0JBQUE7QUFBQSxNQUFBLEdBQUEsR0FBTSxPQUFPLENBQUMsS0FBUixDQUFpQiwrQkFBakIsQ0FBTixDQUFBO0FBQUEsTUFPQSxZQUFBLEdBQWUsTUFBTSxDQUFDLE9BQVAsQ0FBZSxHQUFmLEVBQW9CO0FBQUEsUUFBQSxXQUFBLEVBQWEsQ0FBYjtPQUFwQixDQVBmLENBQUE7QUFBQSxNQVFBLE1BQUEsQ0FBTyxZQUFZLENBQUMsTUFBcEIsQ0FBMkIsQ0FBQyxFQUFFLENBQUMsSUFBSSxDQUFDLE1BQXBDLENBQTJDLENBQTNDLENBUkEsQ0FBQTt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