eslint-plugin-nextfriday 1.22.0 → 1.23.0

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/CHANGELOG.md CHANGED
@@ -1,5 +1,11 @@
1
1
  # eslint-plugin-nextfriday
2
2
 
3
+ ## 1.23.0
4
+
5
+ ### Minor Changes
6
+
7
+ - [#94](https://github.com/next-friday/eslint-plugin-nextfriday/pull/94) [`7eaf83e`](https://github.com/next-friday/eslint-plugin-nextfriday/commit/7eaf83e365bb0867679f9abe764ade784eeb4594) Thanks [@joetakara](https://github.com/joetakara)! - Add auto-fix support to jsx-sort-props rule. Running eslint --fix will automatically reorder JSX props by value type.
8
+
3
9
  ## 1.22.0
4
10
 
5
11
  ### Minor Changes
package/README.md CHANGED
@@ -231,7 +231,7 @@ export default [
231
231
  | [jsx-no-variable-in-callback](docs/rules/JSX_NO_VARIABLE_IN_CALLBACK.md) | Disallow variable declarations inside callback functions in JSX | ❌ |
232
232
  | [jsx-require-suspense](docs/rules/JSX_REQUIRE_SUSPENSE.md) | Require lazy-loaded components to be wrapped in Suspense | ❌ |
233
233
  | [jsx-simple-props](docs/rules/JSX_SIMPLE_PROPS.md) | Enforce simple prop values (strings, variables, callbacks, ReactNode) | ❌ |
234
- | [jsx-sort-props](docs/rules/JSX_SORT_PROPS.md) | Enforce JSX props are sorted by value type | |
234
+ | [jsx-sort-props](docs/rules/JSX_SORT_PROPS.md) | Enforce JSX props are sorted by value type | |
235
235
  | [prefer-jsx-template-literals](docs/rules/PREFER_JSX_TEMPLATE_LITERALS.md) | Enforce template literals instead of mixing text and JSX expressions | ✅ |
236
236
  | [react-props-destructure](docs/rules/REACT_PROPS_DESTRUCTURE.md) | Enforce destructuring props inside React component body | ❌ |
237
237
  | [enforce-props-suffix](docs/rules/ENFORCE_PROPS_SUFFIX.md) | Enforce 'Props' suffix for interfaces and types in \*.tsx files | ❌ |
@@ -2,6 +2,8 @@
2
2
 
3
3
  Enforce JSX props are sorted by value type.
4
4
 
5
+ > This rule is auto-fixable using `--fix`.
6
+
5
7
  ## Rule Details
6
8
 
7
9
  This rule enforces a consistent ordering of JSX props based on the type of their value. Props must appear in the following order:
package/lib/index.cjs CHANGED
@@ -42,7 +42,7 @@ var import_eslint_plugin_unicorn = __toESM(require("eslint-plugin-unicorn"), 1);
42
42
  // package.json
43
43
  var package_default = {
44
44
  name: "eslint-plugin-nextfriday",
45
- version: "1.22.0",
45
+ version: "1.23.0",
46
46
  description: "A comprehensive ESLint plugin providing custom rules and configurations for Next Friday development workflows.",
47
47
  keywords: [
48
48
  "eslint",
@@ -1814,6 +1814,29 @@ function hasUnsortedProps(attributes) {
1814
1814
  return false;
1815
1815
  });
1816
1816
  }
1817
+ function sortSegment(segment, sourceCode, fixer) {
1818
+ const sorted = [...segment].sort((a, b) => (getTypeGroup(a) ?? 0) - (getTypeGroup(b) ?? 0));
1819
+ const sortedTexts = sorted.map((attr) => sourceCode.getText(attr));
1820
+ return segment.map((attr, i) => ({ attr, sortedText: sortedTexts[i] })).filter(({ attr, sortedText }) => sourceCode.getText(attr) !== sortedText).map(({ attr, sortedText }) => fixer.replaceText(attr, sortedText));
1821
+ }
1822
+ function getSegments(attributes) {
1823
+ const result = [];
1824
+ let current = [];
1825
+ attributes.forEach((attr) => {
1826
+ if (attr.type === import_utils24.AST_NODE_TYPES.JSXSpreadAttribute) {
1827
+ if (current.length > 0) {
1828
+ result.push(current);
1829
+ current = [];
1830
+ }
1831
+ } else {
1832
+ current.push(attr);
1833
+ }
1834
+ });
1835
+ if (current.length > 0) {
1836
+ result.push(current);
1837
+ }
1838
+ return result;
1839
+ }
1817
1840
  var jsxSortProps = createRule22({
1818
1841
  name: "jsx-sort-props",
1819
1842
  meta: {
@@ -1821,6 +1844,7 @@ var jsxSortProps = createRule22({
1821
1844
  docs: {
1822
1845
  description: "Enforce JSX props are sorted by value type: strings, hyphenated strings, numbers/booleans, expressions, objects/arrays, functions, JSX elements, then shorthand booleans"
1823
1846
  },
1847
+ fixable: "code",
1824
1848
  messages: {
1825
1849
  unsortedProps: "JSX props should be sorted by value type: strings, hyphenated strings, numbers/booleans/null, expressions, objects/arrays, functions, JSX elements, then shorthand booleans."
1826
1850
  },
@@ -1828,14 +1852,19 @@ var jsxSortProps = createRule22({
1828
1852
  },
1829
1853
  defaultOptions: [],
1830
1854
  create(context) {
1855
+ const { sourceCode } = context;
1831
1856
  return {
1832
1857
  JSXOpeningElement(node) {
1833
- if (hasUnsortedProps(node.attributes)) {
1834
- context.report({
1835
- node,
1836
- messageId: "unsortedProps"
1837
- });
1858
+ if (!hasUnsortedProps(node.attributes)) {
1859
+ return;
1838
1860
  }
1861
+ context.report({
1862
+ node,
1863
+ messageId: "unsortedProps",
1864
+ fix(fixer) {
1865
+ return getSegments(node.attributes).flatMap((segment) => sortSegment(segment, sourceCode, fixer));
1866
+ }
1867
+ });
1839
1868
  }
1840
1869
  };
1841
1870
  }