TSLint 配置规则

TSLint core rules

原文:https://palantir.github.io/tslint/rules/

Lint rules encode logic for syntactic & semantic checks of TypeScript source code.

TypeScript-specific

These rules find errors related to TypeScript features:

adjacent-overload-signatures - Enforces function overloads to be consecutive.

ban-ts-ignore - Bans “// @ts-ignore” comments from being used.

ban-types - Bans specific types from being used. Does not ban the corresponding runtime objects from being used.

member-access - Requires explicit visibility declarations for class members.

member-ordering - Enforces member ordering.

no-any - Disallows usages of any as a type declaration.

no-empty-interface - Forbids empty interfaces.
no-import-side-effect - Avoid import statements with side-effect.

no-inferrable-types - Disallows explicit type declarations for variables or parameters initialized to a number, string, or boolean.

no-internal-module - Disallows internal module
no-magic-numbers - Disallows the use constant number values outside of variable assignments. When no list of allowed values is specified, -1, 0 and 1 are allowed by default.

no-namespace - Disallows use of internal modules and namespaces.

no-non-null-assertion - Disallows non-null assertions using the ! postfix operator.
no-parameter-reassignment - Disallows reassigning parameters.
no-reference - Disallows /// <reference path=> imports (use ES6-style imports instead).

Requires Type Info
no-unnecessary-type-assertion - Warns if a type assertion does not change the type of an expression.

no-var-requires - Disallows the use of require statements except in import statements.
only-arrow-functions - Disallows traditional (non-arrow) function expressions.
prefer-for-of - Recommends a ‘for-of’ loop over a standard ‘for’ loop if the index is only used to access the array being iterated.
Requires Type Info
promise-function-async - Requires any function or method that returns a promise to be marked async.

typedef - Requires type definitions to exist.

typedef-whitespace - Requires or disallows whitespace for type definitions.

unified-signatures - Warns for any two overloads that could be unified into one by using a union or an optional/rest parameter.
Functionality
These rules catch common errors in JS programming or otherwise confusing constructs that are prone to producing bugs:

Requires Type Info
await-promise - Warns for an awaited value that is not a Promise.
ban-comma-operator - Disallows the comma operator to be used.
ban - Bans the use of specific functions or global methods.

curly - Enforces braces for if/for/do/while statements.
forin - Requires a for … in statement to be filtered with an if statement.
function-constructor - Prevents using the built-in Function constructor.
import-blacklist - Disallows importing the specified modules via import and require, or importing specific named exports of the specified modules.
label-position - Only allows labels in sensible locations.
no-arg - Disallows use of arguments.callee.
no-bitwise - Disallows bitwise operators.
no-conditional-assignment - Disallows any type of assignment in conditionals.
no-console - Bans the use of specified console methods.
no-construct - Disallows access to the constructors of String, Number, and Boolean.
no-debugger - Disallows debugger statements.
no-duplicate-super - Warns if ‘super()’ appears twice in a constructor.
no-duplicate-switch-case - Prevents duplicate cases in switch statements.
no-duplicate-variable - Disallows duplicate variable declarations in the same block scope.
no-dynamic-delete - Bans usage of the delete operator with computed key expressions.
no-empty - Disallows empty blocks.
no-eval - Disallows eval function invocations.

Requires Type Info
no-floating-promises - Promises returned by functions must be handled appropriately.
Requires Type Info
no-for-in-array - Disallows iterating over an array with a for-in loop.
no-implicit-dependencies - Disallows importing modules that are not listed as dependency in the project’s package.json

Requires Type Info
no-inferred-empty-object-type - Disallow type inference of {} (empty object type) at function and constructor call sites
no-invalid-template-strings - Warns on use of ${ in non-template strings.
no-invalid-this - Disallows using the this keyword outside of classes.

no-misused-new - Warns on apparent attempts to define constructors for interfaces or new for classes.

no-null-keyword - Disallows use of the null keyword literal.

no-object-literal-type-assertion - Forbids an object literal to appear in a type assertion expression. Casting to any or to unknown is still allowed.

no-return-await - Disallows unnecessary return await.
no-shadowed-variable - Disallows shadowing variable declarations.
no-sparse-arrays - Forbids array literals to contain missing elements.

no-string-literal - Forbids unnecessary string literal property access. Allows obj[“prop-erty”] (can’t be a regular property access). Disallows obj[“property”] (should be obj.property).

no-string-throw - Flags throwing plain strings or concatenations of strings.
no-submodule-imports - Disallows importing any submodule.
no-switch-case-fall-through - Disallows falling through case statements.
no-this-assignment - Disallows unnecessary references to this.

Requires Type Info
no-unbound-method - Warns when a method is used outside of a method call.
no-unnecessary-class - Disallows classes that are not strictly necessary.

Requires Type Info
no-unsafe-any - Warns when using an expression of type ‘any’ in a dynamic way. Uses are only allowed if they would work for {} | null | undefined. Type casts and tests are allowed. Expressions that work on all values (such as “” + x) are allowed.
no-unsafe-finally - Disallows control flow statements, such as return, continue, break and throws in finally blocks.
no-unused-expression - Disallows unused expression statements.

Requires Type Info
no-unused-variable - Disallows unused imports, variables, functions and private class members. Similar to tsc’s –noUnusedParameters and –noUnusedLocals options, but does not interrupt code compilation.
Requires Type Info
no-use-before-declare - Disallows usage of variables before their declaration.

no-var-keyword - Disallows usage of the var keyword.
Requires Type Info
no-void-expression - Requires expressions of type void to appear in statement position.
prefer-conditional-expression - Recommends to use a conditional expression instead of assigning to the same thing in each branch of an if statement.

prefer-object-spread - Enforces the use of the ES2018 object spread operator over Object.assign() where appropriate.
radix - Requires the radix parameter to be specified when calling parseInt.
Requires Type Info
restrict-plus-operands - When adding two variables, operands must both be of type number or of type string.

Requires Type Info
strict-boolean-expressions - Restricts the types allowed in boolean expressions. By default only booleans are allowed. The following nodes are checked:
Arguments to the !, &&, and || operators
The condition in a conditional expression (cond ? x : y)
Conditions for if, for, while, and do-while statements.

Requires Type Info
strict-type-predicates - Warns for type predicates that are always true or always false. Works for ‘typeof’ comparisons to constants (e.g. ‘typeof foo === “string”’), and equality comparison to ‘null’/’undefined’. (TypeScript won’t let you compare ‘1 === 2’, but it has an exception for ‘1 === undefined’.) Does not yet work for ‘instanceof’. Does not warn for ‘if (x.y)’ where ‘x.y’ is always truthy. For that, see strict-boolean-expressions. This rule requires strictNullChecks to work properly.
switch-default - Require a default case in all switch statements.
triple-equals - Requires === and !== in place of == and !=.
typeof-compare - Makes sure result of typeof is compared to correct string values
unnecessary-constructor - Prevents blank constructors, as they are redundant.

Requires Type Info
use-default-type-parameter - Warns if an explicitly specified type argument is the default for that type parameter.
use-isnan - Enforces use of the isNaN() function to check for NaN references instead of a comparison to the NaN constant.
Maintainability
These rules make code maintenance easier:

cyclomatic-complexity - Enforces a threshold of cyclomatic complexity.
Requires Type Info
deprecation - Warns when deprecated APIs are used.

eofline - Ensures the file ends with a newline.

indent - Enforces indentation with tabs or spaces.

linebreak-style - Enforces a consistent linebreak style.
max-classes-per-file - A file may not contain more than the specified number of classes
max-file-line-count - Requires files to remain under a certain number of lines
max-line-length - Requires lines to be under a certain max length.
no-default-export - Disallows default exports in ES6-style modules.
no-default-import - Disallows importing default members from certain ES6-style modules.
no-duplicate-imports - Disallows multiple import statements from the same module.

no-mergeable-namespace - Disallows mergeable namespaces in the same file.
no-require-imports - Disallows invocation of require().
object-literal-sort-keys - Checks ordering of keys in object literals. When using the default alphabetical ordering, additional blank lines may be used to group object properties together while keeping the elements within each group in alphabetical order.

prefer-const - Requires that variable declarations use const instead of let and var if possible.

Requires Type Info
prefer-readonly - Requires that private variables are marked as readonly if they’re never modified outside of the constructor.

trailing-comma - Requires or disallows trailing commas in array and object literals, destructuring assignments, function typings, named imports and exports and function parameters.
Style
These rules enforce consistent style across your codebase:

align - Enforces vertical alignment.

array-type - Requires using either ‘T[]’ or ‘Array’ for arrays.

arrow-parens - Requires parentheses around the parameters of arrow function definitions.

arrow-return-shorthand - Suggests to convert () => { return x; } to () => x.
binary-expression-operand-order - In a binary expression, a literal should always be on the right-hand side if possible. For example, prefer ‘x + 1’ over ‘1 + x’.

callable-types - An interface or literal type with just a call signature can be written as a function type.
class-name - Enforces PascalCased class and interface names.

comment-format - Enforces formatting rules for single-line comments.
comment-type - Allows a limited set of comment types
Requires Type Info
completed-docs - Enforces JSDoc comments for important items be filled out.
encoding - Enforces UTF-8 file encoding.

file-header - Enforces a certain header comment for all files, matched by a regular expression.
file-name-casing - Enforces a consistent file naming convention
import-spacing - Ensures proper spacing between import statement keywords
increment-decrement - Enforces using explicit += 1 or -= 1 operators.

interface-name - Requires interface names to begin with a capital ‘I’

interface-over-type-literal - Prefer an interface declaration over a type literal (type T = { … })
jsdoc-format - Enforces basic format rules for JSDoc comments.

Requires Type Info
match-default-export-name - Requires that a default import have the same name as the declaration it imports. Does nothing for anonymous default exports.
newline-before-return - Enforces blank line before return when not the only line in the block.
newline-per-chained-call - Requires that chained method calls be broken apart onto separate lines.
new-parens - Requires parentheses when invoking a constructor via the new keyword.

no-angle-bracket-type-assertion - Requires the use of as Type for type assertions instead of .

Requires Type Info
no-boolean-literal-compare - Warns on comparison to a boolean literal, as in x === true.

no-consecutive-blank-lines - Disallows one or more blank lines in a row.

no-irregular-whitespace - Disallow irregular whitespace within a file, including strings and comments.

no-parameter-properties - Disallows parameter properties in class constructors.

no-redundant-jsdoc - Forbids JSDoc which duplicates TypeScript functionality.

no-reference-import - Don’t if you import foo anyway.

no-trailing-whitespace - Disallows trailing whitespace at the end of a line.
no-unnecessary-callback-wrapper - Replaces x => f(x) with just f. To catch more cases, enable only-arrow-functions and arrow-return-shorthand too.

no-unnecessary-initializer - Forbids a ‘var’/’let’ statement or destructuring initializer to be initialized to ‘undefined’.

Requires Type Info
no-unnecessary-qualifier - Warns when a namespace qualifier (A.x) is unnecessary.
number-literal-format - Checks that decimal literals should begin with ‘0.’ instead of just ‘.’, and should not end with a trailing ‘0’.

object-literal-key-quotes - Enforces consistent object literal property quote style.

object-literal-shorthand - Enforces/disallows use of ES6 object literal shorthand.

one-line - Requires the specified tokens to be on the same line as the expression preceding them.
one-variable-per-declaration - Disallows multiple variable definitions in the same declaration statement.

ordered-imports - Requires that import statements be alphabetized and grouped.
prefer-function-over-method - Warns for class methods that do not use ‘this’.

prefer-method-signature - Prefer foo(): void over foo: () => void in interfaces and types.
prefer-switch - Prefer a switch statement to an if statement with simple === comparisons.
prefer-template - Prefer a template expression over string literal concatenation.

prefer-while - Prefer while loops instead of for loops without an initializer and incrementor.

quotemark - Enforces quote character for string literals.
Requires Type Info
return-undefined - Prefer return; in void functions and return undefined; in value-returning functions.

semicolon - Enforces consistent semicolon usage at the end of every statement.

space-before-function-paren - Require or disallow a space before function parenthesis

space-within-parens - Enforces spaces within parentheses or disallow them. Empty parentheses () are always allowed.

switch-final-break - Checks whether the final clause of a switch statement ends in break;.

type-literal-delimiter - Checks that type literal members are separated by semicolons. Enforces a trailing semicolon for multiline type literals.
Requires Type Info
unnecessary-bind - Prevents unnecessary and/or misleading scope bindings on functions.
variable-name - Checks variable names for various errors.

whitespace - Enforces whitespace style conventions.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: tslint.json 文件是 TypeScript 项目中使用 TSLint配置文件。它包含了 TSLint配置选项,比如规则集合、编码规范等。 常用的配置项有: - "extends": 指定 TSLint 配置的继承来源,可以是一个预定义的配置,也可以是一个文件路径。 - "rulesDirectory": 指定 TSLint 规则的路径,可以是一个文件夹路径或者是一个文件路径。 - "rules": 指定 TSLint规则,包括规则名称和配置。 示例: ``` { "extends": ["tslint:recommended"], "rulesDirectory": ["./custom-rules/", "./node_modules/custom-rule-lib/"], "rules": { "semicolon": [true, "never"], "custom-rule-name": [true, "some-option-value"] } } ``` 上述配置将继承 tslint:recommended 规则库中的规则,并且在./custom-rules/和./node_modules/custom-rule-lib/ 中寻找自定义的规则,其中设置了分号检查和custom-rule-name规则。 ### 回答2: tslint.json 是一个 TypeScript 的 lint 配置文件,用来定义代码规范和静态代码分析的规则。 在 tslint.json 文件中,包含了很多可以配置规则项,用于指定代码中哪些规则需要被检查和要求。这些规则可以帮助我们发现代码中的潜在问题和错误,提高代码质量和可维护性。 一些常见的配置项包括: 1. "extends":指定继承自哪些预设的配置,可以从其他的配置文件中继承规则,便于共享和重用。 2. "rules":定义具体的规则和检查项。可以通过设置规则的值来开启或关闭某个规则,以及设置规则的严格程度。 3. "linterOptions":提供一些额外的选项配置,例如指定要忽略的文件或目录,设置代码样式的一致性。 4. "exclude":指定不需要进行检查的文件或目录,用于排除一些不必要的文件或自动生成的代码。 5. "rulesDirectory":指定自定义规则的目录,可以引入第三方的 lint 规则。 通过适当配置 tslint.json 文件,我们可以根据项目的需求和团队的实际情况,定义适合自己团队的代码规范和检查规则。这样做有助于提高代码质量、减少错误,并增加代码的可读性和可维护性。 ### 回答3: tslint.json 是用于配置 TypeScript 代码检查工具 TSLint配置文件。以下是一些常见的 TSLint 配置项: 1. "extends": 用于继承其他配置文件,可以通过字符串指定其他配置文件路径,也可以是一个数组,表示继承多个配置文件。 2. "rules": 用于配置具体的规则。每个规则都有一个键值对的形式,键为规则名称,值为规则配置。可以设置规则是否开启、开启级别等。 3. "rulesDirectory": 用于指定规则所在的文件夹路径。 4. "defaultSeverity": 默认的规则级别,可以设置为 "error"、"warning" 或 "off"。 5. "jsRules": 配置 JavaScript 文件的规则,用法和 "rules" 相同。 6. "linterOptions": 用于配置其他检查器选项,如解析器、文件编码等。 7. "exclude": 用于排除某些文件或文件夹不进行检查,可以使用通配符进行匹配。 8. "include": 用于指定需要进行检查的文件或文件夹,同样可以使用通配符。 9. "extends": 用于扩展 TSLint 核心规则。可以是可共享配置文件(如 "@tslint/eslint-config-recommended"),也可以是自定义规则路径。 通过配置 tslint.json 文件,可以根据项目需求来自定义和调整 TSLint 的检查规则,以保证代码质量和规范。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值