eslint-plugin-code-style 1.0.39 → 1.0.40
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/index.js +88 -17
- package/package.json +1 -1
package/index.js
CHANGED
|
@@ -4708,12 +4708,46 @@ const jsxTernaryFormat = {
|
|
|
4708
4708
|
return tokenBefore && tokenAfter && tokenBefore.value === "(" && tokenAfter.value === ")";
|
|
4709
4709
|
};
|
|
4710
4710
|
|
|
4711
|
+
// Check if condition has broken operators (spread across lines)
|
|
4712
|
+
const hasConditionBrokenAcrossLinesHandler = (testNode) => {
|
|
4713
|
+
// Check binary expressions (===, !==, etc.)
|
|
4714
|
+
if (testNode.type === "BinaryExpression") {
|
|
4715
|
+
const leftEnd = testNode.left.loc.end.line;
|
|
4716
|
+
const rightStart = testNode.right.loc.start.line;
|
|
4717
|
+
|
|
4718
|
+
if (leftEnd !== rightStart) return true;
|
|
4719
|
+
|
|
4720
|
+
// Recursively check nested expressions
|
|
4721
|
+
return hasConditionBrokenAcrossLinesHandler(testNode.left)
|
|
4722
|
+
|| hasConditionBrokenAcrossLinesHandler(testNode.right);
|
|
4723
|
+
}
|
|
4724
|
+
|
|
4725
|
+
// Check logical expressions (&&, ||)
|
|
4726
|
+
if (testNode.type === "LogicalExpression") {
|
|
4727
|
+
const leftEnd = testNode.left.loc.end.line;
|
|
4728
|
+
const rightStart = testNode.right.loc.start.line;
|
|
4729
|
+
|
|
4730
|
+
if (leftEnd !== rightStart) return true;
|
|
4731
|
+
|
|
4732
|
+
return hasConditionBrokenAcrossLinesHandler(testNode.left)
|
|
4733
|
+
|| hasConditionBrokenAcrossLinesHandler(testNode.right);
|
|
4734
|
+
}
|
|
4735
|
+
|
|
4736
|
+
return false;
|
|
4737
|
+
};
|
|
4738
|
+
|
|
4739
|
+
// Get the full condition text collapsed to single line
|
|
4740
|
+
const getCollapsedConditionTextHandler = (testNode) => sourceCode.getText(testNode).replace(/\s*\n\s*/g, " ").trim();
|
|
4741
|
+
|
|
4711
4742
|
return {
|
|
4712
4743
|
ConditionalExpression(node) {
|
|
4713
|
-
// Only handle ternaries in JSX context
|
|
4714
4744
|
const parent = node.parent;
|
|
4715
4745
|
|
|
4716
|
-
|
|
4746
|
+
// Handle ternaries in JSX context or return statements
|
|
4747
|
+
const isJsxContext = parent && parent.type === "JSXExpressionContainer";
|
|
4748
|
+
const isReturnContext = parent && parent.type === "ReturnStatement";
|
|
4749
|
+
|
|
4750
|
+
if (!isJsxContext && !isReturnContext) return;
|
|
4717
4751
|
|
|
4718
4752
|
const {
|
|
4719
4753
|
alternate,
|
|
@@ -4732,19 +4766,32 @@ const jsxTernaryFormat = {
|
|
|
4732
4766
|
const baseIndent = getIndentHandler(node);
|
|
4733
4767
|
const contentIndent = baseIndent + " ";
|
|
4734
4768
|
|
|
4735
|
-
// Check
|
|
4736
|
-
|
|
4769
|
+
// Check: Condition should not be broken across multiple lines
|
|
4770
|
+
if (hasConditionBrokenAcrossLinesHandler(node.test)) {
|
|
4771
|
+
const collapsedCondition = getCollapsedConditionTextHandler(node.test);
|
|
4737
4772
|
|
|
4738
|
-
|
|
4739
|
-
|
|
4740
|
-
|
|
4741
|
-
|
|
4742
|
-
|
|
4743
|
-
|
|
4744
|
-
|
|
4745
|
-
|
|
4746
|
-
|
|
4747
|
-
|
|
4773
|
+
context.report({
|
|
4774
|
+
fix: (fixer) => fixer.replaceText(node.test, collapsedCondition),
|
|
4775
|
+
message: "Ternary condition should not be broken across multiple lines",
|
|
4776
|
+
node: node.test,
|
|
4777
|
+
});
|
|
4778
|
+
}
|
|
4779
|
+
|
|
4780
|
+
// Check: Condition should be on same line as opening { (for JSX context)
|
|
4781
|
+
if (isJsxContext) {
|
|
4782
|
+
const openBrace = sourceCode.getFirstToken(parent);
|
|
4783
|
+
|
|
4784
|
+
if (openBrace && openBrace.value === "{") {
|
|
4785
|
+
if (openBrace.loc.end.line !== node.test.loc.start.line) {
|
|
4786
|
+
context.report({
|
|
4787
|
+
fix: (fixer) => fixer.replaceTextRange(
|
|
4788
|
+
[openBrace.range[1], node.test.range[0]],
|
|
4789
|
+
"",
|
|
4790
|
+
),
|
|
4791
|
+
message: "Ternary condition should be on same line as opening '{'",
|
|
4792
|
+
node: node.test,
|
|
4793
|
+
});
|
|
4794
|
+
}
|
|
4748
4795
|
}
|
|
4749
4796
|
}
|
|
4750
4797
|
|
|
@@ -4764,8 +4811,8 @@ const jsxTernaryFormat = {
|
|
|
4764
4811
|
|
|
4765
4812
|
if (!colonToken) return;
|
|
4766
4813
|
|
|
4767
|
-
// Get closing brace of JSXExpressionContainer
|
|
4768
|
-
const closeBrace = sourceCode.getLastToken(parent);
|
|
4814
|
+
// Get closing brace of JSXExpressionContainer (null for return statements)
|
|
4815
|
+
const closeBrace = isJsxContext ? sourceCode.getLastToken(parent) : null;
|
|
4769
4816
|
|
|
4770
4817
|
// Check for unnecessary parentheses around simple JSX
|
|
4771
4818
|
if (consequentSimple && hasUnnecessaryParensHandler(consequent)) {
|
|
@@ -5059,6 +5106,30 @@ const jsxTernaryFormat = {
|
|
|
5059
5106
|
}
|
|
5060
5107
|
}
|
|
5061
5108
|
|
|
5109
|
+
// Check if simple alternate is wrapped in unnecessary parentheses
|
|
5110
|
+
const tokenAfterColon = sourceCode.getTokenAfter(colonToken);
|
|
5111
|
+
|
|
5112
|
+
if (tokenAfterColon && tokenAfterColon.value === "(") {
|
|
5113
|
+
// Find the matching closing paren
|
|
5114
|
+
const closingParen = sourceCode.getTokenAfter(alternate);
|
|
5115
|
+
|
|
5116
|
+
if (closingParen && closingParen.value === ")") {
|
|
5117
|
+
// Remove parentheses and put simple alternate on same line as :
|
|
5118
|
+
const alternateText = sourceCode.getText(alternate);
|
|
5119
|
+
|
|
5120
|
+
context.report({
|
|
5121
|
+
fix: (fixer) => fixer.replaceTextRange(
|
|
5122
|
+
[colonToken.range[1], closingParen.range[1]],
|
|
5123
|
+
` ${alternateText}`,
|
|
5124
|
+
),
|
|
5125
|
+
message: "Simple JSX should not be wrapped in parentheses; put on same line as ':'",
|
|
5126
|
+
node: alternate,
|
|
5127
|
+
});
|
|
5128
|
+
|
|
5129
|
+
return;
|
|
5130
|
+
}
|
|
5131
|
+
}
|
|
5132
|
+
|
|
5062
5133
|
// Simple alternate should be on same line as :
|
|
5063
5134
|
if (colonToken.loc.end.line !== alternate.loc.start.line) {
|
|
5064
5135
|
context.report({
|
|
@@ -5071,7 +5142,7 @@ const jsxTernaryFormat = {
|
|
|
5071
5142
|
});
|
|
5072
5143
|
}
|
|
5073
5144
|
|
|
5074
|
-
// } should be on same line as simple alternate
|
|
5145
|
+
// } should be on same line as simple alternate (only for JSX context)
|
|
5075
5146
|
if (closeBrace && closeBrace.value === "}" && alternate.loc.end.line !== closeBrace.loc.start.line) {
|
|
5076
5147
|
context.report({
|
|
5077
5148
|
fix: (fixer) => fixer.replaceTextRange(
|
package/package.json
CHANGED