UNPKG

1.44 kBJavaScriptView Raw
1/**
2 * Disallows identical destructuring names for the key and value in favor of using shorthand destructuring.
3 *
4 * Type: `Boolean`
5 *
6 * Value: `true`
7 *
8 * #### Example
9 *
10 * ```js
11 * "disallowIdenticalDestructuringNames": true
12 * ```
13 *
14 * ##### Valid for mode `true`
15 *
16 * ```js
17 * var {left, top} = obj; // shorthand
18 * var {left, top: topper} = obj; // different identifier
19 * let { [key]: key } = obj; // computed property
20 * ```
21 *
22 * ##### Invalid for mode `true`
23 *
24 * ```js
25 * var {left: left, top: top} = obj;
26 * ```
27 */
28
29var assert = require('assert');
30
31module.exports = function() {};
32
33module.exports.prototype = {
34 configure: function(options) {
35 assert(
36 options === true,
37 this.getOptionName() + ' option requires a true value or should be removed'
38 );
39 },
40
41 getOptionName: function() {
42 return 'disallowIdenticalDestructuringNames';
43 },
44
45 check: function(file, errors) {
46 file.iterateNodesByType(['ObjectPattern'], function(node) {
47 var props = node.properties;
48 for (var i = 0; i < props.length; i++) {
49 var prop = props[i];
50 if (prop.type === 'ObjectProperty' && !prop.shorthand && !prop.computed &&
51 prop.key.name === prop.value.name) {
52 errors.add('Use the shorthand form of destructuring instead', prop);
53 }
54 }
55 });
56 }
57};