This repository was archived by the owner on Mar 23, 2024. It is now read-only.
File tree Expand file tree Collapse file tree 4 files changed +100
-1
lines changed
Expand file tree Collapse file tree 4 files changed +100
-1
lines changed Original file line number Diff line number Diff line change 162162 " requireObjectDestructuring" ,
163163 " requireEnhancedObjectLiterals" ,
164164 " requireArrayDestructuring" ,
165- " disallowVar"
165+ " disallowVar" ,
166+ " requireObjectShorthand"
166167 ],
167168 "Everything else" : [
168169 " requireParenthesesAroundIIFE" ,
Original file line number Diff line number Diff line change @@ -953,6 +953,7 @@ Configuration.prototype.registerDefaultRules = function() {
953953 this . registerRule ( require ( '../rules/require-enhanced-object-literals' ) ) ;
954954 this . registerRule ( require ( '../rules/require-array-destructuring' ) ) ;
955955 this . registerRule ( require ( '../rules/disallow-var' ) ) ;
956+ this . registerRule ( require ( '../rules/require-object-shorthand' ) ) ;
956957 /* ES6 only (end) */
957958
958959 this . registerRule ( require ( '../rules/require-curly-braces' ) ) ;
Original file line number Diff line number Diff line change 1+ /**
2+ * Group your shorthand properties at the beginning of your object declaration.
3+ * Why? Because it's fancy! Oh, yeah.
4+ *
5+ * Types: `Boolean`
6+ *
7+ * Values: `true`
8+ *
9+ * Version: `ES6`
10+ *
11+ * #### Example
12+ *
13+ * ```js
14+ * "requireObjectShorthand": true
15+ * ```
16+ *
17+ * ##### Valid
18+ *
19+ * ```js
20+ * var x = {
21+ * a,
22+ * handler,
23+ * abc: xyz,
24+ * test: test2
25+ * }
26+ * ```
27+ *
28+ * ##### Invalid
29+ *
30+ * ```js
31+ * var x = {
32+ * a: a
33+ * }
34+ * ```
35+ *
36+ */
37+
38+ var assert = require ( 'assert' ) ;
39+
40+ module . exports = function ( ) { } ;
41+
42+ module . exports . prototype = {
43+
44+ configure : function ( options ) {
45+ assert (
46+ options === true ,
47+ this . getOptionName ( ) + ' option requires true value'
48+ ) ;
49+ } ,
50+
51+ getOptionName : function ( ) {
52+ return 'requireObjectShorthand' ;
53+ } ,
54+
55+ check : function ( file , errors ) {
56+ file . iterateNodesByType ( 'Property' , function ( node ) {
57+ if ( node . key . name === node . value . name && ! node . shorthand ) {
58+ errors . add ( 'You should use shorthand version' , node . loc . start ) ;
59+ }
60+ } ) ;
61+ }
62+ } ;
Original file line number Diff line number Diff line change 1+ var Checker = require ( '../../../lib/checker' ) ;
2+ var expect = require ( 'chai' ) . expect ;
3+
4+ describe ( 'rules/require-object-shorthand.js' , function ( ) {
5+ var checker ;
6+
7+ beforeEach ( function ( ) {
8+ checker = new Checker ( ) ;
9+ checker . registerDefaultRules ( ) ;
10+
11+ checker . configure ( {
12+ requireObjectShorthand : true
13+ } ) ;
14+ } ) ;
15+
16+ it ( 'should warn on incorrect configuration' , function ( ) {
17+ expect ( function ( ) {
18+ checker . configure ( {
19+ requireObjectShorthand : 1
20+ } ) ;
21+ } ) . to . throw ( ) ;
22+ } ) ;
23+
24+ it ( 'should warn on possible shorthand' , function ( ) {
25+ expect ( checker . checkString ( '({a: a})' ) ) . to . have . one . validation . error . from ( 'requireObjectShorthand' ) ;
26+ } ) ;
27+
28+ it ( 'should warn on one possible shorthand for two props' , function ( ) {
29+ expect ( checker . checkString ( '({a: a, b: c})' ) ) . to . have . one . validation . error . from ( 'requireObjectShorthand' ) ;
30+ } ) ;
31+
32+ it ( 'should not warn' , function ( ) {
33+ expect ( checker . checkString ( '({b: c})' ) ) . to . have . no . errors ( ) ;
34+ } ) ;
35+ } ) ;
You can’t perform that action at this time.
0 commit comments