import { Node } from 'acorn'; import { expect } from 'chai'; import InterpreterInstance from '../../lib/interpreter-instance'; import { ArrowFunctionExpression } from '../../lib/node-processors/ArrowFunctionExpression'; import { ValueNode } from '../../lib/types/valueNode'; const generateNode = (type = '', rest?) => { const testNode = new Node({ options: {} } as any, 0, '' as any); Object.assign(testNode, { type }, rest); return testNode; }; context('ArrowFunctionExpression', () => { let instance; beforeEach(() => { instance = new InterpreterInstance({}, [], {}, [], 'test'); }); describe('Edge Cases', () => { it('should throw if multiple rest elements get used in a single object', () => { const expectedValue = { someValue: { a: [1, 2, 3] } }; const computedNode = new ValueNode('test', 'randomValue'); const propertyNode = generateNode('Property', { key: generateNode('Literal', { value: 'otherValue' }), value: { type: 'Identifier' } }); const argumentNode = generateNode('ObjectPattern', { properties: [ propertyNode, { type: 'RestElement', argument: computedNode }, { type: 'RestElement', argument: computedNode } ] }); const bodyNode = generateNode('Identifier', { name: 'randomValue' }); const parentNode = generateNode('ArrowFunctionExpression', { params: [argumentNode], body: bodyNode }) as any; expect(() => ArrowFunctionExpression(instance, parentNode)({ ...expectedValue, otherValue: ['123'] })) .to.throw(); }); it('should handle a non identifier (thus processed) property key', () => { const propertyNode = generateNode('Property', { key: generateNode('Literal', { value: 'otherValue' }), value: new ValueNode('value', 'otherValue') }); const argumentNode = generateNode('ObjectPattern', { properties: [propertyNode] }); const bodyNode = generateNode('Identifier', { name: 'otherValue' }); const parentNode = generateNode('ArrowFunctionExpression', { params: [argumentNode], body: bodyNode }); const results = ArrowFunctionExpression( instance, parentNode as any )({ someValue: { a: 'a', b: '123' }, otherValue: new ValueNode('test', 'expectedValue') }); expect(results).to.equal('expectedValue'); }); it('should handle a non identifier (thus processed) rest property key', () => { const expectedValue = { someValue: { a: [1, 2, 3] } }; const computedNode = new ValueNode('test', 'randomValue'); const propertyNode = generateNode('Property', { key: generateNode('Literal', { value: 'otherValue' }), value: { type: 'Identifier' } }); const argumentNode = generateNode('ObjectPattern', { properties: [propertyNode, { type: 'RestElement', argument: computedNode }] }); const bodyNode = generateNode('Identifier', { name: 'randomValue' }); const parentNode = generateNode('ArrowFunctionExpression', { params: [argumentNode], body: bodyNode }) as any; const results = ArrowFunctionExpression( instance, parentNode )({ ...expectedValue, otherValue: ['123'] }); expect(results).to.deep.eq(expectedValue); }); }); describe('Error Scenarios', () => { it('should throw if a property element gets used after a rest element', () => { const expectedValue = { someValue: { a: [1, 2, 3] } }; const computedNode = new ValueNode('test', 'randomValue'); const propertyNode = generateNode('Property', { key: generateNode('Literal', { value: 'otherValue' }), value: { type: 'Identifier' } }); const argumentNode = generateNode('ObjectPattern', { properties: [{ type: 'RestElement', argument: computedNode }, propertyNode] }); const bodyNode = generateNode('Identifier', { name: 'randomValue' }); const parentNode = generateNode('ArrowFunctionExpression', { params: [argumentNode], body: bodyNode }) as any; expect(() => ArrowFunctionExpression(instance, parentNode).value({ ...expectedValue, otherValue: ['123'] })) .to.throw(); }); }); });