Skip to content

Commit c9e9ded

Browse files
committed
Allow attributes to have their own config
This change does not make sense by itself, but it will with the added children config.
1 parent 9a8f800 commit c9e9ded

File tree

2 files changed

+289
-83
lines changed

2 files changed

+289
-83
lines changed

lib/rules/jsx-curly-spacing.js

Lines changed: 65 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -32,37 +32,80 @@ module.exports = {
3232
fixable: 'code',
3333

3434
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: {
4437
type: 'object',
4538
properties: {
46-
objectLiterals: {
39+
spaces: {
4740
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+
}
4852
}
4953
}
54+
},
55+
basicConfigOrBoolean: {
56+
oneOf: [{
57+
$ref: '#/definitions/basicConfig'
58+
}, {
59+
type: 'boolean'
60+
}]
5061
}
51-
}
62+
},
63+
64+
allOf: [{
65+
$ref: '#/definitions/basicConfig'
66+
}, {
67+
type: 'object',
68+
properties: {
69+
attributes: {
70+
$ref: '#/definitions/basicConfigOrBoolean'
71+
}
72+
}
73+
}]
5274
}]
5375
},
5476

5577
create: function(context) {
5678

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+
5797
var DEFAULT_SPACING = SPACING.never;
5898
var DEFAULT_ALLOW_MULTILINE = true;
99+
var DEFAULT_ATTRIBUTES = true;
59100

60101
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;
66109

67110
// --------------------------------------------------------------------------
68111
// Helpers
@@ -200,6 +243,7 @@ module.exports = {
200243
if (node.parent.type === 'JSXElement') {
201244
return;
202245
}
246+
var config = attributesConfig;
203247
var first = context.getFirstToken(node);
204248
var last = sourceCode.getLastToken(node);
205249
var second = context.getTokenAfter(first, {includeComments: true});
@@ -217,28 +261,28 @@ module.exports = {
217261
}
218262

219263
var isObjectLiteral = first.value === second.value;
220-
var spacing = isObjectLiteral ? objectLiteralSpacing : baseSpacing;
264+
var spacing = isObjectLiteral ? config.objectLiteralSpaces : config.spaces;
221265
if (spacing === SPACING.always) {
222266
if (!sourceCode.isSpaceBetweenTokens(first, second)) {
223267
reportRequiredBeginningSpace(node, first);
224-
} else if (!multiline && isMultiline(first, second)) {
268+
} else if (!config.allowMultiline && isMultiline(first, second)) {
225269
reportNoBeginningNewline(node, first, spacing);
226270
}
227271
if (!sourceCode.isSpaceBetweenTokens(penultimate, last)) {
228272
reportRequiredEndingSpace(node, last);
229-
} else if (!multiline && isMultiline(penultimate, last)) {
273+
} else if (!config.allowMultiline && isMultiline(penultimate, last)) {
230274
reportNoEndingNewline(node, last, spacing);
231275
}
232276
} else if (spacing === SPACING.never) {
233277
if (isMultiline(first, second)) {
234-
if (!multiline) {
278+
if (!config.allowMultiline) {
235279
reportNoBeginningNewline(node, first, spacing);
236280
}
237281
} else if (sourceCode.isSpaceBetweenTokens(first, second)) {
238282
reportNoBeginningSpace(node, first);
239283
}
240284
if (isMultiline(penultimate, last)) {
241-
if (!multiline) {
285+
if (!config.allowMultiline) {
242286
reportNoEndingNewline(node, last, spacing);
243287
}
244288
} else if (sourceCode.isSpaceBetweenTokens(penultimate, last)) {

0 commit comments

Comments
 (0)