From 31ae71259f00a96b83d8653e0613e2383f5e549a Mon Sep 17 00:00:00 2001 From: lamphc Date: Tue, 26 Feb 2019 10:57:28 +0800 Subject: [PATCH] add ts lint --- package-lock.json | 104 +++++++++++++++++++++++++++++++++++++++++----- package.json | 2 + tslint.json | 102 +++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 197 insertions(+), 11 deletions(-) create mode 100644 tslint.json diff --git a/package-lock.json b/package-lock.json index 07f6f89..e07e7e0 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1416,6 +1416,12 @@ "integrity": "sha1-JuYe0UIvtw3ULm42cp7VHYVf6Nk=", "dev": true }, + "builtin-modules": { + "version": "1.1.1", + "resolved": "https://registry.npmjs.org/builtin-modules/-/builtin-modules-1.1.1.tgz", + "integrity": "sha1-Jw8HbFpywC9bZaR9+Uxf46J4iS8=", + "dev": true + }, "builtin-status-codes": { "version": "3.0.0", "resolved": "https://registry.npmjs.org/builtin-status-codes/-/builtin-status-codes-3.0.0.tgz", @@ -2319,6 +2325,12 @@ "repeating": "^2.0.0" } }, + "diff": { + "version": "3.5.0", + "resolved": "https://registry.npmjs.org/diff/-/diff-3.5.0.tgz", + "integrity": "sha512-A46qtFgd+g7pDZinpnwiRJtxbC1hpgf0uzP3iG89scHk0AUC7A1TGxf5OiiOUv/JMZR8GOt8hL900hV0bOy5xA==", + "dev": true + }, "diffie-hellman": { "version": "5.0.3", "resolved": "https://registry.npmjs.org/diffie-hellman/-/diffie-hellman-5.0.3.tgz", @@ -2919,7 +2931,8 @@ "ansi-regex": { "version": "2.1.1", "bundled": true, - "dev": true + "dev": true, + "optional": true }, "aproba": { "version": "1.2.0", @@ -2940,12 +2953,14 @@ "balanced-match": { "version": "1.0.0", "bundled": true, - "dev": true + "dev": true, + "optional": true }, "brace-expansion": { "version": "1.1.11", "bundled": true, "dev": true, + "optional": true, "requires": { "balanced-match": "^1.0.0", "concat-map": "0.0.1" @@ -2960,17 +2975,20 @@ "code-point-at": { "version": "1.1.0", "bundled": true, - "dev": true + "dev": true, + "optional": true }, "concat-map": { "version": "0.0.1", "bundled": true, - "dev": true + "dev": true, + "optional": true }, "console-control-strings": { "version": "1.1.0", "bundled": true, - "dev": true + "dev": true, + "optional": true }, "core-util-is": { "version": "1.0.2", @@ -3087,7 +3105,8 @@ "inherits": { "version": "2.0.3", "bundled": true, - "dev": true + "dev": true, + "optional": true }, "ini": { "version": "1.3.5", @@ -3099,6 +3118,7 @@ "version": "1.0.0", "bundled": true, "dev": true, + "optional": true, "requires": { "number-is-nan": "^1.0.0" } @@ -3113,6 +3133,7 @@ "version": "3.0.4", "bundled": true, "dev": true, + "optional": true, "requires": { "brace-expansion": "^1.1.7" } @@ -3120,12 +3141,14 @@ "minimist": { "version": "0.0.8", "bundled": true, - "dev": true + "dev": true, + "optional": true }, "minipass": { "version": "2.2.4", "bundled": true, "dev": true, + "optional": true, "requires": { "safe-buffer": "^5.1.1", "yallist": "^3.0.0" @@ -3144,6 +3167,7 @@ "version": "0.5.1", "bundled": true, "dev": true, + "optional": true, "requires": { "minimist": "0.0.8" } @@ -3224,7 +3248,8 @@ "number-is-nan": { "version": "1.0.1", "bundled": true, - "dev": true + "dev": true, + "optional": true }, "object-assign": { "version": "4.1.1", @@ -3236,6 +3261,7 @@ "version": "1.4.0", "bundled": true, "dev": true, + "optional": true, "requires": { "wrappy": "1" } @@ -3321,7 +3347,8 @@ "safe-buffer": { "version": "5.1.1", "bundled": true, - "dev": true + "dev": true, + "optional": true }, "safer-buffer": { "version": "2.1.2", @@ -3357,6 +3384,7 @@ "version": "1.0.2", "bundled": true, "dev": true, + "optional": true, "requires": { "code-point-at": "^1.0.0", "is-fullwidth-code-point": "^1.0.0", @@ -3376,6 +3404,7 @@ "version": "3.0.1", "bundled": true, "dev": true, + "optional": true, "requires": { "ansi-regex": "^2.0.0" } @@ -3419,12 +3448,14 @@ "wrappy": { "version": "1.0.2", "bundled": true, - "dev": true + "dev": true, + "optional": true }, "yallist": { "version": "3.0.2", "bundled": true, - "dev": true + "dev": true, + "optional": true } } }, @@ -7920,6 +7951,57 @@ "integrity": "sha1-yy4SAwZ+DI3h9hQJS5/kVwTqYAM=", "dev": true }, + "tslib": { + "version": "1.9.3", + "resolved": "https://registry.npmjs.org/tslib/-/tslib-1.9.3.tgz", + "integrity": "sha512-4krF8scpejhaOgqzBEcGM7yDIEfi0/8+8zDRZhNZZ2kjmHJ4hv3zCbQWxoJGz1iw5U0Jl0nma13xzHXcncMavQ==", + "dev": true + }, + "tslint": { + "version": "5.12.1", + "resolved": "https://registry.npmjs.org/tslint/-/tslint-5.12.1.tgz", + "integrity": "sha512-sfodBHOucFg6egff8d1BvuofoOQ/nOeYNfbp7LDlKBcLNrL3lmS5zoiDGyOMdT7YsEXAwWpTdAHwOGOc8eRZAw==", + "dev": true, + "requires": { + "babel-code-frame": "^6.22.0", + "builtin-modules": "^1.1.1", + "chalk": "^2.3.0", + "commander": "^2.12.1", + "diff": "^3.2.0", + "glob": "^7.1.1", + "js-yaml": "^3.7.0", + "minimatch": "^3.0.4", + "resolve": "^1.3.2", + "semver": "^5.3.0", + "tslib": "^1.8.0", + "tsutils": "^2.27.2" + }, + "dependencies": { + "glob": { + "version": "7.1.3", + "resolved": "https://registry.npmjs.org/glob/-/glob-7.1.3.tgz", + "integrity": "sha512-vcfuiIxogLV4DlGBHIUOwI0IbrJ8HWPc4MU7HzviGeNho/UJDfi6B5p3sHeWIQ0KGIU0Jpxi5ZHxemQfLkkAwQ==", + "dev": true, + "requires": { + "fs.realpath": "^1.0.0", + "inflight": "^1.0.4", + "inherits": "2", + "minimatch": "^3.0.4", + "once": "^1.3.0", + "path-is-absolute": "^1.0.0" + } + } + } + }, + "tsutils": { + "version": "2.29.0", + "resolved": "https://registry.npmjs.org/tsutils/-/tsutils-2.29.0.tgz", + "integrity": "sha512-g5JVHCIJwzfISaXpXE1qvNalca5Jwob6FjI4AoPlqMusJ6ftFE7IkkFoMhVLRgK+4Kx3gkzb8UZK5t5yTTvEmA==", + "dev": true, + "requires": { + "tslib": "^1.8.1" + } + }, "tty-browserify": { "version": "0.0.0", "resolved": "https://registry.npmjs.org/tty-browserify/-/tty-browserify-0.0.0.tgz", diff --git a/package.json b/package.json index 35cd550..91d517f 100644 --- a/package.json +++ b/package.json @@ -6,6 +6,7 @@ "scripts": { "test": "parcel build src/index.ts --out-dir dist", "start": "parcel serve index.html -p 1256 --open", + "lint": "tslint -p tsconfig.json", "build": "cross-env NODE_ENV=production parcel build src/index.ts --detailed-report --out-dir build --out-file index.js --no-source-maps", "build:node": "cross-env NODE_ENV=production parcel build src/index.ts --target node --detailed-report --out-dir build --out-file node.js", "build:ngzip": "tsc -p tsconfig.json --outDir build/tar/ws && gulp ngzip", @@ -20,6 +21,7 @@ "gulp-gzip": "^1.4.2", "gulp-tar": "^2.1.0", "parcel-bundler": "^1.7.0", + "tslint": "^5.12.1", "typescript": "^2.8.1" }, "dependencies": { diff --git a/tslint.json b/tslint.json new file mode 100644 index 0000000..5d034ed --- /dev/null +++ b/tslint.json @@ -0,0 +1,102 @@ +{ + "rules": { + // TS特性 + "member-access": true, // 设置成员对象的访问权限(public,private,protect) + "member-ordering": [ + // 设置修饰符顺序 + true, + { + "order": [ + "public-static-field", + "public-static-method", + "protected-static-field", + "protected-static-method", + "private-static-field", + "private-static-method", + "public-instance-field", + "protected-instance-field", + "private-instance-field", + "public-constructor", + "protected-constructor", + "private-constructor", + "public-instance-method", + "protected-instance-method", + "private-instance-method" + ] + } + ], + "no-empty-interface": true, // 不允许空接口 + "no-parameter-reassignment": true, // 不允许修改方法输入参数 + "prefer-for-of": true, // 如果for循环中没有使用索引,建议是使用for-of + + // 功能特性 + "await-promise": true, // 不允许没有Promise的情况下使用await + "curly": true, // if/for/do/while强制使用大括号 + "forin": true, // 使用for in语句时,强制进行hasOwnProperty检查 + "no-arg": true, // 不允许使用arguments.callee + // "no-bitwise":true, // 不允许使用特殊运算符 &, &=, |, |=, ^, ^=, <<, <<=, >>, >>=, >>>, >>>=, ~ + "no-conditional-assignment": true, // do while/for/if/while 语句中将会对例如if(a=b)进行检查 + // "no-console":true,// 不允许使用console对象 + "no-debugger": true, // 不允许使用debugger + "no-duplicate-super": true, // 不允许super() 两次使用在构造函数中 + "no-empty": true, // 函数体不允许空 + "no-eval": true, // 不允许使用eval + "no-for-in-array": true, // 不允许对Array使用for-in + "no-invalid-template-strings": true, // 只允许在模板字符串中使用${ + "no-invalid-this": true, // 不允许在class之外使用this + "no-null-keyword": true, // 不允许使用null,使用undefined代替null,指代空指针对象 + "no-sparse-arrays": true, // 不允许array中有空元素 + "no-string-throw": true, // 不允许throw一个字符串 + "no-switch-case-fall-through": true, // 不允许case段落中在没有使用breack的情况下,在新启一段case逻辑 + "no-unsafe-finally": true, // 不允许在finally语句中使用return/continue/break/throw + "no-unused-expression": true, // 不允许使用未使用的表达式 + "no-use-before-declare": true, // 在使用前必须声明 + "no-var-keyword": true, // 不允许使用var + "radix": true, // parseInt时,必须输入radix精度参数 + "restrict-plus-operands": true, // 不允许自动类型转换,如果已设置不允许使用关键字var该设置无效 + "triple-equals": true, // 必须使用恒等号,进行等于比较 + "use-isnan": true, // 只允许使用isNaN方法检查数字是否有效 + + // 维护性功能 + "indent": [true, "spaces", 4], // 每行开始以4个空格符开始 + "linebreak-style": [true, "CR/LF"], // 换行符格式 CR/LF可以通用使用在windows和osx + "max-classes-per-file": [true, 1], // 每个文件中可定义类的个数 + "max-file-line-count": [true, 500], // 定义每个文件代码行数 + "max-line-length": [true, 120], // 定义每行代码数 + //"no-default-export": true, // 禁止使用export default关键字,因为当export对象名称发生变化时,需要修改import中的对象名。https://github.com/palantir/tslint/issues/1182#issue-151780453 + "no-duplicate-imports": true, // 禁止在一个文件内,多次引用同一module + + // 格式 + "align": [ + true, + "parameters", + "arguments", + "statements", + "members", + "elements" + ], // 定义对齐风格 + "array-type": [true, "array"], // 建议使用T[]方式声明一个数组对象 + "class-name": true, // 类名以大驼峰格式命名 + "comment-format": [true, "check-space"], // 定义注释格式 + "encoding": true, // 定义编码格式默认utf-8 + "import-spacing": true, // import关键字后加空格 + "interface-name": [true, "always-prefix"], // interface必须以I开头 + "jsdoc-format": true, // 注释基于jsdoc风格 + "new-parens": true, // 调用构造函数时需要用括号 + "no-consecutive-blank-lines": [true, 2], // 不允许有空行 + "no-trailing-whitespace": [ + // 不允许空格结尾 + true, + "ignore-comments", + "ignore-jsdoc" + ], + "no-unnecessary-initializer": true, // 不允许没有必要的初始化 + "variable-name": [ + true, + "check-format", // 定义变量命名规则 + "allow-leading-underscore", + "allow-trailing-underscore", + "ban-keywords" + ] + } +}