Skip to content

Commit

Permalink
Allow attributes to have their own config
Browse files Browse the repository at this point in the history
This change does not make sense by itself, but it will with the added
children config.
  • Loading branch information
fatfisz committed Jun 11, 2017
1 parent 9a8f800 commit c9e9ded
Show file tree
Hide file tree
Showing 2 changed files with 289 additions and 83 deletions.
86 changes: 65 additions & 21 deletions lib/rules/jsx-curly-spacing.js
Expand Up @@ -32,37 +32,80 @@ module.exports = {
fixable: 'code',

schema: [{
type: 'object',
properties: {
spaces: {
enum: SPACING_VALUES
},
allowMultiline: {
type: 'boolean'
},
spacing: {
definitions: {
basicConfig: {
type: 'object',
properties: {
objectLiterals: {
spaces: {
enum: SPACING_VALUES
},
allowMultiline: {
type: 'boolean'
},
spacing: {
type: 'object',
properties: {
objectLiterals: {
enum: SPACING_VALUES
}
}
}
}
},
basicConfigOrBoolean: {
oneOf: [{
$ref: '#/definitions/basicConfig'
}, {
type: 'boolean'
}]
}
}
},

allOf: [{
$ref: '#/definitions/basicConfig'
}, {
type: 'object',
properties: {
attributes: {
$ref: '#/definitions/basicConfigOrBoolean'
}
}
}]
}]
},

create: function(context) {

function normalizeConfig(configOrTrue, defaults, lastPass) {
var config = configOrTrue === true ? {} : configOrTrue;
var spaces = config.spaces || defaults.spaces;
var allowMultiline = has(config, 'allowMultiline') ? config.allowMultiline : defaults.allowMultiline;
var spacing = config.spacing || {};
var objectLiteralSpaces = spacing.objectLiterals || defaults.objectLiteralSpaces;
if (lastPass) {
// On the final pass assign the values that should be derived from others if they are still undefined
objectLiteralSpaces = objectLiteralSpaces || spaces;
}

return {
spaces,
allowMultiline,
objectLiteralSpaces
};
}

var DEFAULT_SPACING = SPACING.never;
var DEFAULT_ALLOW_MULTILINE = true;
var DEFAULT_ATTRIBUTES = true;

var sourceCode = context.getSourceCode();
var config = context.options[0] || {};
var baseSpacing = config.spaces || DEFAULT_SPACING;
var multiline = has(config, 'allowMultiline') ? config.allowMultiline : DEFAULT_ALLOW_MULTILINE;
var spacingConfig = config.spacing || {};
var objectLiteralSpacing = spacingConfig.objectLiterals || baseSpacing;
var originalConfig = context.options[0] || {};
var defaultConfig = normalizeConfig(originalConfig, {
spaces: DEFAULT_SPACING,
allowMultiline: DEFAULT_ALLOW_MULTILINE
});
var attributes = has(originalConfig, 'attributes') ? originalConfig.attributes : DEFAULT_ATTRIBUTES;
var attributesConfig = attributes ? normalizeConfig(attributes, defaultConfig, true) : null;

// --------------------------------------------------------------------------
// Helpers
Expand Down Expand Up @@ -200,6 +243,7 @@ module.exports = {
if (node.parent.type === 'JSXElement') {
return;
}
var config = attributesConfig;
var first = context.getFirstToken(node);
var last = sourceCode.getLastToken(node);
var second = context.getTokenAfter(first, {includeComments: true});
Expand All @@ -217,28 +261,28 @@ module.exports = {
}

var isObjectLiteral = first.value === second.value;
var spacing = isObjectLiteral ? objectLiteralSpacing : baseSpacing;
var spacing = isObjectLiteral ? config.objectLiteralSpaces : config.spaces;
if (spacing === SPACING.always) {
if (!sourceCode.isSpaceBetweenTokens(first, second)) {
reportRequiredBeginningSpace(node, first);
} else if (!multiline && isMultiline(first, second)) {
} else if (!config.allowMultiline && isMultiline(first, second)) {
reportNoBeginningNewline(node, first, spacing);
}
if (!sourceCode.isSpaceBetweenTokens(penultimate, last)) {
reportRequiredEndingSpace(node, last);
} else if (!multiline && isMultiline(penultimate, last)) {
} else if (!config.allowMultiline && isMultiline(penultimate, last)) {
reportNoEndingNewline(node, last, spacing);
}
} else if (spacing === SPACING.never) {
if (isMultiline(first, second)) {
if (!multiline) {
if (!config.allowMultiline) {
reportNoBeginningNewline(node, first, spacing);
}
} else if (sourceCode.isSpaceBetweenTokens(first, second)) {
reportNoBeginningSpace(node, first);
}
if (isMultiline(penultimate, last)) {
if (!multiline) {
if (!config.allowMultiline) {
reportNoEndingNewline(node, last, spacing);
}
} else if (sourceCode.isSpaceBetweenTokens(penultimate, last)) {
Expand Down

0 comments on commit c9e9ded

Please sign in to comment.