// 禁止使用@ ts-ignore注释
"rules": { "ban-ts-ignore": true }
// 禁止特定类型的被使用。不被用来禁止相应的运行对象。
"ban-types": [true, [{}], [""]]
// 需要类成员明确可见性的声明
"member-access": false,
"member-access": true,
// 禁止向指定公众开放,因为这是默认的
"member-access": [true, "no-public"],
// 强制对get/set访问明确的知名度
"member-access": [true, "check-accessor"],
// 强制对构造明确的知名度
"member-access": [true, "check-constructor"],
// 强制执行参数属性明确的知名度
"member-access": [true, "parameter-property"],
// 强制实施成员排序
// 不允许any作为类型声明
"no-any": true
// 允许any[]
"no-any": [true, {"ignore-rest-args": true}]
// 不允许空接口
"no-empty-interface": true
// error example
interface I { }
// 不允许对Array使用for-in
"no-for-in": true
// 避免import语句副作用
"no-import-side-effect": true,
"no-import-side-effect": [true, {"ignore-module": "(\\.html|\\.css)$"}]
// 不允许将显式声明的变量或参数指定类型为一个number,string或boolean
"no-inferrable-types": true
// 允许为函数的参数作解释
"no-inferrable-types": [true, "ignore-params"]
// 允许指定为类性质的inferrable类型注释。
"no-inferrable-types": [true, "ignore-params", "ignore-properties"]
// 禁止使用module来定义命名空间
"no-internal-module": true
// 不允许被使用的数字常量值被外部变量赋值,当未指定的允许值的列表,-1,0和1是默认允许。
// 数字忽略列表。
no-magic-numbers": [true, 1, 2, 3]
// 指定特殊的魔法数字被允许
"no-magic-numbers": [true, {"allowed-numbers": [1, 2, 3], "ignore-jsx": true}]
// 禁止使用内部模块和命名空间
"no-namespace": true,
// 允许申报命名空间... {}来描述的外部API。
"no-namespace": [true, "allow-declarations"]
// 不允许非空的断言使用!后缀运算符
"no-non-null-assertion": true
// success examples
function foo(instance: MyClass | undefined) {
instance!.doWork();
}
// 不允许重新分配参数(为参数重新赋值)
"no-parameter-reassignment": true
// 不允许<reference path=>导入文件
"no-reference": true
// 不允许使用require,使用import替代
"no-var-requires": true
// error example
var module = require("module")
// success example
import foo = require(‘foo‘)
// 不允许非箭头函数的函数表达式
"only-arrow-functions": true
// allow-declarations:允许独立的函数声明
// allow-named-functions:允许函数 function foo() {} 而不是function() {}.
"only-arrow-functions": [true, "allow-declarations", "allow-named-functions"]
// 推荐使用for of 循环
"prefer-for-of": true
// 任何一个函数或者方法要求返回一个异步都要求带有async标记
"promise-function-async": true
// 不允许使用逗号操作符。
"ban-comma-operator": true
// if,while,do,for强制实施{}
"curly": true
// error example
if (x > 0)
doStuff();
if (x > 0) doStuff();
// 禁止任何不必要的大括号。
"curly": [true, "ignore-same-line"]
// success example
if (x > 0) doStuff();
// error example
if (x > 0)
doStuff()
//
"curly": [true, "as-needed"]
// success example
if (x > 0)
doStuff();
if (x > 0) {
customerUpdates.push(getInfo(customerId));
return customerUpdates;
}
// error example
if (x > 0) {
doStuff();
}
// 使用for in语句时,强制进行hasOwnProperty检查
"forin": true
// 使用构造函数的方式,类型得不到检查,用工厂函数返回自己的功能。
"function-constructor": true
// success example
let addNumbers = (a, b) => a + b;
// error example
let addNumbers = new Function("a", "b", "return a + b");
// 禁止直接导入库,要求用户直接导入他们只需要子模块
"import-blacklist": true
// 这条规则只允许标签上do for while switch使用标签
"label-position": true
// 异步函数必须包含一个await或者返回一个声明
"no-async-without-await": true
// success example
async function f() {
await fetch();
}
const f = async () => {
await fetch();
};
const f = async () => {
return ‘value‘;
};
// error example
async function f() {
fetch();
}
async function f() {
async function g() {
await h();
}
}
// 禁止&, &=, |, |=, ^, ^=, <<, <<=, >>, >>=, >>>, >>>=, ~ 的使用
"no-bitwise": true
// 禁止条件语句进行赋值操作
"no-conditional-assignment": true
// error example
if (var1 = var2)
// 禁止console,console无法生成合适的生产环境代码
"no-console": [true, "log", "error"]
// 不允许访问string,number,和boolern的构造函数。
"no-construct": true
// 禁止debugger语句
"no-debugger": true
// 禁止在同一个constructor构造函数出现两次super()
"no-duplicate-super": true
// 防止出现重复switch语句
"no-duplicate-switch-case": true
// 在同一个块级区域内禁止申明相同变量
"no-duplicate-variable": true
// 计算表达式中禁止使用delete命令,可以用map代替
"no-dynamic-delete": true
// 函数体不允许空函数体不允许空
"no-empty":true
// 禁止eval函数调用(eval函数具有完全权限去执行任意代码。
"no-eval": true
// 由函数返回的结果必须妥善处理。
"no-floating-promises": true
// 禁止对Array进行for in 循环
"no-for-in-array": true
// 不允许导入模块未列为项目的package.json依赖
"no-implicit-dependencies": true
// 禁止在函数或者构造函数中对{}空对象进行类型断言
"no-inferred-empty-object-type": true
// 对非模版的string在使用${}时发出警告
"no-invalid-template-strings": true
// 除类以外,不允许使用this关键字
"no-invalid-this": true
// 尝试为接口或者new的Class类重新定义构造函数时发出警告
"no-misused-new": true
// 禁止使用空的关键字文字。
"no-null-keyword": true
// 禁止显式申明或者隐式返回类型为null或者underfind的值
"no-null-undefined-union": true
// 禁止对象出现类型断言表达式,null以及unknown还是被允许的。
"no-object-literal-type-assertion": true
// success example
let foo = {} as any;
let foo = {} as unknown;
let foo = {} as any as Foo;
let foo = {} as unknown as Foo;
// error example
let foo = {} as Foo;
let foo = <Foo>{};
// 不允许特定的全局变量。
"no-restricted-globals": [true, "name", "length", "event"]
// 禁止不必要的return await。
"no-return-await": true
// 不允许阴影变量声明。
"no-shadowed-variable": [
true,
{
"class": true,
"enum": true,
"function": true,
"interface": false,
"namespace": true,
"typeAlias": false,
"typeParameter": false,
"underscore": false
}
]
// 禁止数组含有缺失的元素。
"rules": { "no-sparse-arrays": true }
// error example
const arr: string[] = [‘elemenet1‘,, ‘element2‘];
// 不允许通过case语句下降。
"no-switch-case-fall-through": true
// error example
switch(foo) {
case 1:
someFunc(foo);
/* falls through */
case 2:
case 3:
someOtherFunc(foo);
}
// 禁止对this进行不必要的引用
"no-this-assignment": true
// error example
const self = this;
setTimeout(function () {
self.doWork();
});
// 不允许未使用的表达式语句。
"no-unused-expression": true
// 变量声明前禁止使用
"no-use-before-declare": true
// 禁止使用var申明变量。
"no-var-keyword": true
// 当添加两个变量,操作数必须都是number类型或string类型。
"restrict-plus-operands": true
// 禁止在静态方法中使用this
"static-this": true
// 禁止隐式toString()的调用
"strict-string-expressions": true
// 允许null, undefined和never的隐式调用
"strict-string-expressions": [true, {"allow-empty-types": true}]
// switch循环语句需要有defalut默认值
"switch-default": true
// 使用‘===‘‘!==‘替代‘==‘‘!=‘
"triple-equals": true
// 当比较值为null时允许使用‘==‘‘!=‘
"triple-equals": [true, "allow-null-check"]
// 当比较值为underfind时允许使用‘==‘‘!=‘
"triple-equals": [true, "allow-undefined-check"]
// 校验typeof的值为string类型
"typeof-compare": true
// 禁止空构造函数,JavaScript会隐式创建一个constructor构造函数,不需要手动去添加。
"unnecessary-constructor": true
// 检查不必要的构造函数的参数
"unnecessary-constructor": [true, {"check-super-calls": true}]
// 只允许使用isNaN方法检查数字是否有效
"use-isnan": true
// 圈复杂度衡量,圈复杂度越高,程序代码的判断逻辑越复杂,可能质量越低低且难于测试和维护
"cyclomatic-complexity": true
// 设置圈复杂度最大值,默认为20
"cyclomatic-complexity": [true, 20]
// 使用已弃用的API时会发出警告。
"deprecation": true
// 禁止默认export,使用有效名称导出模块
"no-default-export": true
// 确保每个页面最多多少行代码
"max-file-line-count": [true, 300]
// 禁止使用require()
"no-require-imports": true
// 检查object对象中key值排序
"object-literal-sort-keys": true
// success example
// 默认按照首字母排序
let o = {
bar: 2,
foo: 1
};
// 按照接口排序
interface I {
foo: number;
bar: number;
}
let o: I = {
foo: 1,
bar: 2
};
// 尽量去使用const来代替let或者var(不推荐使用)
"prefer-const": true
// private变量应该为readonly(只读)
"prefer-readonly": true
// arrays数组要用‘T[]‘或者‘Array‘来指定类型
// 强制使用T[]来作为数组指定类型
"array-type": [true, "array"]
// 强制使用Array<T>来作为数组指定类型
"array-type": [true, "generic"]
// 如果数组是一个简单类型,强制使用T[]
"array-type": [true, "array-simple"]
// 箭头函数() => x;来代替() => { return x;},减少不必要的return
"arrow-return-shorthand": true
// 如果指定了多行,那么这将发出警告,即使该函数跨多行
"arrow-return-shorthand": [true, "multiline"]
// 数字字面量必须在加号的右边,即禁止1 + x
"binary-expression-operand-order": true
// 一个接口可调用,需要声明为 Function 类型
"callable-types": true
// 大驼峰命名法命名类名和接口名
"class-name": true
// success example
class MyClass { }
interface MyInterface { }
// 强制单行注释格式
"comment-format": [true, "check-space", "check-uppercase", "allow-trailing-lowercase"]
// check-space 以空格开头 // comment
// check-lowercase 第一个字符为小写
// check-uppercase 第一个字符为大写
// allow-trailing-lowercase 允许只有第一个为大写
// 强制UTF-8编码
"encoding": true
// 文件名称命名规范
"file-name-casing": [true, "camel-case"]
// camel-case 小驼峰 fileName.ts
// pascal-case 大驼峰 FileName.ts
// kebab-case -命名 file-name.ts
// snake-case _命名 file_name.ts
// ignore 忽略
// 不同文件对应命名
"file-name-casing": [true, {".tsx": "pascal-case", ".ts": "camel-case"}]
// 强制‘+=‘‘-=‘来执行运算
"increment-decrement": true
// 接口名称以‘|‘开头
"interface-name": [true, "always-prefix"]
"interface-name": [true, "never-prefix"]
// 链式调用方法换行,保持代码垂直
"newline-per-chained-call": true
// 类型断言的形式替换类型声明 as Type 替换 <Type>
"no-angle-bracket-type-assertion": true
// 比较对象为boolean时,报出警告
"no-boolean-literal-compare": true
// warn example
x === true
// 无必要的包装回调函数
"no-unnecessary-callback-wrapper": true
// warn example
x => f(x)
// 禁止无意义的初始赋值为underfind
"no-unnecessary-initializer": true
// 强制统一对象属性的调用方式
// 对象属性名称应该始终加‘‘
"object-literal-key-quotes": [true, "always"]
// 对象属性名称需要加‘‘时加‘‘
"object-literal-key-quotes": [true, "as-needed"]
// 对象属性名称要么全加‘‘要么全不加
"object-literal-key-quotes": [true, "consistent"]
// 对象属性名称有一个需要加‘‘则全部需要加‘‘
"object-literal-key-quotes": [true, "consistent-as-needed"]
// 检查流程控制代码的衔接部分和空格
"one-line": [true, "check-catch", "check-finally", "check-else","check-whitespace"]
// 在一个声明语句中不允许多个变量定义。
"one-variable-per-declaration": true
// 允许他们在for循环。
"one-variable-per-declaration": [true, "ignore-for-loop"]
// error example
const foo = 1, bar = ‘2‘;
// 必须使用foo(): void而不是foo: () => void
"prefer-method-signature": true
// 当if中只有===时,必须使用switch替换if
"prefer-switch": true
// 可以设置最小case值,默认为3
"prefer-switch": [true, {"min-cases": 2}]
// 使用模版拼接而非字符串拼接
"prefer-template": true
//单个字符串拼接是允许的
"prefer-template": [true, "allow-single-concat"]
// success example
const myString: string = x + ‘ is equals 1‘;
// error example
const myString: string = x + ‘ is equals ‘ + y;
// 当没有初始值的时候,必须使用while而不是for
"prefer-while": true
// 当没有初始值的时候,必须使用while而不是for
"prefer-while": true
// 用return;替换 return undefined;和return一个空的函数
"return-undefined": true
// 用return;替换 return undefined;和return一个空的函数
"return-undefined": true
// 函数名前必须有空格
"space-before-function-paren": true
//
// 括号内首尾禁止有空格,空括号除外
"space-before-function-paren": true
// switch语句的最后一项禁止有break
"switch-final-break": true
// 必须全部都存在break
"switch-final-break": [true, "always"]
// 字面类型的每个成员都必须有分号
"switch-final-break": true
// 禁止无效的bind
"unnecessary-bind": true
// 禁止无效的else当if的代码块以break, continue, return, throw结尾
"unnecessary-else": true
// 允许else if
"unnecessary-else": [true, {"allow-else-if": true}]
// success example
if (someCondition()) {
return;
}
// some code here
// error example
if (someCondition()) {
return;
} else {
// some code here
}
// 检查各种错误的变量名
"variable-name": {
"options": [
"ban-keywords",
"check-format",
"allow-leading-underscore",
"allow-pascal-case"
]
}
// 强制执行垂直对齐
"align": [true, "parameters", "statements"]
// 箭头函数的参数必须有小括号
"arrow-parens": true
// 一个参数没有必要有括号
"arrow-parens": [true, "ban-single-arg-parens"]
// 文件最后一行必须有一个空行
"eofline": true
// import语句中,关键字之间的间距必须是一个空格
"import-spacing": true
// 一个缩进必须用四个空格替代
"indent": [true, "spaces"]
// 限制一行最大长度
"max-line-length": [true, 120]
// return语句前必须有空行
"newline-before-return": true
// 禁止一个或者多个空白行
"no-consecutive-blank-lines": true
// 设置允许空白的行
"no-consecutive-blank-lines": [true, 2]
// 禁止使用特殊空白符包括string和注释
"no-irregular-whitespace": true
// 小数必须以0.开头,禁止以.开头,并且不能以0结尾
"number-literal-format": true
// 必须使用单引号
"quotemark": [true, "single", "avoid-escape", "avoid-template"]
// 必须使用双引号
"quotemark": [true, "double", "jsx-double"]
// 行尾必须有分号
"semicolon": [true, "always"]
// 行尾不能使用分号
"semicolon": [true, "never"]
// 限制对象、数组、解构赋值等的最后一项末尾是否需要逗号
"trailing-comma": [true, {"multiline": "always", "singleline": "never"}]
// 限制空格的位置
"whitespace": [true, "check-branch", "check-operator", "check-typecast"]
原文:https://www.cnblogs.com/wuxin123/p/12924987.html