|
| 1 | +var Checker = require('../../../lib/checker'); |
| 2 | +var expect = require('chai').expect; |
| 3 | + |
| 4 | +describe('rules/require-array-destructuring', function() { |
| 5 | + var nestedWithDestructuring = [ |
| 6 | + 'var attributes = {', |
| 7 | + ' colors: ["red", "green", "blue"]', |
| 8 | + '};', |
| 9 | + 'var first = attributes.colors[0];' |
| 10 | + ].join('\n'); |
| 11 | + |
| 12 | + var withDestructuring = [ |
| 13 | + 'var colors = ["red", "green", "blue"];', |
| 14 | + 'var [ red ] = colors;' |
| 15 | + ].join('\n'); |
| 16 | + |
| 17 | + var withIndexing = [ |
| 18 | + 'var colors = ["red", "green", "blue"];', |
| 19 | + 'var red = colors[0];' |
| 20 | + ].join('\n'); |
| 21 | + |
| 22 | + describe('when { requireArrayDestructuring: true }', function() { |
| 23 | + it('allows array destructuring', function() { |
| 24 | + var checker = buildChecker({ requireArrayDestructuring: true }); |
| 25 | + |
| 26 | + expect(checker.checkString(withDestructuring)).to.have.no.errors(); |
| 27 | + }); |
| 28 | + |
| 29 | + it('disallows variable assignment via array indexing with a literal', function() { |
| 30 | + var checker = buildChecker({ requireArrayDestructuring: true }); |
| 31 | + |
| 32 | + expect(checker.checkString(withIndexing)). |
| 33 | + to.have.one.validation.error.from('requireArrayDestructuring'); |
| 34 | + }); |
| 35 | + |
| 36 | + it('requires nested array destructuring', function() { |
| 37 | + var checker = buildChecker({ requireArrayDestructuring: true }); |
| 38 | + |
| 39 | + expect(checker.checkString(nestedWithDestructuring)) |
| 40 | + .to.have.one.validation.error.from('requireArrayDestructuring'); |
| 41 | + }); |
| 42 | + }); |
| 43 | + |
| 44 | + describe('when { requireArrayDestructuring: false }', function() { |
| 45 | + it('allows array destructuring', function() { |
| 46 | + var checker = buildChecker({ requireArrayDestructuring: false }); |
| 47 | + |
| 48 | + expect(checker.checkString(withDestructuring)).to.have.no.errors(); |
| 49 | + }); |
| 50 | + |
| 51 | + it('allows variable assignment via array indexing with a literal', function() { |
| 52 | + var checker = buildChecker({ requireArrayDestructuring: false }); |
| 53 | + |
| 54 | + expect(checker.checkString(withDestructuring)).to.have.no.errors(); |
| 55 | + }); |
| 56 | + }); |
| 57 | + |
| 58 | + function buildChecker(rules) { |
| 59 | + var checker = new Checker(); |
| 60 | + checker.registerDefaultRules(); |
| 61 | + checker.configure(rules); |
| 62 | + |
| 63 | + return checker; |
| 64 | + } |
| 65 | +}); |
0 commit comments