label.js 9.57 KB
(function() {
  var escope, esprima, expect, harmony;

  expect = require('chai').expect;

  esprima = require('esprima');

  harmony = require('../third_party/esprima');

  escope = require('..');

  describe('label', function() {
    it('should not create variables', function() {
      var ast, globalScope, scope, scopeManager;
      ast = esprima.parse("function bar() { q: for(;;) { break q; } }");
      scopeManager = escope.analyze(ast);
      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('bar');
      expect(globalScope.references).to.have.length(0);
      scope = scopeManager.scopes[1];
      expect(scope.type).to.be.equal('function');
      expect(scope.variables).to.have.length(1);
      expect(scope.variables[0].name).to.be.equal('arguments');
      expect(scope.isArgumentsMaterialized()).to.be["false"];
      return expect(scope.references).to.have.length(0);
    });
    return it('should count child node references', function() {
      var ast, globalScope, scopeManager;
      ast = esprima.parse("var foo = 5;\n\nlabel: while (true) {\n  console.log(foo);\n  break;\n}");
      scopeManager = escope.analyze(ast);
      expect(scopeManager.scopes).to.have.length(1);
      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('foo');
      expect(globalScope.through.length).to.be.equal(3);
      expect(globalScope.through[2].identifier.name).to.be.equal('foo');
      return expect(globalScope.through[2].isRead()).to.be["true"];
    });
  });

}).call(this);

//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJzb3VyY2VzIjpbImxhYmVsLmNvZmZlZSJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiQUF1QkE7QUFBQSxNQUFBLGdDQUFBOztBQUFBLEVBQUEsTUFBQSxHQUFTLE9BQUEsQ0FBUyxNQUFULENBQWUsQ0FBQyxNQUF6QixDQUFBOztBQUFBLEVBQ0EsT0FBQSxHQUFVLE9BQUEsQ0FBUyxTQUFULENBRFYsQ0FBQTs7QUFBQSxFQUVBL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