React 项目中引入 TSLint 做代码规范

在项目开发中我们希望可以编写高质量的规范代码,但是在多人协作开发项目时,每个人都有自己的不同的编码习惯,在项目中随着项目的不断完善,代码量和代码复杂度的不断增加,就会导致项目代码变得越来越杂乱无章,越来越难以理解。这时在团队协作时开发风格统一的规范代码就显得尤为重要。

在多人协作开发项目如何才能做到开发统一风格的规范性代码呢?我们知道可以起到规范代码作用的有 TSLint 和 ESLint ,但在本篇文章中我们主要介绍如何在Create React App 项目中引入 TSLint 。

步骤1、首先我们应该通过 cmd 执行以下命令创建一个 Create React App 项目。

步骤2:打开 cmd 通过 cd my-app 进入到项目根目录中执行 npm install tslint typescript  --save-dev 安装 tslint 和 typescript

步骤3:通过步骤2中已经安装了 typescript,因此我们需要将项目中后缀为 “.js” 的文件改为后缀名为 “.tsx”。

步骤4:在项目根目录中添加一个 tslint.json 文件,并通过 tslint 官网中的 Rules 配置 tslint.json 文件

步骤5:在项目中添加 tslint.json 后可以看到项目目录为

步骤6:以 vscode 编辑器为例,我们需要安装一个第三方的 TSLint 插件,安装成功后重启 vscode 编辑器,即可生效

步骤7:重启 vscode 编辑器后进行测试

a、在 tslint.json 文件配置  "no-var-keyword": true     // 禁止使用var关键字,使用let或const代替

b、app.tsx 文件中使用 var 声明一个变量

c、查看是否会提示错误信息


步骤8:如果在测试时出现上图错误信息表示 tslint 已经成功引入到项目中了。




备注 1:本篇文章我们在项目中引入了 tslint.json 文件是为了做代码规范约束,但是在项目根目录中还存在一个与 tslint.json 文件很相似的tsconfig.json的配置文件,tsconfig.json文件作用及配置项请点击 tsconfig.json 文件说明

备注2:在 window 系统中通过 cmd 执行命令,在 mac 系统中使用 终端 执行命令

备注3:tslint.json 文件中的配置项有很多,可以在 tslint 官网中的 Rules 中选择,也可以在网上查找一些常用到的规则,下面是我的 tslint.json 文件的配置,没有使用截图,方便拷贝:

{

  "rules": {

    "adjacent-overload-signatures": true,

    "arrow-return-shorthand": true,

    "await-promise": true,

    "ban-comma-operator": true,

    "binary-expression-operand-order": true,

    "callable-types": true,

    "class-name": true,

    "comment-format": [

      true,

      "check-space"

    ],

    "component-class-suffix": true,

    "curly": true,

    "deprecation": {

      "severity": "warn"

    },

    "directive-class-suffix": true,

    "encoding": true,

    "eofline": true,

    "forin": true,

    "import-blacklist": [

      true,

      "rxjs/Rx"

    ],

    "import-spacing": true,

    "indent": [

      true,

      "spaces",

      2

    ],

    "interface-name": [true, "never-prefix"],

    "interface-over-type-literal": true,

    "label-position": true,

    "max-line-length": [

      true,

      140

    ],

    "member-access": false,

    "member-ordering": [

      true,

      {

        "order": [

          "static-field",

          "instance-field",

          "static-method",

          "instance-method"

        ]

      }

    ],

    "new-parens": true,

    "no-arg": true,

    "no-bitwise": true,

    "no-boolean-literal-compare": true,

    "no-conditional-assignment": true,

    "no-consecutive-blank-lines": true,

    "no-console": [

      true,

      "debug",

      "info",

      "time",

      "timeEnd",

      "trace"

    ],

    "no-construct": true,

    "no-debugger": true,

    "no-default-export": true,

    "no-duplicate-imports": true,

    "no-duplicate-super": true,

    "no-duplicate-switch-case": true,

    "no-empty": false,

    "no-empty-interface": true,

    "no-eval": true,

    "no-for-in-array": true,

    "no-implicit-dependencies": [

      true,

      "dev"

    ],

    "no-inferrable-types": [

      true,

      "ignore-params"

    ],

    "no-inferred-empty-object-type": true,

    "no-input-rename": true,

    "no-invalid-template-strings": true,

    "no-irregular-whitespace": true,

    "no-misused-new": true,

    "no-namespace": [

      true,

      "allow-declarations"

    ],

//    "no-non-null-assertion": true,

    "no-output-on-prefix": true,

    "no-output-rename": true,

    "no-reference": true,

    "no-require-imports": true,

    "no-return-await": true,

    "no-shadowed-variable": true,

    "no-string-literal": false,

    "no-string-throw": true,

    "no-switch-case-fall-through": true,

    "no-trailing-whitespace": true,

    "no-unnecessary-callback-wrapper": true,

    "no-unnecessary-initializer": true,

    "no-unnecessary-qualifier": true,

    "no-unsafe-any": true,

    "no-unsafe-finally": true,

    "no-unused-expression": true,

    "no-use-before-declare": true,

    "no-var-keyword": true,

    "no-var-requires": true,

    "number-literal-format": true,

    "object-literal-key-quotes": [

      true,

      "as-needed"

    ],

    "object-literal-shorthand": [

      true,

      "never"

    ],

    "object-literal-sort-keys": false,

    "one-line": [

      true,

      "check-open-brace",

      "check-catch",

      "check-else",

      "check-whitespace"

    ],

    "one-variable-per-declaration": true,

    "ordered-imports": [

      true,

      {

        "grouped-imports": true,

        // "import-sources-order": "lowercase-last",

        "named-imports-order": "lowercase-first"

      }

    ],

    "prefer-object-spread": true,

    "prefer-readonly": true,

    "prefer-switch": true,

    "prefer-template": [

      true,

      "allow-single-concat"

    ],

    "prefer-while": true,

    "quotemark": [

      true,

      "single",

      "jsx-double"

    ],

    "radix": true,

    "semicolon": [

      true,

      "always",

      "ignore-bound-class-methods"

    ],

    "space-before-function-paren": [

      true,

      {

        "anonymous": "never",

        "asyncArrow": "always",

        "constructor": "never",

        "method": "never",

        "named": "never"

      }

    ],

    "space-within-parens": true,

    "switch-final-break": true,

    "trailing-comma": [

      true,

      {

        "multiline": "never",

        "singleline": "never"

      }

    ],

    "triple-equals": [

      true,

      "allow-null-check"

    ],

    "type-literal-delimiter": true,

    "typedef": [

      true,

      "array-destructuring",

      "arrow-call-signature",

      "call-signature",

      "object-destructuring",

      "parameter",

      "property-declaration"

    ],

    "typedef-whitespace": [

      true,

      {

        "call-signature": "nospace",

        "index-signature": "nospace",

        "parameter": "nospace",

        "property-declaration": "nospace",

        "variable-declaration": "nospace"

      },

      {

        "call-signature": "onespace",

        "index-signature": "onespace",

        "parameter": "onespace",

        "property-declaration": "onespace",

        "variable-declaration": "onespace"

      }

    ],

    "unified-signatures": true,

    "use-host-property-decorator": true,

    "use-input-property-decorator": true,

    "use-isnan": true,

    "use-life-cycle-interface": true,

    "use-output-property-decorator": true,

    "use-pipe-transform-interface": true,

    "variable-name": [

      true,

      "ban-keywords",

      "check-format",

      "allow-leading-underscore"

    ],

    "whitespace": [

      true,

      "check-branch",

      "check-decl",

      "check-module",

      "check-operator",

      "check-rest-spread",

      "check-separator",

      "check-type",

      "check-type-operator"

    ]

  }

}

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 217,907评论 6 506
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 92,987评论 3 395
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 164,298评论 0 354
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 58,586评论 1 293
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 67,633评论 6 392
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 51,488评论 1 302
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 40,275评论 3 418
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 39,176评论 0 276
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 45,619评论 1 314
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 37,819评论 3 336
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 39,932评论 1 348
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 35,655评论 5 346
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 41,265评论 3 329
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 31,871评论 0 22
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 32,994评论 1 269
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 48,095评论 3 370
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 44,884评论 2 354