Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/reactjs/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Reactjs 当eslint指控max length时,Prettier不适用。prettierignore不起作用_Reactjs_Eslint_Prettier_Typescript Eslint - Fatal编程技术网

Reactjs 当eslint指控max length时,Prettier不适用。prettierignore不起作用

Reactjs 当eslint指控max length时,Prettier不适用。prettierignore不起作用,reactjs,eslint,prettier,typescript-eslint,Reactjs,Eslint,Prettier,Typescript Eslint,案例1(PrintWidth): 这种情况下会发生以下情况:我在.prettierrc文件中、在vscodesettigns.json和.eslintrc.js中配置了printWidth。但是,当达到最大长度(在本例中为120)时,更漂亮的不会应用其样式 案例2(.prettierignore) prettier只是忽略此文件,不应用其中包含的规则 情况1所需的分辨率: "editor.defaultFormatter": "esbenp.prettier-vsc

案例1
PrintWidth
):

这种情况下会发生以下情况:我在
.prettierrc
文件中、在vscode
settigns.json
.eslintrc.js
中配置了printWidth。但是,当达到
最大长度
(在本例中为
120
)时,
更漂亮的
不会应用其样式

案例2
.prettierignore

prettier
只是忽略此文件,不应用其中包含的规则

情况1所需的分辨率:

"editor.defaultFormatter": "esbenp.prettier-vscode",
"editor.formatOnSave": true,
"editor.rulers": [120, 120]
{
    "printWidth": 120,
    "tabWidth": 2,
    "useTabs": true,
    "semi": true,
    "singleQuote": true,
    "jsxSingleQuote": true,
    "jsxBracketSameLine": false,
    "bracketSpacing": true,
    "arrowParens": "always",
    "endOfLine": "lf",
    "quoteProps": "as-needed",
    "trailingComma": "none"
}
module.exports = {
    env: {
        browser: true,
        es2021: true
    },
    extends: [
        'plugin:react/recommended',
        'plugin:react-hooks/recommended',
        'plugin:prettier/recommended',
        'plugin:import/errors',
        'plugin:import/warnings',
        'react-app',
        'react-app/jest',
        'airbnb'
    ],
    parser: '@typescript-eslint/parser',
    parserOptions: {
        ecmaFeatures: {
            jsx: true
        },
        ecmaVersion: 5,
        sourceType: 'module'
    },
    plugins: ['react', 'react-hooks', 'prefer-arrow', 'prettier', '@typescript-eslint'],
    settings: {
        react: {
            version: 'detect'
        },
        'import/resolver': {
            node: {
                paths: ['src'],
                moduleDirectory: ['node_modules', 'src/'],
                extensions: ['.js', '.jsx', '.ts', '.tsx']
            }
        }
    },
    overrides: [
        {
            files: ['**/*.js', '**/*.jsx', '**/*.ts', '**/*.tsx'],
            plugins: ['@typescript-eslint'],
            rules: {
                'no-use-before-define': 'off',
                '@typescript-eslint/no-use-before-define': ['error']
            }
        }
    ],
    ignorePatterns: ['src/assets/images/*', 'src/assets/fonts/*'],
    rules: {
        '@typescript-eslint/no-unused-vars': ['error'],
        'prettier/prettier': 'error',
        'arrow-body-style': 'off',
        'arrow-parens': ['error', 'always'],
        'object-curly-newline': 'off',
        'no-tabs': 0,
        indent: [2, 'tab', { SwitchCase: 1 }],
        'comma-dangle': ['error', 'never'],
        'max-len': ['error', { code: 120 }],
        'jsx-quotes': ['error', 'prefer-single'],
        'implicit-arrow-linebreak': 'off',
        'operator-linebreak': [
            'error',
            'after',
            {
                overrides: {
                    ':': 'before'
                }
            }
        ],
        'react-hooks/rules-of-hooks': 'error', // Checks rules of Hooks
        'react-hooks/exhaustive-deps': 'warn', // Checks effect,
        'react/jsx-indent-props': 'off',
        'react/react-in-jsx-scope': 'off',
        'react/jsx-props-no-spreading': 'off',
        'react/jsx-boolean-value': 'off',
        'react/jsx-indent': ['error', 'tab', { checkAttributes: true, indentLogicalExpressions: true }],
        'react/jsx-filename-extension': [1, { extensions: ['.js', '.jsx', '.ts', '.tsx'] }],
        'react/jsx-wrap-multilines': [
            'error',
            {
                declaration: 'parens-new-line',
                assignment: 'parens-new-line',
                return: 'parens-new-line',
                arrow: 'parens-new-line',
                condition: 'parens-new-line',
                logical: 'parens-new-line',
                prop: 'parens-new-line'
            }
        ],
        'prefer-arrow-callback': 'off',
        'prefer-arrow/prefer-arrow-functions': [
            'error',
            {
                disallowPrototype: true,
                singleReturnOnly: false,
                classPropertiesAllowed: false
            }
        ],
        'import/prefer-default-export': 'off',
        'import/extensions': [
            'error',
            'ignorePackages',
            {
                js: 'never',
                jsx: 'never',
                ts: 'never',
                tsx: 'never'
            }
        ]
    }
};
eslint
验证代码是否处于行大小的限制(在本例中为120且大于限制)时,它会“中断”代码,例如降低
JSX组件的属性

情况2所需的分辨率:

"editor.defaultFormatter": "esbenp.prettier-vscode",
"editor.formatOnSave": true,
"editor.rulers": [120, 120]
{
    "printWidth": 120,
    "tabWidth": 2,
    "useTabs": true,
    "semi": true,
    "singleQuote": true,
    "jsxSingleQuote": true,
    "jsxBracketSameLine": false,
    "bracketSpacing": true,
    "arrowParens": "always",
    "endOfLine": "lf",
    "quoteProps": "as-needed",
    "trailingComma": "none"
}
module.exports = {
    env: {
        browser: true,
        es2021: true
    },
    extends: [
        'plugin:react/recommended',
        'plugin:react-hooks/recommended',
        'plugin:prettier/recommended',
        'plugin:import/errors',
        'plugin:import/warnings',
        'react-app',
        'react-app/jest',
        'airbnb'
    ],
    parser: '@typescript-eslint/parser',
    parserOptions: {
        ecmaFeatures: {
            jsx: true
        },
        ecmaVersion: 5,
        sourceType: 'module'
    },
    plugins: ['react', 'react-hooks', 'prefer-arrow', 'prettier', '@typescript-eslint'],
    settings: {
        react: {
            version: 'detect'
        },
        'import/resolver': {
            node: {
                paths: ['src'],
                moduleDirectory: ['node_modules', 'src/'],
                extensions: ['.js', '.jsx', '.ts', '.tsx']
            }
        }
    },
    overrides: [
        {
            files: ['**/*.js', '**/*.jsx', '**/*.ts', '**/*.tsx'],
            plugins: ['@typescript-eslint'],
            rules: {
                'no-use-before-define': 'off',
                '@typescript-eslint/no-use-before-define': ['error']
            }
        }
    ],
    ignorePatterns: ['src/assets/images/*', 'src/assets/fonts/*'],
    rules: {
        '@typescript-eslint/no-unused-vars': ['error'],
        'prettier/prettier': 'error',
        'arrow-body-style': 'off',
        'arrow-parens': ['error', 'always'],
        'object-curly-newline': 'off',
        'no-tabs': 0,
        indent: [2, 'tab', { SwitchCase: 1 }],
        'comma-dangle': ['error', 'never'],
        'max-len': ['error', { code: 120 }],
        'jsx-quotes': ['error', 'prefer-single'],
        'implicit-arrow-linebreak': 'off',
        'operator-linebreak': [
            'error',
            'after',
            {
                overrides: {
                    ':': 'before'
                }
            }
        ],
        'react-hooks/rules-of-hooks': 'error', // Checks rules of Hooks
        'react-hooks/exhaustive-deps': 'warn', // Checks effect,
        'react/jsx-indent-props': 'off',
        'react/react-in-jsx-scope': 'off',
        'react/jsx-props-no-spreading': 'off',
        'react/jsx-boolean-value': 'off',
        'react/jsx-indent': ['error', 'tab', { checkAttributes: true, indentLogicalExpressions: true }],
        'react/jsx-filename-extension': [1, { extensions: ['.js', '.jsx', '.ts', '.tsx'] }],
        'react/jsx-wrap-multilines': [
            'error',
            {
                declaration: 'parens-new-line',
                assignment: 'parens-new-line',
                return: 'parens-new-line',
                arrow: 'parens-new-line',
                condition: 'parens-new-line',
                logical: 'parens-new-line',
                prop: 'parens-new-line'
            }
        ],
        'prefer-arrow-callback': 'off',
        'prefer-arrow/prefer-arrow-functions': [
            'error',
            {
                disallowPrototype: true,
                singleReturnOnly: false,
                classPropertiesAllowed: false
            }
        ],
        'import/prefer-default-export': 'off',
        'import/extensions': [
            'error',
            'ignorePackages',
            {
                js: 'never',
                jsx: 'never',
                ts: 'never',
                tsx: 'never'
            }
        ]
    }
};
prettier
可以使用
.prettierignore
文件

我的VSCODE设置:

"editor.defaultFormatter": "esbenp.prettier-vscode",
"editor.formatOnSave": true,
"editor.rulers": [120, 120]
{
    "printWidth": 120,
    "tabWidth": 2,
    "useTabs": true,
    "semi": true,
    "singleQuote": true,
    "jsxSingleQuote": true,
    "jsxBracketSameLine": false,
    "bracketSpacing": true,
    "arrowParens": "always",
    "endOfLine": "lf",
    "quoteProps": "as-needed",
    "trailingComma": "none"
}
module.exports = {
    env: {
        browser: true,
        es2021: true
    },
    extends: [
        'plugin:react/recommended',
        'plugin:react-hooks/recommended',
        'plugin:prettier/recommended',
        'plugin:import/errors',
        'plugin:import/warnings',
        'react-app',
        'react-app/jest',
        'airbnb'
    ],
    parser: '@typescript-eslint/parser',
    parserOptions: {
        ecmaFeatures: {
            jsx: true
        },
        ecmaVersion: 5,
        sourceType: 'module'
    },
    plugins: ['react', 'react-hooks', 'prefer-arrow', 'prettier', '@typescript-eslint'],
    settings: {
        react: {
            version: 'detect'
        },
        'import/resolver': {
            node: {
                paths: ['src'],
                moduleDirectory: ['node_modules', 'src/'],
                extensions: ['.js', '.jsx', '.ts', '.tsx']
            }
        }
    },
    overrides: [
        {
            files: ['**/*.js', '**/*.jsx', '**/*.ts', '**/*.tsx'],
            plugins: ['@typescript-eslint'],
            rules: {
                'no-use-before-define': 'off',
                '@typescript-eslint/no-use-before-define': ['error']
            }
        }
    ],
    ignorePatterns: ['src/assets/images/*', 'src/assets/fonts/*'],
    rules: {
        '@typescript-eslint/no-unused-vars': ['error'],
        'prettier/prettier': 'error',
        'arrow-body-style': 'off',
        'arrow-parens': ['error', 'always'],
        'object-curly-newline': 'off',
        'no-tabs': 0,
        indent: [2, 'tab', { SwitchCase: 1 }],
        'comma-dangle': ['error', 'never'],
        'max-len': ['error', { code: 120 }],
        'jsx-quotes': ['error', 'prefer-single'],
        'implicit-arrow-linebreak': 'off',
        'operator-linebreak': [
            'error',
            'after',
            {
                overrides: {
                    ':': 'before'
                }
            }
        ],
        'react-hooks/rules-of-hooks': 'error', // Checks rules of Hooks
        'react-hooks/exhaustive-deps': 'warn', // Checks effect,
        'react/jsx-indent-props': 'off',
        'react/react-in-jsx-scope': 'off',
        'react/jsx-props-no-spreading': 'off',
        'react/jsx-boolean-value': 'off',
        'react/jsx-indent': ['error', 'tab', { checkAttributes: true, indentLogicalExpressions: true }],
        'react/jsx-filename-extension': [1, { extensions: ['.js', '.jsx', '.ts', '.tsx'] }],
        'react/jsx-wrap-multilines': [
            'error',
            {
                declaration: 'parens-new-line',
                assignment: 'parens-new-line',
                return: 'parens-new-line',
                arrow: 'parens-new-line',
                condition: 'parens-new-line',
                logical: 'parens-new-line',
                prop: 'parens-new-line'
            }
        ],
        'prefer-arrow-callback': 'off',
        'prefer-arrow/prefer-arrow-functions': [
            'error',
            {
                disallowPrototype: true,
                singleReturnOnly: false,
                classPropertiesAllowed: false
            }
        ],
        'import/prefer-default-export': 'off',
        'import/extensions': [
            'error',
            'ignorePackages',
            {
                js: 'never',
                jsx: 'never',
                ts: 'never',
                tsx: 'never'
            }
        ]
    }
};
我的漂亮配置:

"editor.defaultFormatter": "esbenp.prettier-vscode",
"editor.formatOnSave": true,
"editor.rulers": [120, 120]
{
    "printWidth": 120,
    "tabWidth": 2,
    "useTabs": true,
    "semi": true,
    "singleQuote": true,
    "jsxSingleQuote": true,
    "jsxBracketSameLine": false,
    "bracketSpacing": true,
    "arrowParens": "always",
    "endOfLine": "lf",
    "quoteProps": "as-needed",
    "trailingComma": "none"
}
module.exports = {
    env: {
        browser: true,
        es2021: true
    },
    extends: [
        'plugin:react/recommended',
        'plugin:react-hooks/recommended',
        'plugin:prettier/recommended',
        'plugin:import/errors',
        'plugin:import/warnings',
        'react-app',
        'react-app/jest',
        'airbnb'
    ],
    parser: '@typescript-eslint/parser',
    parserOptions: {
        ecmaFeatures: {
            jsx: true
        },
        ecmaVersion: 5,
        sourceType: 'module'
    },
    plugins: ['react', 'react-hooks', 'prefer-arrow', 'prettier', '@typescript-eslint'],
    settings: {
        react: {
            version: 'detect'
        },
        'import/resolver': {
            node: {
                paths: ['src'],
                moduleDirectory: ['node_modules', 'src/'],
                extensions: ['.js', '.jsx', '.ts', '.tsx']
            }
        }
    },
    overrides: [
        {
            files: ['**/*.js', '**/*.jsx', '**/*.ts', '**/*.tsx'],
            plugins: ['@typescript-eslint'],
            rules: {
                'no-use-before-define': 'off',
                '@typescript-eslint/no-use-before-define': ['error']
            }
        }
    ],
    ignorePatterns: ['src/assets/images/*', 'src/assets/fonts/*'],
    rules: {
        '@typescript-eslint/no-unused-vars': ['error'],
        'prettier/prettier': 'error',
        'arrow-body-style': 'off',
        'arrow-parens': ['error', 'always'],
        'object-curly-newline': 'off',
        'no-tabs': 0,
        indent: [2, 'tab', { SwitchCase: 1 }],
        'comma-dangle': ['error', 'never'],
        'max-len': ['error', { code: 120 }],
        'jsx-quotes': ['error', 'prefer-single'],
        'implicit-arrow-linebreak': 'off',
        'operator-linebreak': [
            'error',
            'after',
            {
                overrides: {
                    ':': 'before'
                }
            }
        ],
        'react-hooks/rules-of-hooks': 'error', // Checks rules of Hooks
        'react-hooks/exhaustive-deps': 'warn', // Checks effect,
        'react/jsx-indent-props': 'off',
        'react/react-in-jsx-scope': 'off',
        'react/jsx-props-no-spreading': 'off',
        'react/jsx-boolean-value': 'off',
        'react/jsx-indent': ['error', 'tab', { checkAttributes: true, indentLogicalExpressions: true }],
        'react/jsx-filename-extension': [1, { extensions: ['.js', '.jsx', '.ts', '.tsx'] }],
        'react/jsx-wrap-multilines': [
            'error',
            {
                declaration: 'parens-new-line',
                assignment: 'parens-new-line',
                return: 'parens-new-line',
                arrow: 'parens-new-line',
                condition: 'parens-new-line',
                logical: 'parens-new-line',
                prop: 'parens-new-line'
            }
        ],
        'prefer-arrow-callback': 'off',
        'prefer-arrow/prefer-arrow-functions': [
            'error',
            {
                disallowPrototype: true,
                singleReturnOnly: false,
                classPropertiesAllowed: false
            }
        ],
        'import/prefer-default-export': 'off',
        'import/extensions': [
            'error',
            'ignorePackages',
            {
                js: 'never',
                jsx: 'never',
                ts: 'never',
                tsx: 'never'
            }
        ]
    }
};
我的ESLINTRC配置:

"editor.defaultFormatter": "esbenp.prettier-vscode",
"editor.formatOnSave": true,
"editor.rulers": [120, 120]
{
    "printWidth": 120,
    "tabWidth": 2,
    "useTabs": true,
    "semi": true,
    "singleQuote": true,
    "jsxSingleQuote": true,
    "jsxBracketSameLine": false,
    "bracketSpacing": true,
    "arrowParens": "always",
    "endOfLine": "lf",
    "quoteProps": "as-needed",
    "trailingComma": "none"
}
module.exports = {
    env: {
        browser: true,
        es2021: true
    },
    extends: [
        'plugin:react/recommended',
        'plugin:react-hooks/recommended',
        'plugin:prettier/recommended',
        'plugin:import/errors',
        'plugin:import/warnings',
        'react-app',
        'react-app/jest',
        'airbnb'
    ],
    parser: '@typescript-eslint/parser',
    parserOptions: {
        ecmaFeatures: {
            jsx: true
        },
        ecmaVersion: 5,
        sourceType: 'module'
    },
    plugins: ['react', 'react-hooks', 'prefer-arrow', 'prettier', '@typescript-eslint'],
    settings: {
        react: {
            version: 'detect'
        },
        'import/resolver': {
            node: {
                paths: ['src'],
                moduleDirectory: ['node_modules', 'src/'],
                extensions: ['.js', '.jsx', '.ts', '.tsx']
            }
        }
    },
    overrides: [
        {
            files: ['**/*.js', '**/*.jsx', '**/*.ts', '**/*.tsx'],
            plugins: ['@typescript-eslint'],
            rules: {
                'no-use-before-define': 'off',
                '@typescript-eslint/no-use-before-define': ['error']
            }
        }
    ],
    ignorePatterns: ['src/assets/images/*', 'src/assets/fonts/*'],
    rules: {
        '@typescript-eslint/no-unused-vars': ['error'],
        'prettier/prettier': 'error',
        'arrow-body-style': 'off',
        'arrow-parens': ['error', 'always'],
        'object-curly-newline': 'off',
        'no-tabs': 0,
        indent: [2, 'tab', { SwitchCase: 1 }],
        'comma-dangle': ['error', 'never'],
        'max-len': ['error', { code: 120 }],
        'jsx-quotes': ['error', 'prefer-single'],
        'implicit-arrow-linebreak': 'off',
        'operator-linebreak': [
            'error',
            'after',
            {
                overrides: {
                    ':': 'before'
                }
            }
        ],
        'react-hooks/rules-of-hooks': 'error', // Checks rules of Hooks
        'react-hooks/exhaustive-deps': 'warn', // Checks effect,
        'react/jsx-indent-props': 'off',
        'react/react-in-jsx-scope': 'off',
        'react/jsx-props-no-spreading': 'off',
        'react/jsx-boolean-value': 'off',
        'react/jsx-indent': ['error', 'tab', { checkAttributes: true, indentLogicalExpressions: true }],
        'react/jsx-filename-extension': [1, { extensions: ['.js', '.jsx', '.ts', '.tsx'] }],
        'react/jsx-wrap-multilines': [
            'error',
            {
                declaration: 'parens-new-line',
                assignment: 'parens-new-line',
                return: 'parens-new-line',
                arrow: 'parens-new-line',
                condition: 'parens-new-line',
                logical: 'parens-new-line',
                prop: 'parens-new-line'
            }
        ],
        'prefer-arrow-callback': 'off',
        'prefer-arrow/prefer-arrow-functions': [
            'error',
            {
                disallowPrototype: true,
                singleReturnOnly: false,
                classPropertiesAllowed: false
            }
        ],
        'import/prefer-default-export': 'off',
        'import/extensions': [
            'error',
            'ignorePackages',
            {
                js: 'never',
                jsx: 'never',
                ts: 'never',
                tsx: 'never'
            }
        ]
    }
};