Skip to content

Commit

Permalink
Change schema
Browse files Browse the repository at this point in the history
  • Loading branch information
golopot committed May 7, 2019
1 parent d9597fe commit b7a60bd
Show file tree
Hide file tree
Showing 2 changed files with 191 additions and 140 deletions.
83 changes: 51 additions & 32 deletions lib/rules/jsx-curly-newline.js
Expand Up @@ -8,6 +8,29 @@ const docsUrl = require('../util/docsUrl');
// Rule Definition
// ------------------------------------------------------------------------------

function getNormalizedOption(context) {
const rawOption = context.options[0] || 'consistent';

if (rawOption === 'consistent') {
return {
multiline: 'consistent',
singleline: 'consistent'
};
}

if (rawOption === 'never') {
return {
multiline: 'forbid',
singleline: 'forbid'
};
}

return {
multiline: rawOption.multiline || 'consistent',
singleline: rawOption.singleline || 'consistent'
};
}

module.exports = {
meta: {
type: 'layout',
Expand All @@ -23,10 +46,23 @@ module.exports = {

schema: [
{
enum: ['consistent', 'multiline', 'multiline-lax']
oneOf: [
{
enum: ['consistent', 'never']
},
{
type: 'object',
properties: {
singleline: {enum: ['consistent', 'require', 'forbid']},
multiline: {enum: ['consistent', 'require', 'forbid']}
},
additionalProperties: false
}
]
}
],


messages: {
expectedBefore: 'Expected newline before \'}\'.',
expectedAfter: 'Expected newline after \'{\'.',
Expand All @@ -37,15 +73,12 @@ module.exports = {

create(context) {
const sourceCode = context.getSourceCode();
const rawOption = context.options[0] || 'multiline-lax';
const multilineOption = rawOption === 'multiline';
const multilineLaxOption = rawOption === 'multiline-lax';
const option = getNormalizedOption(context);

// ----------------------------------------------------------------------
// Helpers
// ----------------------------------------------------------------------


/**
* Determines whether two adjacent tokens are on the same line.
* @param {Object} left - The left token object.
Expand All @@ -58,21 +91,19 @@ module.exports = {

/**
* Determines whether there should be newlines inside curlys
* @param {ASTNode[]} expression The arguments or parameters in the list
* @param {ASTNode} expression The expression contained in the curlys
* @param {boolean} hasLeftNewline `true` if the left curly has a newline in the current code.
* @returns {boolean} `true` if there should be newlines inside the function curlys
*/
function shouldHaveNewlines(expression, hasLeftNewline) {
const expressionIsMultiline = expression.loc.start.line !== expression.loc.end.line;
const isMultiline = expression.loc.start.line !== expression.loc.end.line;

if (multilineLaxOption && !expressionIsMultiline) {
return hasLeftNewline;
switch (isMultiline ? option.multiline : option.singleline) {
case 'forbid': return false;
case 'require': return true;
case 'consistent':
default: return hasLeftNewline;
}
if (multilineOption || multilineLaxOption) {
return expressionIsMultiline;
}

return hasLeftNewline;
}

/**
Expand All @@ -86,14 +117,8 @@ module.exports = {
const rightCurly = curlys.rightCurly;
const tokenAfterLeftCurly = sourceCode.getTokenAfter(leftCurly);
const tokenBeforeRightCurly = sourceCode.getTokenBefore(rightCurly);
const hasLeftNewline = !isTokenOnSameLine(
leftCurly,
tokenAfterLeftCurly
);
const hasRightNewline = !isTokenOnSameLine(
tokenBeforeRightCurly,
rightCurly
);
const hasLeftNewline = !isTokenOnSameLine(leftCurly, tokenAfterLeftCurly);
const hasRightNewline = !isTokenOnSameLine(tokenBeforeRightCurly, rightCurly);
const needsNewlines = shouldHaveNewlines(expression, hasLeftNewline);

if (hasLeftNewline && !needsNewlines) {
Expand All @@ -106,11 +131,8 @@ module.exports = {
.slice(leftCurly.range[1], tokenAfterLeftCurly.range[0])
.trim()
? // If there is a comment between the { and the first element, don't do a fix.
null
: fixer.removeRange([
leftCurly.range[1],
tokenAfterLeftCurly.range[0]
]);
null
: fixer.removeRange([leftCurly.range[1], tokenAfterLeftCurly.range[0]]);
}
});
} else if (!hasLeftNewline && needsNewlines) {
Expand All @@ -131,7 +153,7 @@ module.exports = {
.slice(tokenBeforeRightCurly.range[1], rightCurly.range[0])
.trim()
? // If there is a comment between the last element and the }, don't do a fix.
null
null
: fixer.removeRange([
tokenBeforeRightCurly.range[1],
rightCurly.range[0]
Expand Down Expand Up @@ -165,10 +187,7 @@ module.exports = {
* @returns {void}
*/
function validateNode(node) {
validateCurlys(
getCurlyTokens(node),
node.expression
);
validateCurlys(getCurlyTokens(node), node.expression);
}

// ----------------------------------------------------------------------
Expand Down

0 comments on commit b7a60bd

Please sign in to comment.