eslint-plugin-code-style 1.0.37 → 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/README.md CHANGED
@@ -186,7 +186,7 @@ rules: {
186
186
  "code-style/curried-arrow-same-line": "error",
187
187
  "code-style/assignment-value-same-line": "error",
188
188
  "code-style/block-statement-newlines": "error",
189
- "code-style/comment-spacing": "error",
189
+ "code-style/comment-format": "error",
190
190
  "code-style/function-call-spacing": "error",
191
191
  "code-style/function-naming-convention": "error",
192
192
  "code-style/function-params-per-line": "error",
@@ -248,7 +248,7 @@ rules: {
248
248
  | `curried-arrow-same-line` | Curried arrow functions start on same line as `=>`, not on new line |
249
249
  | `assignment-value-same-line` | Assignment values start on same line as `=`, not on new line |
250
250
  | `block-statement-newlines` | Newline after `{` and before `}` in if/for/while/function blocks |
251
- | `comment-spacing` | Space after `//`, space inside `/* */`, convert single-line blocks to `//`, no blank lines between file-top comments |
251
+ | `comment-format` | Space after `//`, space inside `/* */`, convert single-line blocks to `//`, no blank lines between file-top comments |
252
252
  | `function-call-spacing` | No space between function name and `(`: `fn()` not `fn ()` |
253
253
  | `function-naming-convention` | Functions use camelCase, start with verb (get/set/handle/is/has), handlers end with Handler |
254
254
  | `function-params-per-line` | When multiline, each param on own line with consistent indentation |
@@ -554,7 +554,7 @@ for (const item of items) { process(item);
554
554
 
555
555
  ---
556
556
 
557
- ### `comment-spacing`
557
+ ### `comment-format`
558
558
 
559
559
  **What it does:** Enforces proper comment formatting:
560
560
  - Space after `//` in line comments
package/index.d.ts CHANGED
@@ -12,7 +12,7 @@ export type RuleNames =
12
12
  | "code-style/arrow-function-simplify"
13
13
  | "code-style/assignment-value-same-line"
14
14
  | "code-style/block-statement-newlines"
15
- | "code-style/comment-spacing"
15
+ | "code-style/comment-format"
16
16
  | "code-style/curried-arrow-same-line"
17
17
  | "code-style/export-format"
18
18
  | "code-style/function-arguments-format"
@@ -81,7 +81,7 @@ interface PluginRules {
81
81
  "arrow-function-simplify": Rule.RuleModule;
82
82
  "assignment-value-same-line": Rule.RuleModule;
83
83
  "block-statement-newlines": Rule.RuleModule;
84
- "comment-spacing": Rule.RuleModule;
84
+ "comment-format": Rule.RuleModule;
85
85
  "curried-arrow-same-line": Rule.RuleModule;
86
86
  "export-format": Rule.RuleModule;
87
87
  "function-arguments-format": Rule.RuleModule;
package/index.js CHANGED
@@ -1138,7 +1138,7 @@ const blockStatementNewlines = {
1138
1138
  * //This is a comment (missing space)
1139
1139
  * [block]No space after opener[/block]
1140
1140
  */
1141
- const commentSpacing = {
1141
+ const commentFormat = {
1142
1142
  create(context) {
1143
1143
  const sourceCode = context.sourceCode || context.getSourceCode();
1144
1144
 
@@ -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
- if (!parent || parent.type !== "JSXExpressionContainer") return;
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 0: Condition should be on same line as opening {
4736
- const openBrace = sourceCode.getFirstToken(parent);
4769
+ // Check: Condition should not be broken across multiple lines
4770
+ if (hasConditionBrokenAcrossLinesHandler(node.test)) {
4771
+ const collapsedCondition = getCollapsedConditionTextHandler(node.test);
4737
4772
 
4738
- if (openBrace && openBrace.value === "{") {
4739
- if (openBrace.loc.end.line !== node.test.loc.start.line) {
4740
- context.report({
4741
- fix: (fixer) => fixer.replaceTextRange(
4742
- [openBrace.range[1], node.test.range[0]],
4743
- "",
4744
- ),
4745
- message: "Ternary condition should be on same line as opening '{'",
4746
- node: node.test,
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(
@@ -8410,7 +8481,7 @@ export default {
8410
8481
  "block-statement-newlines": blockStatementNewlines,
8411
8482
 
8412
8483
  // Comment rules
8413
- "comment-spacing": commentSpacing,
8484
+ "comment-format": commentFormat,
8414
8485
 
8415
8486
  // Function rules
8416
8487
  "function-call-spacing": functionCallSpacing,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "eslint-plugin-code-style",
3
- "version": "1.0.37",
3
+ "version": "1.0.40",
4
4
  "description": "A custom ESLint plugin for enforcing consistent code formatting and style rules in React/JSX projects",
5
5
  "main": "index.js",
6
6
  "types": "index.d.ts",