|
| 1 | +/** |
| 2 | + * @fileoverview Typescript scope tests |
| 3 | + * @author Reyad Attiyat |
| 4 | + */ |
| 5 | +"use strict"; |
| 6 | + |
| 7 | +//------------------------------------------------------------------------------ |
| 8 | +// Requirements |
| 9 | +//------------------------------------------------------------------------------ |
| 10 | + |
| 11 | +require("babel-register"); |
| 12 | + |
| 13 | +const expect = require('chai').expect, |
| 14 | + parse = require('typescript-eslint-parser').parse, |
| 15 | + analyze = require('../src').analyze; |
| 16 | + |
| 17 | +//------------------------------------------------------------------------------ |
| 18 | +// Tests |
| 19 | +//------------------------------------------------------------------------------ |
| 20 | + |
| 21 | +describe('typescript', () => { |
| 22 | + describe('multiple call signatures', () => { |
| 23 | + it('should create a function scope', () => { |
| 24 | + const ast = parse(` |
| 25 | + function foo(bar: number): number; |
| 26 | + function foo(bar: string): string; |
| 27 | + function foo(bar: string | number): string | number { |
| 28 | + return bar; |
| 29 | + } |
| 30 | + `); |
| 31 | + |
| 32 | + const scopeManager = analyze(ast); |
| 33 | + |
| 34 | + expect(scopeManager.scopes).to.have.length(4); |
| 35 | + |
| 36 | + const globalScope = scopeManager.scopes[0]; |
| 37 | + expect(globalScope.type).to.be.equal('global'); |
| 38 | + expect(globalScope.variables).to.have.length(1); |
| 39 | + expect(globalScope.references).to.have.length(0); |
| 40 | + expect(globalScope.isArgumentsMaterialized()).to.be.true; |
| 41 | + |
| 42 | + // Function scopes |
| 43 | + let scope = scopeManager.scopes[1]; |
| 44 | + expect(scope.type).to.be.equal('function'); |
| 45 | + expect(scope.variables).to.have.length(2); |
| 46 | + expect(scope.variables[0].name).to.be.equal('arguments'); |
| 47 | + expect(scope.isArgumentsMaterialized()).to.be.false; |
| 48 | + expect(scope.references).to.have.length(0); |
| 49 | + |
| 50 | + scope = scopeManager.scopes[2]; |
| 51 | + expect(scope.type).to.be.equal('function'); |
| 52 | + expect(scope.variables).to.have.length(2); |
| 53 | + expect(scope.variables[0].name).to.be.equal('arguments'); |
| 54 | + expect(scope.isArgumentsMaterialized()).to.be.false; |
| 55 | + expect(scope.references).to.have.length(0); |
| 56 | + |
| 57 | + scope = scopeManager.scopes[3]; |
| 58 | + expect(scope.type).to.be.equal('function'); |
| 59 | + expect(scope.variables).to.have.length(2); |
| 60 | + expect(scope.variables[0].name).to.be.equal('arguments'); |
| 61 | + expect(scope.isArgumentsMaterialized()).to.be.false; |
| 62 | + expect(scope.references).to.have.length(1); |
| 63 | + |
| 64 | + |
| 65 | + }); |
| 66 | + }); |
| 67 | +}); |
0 commit comments