最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How do you format React Component props alphabetically on save? - Stack Overflow

programmeradmin1浏览0评论

How does one format props and typescript definitions alphabetically on save? Ideally it is some configuration for the whole project as opposed to a VSCode plugin (though I'm open a plugin too at this point).

// the items inside ButtonProps should be sorted alphabetically
type ButtonProps = {
    name: string;
    id: string;
    onPress: () => void;
    disabled: boolean;
}

const Button = (props: ButtonProps) => {
    // this should also be sorted alphabetically
    const {name, id, onPress, disabled} = props;

    return <>My Prop</>
}

I tried react/sort-prop-types and sort-keys but I really don't know what I'm doing with eslint. It still doesn't format alphabetically on save. My eslint config is below:

// .eslintrc.json
{
  // ...
  "rules": {
    "react/sort-prop-types": [
      2,
        {
            "callbacksLast": true,
            "shorthandFirst": true,
            "ignoreCase": false,
            "noSortAlphabetically": false,
            "reservedFirst": true
        }
    ],
    "sort-keys": ["error", "asc", {"caseSensitive": true, "natural": false, "minKeys": 2}], 
  }

How does one format props and typescript definitions alphabetically on save? Ideally it is some configuration for the whole project as opposed to a VSCode plugin (though I'm open a plugin too at this point).

// the items inside ButtonProps should be sorted alphabetically
type ButtonProps = {
    name: string;
    id: string;
    onPress: () => void;
    disabled: boolean;
}

const Button = (props: ButtonProps) => {
    // this should also be sorted alphabetically
    const {name, id, onPress, disabled} = props;

    return <>My Prop</>
}

I tried react/sort-prop-types and sort-keys but I really don't know what I'm doing with eslint. It still doesn't format alphabetically on save. My eslint config is below:

// .eslintrc.json
{
  // ...
  "rules": {
    "react/sort-prop-types": [
      2,
        {
            "callbacksLast": true,
            "shorthandFirst": true,
            "ignoreCase": false,
            "noSortAlphabetically": false,
            "reservedFirst": true
        }
    ],
    "sort-keys": ["error", "asc", {"caseSensitive": true, "natural": false, "minKeys": 2}], 
  }
Share Improve this question asked Sep 8, 2021 at 0:20 wongxwongx 12.3k11 gold badges45 silver badges76 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 6

You can use a VSCode extension called Sort lines. It doesn't do it on save though but all you have to do is highlight your props, press cmd + shift + p for Mac or ctrl + shift + p for Windows to open the Command Palette and select Sort lines.

I tried eslint-plugin-sort-keys-fix in vscode with proper settings mentioned in https://www.npmjs./package/eslint-plugin-sort-keys-fix

I did not use sort-keys but just eslint-plugin-sort-keys-fix

发布评论

评论列表(0)

  1. 暂无评论