美文网首页
React Alloy ESLint & Prettier

React Alloy ESLint & Prettier

作者: 咖A喱 | 来源:发表于2020-01-20 18:39 被阅读0次

    使用 AlloyTeam 的 ESLint 配置

    安装

    npm install --save-dev eslint typescript @typescript-eslint/parser @typescript-eslint/eslint-plugin eslint-plugin-react eslint-config-alloy

    在你的项目的根目录下创建一个 .eslintrc.js 文件,并将以下内容复制进去:

    module.exports = {
        extends: ['alloy', 'alloy/typescript'],
        env: {
            browser: true,
            node: true,
        },
        globals: {
            appTool: true,
            require: true,
            $: true,
            ant: true,
        },
        parser: '@typescript-eslint/parser',
        plugins: ['@typescript-eslint'],
        rules: {
            "@typescript-eslint/explicit-member-accessibility": "off",
            'accessor-pairs': 'error',
            'array-bracket-spacing': ['error', 'never'],
            'array-callback-return': 'error',
            'arrow-body-style': ['off', 'always'],
            'arrow-parens': 'error',
            'arrow-spacing': 'error',
            'block-scoped-var': 'off',
            'block-spacing': ['error', 'never'],
            'brace-style': 'off',
            'callback-return': 'off',
            camelcase: 'off',
            'comma-dangle': ['error', 'never'],
            'comma-spacing': 'off',
            'comma-style': ['error', 'last'],
            complexity: 'off',
            'computed-property-spacing': ['error', 'never'],
            'consistent-return': 'off',
            'consistent-this': 'off',
            curly: 'off',
            'default-case': 'off',
            'dot-location': ['error', 'property'],
            'dot-notation': 'error',
            'eol-last': 'off',
            eqeqeq: 'off',
            'func-names': 'off',
            'func-style': 'off',
            'generator-star-spacing': 'error',
            'global-require': 'error',
            'guard-for-in': 'off',
            'handle-callback-err': 'error',
            'id-blacklist': 'error',
            'id-length': 'off',
            'id-match': 'error',
            indent: ['error', 4, { SwitchCase: 1 }],
            'init-declarations': 'off',
            'jsx-quotes': ['error', 'prefer-double'],
            'key-spacing': 'off',
            'keyword-spacing': 'off',
            'linebreak-style': ['error', 'unix'],
            'lines-around-comment': 'off',
            'max-depth': 'off',
            'max-len': 'off',
            'max-lines': 'off',
            'max-nested-callbacks': 'error',
            'max-params': 'off',
            'max-statements': 'off',
            'max-statements-per-line': 'off',
            'new-parens': 'off',
            'new-cap': 'off',
            'newline-after-var': 'off',
            'newline-before-return': 'off',
            'newline-per-chained-call': 'off',
            'no-alert': 'off',
            'no-array-constructor': 'error',
            'no-bitwise': 'off',
            'no-caller': 'error',
            'no-catch-shadow': 'off',
            'no-confusing-arrow': 'error',
            'no-console': 'off',
            'no-continue': 'off',
            'no-div-regex': 'error',
            'no-duplicate-imports': 'off',
            'no-else-return': 'off',
            'no-empty-function': 'off',
            'no-eq-null': 'off',
            'no-eval': [
                'error',
                {
                    allowIndirect: true,
                },
            ],
            'no-extend-native': 'error',
            'no-extra-bind': 'error',
            'no-extra-label': 'error',
            'no-extra-parens': 'off',
            'no-floating-decimal': 'off',
            'no-implicit-coercion': [
                'error',
                {
                    boolean: false,
                    number: false,
                    string: false,
                },
            ],
            'no-implicit-globals': 'off',
            'no-implied-eval': 'error',
            'no-inline-comments': 'off',
            'no-inner-declarations': ['error', 'functions'],
            'no-invalid-this': 'off',
            'no-iterator': 'error',
            'no-label-var': 'error',
            'no-labels': 'error',
            'no-lone-blocks': 'error',
            'no-lonely-if': 'off',
            'no-loop-func': 'off',
            'no-magic-numbers': 'off',
            'no-mixed-operators': 'off',
            'no-mixed-requires': 'error',
            'no-multi-spaces': 'error',
            'no-multi-str': 'off',
            'no-multiple-empty-lines': ['error', { max: 1, maxEOF: 1 }],
            'no-native-reassign': 'error',
            'no-negated-condition': 'off',
            'no-nested-ternary': 'off',
            'no-new': 'error',
            'no-new-func': 'off',
            'no-new-object': 'error',
            'no-new-require': 'error',
            'no-new-wrappers': 'error',
            'no-octal-escape': 'error',
            'no-param-reassign': 'off',
            'no-path-concat': 'error',
            'no-plusplus': 'off',
            'no-process-env': 'error',
            'no-process-exit': 'error',
            'no-proto': 'off',
            'no-prototype-builtins': 'off',
            'no-restricted-globals': 'error',
            'no-restricted-imports': 'error',
            'no-restricted-modules': 'error',
            'no-restricted-syntax': ['error', 'WithStatement'],
            'no-return-assign': 'off',
            'no-script-url': 'off',
            'no-self-compare': 'off',
            'no-sequences': 'off',
            'no-shadow': 'off',
            'no-shadow-restricted-names': 'off',
            'no-spaced-func': 'error',
            'no-sync': 'off',
            'no-ternary': 'off',
            'no-throw-literal': 'error',
            'no-trailing-spaces': 'off',
            'no-undef-init': 'error',
            'no-undef': 'off',
            'no-undefined': 'off',
            'no-underscore-dangle': 'off',
            'no-unmodified-loop-condition': 'error',
            'no-unneeded-ternary': [
                'error',
                {
                    defaultAssignment: true,
                },
            ],
            'no-unsafe-finally': 'error',
            'no-unused-expressions': 'off',
            'no-unused-vars': 'off',
            'no-use-before-define': 'off',
            'no-useless-call': 'off',
            'no-useless-computed-key': 'error',
            'no-useless-concat': 'off',
            'no-useless-constructor': 'error',
            'no-useless-escape': 'off',
            'no-useless-rename': 'error',
            'no-var': 'error',
            'no-void': 'off',
            'no-warning-comments': 'off',
            'no-whitespace-before-property': 'error',
            'no-with': 'error',
            'object-curly-newline': 'off',
            'object-curly-spacing': ['error', 'always'],
            'object-property-newline': [
                'error',
                {
                    allowMultiplePropertiesPerLine: true,
                },
            ],
            'object-shorthand': 'off',
            'one-var': 'off',
            'one-var-declaration-per-line': 'off',
            'operator-assignment': 'off',
            'operator-linebreak': ['error', 'after'],
            'padded-blocks': 'off',
            'prefer-arrow-callback': 'off',
            'prefer-const': 'error',
            'prefer-reflect': 'off',
            'prefer-rest-params': 'off',
            'prefer-spread': 'off',
            'prefer-template': 'off',
            'quote-props': 'off',
            quotes: ['error', 'single'],
            radix: 'off',
            'require-jsdoc': 'off',
            'require-yield': 'error',
            'rest-spread-spacing': 'error',
            semi: ['error', 'always'],
            'semi-spacing': 'off',
            'sort-imports': 'off',
            'sort-vars': 'off',
            'space-before-blocks': 'off',
            'space-before-function-paren': ['error', { anonymous: 'always', named: 'never' }],
            'space-in-parens': 'off',
            'space-infix-ops': 'off',
            'space-unary-ops': [
                'error',
                {
                    nonwords: false,
                    words: false,
                },
            ],
            strict: 'off',
            'template-curly-spacing': 'error',
            'unicode-bom': ['error', 'never'],
            'valid-jsdoc': 'off',
            'vars-on-top': 'off',
            'wrap-iife': 'off',
            'wrap-regex': 'off',
            'yield-star-spacing': 'error',
            yoda: 'off',
            'react/sort-comp': 'off',
            'react/prefer-stateless-function': 'off',
            'spaced-comment': ['error', 'always'],
            'react/jsx-no-bind': 'off',
            'no-extra-boolean-cast': 'off',
        },
    };
    
    
    npm run eslint:fix
    # 自动修复格式错误
    npm run prettier:fix
    # 检查当前是否覆盖了所有的规则
    

    eslint-config-alloy 从 v3 开始,已经不包含所有样式相关的规则了,故不需要引入 eslint-config-prettier。只需要安装 prettier 及相关 VSCode 插件即可。

    下面给出一个 AlloyTeam 使用的 .prettierrc.js 配置,仅供参考:

    // .prettierrc.js
    module.exports = {
        // 一行最多 100 字符
        printWidth: 100,
        // 使用 4 个空格缩进
        tabWidth: 4,
        // 不使用缩进符,而使用空格
        useTabs: false,
        // 行尾需要有分号
        semi: true,
        // 使用单引号
        singleQuote: true,
        // 对象的 key 仅在必要时用引号
        quoteProps: 'as-needed',
        // jsx 不使用单引号,而使用双引号
        jsxSingleQuote: false,
        // 末尾不需要逗号
        trailingComma: 'none',
        // 大括号内的首尾需要空格
        bracketSpacing: true,
        // jsx 标签的反尖括号需要换行
        jsxBracketSameLine: false,
        // 箭头函数,只有一个参数的时候,也需要括号
        arrowParens: 'always',
        // 每个文件格式化的范围是文件的全部内容
        rangeStart: 0,
        rangeEnd: Infinity,
        // 不需要写文件开头的 @prettier
        requirePragma: false,
        // 不需要自动在文件开头插入 @prettier
        insertPragma: false,
        // 使用默认的折行标准
        proseWrap: 'preserve',
        // 根据显示样式决定 html 要不要折行
        htmlWhitespaceSensitivity: 'css',
        // 换行符使用 lf
        endOfLine: 'lf'
    };
    

    prettier

    yarn add prettier --dev

    IDEA 配置

    Go to Preferences | Tools | External Tools and click + to add a new tool. Let’s name it Prettier.

    • Program: $ProjectFileDir$/node_modules/.bin/prettier
    • Parameters: --write $FilePathRelativeToProjectRoot$
    • Working directory: $ProjectFileDir$

    File watchs

    To automatically format your files using prettier on save, you can use a File Watcher.

    Go to Preferences | Tools | File Watchers and click + to add a new watcher.

    image.png

    Git整合

    // 这一行就可以安装husky和lint-stage,并且配置好husky。
    npx mrm lint-staged

    相关文章

      网友评论

          本文标题:React Alloy ESLint & Prettier

          本文链接:https://www.haomeiwen.com/subject/pazjzctx.html