@@ -32,37 +32,80 @@ module.exports = {
32
32
fixable : 'code' ,
33
33
34
34
schema : [ {
35
- type : 'object' ,
36
- properties : {
37
- spaces : {
38
- enum : SPACING_VALUES
39
- } ,
40
- allowMultiline : {
41
- type : 'boolean'
42
- } ,
43
- spacing : {
35
+ definitions : {
36
+ basicConfig : {
44
37
type : 'object' ,
45
38
properties : {
46
- objectLiterals : {
39
+ spaces : {
47
40
enum : SPACING_VALUES
41
+ } ,
42
+ allowMultiline : {
43
+ type : 'boolean'
44
+ } ,
45
+ spacing : {
46
+ type : 'object' ,
47
+ properties : {
48
+ objectLiterals : {
49
+ enum : SPACING_VALUES
50
+ }
51
+ }
48
52
}
49
53
}
54
+ } ,
55
+ basicConfigOrBoolean : {
56
+ oneOf : [ {
57
+ $ref : '#/definitions/basicConfig'
58
+ } , {
59
+ type : 'boolean'
60
+ } ]
50
61
}
51
- }
62
+ } ,
63
+
64
+ allOf : [ {
65
+ $ref : '#/definitions/basicConfig'
66
+ } , {
67
+ type : 'object' ,
68
+ properties : {
69
+ attributes : {
70
+ $ref : '#/definitions/basicConfigOrBoolean'
71
+ }
72
+ }
73
+ } ]
52
74
} ]
53
75
} ,
54
76
55
77
create : function ( context ) {
56
78
79
+ function normalizeConfig ( configOrTrue , defaults , lastPass ) {
80
+ var config = configOrTrue === true ? { } : configOrTrue ;
81
+ var spaces = config . spaces || defaults . spaces ;
82
+ var allowMultiline = has ( config , 'allowMultiline' ) ? config . allowMultiline : defaults . allowMultiline ;
83
+ var spacing = config . spacing || { } ;
84
+ var objectLiteralSpaces = spacing . objectLiterals || defaults . objectLiteralSpaces ;
85
+ if ( lastPass ) {
86
+ // On the final pass assign the values that should be derived from others if they are still undefined
87
+ objectLiteralSpaces = objectLiteralSpaces || spaces ;
88
+ }
89
+
90
+ return {
91
+ spaces,
92
+ allowMultiline,
93
+ objectLiteralSpaces
94
+ } ;
95
+ }
96
+
57
97
var DEFAULT_SPACING = SPACING . never ;
58
98
var DEFAULT_ALLOW_MULTILINE = true ;
99
+ var DEFAULT_ATTRIBUTES = true ;
59
100
60
101
var sourceCode = context . getSourceCode ( ) ;
61
- var config = context . options [ 0 ] || { } ;
62
- var baseSpacing = config . spaces || DEFAULT_SPACING ;
63
- var multiline = has ( config , 'allowMultiline' ) ? config . allowMultiline : DEFAULT_ALLOW_MULTILINE ;
64
- var spacingConfig = config . spacing || { } ;
65
- var objectLiteralSpacing = spacingConfig . objectLiterals || baseSpacing ;
102
+ var originalConfig = context . options [ 0 ] || { } ;
103
+ var defaultConfig = normalizeConfig ( originalConfig , {
104
+ spaces : DEFAULT_SPACING ,
105
+ allowMultiline : DEFAULT_ALLOW_MULTILINE
106
+ } ) ;
107
+ var attributes = has ( originalConfig , 'attributes' ) ? originalConfig . attributes : DEFAULT_ATTRIBUTES ;
108
+ var attributesConfig = attributes ? normalizeConfig ( attributes , defaultConfig , true ) : null ;
66
109
67
110
// --------------------------------------------------------------------------
68
111
// Helpers
@@ -200,6 +243,7 @@ module.exports = {
200
243
if ( node . parent . type === 'JSXElement' ) {
201
244
return ;
202
245
}
246
+ var config = attributesConfig ;
203
247
var first = context . getFirstToken ( node ) ;
204
248
var last = sourceCode . getLastToken ( node ) ;
205
249
var second = context . getTokenAfter ( first , { includeComments : true } ) ;
@@ -217,28 +261,28 @@ module.exports = {
217
261
}
218
262
219
263
var isObjectLiteral = first . value === second . value ;
220
- var spacing = isObjectLiteral ? objectLiteralSpacing : baseSpacing ;
264
+ var spacing = isObjectLiteral ? config . objectLiteralSpaces : config . spaces ;
221
265
if ( spacing === SPACING . always ) {
222
266
if ( ! sourceCode . isSpaceBetweenTokens ( first , second ) ) {
223
267
reportRequiredBeginningSpace ( node , first ) ;
224
- } else if ( ! multiline && isMultiline ( first , second ) ) {
268
+ } else if ( ! config . allowMultiline && isMultiline ( first , second ) ) {
225
269
reportNoBeginningNewline ( node , first , spacing ) ;
226
270
}
227
271
if ( ! sourceCode . isSpaceBetweenTokens ( penultimate , last ) ) {
228
272
reportRequiredEndingSpace ( node , last ) ;
229
- } else if ( ! multiline && isMultiline ( penultimate , last ) ) {
273
+ } else if ( ! config . allowMultiline && isMultiline ( penultimate , last ) ) {
230
274
reportNoEndingNewline ( node , last , spacing ) ;
231
275
}
232
276
} else if ( spacing === SPACING . never ) {
233
277
if ( isMultiline ( first , second ) ) {
234
- if ( ! multiline ) {
278
+ if ( ! config . allowMultiline ) {
235
279
reportNoBeginningNewline ( node , first , spacing ) ;
236
280
}
237
281
} else if ( sourceCode . isSpaceBetweenTokens ( first , second ) ) {
238
282
reportNoBeginningSpace ( node , first ) ;
239
283
}
240
284
if ( isMultiline ( penultimate , last ) ) {
241
- if ( ! multiline ) {
285
+ if ( ! config . allowMultiline ) {
242
286
reportNoEndingNewline ( node , last , spacing ) ;
243
287
}
244
288
} else if ( sourceCode . isSpaceBetweenTokens ( penultimate , last ) ) {
0 commit comments