diff --git a/.editorconfig b/.editorconfig index 4714976..8d43e56 100644 --- a/.editorconfig +++ b/.editorconfig @@ -6,6 +6,7 @@ indent_size = 4 trim_trailing_whitespace = true end_of_line = lf insert_final_newline = true +quote_type = double # yarn / npm uses 2 spaces when it dumps the file after a change [package.json] diff --git a/.github/ISSUE_TEMPLATE/bug-report.md b/.github/ISSUE_TEMPLATE/bug-report.md index f81328b..1020297 100644 --- a/.github/ISSUE_TEMPLATE/bug-report.md +++ b/.github/ISSUE_TEMPLATE/bug-report.md @@ -1,20 +1,21 @@ --- name: Bug report about: Report an issue -labels: - +labels: --- **Repro** + + ```JSON { "rules": { @@ -27,16 +28,12 @@ Please try to avoid code that isn't directly related to the bug, as it makes it // your repro code case ``` - **Expected Result** - **Actual Result** - **Additional Info** - **Versions** | package | version | diff --git a/.github/ISSUE_TEMPLATE/feature_request.md b/.github/ISSUE_TEMPLATE/feature_request.md index bed0c05..22eb9cd 100644 --- a/.github/ISSUE_TEMPLATE/feature_request.md +++ b/.github/ISSUE_TEMPLATE/feature_request.md @@ -1,14 +1,14 @@ --- name: Feature request about: Suggest an idea for this project -labels: - +labels: --- I'd like to propose a **new rule / change to an existing rule**. **Description** - diff --git a/.github/PULL_REQUEST_TEMPLATE.md b/.github/PULL_REQUEST_TEMPLATE.md index 1857154..64c319d 100644 --- a/.github/PULL_REQUEST_TEMPLATE.md +++ b/.github/PULL_REQUEST_TEMPLATE.md @@ -1,11 +1,13 @@ -- [ ] I’ve filled out the documentation, including all relevant sections and a link to the equivalent TSLint rule if available. -- [ ] I’ve filled out the rule’s category -- [ ] I’ve added documentation for the equivalent TSLint rule (in `extraDescription`) -- [ ] I’ve added the rule documentation link via the helper from `../utils`. -- [ ] I’ve added the rule to the [roadmap](https://github.com/bradzacher/eslint-plugin-typescript/blob/master/ROADMAP.md) + +- [ ] I’ve filled out the documentation, including all relevant sections and a link to the equivalent TSLint rule if available. +- [ ] I’ve filled out the rule’s category +- [ ] I’ve added documentation for the equivalent TSLint rule (in `extraDescription`) +- [ ] I’ve added the rule documentation link via the helper from `../utils`. +- [ ] I’ve added the rule to the [roadmap](https://github.com/bradzacher/eslint-plugin-typescript/blob/master/ROADMAP.md) -- [ ] I’ve added tests for my changes + +- [ ] I’ve added tests for my changes diff --git a/.prettierrc b/.prettierrc deleted file mode 100644 index 22c4bc6..0000000 --- a/.prettierrc +++ /dev/null @@ -1,11 +0,0 @@ -{ - "arrowParens": "avoid", - "bracketSpacing": true, - "jsxBracketSameLine": true, - "proseWrap": "preserve", - "semi": true, - "singleQuote": false, - "tabWidth": 4, - "trailingComma": "es5", - "useTabs": false -} diff --git a/.prettierrc.yml b/.prettierrc.yml new file mode 100644 index 0000000..e3385f3 --- /dev/null +++ b/.prettierrc.yml @@ -0,0 +1,2 @@ +jsxBracketSameLine: true +trailingComma: es5 diff --git a/.travis.yml b/.travis.yml index 2c31c73..e4fb4e6 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,13 +1,13 @@ language: node_js sudo: false node_js: - - "6" - - "8" - - "10" - - "11" + - "6" + - "8" + - "10" + - "11" install: yarn script: - - yarn lint - - yarn format-check - - yarn docs:check - - yarn test + - yarn lint + - yarn format-check + - yarn docs:check + - yarn test diff --git a/.vscode/extensions.json b/.vscode/extensions.json index 4ccda4d..7a76792 100644 --- a/.vscode/extensions.json +++ b/.vscode/extensions.json @@ -4,8 +4,5 @@ "dbaeumer.vscode-eslint", "editorconfig.editorconfig" ], - "unwantedRecommendations": [ - "hookyqr.beautify", - "dbaeumer.jshint" - ] + "unwantedRecommendations": ["hookyqr.beautify", "dbaeumer.jshint"] } diff --git a/.vscode/settings.json b/.vscode/settings.json index 93f6e91..a9182a8 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -1,9 +1,6 @@ { // An array of languages where Emmet abbreviations should not be expanded. - "emmet.excludeLanguages": [ - "markdown", - "typescript" - ], + "emmet.excludeLanguages": ["markdown", "typescript"], // An array of language ids which should be validated by ESLint "eslint.validate": [ @@ -31,5 +28,5 @@ "typescript.preferences.importModuleSpecifier": "auto", "javascript.preferences.quoteStyle": "double", "typescript.preferences.quoteStyle": "double", - "typescript.updateImportsOnFileMove.enabled": "always", + "typescript.updateImportsOnFileMove.enabled": "always" } diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 6a1e803..cf6dea2 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -2,21 +2,21 @@ ## Commenting and Discussing -***Don't be a douche.*** +**_Don't be a douche._** Consider the following when commenting: -- Everyone is a human (except for the bots). -- Everyone is freely providing their time for this project. -- Everyone wants to build the best tool. +- Everyone is a human (except for the bots). +- Everyone is freely providing their time for this project. +- Everyone wants to build the best tool. ## PR Requirements For a PR to be merged, you must pass the following checks: -- there must be no lint errors - `yarn lint` -- the code must be formatted - `yarn format` -- your documentation must be up to date - `yarn docs-check` -- all tests must pass - `yarn test` +- there must be no lint errors - `yarn lint` +- the code must be formatted - `yarn format` +- your documentation must be up to date - `yarn docs-check` +- all tests must pass - `yarn test` There is a commit hook which will help you follow this. Travis will also automatically run these checks when you submit your PR (and will block us merging until you fix it). @@ -25,22 +25,22 @@ Travis will also automatically run these checks when you submit your PR (and wil When adding or changing a rule, you must: -- Ensure your feature / bug has an issue behind it. - - This just makes it easier for people to find information in future, because PRs aren't included in the default issue search. -- Ensure your changes are covered by tests. - - There's no hard and fast rule for how much testing is required, but try to cover as many bases as you can. -- Ensure your changes are documented in the `docs` folder. We're working to standardise how we document rules, but your docs should: - - describe what the rule does, and why you might enable it. - - (if any) outline the settings; how to configure them and what each one does - - have clear examples of valid and invalid code when using the rule. Bonus points for extra cases showing what each setting does. +- Ensure your feature / bug has an issue behind it. + - This just makes it easier for people to find information in future, because PRs aren't included in the default issue search. +- Ensure your changes are covered by tests. + - There's no hard and fast rule for how much testing is required, but try to cover as many bases as you can. +- Ensure your changes are documented in the `docs` folder. We're working to standardise how we document rules, but your docs should: + - describe what the rule does, and why you might enable it. + - (if any) outline the settings; how to configure them and what each one does + - have clear examples of valid and invalid code when using the rule. Bonus points for extra cases showing what each setting does. When adding a rule, you must also add a link to the rule in the README.md. ## Submitting Issues -- If your issue relates to a rule, start your title with the rule name: - - `[no-unused-vars] False positive when fooing the bar` -- Search for the issue before you ask; we try hard to ensure it's easy to find existing issues. -- Follow the template. - - We've built it to reduce the chance of us going back to ask you for things. - - Don't be lazy and skip parts of it; we'll just ask you for that information anyway, so it'll only delay the process. +- If your issue relates to a rule, start your title with the rule name: + - `[no-unused-vars] False positive when fooing the bar` +- Search for the issue before you ask; we try hard to ensure it's easy to find existing issues. +- Follow the template. + - We've built it to reduce the chance of us going back to ask you for things. + - Don't be lazy and skip parts of it; we'll just ask you for that information anyway, so it'll only delay the process. diff --git a/docs/rules/adjacent-overload-signatures.md b/docs/rules/adjacent-overload-signatures.md index 2be48fb..3a50db7 100644 --- a/docs/rules/adjacent-overload-signatures.md +++ b/docs/rules/adjacent-overload-signatures.md @@ -17,24 +17,24 @@ declare namespace Foo { } type Foo = { - foo(s: string): void; - foo(n: number): void; - bar(): void; - foo(sn: string | number): void; -} + foo(s: string): void; + foo(n: number): void; + bar(): void; + foo(sn: string | number): void; +}; interface Foo { - foo(s: string): void; - foo(n: number): void; - bar(): void; - foo(sn: string | number): void; + foo(s: string): void; + foo(n: number): void; + bar(): void; + foo(sn: string | number): void; } class Foo { - foo(s: string): void; - foo(n: number): void; - bar(): void {} - foo(sn: string | number): void {} + foo(s: string): void; + foo(n: number): void; + bar(): void {} + foo(sn: string | number): void {} } export function foo(s: string): void; @@ -54,24 +54,24 @@ declare namespace Foo { } type Foo = { - foo(s: string): void; - foo(n: number): void; - foo(sn: string | number): void; - bar(): void; -} + foo(s: string): void; + foo(n: number): void; + foo(sn: string | number): void; + bar(): void; +}; interface Foo { - foo(s: string): void; - foo(n: number): void; - foo(sn: string | number): void; - bar(): void; + foo(s: string): void; + foo(n: number): void; + foo(sn: string | number): void; + bar(): void; } class Foo { - foo(s: string): void; - foo(n: number): void; - foo(sn: string | number): void {} - bar(): void {} + foo(s: string): void; + foo(n: number): void; + foo(sn: string | number): void {} + bar(): void {} } export function bar(): void; @@ -86,4 +86,4 @@ If you don't care about the general structure of the code, then you will not nee ## Compatibility -* TSLint: [adjacent-overload-signatures](https://palantir.github.io/tslint/rules/adjacent-overload-signatures/) +- TSLint: [adjacent-overload-signatures](https://palantir.github.io/tslint/rules/adjacent-overload-signatures/) diff --git a/docs/rules/array-type.md b/docs/rules/array-type.md index 7c328e9..e7f401f 100644 --- a/docs/rules/array-type.md +++ b/docs/rules/array-type.md @@ -1,11 +1,12 @@ # Requires using either `T[]` or `Array` for arrays (array-type) ```ts -class Foo>> extends Bar> implements Baz> { - private s: Array +class Foo>> extends Bar> + implements Baz> { + private s: Array; - constructor (p: Array) { - return new Array() + constructor(p: Array) { + return new Array(); } } ``` @@ -24,10 +25,10 @@ Default config: } ``` -- `array` enforces use of `T[]` for all types `T`. -- `generic` enforces use of `Array` for all types `T`. -- `array-simple` enforces use of `T[]` if `T` is a simple type. +- `array` enforces use of `T[]` for all types `T`. +- `generic` enforces use of `Array` for all types `T`. +- `array-simple` enforces use of `T[]` if `T` is a simple type. ## Related to -* TSLint: [array-type](https://palantir.github.io/tslint/rules/array-type/) +- TSLint: [array-type](https://palantir.github.io/tslint/rules/array-type/) diff --git a/docs/rules/ban-types.md b/docs/rules/ban-types.md index 263cc0d..655ecae 100644 --- a/docs/rules/ban-types.md +++ b/docs/rules/ban-types.md @@ -8,11 +8,10 @@ Examples of **incorrect** code for this rule `"String": "Use string instead"` ```ts class Foo extends Bar implements Baz { - constructor (foo: String) { - } + constructor(foo: String) {} - exit () : Array { - const foo: String = 1 as String + exit(): Array { + const foo: String = 1 as String; } } ``` @@ -21,16 +20,16 @@ Examples of **correct** code for this rule `"String": "Use string instead"` ```ts class Foo extends Bar implements Baz { - constructor (foo: string) { - } + constructor(foo: string) {} - exit () : Array { - const foo: string = 1 as string + exit(): Array { + const foo: string = 1 as string; } } ``` ## Options + ```CJSON { "typescript/ban-types": ["error", { @@ -52,22 +51,25 @@ class Foo extends Bar implements Baz { ``` ### Example + ```json { - "typescript/ban-types": ["error", { - "types": { - "Array": null, - "Object": "Use {} instead", - "String": { - "message": "Use string instead", - "fixWith": "string" + "typescript/ban-types": [ + "error", + { + "types": { + "Array": null, + "Object": "Use {} instead", + "String": { + "message": "Use string instead", + "fixWith": "string" + } } } - }] + ] } ``` - ## Compatibility -* TSLint: [ban-types](https://palantir.github.io/tslint/rules/ban-types/) +- TSLint: [ban-types](https://palantir.github.io/tslint/rules/ban-types/) diff --git a/docs/rules/camelcase.md b/docs/rules/camelcase.md index a1bc806..af3904c 100644 --- a/docs/rules/camelcase.md +++ b/docs/rules/camelcase.md @@ -15,8 +15,8 @@ thrown. This rule only flags definitions and assignments but not function calls. In case of ES6 `import` statements, this rule only targets the name of the variable that will be imported into the local module scope. -***This rule was taken from the ESLint core rule `camelcase`.*** -***Available options and test cases may vary depending on the version of ESLint installed in the system.*** +**_This rule was taken from the ESLint core rule `camelcase`._** +**_Available options and test cases may vary depending on the version of ESLint installed in the system._** ## Options @@ -30,11 +30,11 @@ variable that will be imported into the local module scope. This rule has an object option: -* `"properties": "always"` (default) enforces camelcase style for property names -* `"properties": "never"` does not check property names -* `"ignoreDestructuring": false` (default) enforces camelcase style for destructured identifiers -* `"ignoreDestructuring": true` does not check destructured identifiers -* `allow` (`string[]`) list of properties to accept. Accept regex. +- `"properties": "always"` (default) enforces camelcase style for property names +- `"properties": "never"` does not check property names +- `"ignoreDestructuring": false` (default) enforces camelcase style for destructured identifiers +- `"ignoreDestructuring": true` does not check destructured identifiers +- `allow` (`string[]`) list of properties to accept. Accept regex. ### properties: "always" @@ -43,7 +43,7 @@ Examples of **incorrect** code for this rule with the default `{ "properties": " ```js /*eslint typescript/camelcase: "error"*/ -import { no_camelcased } from "external-module" +import { no_camelcased } from "external-module"; var my_favorite_color = "#112C85"; @@ -57,18 +57,18 @@ obj.do_something = function() { function foo({ no_camelcased }) { // ... -}; +} function foo({ isCamelcased: no_camelcased }) { // ... } -function foo({ no_camelcased = 'default value' }) { +function foo({ no_camelcased = "default value" }) { // ... -}; +} var obj = { - my_pref: 1 + my_pref: 1, }; var { category_id = 1 } = query; @@ -85,9 +85,9 @@ Examples of **correct** code for this rule with the default `{ "properties": "al import { no_camelcased as camelCased } from "external-module"; -var myFavoriteColor = "#112C85"; -var _myFavoriteColor = "#112C85"; -var myFavoriteColor_ = "#112C85"; +var myFavoriteColor = "#112C85"; +var _myFavoriteColor = "#112C85"; +var myFavoriteColor_ = "#112C85"; var MY_FAVORITE_COLOR = "#112C85"; var foo = bar.baz_boom; var foo = { qux: bar.baz_boom }; @@ -100,22 +100,21 @@ var { category_id: category } = query; function foo({ isCamelCased }) { // ... -}; +} function foo({ isCamelCased: isAlsoCamelCased }) { // ... } -function foo({ isCamelCased = 'default value' }) { +function foo({ isCamelCased = "default value" }) { // ... -}; +} var { categoryId = 1 } = query; var { foo: isCamelCased } = bar; var { foo: isCamelCased = 1 } = quz; - ``` ### properties: "never" @@ -126,7 +125,7 @@ Examples of **correct** code for this rule with the `{ "properties": "never" }` /*eslint typescript/camelcase: ["error", {properties: "never"}]*/ var obj = { - my_pref: 1 + my_pref: 1, }; ``` diff --git a/docs/rules/class-name-casing.md b/docs/rules/class-name-casing.md index c4e9ff9..28a0bb8 100644 --- a/docs/rules/class-name-casing.md +++ b/docs/rules/class-name-casing.md @@ -9,37 +9,25 @@ This rule aims to make it easy to differentiate classes from regular variables a Examples of **incorrect** code for this rule: ```ts +class invalidClassName {} -class invalidClassName { +class Another_Invalid_Class_Name {} -} - -class Another_Invalid_Class_Name { - -} - -var bar = class invalidName {} +var bar = class invalidName {}; interface someInterface {} - ``` Examples of **correct** code for this rule: ```ts +class ValidClassName {} -class ValidClassName { - -} - -export default class { - -} +export default class {} var foo = class {}; interface SomeInterface {} - ``` ## When Not To Use It @@ -49,4 +37,4 @@ you use a different type of casing. ## Further Reading -* [`class-name`](https://palantir.github.io/tslint/rules/class-name/) in [TSLint](https://palantir.github.io/tslint/) +- [`class-name`](https://palantir.github.io/tslint/rules/class-name/) in [TSLint](https://palantir.github.io/tslint/) diff --git a/docs/rules/explicit-function-return-type.md b/docs/rules/explicit-function-return-type.md index a6f1ac1..49de437 100644 --- a/docs/rules/explicit-function-return-type.md +++ b/docs/rules/explicit-function-return-type.md @@ -20,10 +20,10 @@ function test() { // Should indicate that a number is returned var fn = function() { return 1; -} +}; // Should indicate that a string is returned -var arrowFn = () => 'test'; +var arrowFn = () => "test"; class Test { // Should indicate that no value is returned (void) @@ -44,10 +44,10 @@ function test(): void { // A return value of type number var fn = function(): number { return 1; -} +}; // A return value of type string -var arrowFn = (): string => 'test'; +var arrowFn = (): string => "test"; class Test { // No return value should be expected (void) @@ -61,7 +61,7 @@ class Test { The rule accepts an options object with the following properties: -* `allowExpressions` if true, only functions which are part of a declaration will be checked +- `allowExpressions` if true, only functions which are part of a declaration will be checked By default, `allowExpressions: false` is used, meaning all declarations and expressions _must_ have a return type. @@ -71,16 +71,15 @@ expressions _must_ have a return type. Examples of **incorrect** code for this rule with `{ allowExpressions: true }`: ```ts -function test() { -} +function test() {} ``` Examples of **correct** code for this rule with `{ allowExpressions: true }`: ```ts -node.addEventListener('click', () => {}); +node.addEventListener("click", () => {}); -node.addEventListener('click', function() {}); +node.addEventListener("click", function() {}); const foo = arr.map(i => i * i); ``` @@ -92,4 +91,4 @@ you will not need this rule. ## Further Reading -* TypeScript [Functions](https://www.typescriptlang.org/docs/handbook/functions.html#function-types) +- TypeScript [Functions](https://www.typescriptlang.org/docs/handbook/functions.html#function-types) diff --git a/docs/rules/explicit-member-accessibility.md b/docs/rules/explicit-member-accessibility.md index 1cee08d..ff491b7 100644 --- a/docs/rules/explicit-member-accessibility.md +++ b/docs/rules/explicit-member-accessibility.md @@ -14,8 +14,8 @@ The following patterns are considered warnings: ```ts class Animal { - name: string // No accessibility modifier - getName (): string {} // No accessibility modifier + name: string; // No accessibility modifier + getName(): string {} // No accessibility modifier } ``` @@ -23,8 +23,8 @@ The following patterns are not warnings: ```ts class Animal { - private name: string // explicit accessibility modifier - public getName (): string {} // explicit accessibility modifier + private name: string; // explicit accessibility modifier + public getName(): string {} // explicit accessibility modifier } ``` @@ -35,8 +35,8 @@ this rule. ## Further Reading -* TypeScript [Accessibility Modifiers](https://www.typescriptlang.org/docs/handbook/classes.html#public-private-and-protected-modifiers) +- TypeScript [Accessibility Modifiers](https://www.typescriptlang.org/docs/handbook/classes.html#public-private-and-protected-modifiers) ## Compatibility -* TSLint: [member-access](http://palantir.github.io/tslint/rules/member-access/) +- TSLint: [member-access](http://palantir.github.io/tslint/rules/member-access/) diff --git a/docs/rules/generic-type-naming.md b/docs/rules/generic-type-naming.md index a13513d..5e3f13c 100644 --- a/docs/rules/generic-type-naming.md +++ b/docs/rules/generic-type-naming.md @@ -15,28 +15,28 @@ Examples of **correct** code with a configuration of `'^T[A-Z][a-zA-Z]+$'`: ```typescript type ReadOnly = { - readonly [TKey in keyof TType]: TType[TKey]; -} + readonly [TKey in keyof TType]: TType[TKey] +}; interface SimpleMap { - [key: string]: TValue; + [key: string]: TValue; } ``` Examples of **incorrect** code with a configuration of `'^T[A-Z][a-zA-Z]+$'`: ```typescript -type ReadOnly = { - readonly [Key in keyof T]: T[Key]; -} +type ReadOnly = { readonly [Key in keyof T]: T[Key] }; interface SimpleMap { - [key: string]: T; + [key: string]: T; } ``` ## When Not To Use It + If you do not want to enforce a naming convention for type variables. ## Further Reading -- [TypeScript Generics](https://www.typescriptlang.org/docs/handbook/generics.html) + +- [TypeScript Generics](https://www.typescriptlang.org/docs/handbook/generics.html) diff --git a/docs/rules/indent.md b/docs/rules/indent.md index 7c153a5..25d30fc 100644 --- a/docs/rules/indent.md +++ b/docs/rules/indent.md @@ -4,17 +4,17 @@ There are several common guidelines which require specific indentation of nested ```js function hello(indentSize, type) { - if (indentSize === 4 && type !== 'tab') { - console.log('Each next indentation will increase on 4 spaces'); + if (indentSize === 4 && type !== "tab") { + console.log("Each next indentation will increase on 4 spaces"); } } ``` These are the most common scenarios recommended in different style guides: -* Two spaces, not longer and no tabs: Google, npm, Node.js, Idiomatic, Felix -* Tabs: jQuery -* Four spaces: Crockford +- Two spaces, not longer and no tabs: Google, npm, Node.js, Idiomatic, Felix +- Tabs: jQuery +- Four spaces: Crockford ## Rule Details @@ -46,6 +46,7 @@ Or for tabbed indentation: Examples of **incorrect** code for this rule with the default options: + ```js /*eslint typescript/indent: "error"*/ @@ -59,6 +60,7 @@ if (a) { Examples of **correct** code for this rule with the default options: + ```js /*eslint typescript/indent: "error"*/ @@ -72,46 +74,47 @@ if (a) { This rule has an object option: -* `"SwitchCase"` (default: 0) enforces indentation level for `case` clauses in `switch` statements -* `"VariableDeclarator"` (default: 1) enforces indentation level for `var` declarators; can also take an object to define separate rules for `var`, `let` and `const` declarations. -* `"outerIIFEBody"` (default: 1) enforces indentation level for file-level IIFEs. -* `"MemberExpression"` (default: 1) enforces indentation level for multi-line property chains. This can also be set to `"off"` to disable checking for MemberExpression indentation. -* `"FunctionDeclaration"` takes an object to define rules for function declarations. - * `parameters` (default: 1) enforces indentation level for parameters in a function declaration. This can either be a number indicating indentation level, or the string `"first"` indicating that all parameters of the declaration must be aligned with the first parameter. This can also be set to `"off"` to disable checking for FunctionDeclaration parameters. - * `body` (default: 1) enforces indentation level for the body of a function declaration. -* `"FunctionExpression"` takes an object to define rules for function expressions. - * `parameters` (default: 1) enforces indentation level for parameters in a function expression. This can either be a number indicating indentation level, or the string `"first"` indicating that all parameters of the expression must be aligned with the first parameter. This can also be set to `"off"` to disable checking for FunctionExpression parameters. - * `body` (default: 1) enforces indentation level for the body of a function expression. -* `"CallExpression"` takes an object to define rules for function call expressions. - * `arguments` (default: 1) enforces indentation level for arguments in a call expression. This can either be a number indicating indentation level, or the string `"first"` indicating that all arguments of the expression must be aligned with the first argument. This can also be set to `"off"` to disable checking for CallExpression arguments. -* `"ArrayExpression"` (default: 1) enforces indentation level for elements in arrays. It can also be set to the string `"first"`, indicating that all the elements in the array should be aligned with the first element. This can also be set to `"off"` to disable checking for array elements. -* `"ObjectExpression"` (default: 1) enforces indentation level for properties in objects. It can be set to the string `"first"`, indicating that all properties in the object should be aligned with the first property. This can also be set to `"off"` to disable checking for object properties. -* `"ImportDeclaration"` (default: 1) enforces indentation level for import statements. It can be set to the string `"first"`, indicating that all imported members from a module should be aligned with the first member in the list. This can also be set to `"off"` to disable checking for imported module members. -* `"flatTernaryExpressions": true` (`false` by default) requires no indentation for ternary expressions which are nested in other ternary expressions. -* `"ignoredNodes"` accepts an array of [selectors](/docs/developer-guide/selectors.md). If an AST node is matched by any of the selectors, the indentation of tokens which are direct children of that node will be ignored. This can be used as an escape hatch to relax the rule if you disagree with the indentation that it enforces for a particular syntactic pattern. -* `"ignoreComments"` (default: false) can be used when comments do not need to be aligned with nodes on the previous or next line. +- `"SwitchCase"` (default: 0) enforces indentation level for `case` clauses in `switch` statements +- `"VariableDeclarator"` (default: 1) enforces indentation level for `var` declarators; can also take an object to define separate rules for `var`, `let` and `const` declarations. +- `"outerIIFEBody"` (default: 1) enforces indentation level for file-level IIFEs. +- `"MemberExpression"` (default: 1) enforces indentation level for multi-line property chains. This can also be set to `"off"` to disable checking for MemberExpression indentation. +- `"FunctionDeclaration"` takes an object to define rules for function declarations. + - `parameters` (default: 1) enforces indentation level for parameters in a function declaration. This can either be a number indicating indentation level, or the string `"first"` indicating that all parameters of the declaration must be aligned with the first parameter. This can also be set to `"off"` to disable checking for FunctionDeclaration parameters. + - `body` (default: 1) enforces indentation level for the body of a function declaration. +- `"FunctionExpression"` takes an object to define rules for function expressions. + - `parameters` (default: 1) enforces indentation level for parameters in a function expression. This can either be a number indicating indentation level, or the string `"first"` indicating that all parameters of the expression must be aligned with the first parameter. This can also be set to `"off"` to disable checking for FunctionExpression parameters. + - `body` (default: 1) enforces indentation level for the body of a function expression. +- `"CallExpression"` takes an object to define rules for function call expressions. + - `arguments` (default: 1) enforces indentation level for arguments in a call expression. This can either be a number indicating indentation level, or the string `"first"` indicating that all arguments of the expression must be aligned with the first argument. This can also be set to `"off"` to disable checking for CallExpression arguments. +- `"ArrayExpression"` (default: 1) enforces indentation level for elements in arrays. It can also be set to the string `"first"`, indicating that all the elements in the array should be aligned with the first element. This can also be set to `"off"` to disable checking for array elements. +- `"ObjectExpression"` (default: 1) enforces indentation level for properties in objects. It can be set to the string `"first"`, indicating that all properties in the object should be aligned with the first property. This can also be set to `"off"` to disable checking for object properties. +- `"ImportDeclaration"` (default: 1) enforces indentation level for import statements. It can be set to the string `"first"`, indicating that all imported members from a module should be aligned with the first member in the list. This can also be set to `"off"` to disable checking for imported module members. +- `"flatTernaryExpressions": true` (`false` by default) requires no indentation for ternary expressions which are nested in other ternary expressions. +- `"ignoredNodes"` accepts an array of [selectors](/docs/developer-guide/selectors.md). If an AST node is matched by any of the selectors, the indentation of tokens which are direct children of that node will be ignored. This can be used as an escape hatch to relax the rule if you disagree with the indentation that it enforces for a particular syntactic pattern. +- `"ignoreComments"` (default: false) can be used when comments do not need to be aligned with nodes on the previous or next line. Level of indentation denotes the multiple of the indent specified. Example: -* Indent of 4 spaces with `VariableDeclarator` set to `2` will indent the multi-line variable declarations with 8 spaces. -* Indent of 2 spaces with `VariableDeclarator` set to `2` will indent the multi-line variable declarations with 4 spaces. -* Indent of 2 spaces with `VariableDeclarator` set to `{"var": 2, "let": 2, "const": 3}` will indent the multi-line variable declarations with 4 spaces for `var` and `let`, 6 spaces for `const` statements. -* Indent of tab with `VariableDeclarator` set to `2` will indent the multi-line variable declarations with 2 tabs. -* Indent of 2 spaces with `SwitchCase` set to `0` will not indent `case` clauses with respect to `switch` statements. -* Indent of 2 spaces with `SwitchCase` set to `1` will indent `case` clauses with 2 spaces with respect to `switch` statements. -* Indent of 2 spaces with `SwitchCase` set to `2` will indent `case` clauses with 4 spaces with respect to `switch` statements. -* Indent of tab with `SwitchCase` set to `2` will indent `case` clauses with 2 tabs with respect to `switch` statements. -* Indent of 2 spaces with `MemberExpression` set to `0` will indent the multi-line property chains with 0 spaces. -* Indent of 2 spaces with `MemberExpression` set to `1` will indent the multi-line property chains with 2 spaces. -* Indent of 2 spaces with `MemberExpression` set to `2` will indent the multi-line property chains with 4 spaces. -* Indent of 4 spaces with `MemberExpression` set to `0` will indent the multi-line property chains with 0 spaces. -* Indent of 4 spaces with `MemberExpression` set to `1` will indent the multi-line property chains with 4 spaces. -* Indent of 4 spaces with `MemberExpression` set to `2` will indent the multi-line property chains with 8 spaces. +- Indent of 4 spaces with `VariableDeclarator` set to `2` will indent the multi-line variable declarations with 8 spaces. +- Indent of 2 spaces with `VariableDeclarator` set to `2` will indent the multi-line variable declarations with 4 spaces. +- Indent of 2 spaces with `VariableDeclarator` set to `{"var": 2, "let": 2, "const": 3}` will indent the multi-line variable declarations with 4 spaces for `var` and `let`, 6 spaces for `const` statements. +- Indent of tab with `VariableDeclarator` set to `2` will indent the multi-line variable declarations with 2 tabs. +- Indent of 2 spaces with `SwitchCase` set to `0` will not indent `case` clauses with respect to `switch` statements. +- Indent of 2 spaces with `SwitchCase` set to `1` will indent `case` clauses with 2 spaces with respect to `switch` statements. +- Indent of 2 spaces with `SwitchCase` set to `2` will indent `case` clauses with 4 spaces with respect to `switch` statements. +- Indent of tab with `SwitchCase` set to `2` will indent `case` clauses with 2 tabs with respect to `switch` statements. +- Indent of 2 spaces with `MemberExpression` set to `0` will indent the multi-line property chains with 0 spaces. +- Indent of 2 spaces with `MemberExpression` set to `1` will indent the multi-line property chains with 2 spaces. +- Indent of 2 spaces with `MemberExpression` set to `2` will indent the multi-line property chains with 4 spaces. +- Indent of 4 spaces with `MemberExpression` set to `0` will indent the multi-line property chains with 0 spaces. +- Indent of 4 spaces with `MemberExpression` set to `1` will indent the multi-line property chains with 4 spaces. +- Indent of 4 spaces with `MemberExpression` set to `2` will indent the multi-line property chains with 8 spaces. ### tab Examples of **incorrect** code for this rule with the `"tab"` option: + ```js /*eslint typescript/indent: ["error", "tab"]*/ @@ -125,6 +128,7 @@ function foo(d) { Examples of **correct** code for this rule with the `"tab"` option: + ```js /*eslint typescript/indent: ["error", "tab"]*/ @@ -140,6 +144,7 @@ if (a) { Examples of **incorrect** code for this rule with the `2, { "SwitchCase": 1 }` options: + ```js /*eslint typescript/indent: ["error", 2, { "SwitchCase": 1 }]*/ @@ -153,6 +158,7 @@ case "b": Examples of **correct** code for this rule with the `2, { "SwitchCase": 1 }` option: + ```js /*eslint typescript/indent: ["error", 2, { "SwitchCase": 1 }]*/ @@ -168,6 +174,7 @@ switch(a){ Examples of **incorrect** code for this rule with the `2, { "VariableDeclarator": 1 }` options: + ```js /*eslint typescript/indent: ["error", 2, { "VariableDeclarator": 1 }]*/ /*eslint-env es6*/ @@ -185,6 +192,7 @@ const a = 1, Examples of **correct** code for this rule with the `2, { "VariableDeclarator": 1 }` options: + ```js /*eslint typescript/indent: ["error", 2, { "VariableDeclarator": 1 }]*/ /*eslint-env es6*/ @@ -202,6 +210,7 @@ const a = 1, Examples of **correct** code for this rule with the `2, { "VariableDeclarator": 2 }` options: + ```js /*eslint typescript/indent: ["error", 2, { "VariableDeclarator": 2 }]*/ /*eslint-env es6*/ @@ -219,6 +228,7 @@ const a = 1, Examples of **correct** code for this rule with the `2, { "VariableDeclarator": { "var": 2, "let": 2, "const": 3 } }` options: + ```js /*eslint typescript/indent: ["error", 2, { "VariableDeclarator": { "var": 2, "let": 2, "const": 3 } }]*/ /*eslint-env es6*/ @@ -238,6 +248,7 @@ const a = 1, Examples of **incorrect** code for this rule with the options `2, { "outerIIFEBody": 0 }`: + ```js /*eslint typescript/indent: ["error", 2, { "outerIIFEBody": 0 }]*/ @@ -257,6 +268,7 @@ console.log('foo'); Examples of **correct** code for this rule with the options `2, {"outerIIFEBody": 0}`: + ```js /*eslint typescript/indent: ["error", 2, { "outerIIFEBody": 0 }]*/ @@ -278,6 +290,7 @@ if(y) { Examples of **incorrect** code for this rule with the `2, { "MemberExpression": 1 }` options: + ```js /*eslint typescript/indent: ["error", 2, { "MemberExpression": 1 }]*/ @@ -288,6 +301,7 @@ foo Examples of **correct** code for this rule with the `2, { "MemberExpression": 1 }` option: + ```js /*eslint typescript/indent: ["error", 2, { "MemberExpression": 1 }]*/ @@ -300,6 +314,7 @@ foo Examples of **incorrect** code for this rule with the `2, { "FunctionDeclaration": {"body": 1, "parameters": 2} }` option: + ```js /*eslint typescript/indent: ["error", 2, { "FunctionDeclaration": {"body": 1, "parameters": 2} }]*/ @@ -312,6 +327,7 @@ function foo(bar, Examples of **correct** code for this rule with the `2, { "FunctionDeclaration": {"body": 1, "parameters": 2} }` option: + ```js /*eslint typescript/indent: ["error", 2, { "FunctionDeclaration": {"body": 1, "parameters": 2} }]*/ @@ -324,6 +340,7 @@ function foo(bar, Examples of **incorrect** code for this rule with the `2, { "FunctionDeclaration": {"parameters": "first"} }` option: + ```js /*eslint typescript/indent: ["error", 2, {"FunctionDeclaration": {"parameters": "first"}}]*/ @@ -335,6 +352,7 @@ function foo(bar, baz, Examples of **correct** code for this rule with the `2, { "FunctionDeclaration": {"parameters": "first"} }` option: + ```js /*eslint typescript/indent: ["error", 2, {"FunctionDeclaration": {"parameters": "first"}}]*/ @@ -348,6 +366,7 @@ function foo(bar, baz, Examples of **incorrect** code for this rule with the `2, { "FunctionExpression": {"body": 1, "parameters": 2} }` option: + ```js /*eslint typescript/indent: ["error", 2, { "FunctionExpression": {"body": 1, "parameters": 2} }]*/ @@ -360,6 +379,7 @@ var foo = function(bar, Examples of **correct** code for this rule with the `2, { "FunctionExpression": {"body": 1, "parameters": 2} }` option: + ```js /*eslint typescript/indent: ["error", 2, { "FunctionExpression": {"body": 1, "parameters": 2} }]*/ @@ -372,6 +392,7 @@ var foo = function(bar, Examples of **incorrect** code for this rule with the `2, { "FunctionExpression": {"parameters": "first"} }` option: + ```js /*eslint typescript/indent: ["error", 2, {"FunctionExpression": {"parameters": "first"}}]*/ @@ -383,6 +404,7 @@ var foo = function(bar, baz, Examples of **correct** code for this rule with the `2, { "FunctionExpression": {"parameters": "first"} }` option: + ```js /*eslint typescript/indent: ["error", 2, {"FunctionExpression": {"parameters": "first"}}]*/ @@ -396,6 +418,7 @@ var foo = function(bar, baz, Examples of **incorrect** code for this rule with the `2, { "CallExpression": {"arguments": 1} }` option: + ```js /*eslint typescript/indent: ["error", 2, { "CallExpression": {"arguments": 1} }]*/ @@ -407,6 +430,7 @@ foo(bar, Examples of **correct** code for this rule with the `2, { "CallExpression": {"arguments": 1} }` option: + ```js /*eslint typescript/indent: ["error", 2, { "CallExpression": {"arguments": 1} }]*/ @@ -418,6 +442,7 @@ foo(bar, Examples of **incorrect** code for this rule with the `2, { "CallExpression": {"arguments": "first"} }` option: + ```js /*eslint typescript/indent: ["error", 2, {"CallExpression": {"arguments": "first"}}]*/ @@ -427,6 +452,7 @@ foo(bar, baz, Examples of **correct** code for this rule with the `2, { "CallExpression": {"arguments": "first"} }` option: + ```js /*eslint typescript/indent: ["error", 2, {"CallExpression": {"arguments": "first"}}]*/ @@ -438,6 +464,7 @@ foo(bar, baz, Examples of **incorrect** code for this rule with the `2, { "ArrayExpression": 1 }` option: + ```js /*eslint typescript/indent: ["error", 2, { "ArrayExpression": 1 }]*/ @@ -450,6 +477,7 @@ baz, Examples of **correct** code for this rule with the `2, { "ArrayExpression": 1 }` option: + ```js /*eslint typescript/indent: ["error", 2, { "ArrayExpression": 1 }]*/ @@ -462,6 +490,7 @@ var foo = [ Examples of **incorrect** code for this rule with the `2, { "ArrayExpression": "first" }` option: + ```js /*eslint typescript/indent: ["error", 2, {"ArrayExpression": "first"}]*/ @@ -473,6 +502,7 @@ var foo = [bar, Examples of **correct** code for this rule with the `2, { "ArrayExpression": "first" }` option: + ```js /*eslint typescript/indent: ["error", 2, {"ArrayExpression": "first"}]*/ @@ -486,6 +516,7 @@ var foo = [bar, Examples of **incorrect** code for this rule with the `2, { "ObjectExpression": 1 }` option: + ```js /*eslint typescript/indent: ["error", 2, { "ObjectExpression": 1 }]*/ @@ -498,6 +529,7 @@ baz: 2, Examples of **correct** code for this rule with the `2, { "ObjectExpression": 1 }` option: + ```js /*eslint typescript/indent: ["error", 2, { "ObjectExpression": 1 }]*/ @@ -510,6 +542,7 @@ var foo = { Examples of **incorrect** code for this rule with the `2, { "ObjectExpression": "first" }` option: + ```js /*eslint typescript/indent: ["error", 2, {"ObjectExpression": "first"}]*/ @@ -519,6 +552,7 @@ var foo = { bar: 1, Examples of **correct** code for this rule with the `2, { "ObjectExpression": "first" }` option: + ```js /*eslint typescript/indent: ["error", 2, {"ObjectExpression": "first"}]*/ @@ -530,6 +564,7 @@ var foo = { bar: 1, Examples of **correct** code for this rule with the `4, { "ImportDeclaration": 1 }` option (the default): + ```js /*eslint typescript/indent: ["error", 4, { ImportDeclaration: 1 }]*/ @@ -547,6 +582,7 @@ import { Examples of **incorrect** code for this rule with the `4, { ImportDeclaration: "first" }` option: + ```js /*eslint typescript/indent: ["error", 4, { ImportDeclaration: "first" }]*/ @@ -558,6 +594,7 @@ import { foo, Examples of **correct** code for this rule with the `4, { ImportDeclaration: "first" }` option: + ```js /*eslint typescript/indent: ["error", 4, { ImportDeclaration: "first" }]*/ @@ -571,6 +608,7 @@ import { foo, Examples of **incorrect** code for this rule with the default `4, { "flatTernaryExpressions": false }` option: + ```js /*eslint typescript/indent: ["error", 4, { "flatTernaryExpressions": false }]*/ @@ -582,6 +620,7 @@ var a = Examples of **correct** code for this rule with the default `4, { "flatTernaryExpressions": false }` option: + ```js /*eslint typescript/indent: ["error", 4, { "flatTernaryExpressions": false }]*/ @@ -593,6 +632,7 @@ var a = Examples of **incorrect** code for this rule with the `4, { "flatTernaryExpressions": true }` option: + ```js /*eslint typescript/indent: ["error", 4, { "flatTernaryExpressions": true }]*/ @@ -604,6 +644,7 @@ var a = Examples of **correct** code for this rule with the `4, { "flatTernaryExpressions": true }` option: + ```js /*eslint typescript/indent: ["error", 4, { "flatTernaryExpressions": true }]*/ @@ -619,6 +660,7 @@ The following configuration ignores the indentation of `ConditionalExpression` ( Examples of **correct** code for this rule with the `4, { "ignoredNodes": ["ConditionalExpression"] }` option: + ```js /*eslint typescript/indent: ["error", 4, { "ignoredNodes": ["ConditionalExpression"] }]*/ @@ -635,6 +677,7 @@ The following configuration ignores indentation in the body of IIFEs. Examples of **correct** code for this rule with the `4, { "ignoredNodes": ["CallExpression > FunctionExpression.callee > BlockStatement.body"] }` option: + ```js /*eslint typescript/indent: ["error", 4, { "ignoredNodes": ["CallExpression > FunctionExpression.callee > BlockStatement.body"] }]*/ @@ -650,6 +693,7 @@ bar(); Examples of additional **correct** code for this rule with the `4, { "ignoreComments": true }` option: + ```js /*eslint typescript/indent: ["error", 4, { "ignoreComments": true }] */ @@ -661,8 +705,7 @@ if (foo) { } ``` - ## Compatibility -* **JSHint**: `indent` -* **JSCS**: [validateIndentation](https://jscs-dev.github.io/rule/validateIndentation) +- **JSHint**: `indent` +- **JSCS**: [validateIndentation](https://jscs-dev.github.io/rule/validateIndentation) diff --git a/docs/rules/interface-name-prefix.md b/docs/rules/interface-name-prefix.md index 0bcb5fb..0a8cb4e 100644 --- a/docs/rules/interface-name-prefix.md +++ b/docs/rules/interface-name-prefix.md @@ -11,8 +11,8 @@ This rule enforces consistency of interface naming prefix conventions. This rule has a string option. -* `"never"` (default) disallows all interfaces being prefixed with `"I"` -* `"always"` requires all interfaces be prefixed with `"I"` +- `"never"` (default) disallows all interfaces being prefixed with `"I"` +- `"always"` requires all interfaces be prefixed with `"I"` ### never diff --git a/docs/rules/member-delimiter-style.md b/docs/rules/member-delimiter-style.md index 4c14275..0774de6 100644 --- a/docs/rules/member-delimiter-style.md +++ b/docs/rules/member-delimiter-style.md @@ -2,8 +2,9 @@ Enforces a consistent member delimiter style in interfaces and type literals. There are three member delimiter styles primarily used in TypeScript: -- Semicolon style (default, preferred in TypeScript). +- Semicolon style (default, preferred in TypeScript). + ```ts interface Foo { name: string; @@ -16,8 +17,9 @@ type Bar = { } ``` -- Comma style (JSON style). +- Comma style (JSON style). + ```ts interface Foo { name: string, @@ -30,7 +32,9 @@ type Bar = { } ``` -- Linebreak (none) style. +- Linebreak (none) style. + + ```ts interface Foo { name: string @@ -54,21 +58,21 @@ This rule aims to standardise the way interface and type literal members are del ```ts interface BaseConfig { - multiline ?: { - delimiter ?: 'none' | 'semi' | 'comma' - requireLast ?: boolean - } - singleline ?: { - delimiter ?: 'semi' | 'comma' - requireLast ?: boolean - } + multiline?: { + delimiter?: "none" | "semi" | "comma"; + requireLast?: boolean; + }; + singleline?: { + delimiter?: "semi" | "comma"; + requireLast?: boolean; + }; } type Config = BaseConfig & { - overrides ?: { - interface ?: BaseConfig - typeLiteral ?: BaseConfig - } -} + overrides?: { + interface?: BaseConfig; + typeLiteral?: BaseConfig; + }; +}; ``` Default config: @@ -94,17 +98,17 @@ The two configs are entirely separate, and do not effect one another. Accepts three values (or two for `singleline`): -- `comma` - each member should be delimited with a comma (`,`). -- `semi` - each member should be delimited with a semicolon (`;`). -- `none` - each member should be delimited with nothing. - - NOTE - this is not an option for `singleline` because having no delimiter between members on a single line is a syntax error in TS. +- `comma` - each member should be delimited with a comma (`,`). +- `semi` - each member should be delimited with a semicolon (`;`). +- `none` - each member should be delimited with nothing. + - NOTE - this is not an option for `singleline` because having no delimiter between members on a single line is a syntax error in TS. ### `requireLast` Determines whether or not the last member in the `interface`/`type` should have a delimiter: -- `true` - the last member ***must*** have a delimiter. -- `false` - the last member ***must not*** have a delimiter. +- `true` - the last member **_must_** have a delimiter. +- `false` - the last member **_must not_** have a delimiter. ### `overrides` @@ -137,6 +141,7 @@ For example, to require commas for `type`s, and semicolons for multiline `interf Examples of **incorrect** code for this rule with the default config: + ```ts // missing semicolon delimiter interface Foo { @@ -165,6 +170,7 @@ type FooBar = { name: string; greet(): string; } Examples of **correct** code for this rule with the default config: + ```ts interface Foo { name: string; diff --git a/docs/rules/member-naming.md b/docs/rules/member-naming.md index 31408ca..fd3d4ca 100644 --- a/docs/rules/member-naming.md +++ b/docs/rules/member-naming.md @@ -4,7 +4,7 @@ It can be helpful to enforce naming conventions for `private` (and sometimes `pr ## Rule Details -This rule allows you to enforce conventions for class property names by their visibility. By default, it enforces nothing. +This rule allows you to enforce conventions for class property names by their visibility. By default, it enforces nothing. ## Options @@ -36,4 +36,4 @@ If you do not want to enforce per-visibility naming rules for member properties. ## Further Reading -* ESLint's [`camelcase` rule](https://eslint.org/docs/rules/camelcase) +- ESLint's [`camelcase` rule](https://eslint.org/docs/rules/camelcase) diff --git a/docs/rules/member-ordering.md b/docs/rules/member-ordering.md index 53a0d9d..00137ff 100644 --- a/docs/rules/member-ordering.md +++ b/docs/rules/member-ordering.md @@ -1,6 +1,6 @@ # Require a consistent member declaration order (member-ordering) -A consistent ordering of fields, methods and constructors can make interfaces, type literals, classes and class +A consistent ordering of fields, methods and constructors can make interfaces, type literals, classes and class expressions easier to read, navigate and edit. ## Rule Details @@ -9,130 +9,135 @@ This rule aims to standardise the way interfaces, type literals, classes and cla ## Options -This rule, in its default state, does not require any argument, in which case the following order is enforced: -- `public-static-field` -- `protected-static-field` -- `private-static-field` -- `public-instance-field` -- `protected-instance-field` -- `private-instance-field` -- `public-field` (ignores scope) -- `protected-field` (ignores scope) -- `private-field` (ignores scope) -- `static-field` (ignores accessibility) -- `instance-field` (ignores accessibility) -- `field` (ignores scope and/or accessibility) -- `constructor` (ignores scope and/or accessibility) -- `public-static-method` -- `protected-static-method` -- `private-static-method` -- `public-instance-method` -- `protected-instance-method` -- `private-instance-method` -- `public-method` (ignores scope) -- `protected-method` (ignores scope) -- `private-method` (ignores scope) -- `static-method` (ignores accessibility) -- `instance-method` (ignores accessibility) -- `method` (ignores scope and/or accessibility) +This rule, in its default state, does not require any argument, in which case the following order is enforced: + +- `public-static-field` +- `protected-static-field` +- `private-static-field` +- `public-instance-field` +- `protected-instance-field` +- `private-instance-field` +- `public-field` (ignores scope) +- `protected-field` (ignores scope) +- `private-field` (ignores scope) +- `static-field` (ignores accessibility) +- `instance-field` (ignores accessibility) +- `field` (ignores scope and/or accessibility) +- `constructor` (ignores scope and/or accessibility) +- `public-static-method` +- `protected-static-method` +- `private-static-method` +- `public-instance-method` +- `protected-instance-method` +- `private-instance-method` +- `public-method` (ignores scope) +- `protected-method` (ignores scope) +- `private-method` (ignores scope) +- `static-method` (ignores accessibility) +- `instance-method` (ignores accessibility) +- `method` (ignores scope and/or accessibility) The rule can also take one or more of the following options: -- `default`, use this to change the default order (used when no specific configuration has been provided). -- `classes`, use this to change the order in classes. -- `classExpressions`, use this to change the order in class expressions. -- `interfaces`, use this to change the order in interfaces. -- `typeLiterals`, use this to change the order in type literals. + +- `default`, use this to change the default order (used when no specific configuration has been provided). +- `classes`, use this to change the order in classes. +- `classExpressions`, use this to change the order in class expressions. +- `interfaces`, use this to change the order in interfaces. +- `typeLiterals`, use this to change the order in type literals. ### default -Disable using `never` or use one of the following values to specify an order: -- Fields: -`public-static-field` -`protected-static-field` -`private-static-field` -`public-instance-field` -`protected-instance-field` -`private-instance-field` -`public-field` (= public-*-field) -`protected-field` (= protected-*-field) -`private-field` (= private-*-field) -`static-field` (= *-static-field) -`instance-field` (= *-instance-field) -`field` (= all) - -- Constructors: -`public-constructor` -`protected-constructor` -`private-constructor` -`constructor` (= *-constructor) - -- Methods: -`public-static-method` -`protected-static-method` -`private-static-method` -`public-instance-method` -`protected-instance-method` -`private-instance-method` -`public-method` (= public-*-method) -`protected-method` (= protected-*-method) -`private-method` (= private-*-method) -`static-method` (= *-static-method) -`instance-method` (= *-instance-method) -`method` (= all) + +Disable using `never` or use one of the following values to specify an order: + +- Fields: + `public-static-field` + `protected-static-field` + `private-static-field` + `public-instance-field` + `protected-instance-field` + `private-instance-field` + `public-field` (= public-_-field) + `protected-field` (= protected-_-field) + `private-field` (= private-_-field) + `static-field` (= _-static-field) + `instance-field` (= \*-instance-field) + `field` (= all) + +- Constructors: + `public-constructor` + `protected-constructor` + `private-constructor` + `constructor` (= \*-constructor) + +- Methods: + `public-static-method` + `protected-static-method` + `private-static-method` + `public-instance-method` + `protected-instance-method` + `private-instance-method` + `public-method` (= public-_-method) + `protected-method` (= protected-_-method) + `private-method` (= private-_-method) + `static-method` (= _-static-method) + `instance-method` (= \*-instance-method) + `method` (= all) Examples of **incorrect** code for the `{ "default": [...] }` option: + ```ts // { "default": ["method", "constructor", "field"] } interface Foo { // -> field B: string; - + // -> constructor - new(); - + new (); + // -> method - A() : void; + A(): void; } type Foo = { // -> field B: string; - + // no constructor - + // -> method - A() : void; -} + A(): void; +}; class Foo { // -> * field - private C: string - public D: string - protected static E: string - + private C: string; + public D: string; + protected static E: string; + // -> constructor constructor() {} - + // -> * method public static A(): void {} - public B(): void {} + public B(): void {} } const Foo = class { // -> * field - private C: string - public D: string - + private C: string; + public D: string; + // -> constructor constructor() {} - + // -> * method public static A(): void {} public B(): void {} - + // * field - protected static E: string -} + protected static E: string; +}; // { "default": ["public-instance-method", "public-static-field"] } @@ -140,56 +145,57 @@ const Foo = class { class Foo { // private instance field - private C: string - + private C: string; + // public instance field - public D: string - + public D: string; + // -> public static field - public static E: string - + public static E: string; + // constructor constructor() {} - + // public static method public static A(): void {} - + // -> public instance method - public B(): void {} + public B(): void {} } const Foo = class { // private instance field - private C: string - + private C: string; + // -> public static field - public static E: string - - // public instance field - public D: string - + public static E: string; + + // public instance field + public D: string; + // constructor constructor() {} - + // public static method public static A(): void {} - + // -> public instance method - public B(): void {} -} + public B(): void {} +}; ``` Examples of **correct** code for the `{ "default": [...] }` option: + ```ts // { "default": ["method", "constructor", "field"] } interface Foo { // -> method A() : void; - + // -> constructor new(); - + // -> field B: string; } @@ -197,7 +203,7 @@ interface Foo { type Foo = { // -> method A() : void; - + // -> field B: string; } @@ -206,24 +212,24 @@ class Foo { // -> * method public static A(): void {} public B(): void {} - + // -> constructor constructor() {} - + // -> * field private C: string public D: string - protected static E: string + protected static E: string } const Foo = class { // -> * method public static A(): void {} public B(): void {} - + // -> constructor constructor() {} - + // -> * field private C: string public D: string @@ -237,39 +243,39 @@ const Foo = class { class Foo { // -> public instance method public B(): void {} - + // private instance field private C: string - + // public instance field public D: string - + // -> public static field public static E: string - + // constructor constructor() {} - + // public static method - public static A(): void {} + public static A(): void {} } const Foo = class { // -> public instance method public B(): void {} - + // private instance field private C: string - + // public instance field public D: string - + // constructor constructor() {} - + // public static method public static A(): void {} - + // -> protected static field protected static: string } @@ -280,13 +286,13 @@ const Foo = class { class Foo { // -> public static field - public static A: string; - + public static A: string; + // -> * static field private static B: string; protected statis C:string; private static D: string; - + // -> * instance field private E: string; } @@ -294,27 +300,29 @@ class Foo { const foo = class { // * method public T(): void {} - + // -> public static field - public static A: string; + public static A: string; // constructor constructor(){} - + // -> * static field private static B: string; protected statis C:string; private static D: string; - + // -> * instance field private E: string; } ``` ### classes -Disable using `never` or use one of the valid values (see default) to specify an order. + +Disable using `never` or use one of the valid values (see default) to specify an order. Examples of **incorrect** code for the `{ "classes": [...] }` option: + ```ts // { "classes": ["method", "constructor", "field"] } @@ -322,16 +330,16 @@ Examples of **incorrect** code for the `{ "classes": [...] }` option: class Foo { // -> field - private C: string - public D: string - protected static E: string - + private C: string; + public D: string; + protected static E: string; + // -> constructor constructor() {} - + // -> method public static A(): void {} - public B(): void {} + public B(): void {} } // { "classes": ["public-instance-method", "public-static-field"] } @@ -340,26 +348,27 @@ class Foo { class Foo { // private instance field - private C: string - + private C: string; + // public instance field - public D: string - + public D: string; + // -> public static field - public static E: string - + public static E: string; + // constructor constructor() {} - + // public static method public static A(): void {} - + // -> public instance method - public B(): void {} + public B(): void {} } ``` Examples of **correct** code for `{ "classes": [...] }` option: + ```ts // { "classes": ["method", "constructor", "field"] } @@ -369,14 +378,14 @@ class Foo { // -> * method public static A(): void {} public B(): void {} - + // -> constructor constructor() {} - + // -> * field - private C: string - public D: string - protected static E: string + private C: string; + public D: string; + protected static E: string; } // { "classes": ["public-instance-method", "public-static-field"] } @@ -385,29 +394,31 @@ class Foo { class Foo { // private instance field - private C: string - + private C: string; + // public instance field - public D: string - + public D: string; + // -> public static field - public static E: string - + public static E: string; + // constructor constructor() {} - + // public static method public static A(): void {} - + // -> public instance method - public B(): void {} + public B(): void {} } ``` ### classExpressions -Disable using `never` or use one of the valid values (see default) to specify an order. + +Disable using `never` or use one of the valid values (see default) to specify an order. Examples of **incorrect** code for the `{ "classExpressions": [...] }` option: + ```ts // { "classExpressions": ["method", "constructor", "field"] } @@ -415,17 +426,17 @@ Examples of **incorrect** code for the `{ "classExpressions": [...] }` option: const foo = class { // -> field - private C: string - public D: string - protected static E: string - + private C: string; + public D: string; + protected static E: string; + // -> constructor constructor() {} - + // -> method public static A(): void {} - public B(): void {} -} + public B(): void {} +}; // { "classExpressions": ["public-instance-method", "public-static-field"] } @@ -433,26 +444,27 @@ const foo = class { const foo = class { // private instance field - private C: string - + private C: string; + // public instance field - public D: string - + public D: string; + // -> public static field - public static E: string - + public static E: string; + // constructor constructor() {} - + // public static method public static A(): void {} - + // -> public instance method - public B(): void {} -} + public B(): void {} +}; ``` Examples of **correct** code for `{ "classExpressions": [...] }` option: + ```ts // { "classExpressions": ["method", "constructor", "field"] } @@ -462,15 +474,15 @@ const foo = class { // -> * method public static A(): void {} public B(): void {} - + // -> constructor constructor() {} - + // -> * field - private C: string - public D: string - protected static E: string -} + private C: string; + public D: string; + protected static E: string; +}; // { "classExpressions": ["public-instance-method", "public-static-field"] } @@ -478,32 +490,34 @@ const foo = class { const foo = class { // private instance field - private C: string - + private C: string; + // public instance field - public D: string - + public D: string; + // -> public static field - public static E: string - + public static E: string; + // constructor constructor() {} - + // public static method public static A(): void {} - + // -> public instance method - public B(): void {} -} + public B(): void {} +}; ``` ### interfaces -Disable using `never` or use one of the following values to specify an order: -`field` -`constructor` -`method` + +Disable using `never` or use one of the following values to specify an order: +`field` +`constructor` +`method` Examples of **incorrect** code for the `{ "interfaces": [...] }` option: + ```ts // { "interfaces": ["method", "constructor", "field"] } @@ -512,37 +526,40 @@ Examples of **incorrect** code for the `{ "interfaces": [...] }` option: interface Foo { // -> field B: string; - + // -> constructor - new(); - + new (); + // -> method - A() : void; + A(): void; } ``` Examples of **correct** code for the `{ "interfaces": [...] }` option: + ```ts // { "interfaces": ["method", "constructor", "field"] } - + // does not apply for classes/class expressions/type literals - + interface Foo { // -> method - A() : void; - + A(): void; + // -> constructor - new(); - + new (); + // -> field B: string; } ``` ### typeLiterals -Disable using `never` or use one of the valid values (see interfaces) to specify an order. - + +Disable using `never` or use one of the valid values (see interfaces) to specify an order. + Examples of **incorrect** code for the `{ "typeLiterals": [...] }` option: + ```ts // { "typeLiterals": ["method", "constructor", "field"] } @@ -551,28 +568,29 @@ Examples of **incorrect** code for the `{ "typeLiterals": [...] }` option: type Foo = { // -> field B: string; - + // -> method - A() : void; -} + A(): void; +}; ``` Examples of **correct** code for the `{ "typeLiterals": [...] }` option: + ```ts // { "typeLiterals": ["method", "constructor", "field"] } - + // does not apply for classes/class expressions/interfaces - + type Foo = { // -> method - A() : void; - + A(): void; + // -> constructor - new(); - + new (); + // -> field B: string; -} +}; ``` ## When Not To Use It @@ -581,4 +599,4 @@ If you don't care about the general structure of your classes and interfaces, th ## Compatibility -* TSLint: [member-ordering](https://palantir.github.io/tslint/rules/member-ordering/) \ No newline at end of file +- TSLint: [member-ordering](https://palantir.github.io/tslint/rules/member-ordering/) diff --git a/docs/rules/no-angle-bracket-type-assertion.md b/docs/rules/no-angle-bracket-type-assertion.md index 4e4a87f..cb90d86 100644 --- a/docs/rules/no-angle-bracket-type-assertion.md +++ b/docs/rules/no-angle-bracket-type-assertion.md @@ -1,7 +1,7 @@ # Enforces the use of `as Type` assertions instead of `` assertions (no-angle-bracket-type-assertion) TypeScript disallows the use of `` assertions in `.tsx` because of the similarity with -JSX's syntax, which makes it impossible to parse. +JSX's syntax, which makes it impossible to parse. ## Rule Details @@ -25,8 +25,8 @@ If your codebase does not include `.tsx` files, then you will not need this rule ## Further Reading -* [Typescript and JSX](https://www.typescriptlang.org/docs/handbook/jsx.html) +- [Typescript and JSX](https://www.typescriptlang.org/docs/handbook/jsx.html) ## Compatibility -* TSLint: [no-angle-bracket-type-assertion](https://palantir.github.io/tslint/rules/no-angle-bracket-type-assertion/) +- TSLint: [no-angle-bracket-type-assertion](https://palantir.github.io/tslint/rules/no-angle-bracket-type-assertion/) diff --git a/docs/rules/no-array-constructor.md b/docs/rules/no-array-constructor.md index 85e07a2..138f98a 100644 --- a/docs/rules/no-array-constructor.md +++ b/docs/rules/no-array-constructor.md @@ -11,8 +11,8 @@ Examples of **incorrect** code for this rule: ```ts /*eslint no-array-constructor: "error"*/ -Array(0, 1, 2) -new Array(0, 1, 2) +Array(0, 1, 2); +new Array(0, 1, 2); ``` Examples of **correct** code for this rule: @@ -20,11 +20,11 @@ Examples of **correct** code for this rule: ```ts /*eslint no-array-constructor: "error"*/ -Array(0, 1, 2) -new Array(x, y, z) +Array(0, 1, 2); +new Array(x, y, z); -Array(500) -new Array(someOtherArray.length) +Array(500); +new Array(someOtherArray.length); ``` ## When Not To Use It diff --git a/docs/rules/no-empty-interface.md b/docs/rules/no-empty-interface.md index 9906d1b..13ac1be 100644 --- a/docs/rules/no-empty-interface.md +++ b/docs/rules/no-empty-interface.md @@ -1,6 +1,6 @@ # Disallow the declaration of empty interfaces (no-empty-interface) -An empty interface is equivalent to its supertype. If the interface does not implement a supertype, then +An empty interface is equivalent to its supertype. If the interface does not implement a supertype, then the interface is equivalent to an empty object (`{}`). In both cases it can be omitted. ## Rule Details @@ -8,18 +8,20 @@ the interface is equivalent to an empty object (`{}`). In both cases it can be o This rule aims to ensure that only meaningful interfaces are declared in the code. The following patterns are considered warnings: + ```ts // an empty interface -interface Foo { } +interface Foo {} // an interface with only one supertype (Bar === Foo) -interface Bar extends Foo { } +interface Bar extends Foo {} // an interface with an empty list of supertypes -interface Baz extends { } +interface Baz {} ``` The following patterns are not warnings: + ```ts // an interface with any number of members interface Foo { @@ -33,7 +35,7 @@ interface Bar { // an interface with more than one supertype // in this case the interface can be used as a replacement of a union type. -interface Baz extends Foo, Bar { } +interface Baz extends Foo, Bar {} ``` ## When Not To Use It @@ -42,4 +44,4 @@ If you don't care about having empty/meaningless interfaces, then you will not n ## Compatibility -* TSLint: [no-empty-interface](https://palantir.github.io/tslint/rules/no-empty-interface/) \ No newline at end of file +- TSLint: [no-empty-interface](https://palantir.github.io/tslint/rules/no-empty-interface/) diff --git a/docs/rules/no-explicit-any.md b/docs/rules/no-explicit-any.md index 06829f1..481f0b8 100644 --- a/docs/rules/no-explicit-any.md +++ b/docs/rules/no-explicit-any.md @@ -14,15 +14,15 @@ an `any` type from being implied by the compiler, but doesn't prevent The following patterns are considered warnings: ```ts -const age: any = "seventeen" +const age: any = "seventeen"; ``` ```ts -const ages: any[] = ["seventeen"] +const ages: any[] = ["seventeen"]; ``` ```ts -const ages: Array = ["seventeen"] +const ages: Array = ["seventeen"]; ``` ```ts @@ -52,15 +52,15 @@ function greet(param: Array): Array {} The following patterns are not warnings: ```ts -const age: number = 17 +const age: number = 17; ``` ```ts -const ages: number[] = [17] +const ages: number[] = [17]; ``` ```ts -const ages: Array = [17] +const ages: Array = [17]; ``` ```ts @@ -94,8 +94,8 @@ and you want to be able to specify `any`. ## Further Reading -* TypeScript [any type](https://www.typescriptlang.org/docs/handbook/basic-types.html#any) +- TypeScript [any type](https://www.typescriptlang.org/docs/handbook/basic-types.html#any) ## Compatibility -* TSLint: [no-any](https://palantir.github.io/tslint/rules/no-any/) +- TSLint: [no-any](https://palantir.github.io/tslint/rules/no-any/) diff --git a/docs/rules/no-extraneous-class.md b/docs/rules/no-extraneous-class.md index 277bd38..5ff2624 100644 --- a/docs/rules/no-extraneous-class.md +++ b/docs/rules/no-extraneous-class.md @@ -15,17 +15,17 @@ Examples of **incorrect** code for this rule: class EmptyClass {} class ConstructorOnly { - constructor() { - foo(); - } + constructor() { + foo(); + } } // Use an object instead: class StaticOnly { - static version = 42; - static hello() { - console.log("Hello, world!"); - } + static version = 42; + static hello() { + console.log("Hello, world!"); + } } ``` @@ -35,14 +35,14 @@ Examples of **correct** code for this rule: class EmptyClass extends SuperClass {} class ParameterProperties { - constructor(public name: string) {} + constructor(public name: string) {} } const StaticOnly = { - version: 42, - hello() { - console.log("Hello, world!"); - }, + version: 42, + hello() { + console.log("Hello, world!"); + }, }; ``` @@ -50,13 +50,13 @@ const StaticOnly = { This rule accepts a single object option. -- `constructorOnly: true` will silence warnings about classes containing only a constructor. -- `allowEmpty: true` will silence warnings about empty classes. -- `staticOnly: true` will silence warnings about classes containing only static members. +- `constructorOnly: true` will silence warnings about classes containing only a constructor. +- `allowEmpty: true` will silence warnings about empty classes. +- `staticOnly: true` will silence warnings about classes containing only static members. ## When Not To Use It -You can disable this rule if you don’t have anyone who would make these kinds of mistakes on your +You can disable this rule if you don’t have anyone who would make these kinds of mistakes on your team or if you use classes as namespaces. ## Compatibility diff --git a/docs/rules/no-inferrable-types.md b/docs/rules/no-inferrable-types.md index 7180c49..42391ec 100644 --- a/docs/rules/no-inferrable-types.md +++ b/docs/rules/no-inferrable-types.md @@ -25,17 +25,15 @@ When none of the options are truthy, the following patterns are valid: ```ts const foo = 5; const bar = true; -const baz = 'str'; +const baz = "str"; class Foo { prop = 5; } -function fn(a = 5, b = true) { -} +function fn(a = 5, b = true) {} -function fn(a: number, b: boolean, c: string) { -} +function fn(a: number, b: boolean, c: string) {} ``` The following are invalid: @@ -43,14 +41,13 @@ The following are invalid: ```ts const foo: number = 5; const bar: boolean = true; -const baz: string = 'str'; +const baz: string = "str"; class Foo { prop: number = 5; } -function fn(a: number = 5, b: boolean = true) { -} +function fn(a: number = 5, b: boolean = true) {} ``` ### `ignoreProperties` diff --git a/docs/rules/no-misused-new.md b/docs/rules/no-misused-new.md index 97581b1..b5f55e0 100644 --- a/docs/rules/no-misused-new.md +++ b/docs/rules/no-misused-new.md @@ -12,7 +12,7 @@ class C { } interface I { - new(): I; + new (): I; constructor(): void; } ``` @@ -24,11 +24,12 @@ class C { constructor() {} } interface I { - new(): C; + new (): C; } ``` ## Options + ```json { "typescript/no-misused-new": "error" @@ -37,4 +38,4 @@ interface I { ## Compatibility -* TSLint: [no-misused-new](https://palantir.github.io/tslint/rules/no-misused-new/) +- TSLint: [no-misused-new](https://palantir.github.io/tslint/rules/no-misused-new/) diff --git a/docs/rules/no-namespace.md b/docs/rules/no-namespace.md index 302b130..d59b791 100644 --- a/docs/rules/no-namespace.md +++ b/docs/rules/no-namespace.md @@ -1,7 +1,7 @@ # Disallow the use of custom TypeScript modules and namespaces (no-namespace) -Custom TypeScript modules (`module foo {}`) and namespaces (`namespace foo {}`) are considered outdated -ways to organize TypeScript code. ES2015 module syntax is now preferred (`import`/`export`). +Custom TypeScript modules (`module foo {}`) and namespaces (`namespace foo {}`) are considered outdated +ways to organize TypeScript code. ES2015 module syntax is now preferred (`import`/`export`). This rule still allows the use of TypeScript module declarations to describe external APIs (`declare module 'foo' {}`). @@ -11,13 +11,15 @@ This rule aims to standardise the way modules are declared. ## Options -This rule, in its default state, does not require any argument. If you would like to enable one +This rule, in its default state, does not require any argument. If you would like to enable one or more of the following you may pass an object with the options set as follows: -- `allowDeclarations` set to `true` will allow you to `declare` custom TypeScript modules and namespaces (Default: `false`). -- `allowDefinitionFiles` set to `true` will allow you to `declare` and use custom TypeScript modules and namespaces -inside definition files (Default: `false`). + +- `allowDeclarations` set to `true` will allow you to `declare` custom TypeScript modules and namespaces (Default: `false`). +- `allowDefinitionFiles` set to `true` will allow you to `declare` and use custom TypeScript modules and namespaces + inside definition files (Default: `false`). Examples of **incorrect** code for the default `{ "allowDeclarations": false, "allowDefinitionFiles": false }` options: + ```ts module foo {} namespace foo {} @@ -29,25 +31,30 @@ declare namespace foo {} ``` Examples of **correct** code for the default `{ "allowDeclarations": false, "allowDefinitionFiles": false }` options: + ```ts -declare module 'foo' {} +declare module "foo" {} ``` ### allowDeclarations + Examples of **incorrect** code for the `{ "allowDeclarations": true }` option: + ```ts module foo {} namespace foo {} ``` Examples of **correct** code for the `{ "allowDeclarations": true }` option: + ```ts -declare module 'foo' {} +declare module "foo" {} declare module foo {} declare namespace foo {} ``` Examples of **incorrect** code for the `{ "allowDeclarations": false }` option: + ```ts module foo {} namespace foo {} @@ -56,12 +63,15 @@ declare namespace foo {} ``` Examples of **correct** code for the `{ "allowDeclarations": false }` option: + ```ts -declare module 'foo' {} +declare module "foo" {} ``` ### allowDefinitionFiles + Examples of **incorrect** code for the `{ "allowDefinitionFiles": true }` option: + ```ts // if outside a d.ts file module foo {} @@ -75,8 +85,9 @@ declare namespace foo {} ``` Examples of **correct** code for the `{ "allowDefinitionFiles": true }` option: + ```ts -declare module 'foo' {} +declare module "foo" {} // anything inside a d.ts file ``` @@ -87,10 +98,10 @@ If you are using the ES2015 module syntax, then you will not need this rule. ## Further Reading -* [Modules](https://www.typescriptlang.org/docs/handbook/modules.html) -* [Namespaces](https://www.typescriptlang.org/docs/handbook/namespaces.html) -* [Namespaces and Modules](https://www.typescriptlang.org/docs/handbook/namespaces-and-modules.html) +- [Modules](https://www.typescriptlang.org/docs/handbook/modules.html) +- [Namespaces](https://www.typescriptlang.org/docs/handbook/namespaces.html) +- [Namespaces and Modules](https://www.typescriptlang.org/docs/handbook/namespaces-and-modules.html) ## Compatibility -* TSLint: [no-namespace](https://palantir.github.io/tslint/rules/no-namespace/) +- TSLint: [no-namespace](https://palantir.github.io/tslint/rules/no-namespace/) diff --git a/docs/rules/no-non-null-assertion.md b/docs/rules/no-non-null-assertion.md index 5e9f2e3..fa3a798 100644 --- a/docs/rules/no-non-null-assertion.md +++ b/docs/rules/no-non-null-assertion.md @@ -32,4 +32,4 @@ If you don't care about strict null-checking, then you will not need this rule. ## Further Reading -* [`no-non-null-assertion`](https://palantir.github.io/tslint/rules/no-non-null-assertion/) in [TSLint](https://palantir.github.io/tslint/) +- [`no-non-null-assertion`](https://palantir.github.io/tslint/rules/no-non-null-assertion/) in [TSLint](https://palantir.github.io/tslint/) diff --git a/docs/rules/no-object-literal-type-assertion.md b/docs/rules/no-object-literal-type-assertion.md index 69da849..bf0e961 100644 --- a/docs/rules/no-object-literal-type-assertion.md +++ b/docs/rules/no-object-literal-type-assertion.md @@ -19,6 +19,7 @@ const z = { ... } as unknown; ``` ## Options + ```json { "typescript/no-object-literal-type-assertion": "error" @@ -27,4 +28,4 @@ const z = { ... } as unknown; ## Compatibility -* TSLint: [no-object-literal-type-assertion](https://palantir.github.io/tslint/rules/no-object-literal-type-assertion/) +- TSLint: [no-object-literal-type-assertion](https://palantir.github.io/tslint/rules/no-object-literal-type-assertion/) diff --git a/docs/rules/no-parameter-properties.md b/docs/rules/no-parameter-properties.md index c7dfc86..fc729a2 100644 --- a/docs/rules/no-parameter-properties.md +++ b/docs/rules/no-parameter-properties.md @@ -12,17 +12,20 @@ declare all properties in the class. This rule, in its default state, does not require any argument and would completely disallow the use of parameter properties. If you would like to allow certain types of parameter properties then you may pass an object with the following options: -- `allows`, an array containing one or more fo the allowed modifiers. Valid values are: - - `readonly`, allows **readonly** parameter properties. - - `private`, allows **private** parameter properties. - - `protected`, allows **protected** parameter properties. - - `public`, allows **public** parameter properties. - - `private readonly`, allows **private readonly** parameter properties. - - `protected readonly`, allows **protected readonly** parameter properties. - - `public readonly`, allows **public readonly** parameter properties. + +- `allows`, an array containing one or more fo the allowed modifiers. Valid values are: + - `readonly`, allows **readonly** parameter properties. + - `private`, allows **private** parameter properties. + - `protected`, allows **protected** parameter properties. + - `public`, allows **public** parameter properties. + - `private readonly`, allows **private readonly** parameter properties. + - `protected readonly`, allows **protected readonly** parameter properties. + - `public readonly`, allows **public readonly** parameter properties. ### default + Examples of **incorrect** code for this rule with no options at all: + ```ts class Foo { constructor(readonly name: string) {} @@ -54,6 +57,7 @@ class Foo { ``` Examples of **correct** code for this rule with no options at all: + ```ts class Foo { constructor(name: string) {} @@ -61,7 +65,9 @@ class Foo { ``` ### readonly + Examples of **incorrect** code for the `{ "allows": ["readonly"] }` options: + ```ts class Foo { constructor(private name: string) {} @@ -89,6 +95,7 @@ class Foo { ``` Examples of **correct** code for the `{ "allows": ["readonly"] }` options: + ```ts class Foo { constructor(name: string) {} @@ -100,7 +107,9 @@ class Foo { ``` ### private + Examples of **incorrect** code for the `{ "allows": ["private"] }` options: + ```ts class Foo { constructor(readonly name: string) {} @@ -128,6 +137,7 @@ class Foo { ``` Examples of **correct** code for the `{ "allows": ["private"] }` options: + ```ts class Foo { constructor(name: string) {} @@ -139,7 +149,9 @@ class Foo { ``` ### protected + Examples of **incorrect** code for the `{ "allows": ["protected"] }` options: + ```ts class Foo { constructor(readonly name: string) {} @@ -167,6 +179,7 @@ class Foo { ``` Examples of **correct** code for the `{ "allows": ["protected"] }` options: + ```ts class Foo { constructor(name: string) {} @@ -178,7 +191,9 @@ class Foo { ``` ### public + Examples of **incorrect** code for the `{ "allows": ["public"] }` options: + ```ts class Foo { constructor(readonly name: string) {} @@ -206,6 +221,7 @@ class Foo { ``` Examples of **correct** code for the `{ "allows": ["public"] }` options: + ```ts class Foo { constructor(name: string) {} @@ -217,7 +233,9 @@ class Foo { ``` ### private readonly + Examples of **incorrect** code for the `{ "allows": ["private readonly"] }` options: + ```ts class Foo { constructor(readonly name: string) {} @@ -245,6 +263,7 @@ class Foo { ``` Examples of **correct** code for the `{ "allows": ["private readonly"] }` options: + ```ts class Foo { constructor(name: string) {} @@ -256,7 +275,9 @@ class Foo { ``` ### protected readonly + Examples of **incorrect** code for the `{ "allows": ["protected readonly"] }` options: + ```ts class Foo { constructor(readonly name: string) {} @@ -284,6 +305,7 @@ class Foo { ``` Examples of **correct** code for the `{ "allows": ["protected readonly"] }` options: + ```ts class Foo { constructor(name: string) {} @@ -295,7 +317,9 @@ class Foo { ``` ### public readonly + Examples of **incorrect** code for the `{ "allows": ["public readonly"] }` options: + ```ts class Foo { constructor(readonly name: string) {} @@ -323,6 +347,7 @@ class Foo { ``` Examples of **correct** code for the `{ "allows": ["public readonly"] }` options: + ```ts class Foo { constructor(name: string) {} @@ -339,4 +364,4 @@ If you don't care about the using parameter properties in constructors, then you ## Compatibility -* TSLint: [no-parameter-properties](https://palantir.github.io/tslint/rules/no-parameter-properties/) +- TSLint: [no-parameter-properties](https://palantir.github.io/tslint/rules/no-parameter-properties/) diff --git a/docs/rules/no-this-alias.md b/docs/rules/no-this-alias.md index a9e68b6..35f18a9 100644 --- a/docs/rules/no-this-alias.md +++ b/docs/rules/no-this-alias.md @@ -14,7 +14,7 @@ Rationale from TSLint: > ```js > const self = this; > -> setTimeout(function () { +> setTimeout(function() { > self.doWork(); > }); > ``` @@ -27,7 +27,6 @@ Rationale from TSLint: > }); > ``` - Examples of **incorrect** code for this rule: (see the rationale above) @@ -42,10 +41,13 @@ You can pass an object option: ```json5 { - "typescript/no-this-alias": ["error", { - "allowDestructuring": true, // Allow `const { props, state } = this`; false by default - "allowedNames": ["self"], // Allow `const self = this`; `[]` by default - }], + "typescript/no-this-alias": [ + "error", + { + allowDestructuring: true, // Allow `const { props, state } = this`; false by default + allowedNames: ["self"], // Allow `const self = this`; `[]` by default + }, + ], } ``` @@ -55,4 +57,4 @@ If you need to assign `this` to variables, you shouldn’t use this rule. ## Related to -- TSLint: [`no-this-assignment`](https://palantir.github.io/tslint/rules/no-this-assignment/) +- TSLint: [`no-this-assignment`](https://palantir.github.io/tslint/rules/no-this-assignment/) diff --git a/docs/rules/no-triple-slash-reference.md b/docs/rules/no-triple-slash-reference.md index 1fb9ff0..a033eea 100644 --- a/docs/rules/no-triple-slash-reference.md +++ b/docs/rules/no-triple-slash-reference.md @@ -25,7 +25,7 @@ The following patterns are considered warnings: The following patterns are not warnings: ```ts -import Animal from "Animal" +import Animal from "Animal"; ``` ## When Not To Use It @@ -34,8 +34,8 @@ If you use `/// ` style imports. ## Further Reading -* TypeScript [Triple-Slash Directives](https://www.typescriptlang.org/docs/handbook/triple-slash-directives.html) +- TypeScript [Triple-Slash Directives](https://www.typescriptlang.org/docs/handbook/triple-slash-directives.html) ## Compatibility -* TSLint: [no-reference](http://palantir.github.io/tslint/rules/no-reference/) +- TSLint: [no-reference](http://palantir.github.io/tslint/rules/no-reference/) diff --git a/docs/rules/no-type-alias.md b/docs/rules/no-type-alias.md index b5b6ca6..d1ae4d1 100644 --- a/docs/rules/no-type-alias.md +++ b/docs/rules/no-type-alias.md @@ -1,7 +1,8 @@ # Disallow the use of type aliases (no-type-alias) In TypeScript, type aliases serve three purposes: -- Aliasing other types so that we can refer to them using a simpler name. + +- Aliasing other types so that we can refer to them using a simpler name. ```ts // this... @@ -10,14 +11,15 @@ type Person = { lastName: string, age: number }; - + function addPerson(person : Person) { ... } - + // is easier to read than this... -function addPerson(person : { firstName: string, lastName: string, age: number}) { ... } +function addPerson(person : { firstName: string, lastName: string, age: number}) { ... } ``` -- Act sort of like an interface, providing a set of methods and properties that must exist -in the objects implementing the type. + +- Act sort of like an interface, providing a set of methods and properties that must exist + in the objects implementing the type. ```ts type Person = { @@ -27,36 +29,34 @@ type Person = { walk: () => void, talk: () => void }; - -// you know person will have 3 properties and 2 methods, + +// you know person will have 3 properties and 2 methods, // because the structure has already been defined. var person : Person = { ... } - -// so we can be sure that this will work + +// so we can be sure that this will work person.walk(); ``` -- Act like mapping tools between types to allow quick modifications. +- Act like mapping tools between types to allow quick modifications. ```ts -type Immutable = { - readonly [P in keyof T] : T[P] -} +type Immutable = { readonly [P in keyof T]: T[P] }; type Person = { - name: string, - age: number -} + name: string; + age: number; +}; type ImmutablePerson = Immutable; -var person : ImmutablePerson = { name: 'John', age: 30 }; -person.name = 'Brad'; // error, readonly property +var person: ImmutablePerson = { name: "John", age: 30 }; +person.name = "Brad"; // error, readonly property ``` -When aliasing, the type alias does not create a new type, it just creates a new name +When aliasing, the type alias does not create a new type, it just creates a new name to refer to the original type. So aliasing primitives and other simple types, tuples, unions -or intersections can some times be redundant. +or intersections can some times be redundant. ```ts // this doesn't make much sense @@ -64,439 +64,404 @@ type myString = string; ``` On the other hand, using a type alias as an interface can limit your ability to: -- Reuse your code: interfaces can be extended or implemented by other types. Type aliases cannot. -- Debug your code: interfaces create a new name, so is easy to identify the base type of an object -while debugging the application. -Finally, mapping types is an advance technique, leaving it open can quickly become a pain point +- Reuse your code: interfaces can be extended or implemented by other types. Type aliases cannot. +- Debug your code: interfaces create a new name, so is easy to identify the base type of an object + while debugging the application. + +Finally, mapping types is an advance technique, leaving it open can quickly become a pain point in your application. ## Rule Details -This rule disallows the use of type aliases in favor of interfaces +This rule disallows the use of type aliases in favor of interfaces and simplified types (primitives, tuples, unions, intersections, etc). ## Options -This rule, in its default state, does not require any argument. If you would like to enable one + +This rule, in its default state, does not require any argument. If you would like to enable one or more of the following you may pass an object with the options set as follows: -- `allowAliases` set to `true` or `"always"` will allow you to do aliasing (Defaults to `false`/`"never"`). -- `allowCallbacks` set to `true` or `"always"` will allow you to use type aliases with callbacks (Defaults to `false`/`"never"`) -- `allowLiterals` set to `true` or `"always"` will allow you to use type aliases with literal objects (Defaults to `false`/`"never"`) -- `allowMappedTypes` set to `true` or `"always"` will allow you to use type aliases as mapping tools (Defaults to `false`/`"never"`) + +- `allowAliases` set to `true` or `"always"` will allow you to do aliasing (Defaults to `false`/`"never"`). +- `allowCallbacks` set to `true` or `"always"` will allow you to use type aliases with callbacks (Defaults to `false`/`"never"`) +- `allowLiterals` set to `true` or `"always"` will allow you to use type aliases with literal objects (Defaults to `false`/`"never"`) +- `allowMappedTypes` set to `true` or `"always"` will allow you to use type aliases as mapping tools (Defaults to `false`/`"never"`) ### allowAliases + This applies to primitive types and reference types. The setting accepts the following values: -- `true` or `false` (`"always"` or `"never"`) to active or deactivate the feature. -- `"in-unions"`, allows aliasing in union statements, e.g. `type Foo = string | string[];` -- `"in-intersections"`, allows aliasing in intersection statements, e.g. `type Foo = string & string[];` -- `"in-unions-and-intersections"`, allows aliasing in union and/or intersection statements. + +- `true` or `false` (`"always"` or `"never"`) to active or deactivate the feature. +- `"in-unions"`, allows aliasing in union statements, e.g. `type Foo = string | string[];` +- `"in-intersections"`, allows aliasing in intersection statements, e.g. `type Foo = string & string[];` +- `"in-unions-and-intersections"`, allows aliasing in union and/or intersection statements. Examples of **correct** code for the `{ "allowAliases": true }` or `{ "allowAliases": "always" }` options: + ```ts // primitives -type Foo = 'a'; - -type Foo = 'a' | 'b'; - +type Foo = "a"; + +type Foo = "a" | "b"; + type Foo = string; - + type Foo = string | string[]; - + type Foo = string & string[]; - + // reference types interface Bar {} class Baz implements Bar {} - + type Foo = Bar; - + type Foo = Bar | Baz; - + type Foo = Bar & Baz; ``` Examples of **incorrect** code for the `{ "allowAliases": "in-unions" }` option: + ```ts // primitives -type Foo = 'a'; - +type Foo = "a"; + type Foo = string; - + type Foo = string & string[]; - + // reference types interface Bar {} class Baz implements Bar {} - + type Foo = Bar; - + type Foo = Bar & Baz; ``` Examples of **correct** code for the `{ "allowAliases": "in-unions" }` option: + ```ts // primitives -type Foo = 'a' | 'b'; - +type Foo = "a" | "b"; + type Foo = string | string[]; - + // reference types interface Bar {} class Baz implements Bar {} - + type Foo = Bar | Baz; ``` Examples of **incorrect** code for the `{ "allowAliases": "in-intersections" }` option: + ```ts // primitives -type Foo = 'a'; - -type Foo = 'a' | 'b'; - +type Foo = "a"; + +type Foo = "a" | "b"; + type Foo = string; - + type Foo = string | string[]; - + // reference types interface Bar {} class Baz implements Bar {} - + type Foo = Bar; - + type Foo = Bar | Baz; ``` Examples of **correct** code for the `{ "allowAliases": "in-intersections" }` option: + ```ts // primitives type Foo = string & string[]; - + // reference types interface Bar {} class Baz implements Bar {} - + type Foo = Bar & Baz; ``` Examples of **incorrect** code for the `{ "allowAliases": "in-unions-and-intersections" }` option: + ```ts // primitives -type Foo = 'a'; - +type Foo = "a"; + type Foo = string; - + // reference types interface Bar {} class Baz implements Bar {} - + type Foo = Bar; ``` Examples of **correct** code for the `{ "allowAliases": "in-unions-and-intersections" }` option: + ```ts // primitives -type Foo = 'a' | 'b'; - +type Foo = "a" | "b"; + type Foo = string | string[]; - + type Foo = string & string[]; - + // reference types interface Bar {} class Baz implements Bar {} - + type Foo = Bar | Baz; - + type Foo = Bar & Baz; ``` ### allowCallbacks + This applies to function types. The setting accepts the following values: -- `true` or `false` (`"always"` or `"never"`) to active or deactivate the feature. + +- `true` or `false` (`"always"` or `"never"`) to active or deactivate the feature. Examples of **correct** code for the `{ "allowCallbacks": true }` or `{ "allowCallbacks": "always" }` option: + ```ts type Foo = () => void; - + type Foo = (name: string) => string; - + class Person {} - + type Foo = (name: string, age: number) => string | Person; - + type Foo = (name: string, age: number) => string & Person; ``` ### allowsLiterals + This applies to literal types (`type Foo = { ... }`). The setting accepts the following options: -- `true` or `false` (`"always"` or `"never"`) to active or deactivate the feature. -- `"in-unions"`, allows literals in union statements, e.g. `type Foo = string | string[];` -- `"in-intersections"`, allows literals in intersection statements, e.g. `type Foo = string & string[];` -- `"in-unions-and-intersections"`, allows literals in union and/or intersection statements. + +- `true` or `false` (`"always"` or `"never"`) to active or deactivate the feature. +- `"in-unions"`, allows literals in union statements, e.g. `type Foo = string | string[];` +- `"in-intersections"`, allows literals in intersection statements, e.g. `type Foo = string & string[];` +- `"in-unions-and-intersections"`, allows literals in union and/or intersection statements. Examples of **correct** code for the `{ "allowLiterals": true }` or `{ "allowLiterals": "always" }` options: + ```ts type Foo = {}; - + type Foo = { - name: string, - age: number + name: string; + age: number; }; - + type Foo = { - name: string, - age: number, - walk: (miles: number) => void + name: string; + age: number; + walk: (miles: number) => void; }; - + type Foo = { name: string } | { age: number }; - + type Foo = { name: string } & { age: number }; ``` Examples of **incorrect** code for the `{ "allowLiterals": "in-unions" }` option: + ```ts type Foo = {}; - + type Foo = { - name: string, - age: number + name: string; + age: number; }; - + type Foo = { - name: string, - age: number, - walk: (miles: number) => void + name: string; + age: number; + walk: (miles: number) => void; }; - + type Foo = { name: string } & { age: number }; ``` Examples of **correct** code for the `{ "allowLiterals": "in-unions" }` option: + ```ts type Foo = { name: string } | { age: number }; ``` Examples of **incorrect** code for the `{ "allowLiterals": "in-intersections" }` option: + ```ts type Foo = {}; - + type Foo = { - name: string, - age: number + name: string; + age: number; }; - + type Foo = { - name: string, - age: number, - walk: (miles: number) => void + name: string; + age: number; + walk: (miles: number) => void; }; - + type Foo = { name: string } | { age: number }; ``` Examples of **correct** code for the `{ "allowLiterals": "in-intersections" }` option: + ```ts type Foo = { name: string } & { age: number }; ``` Examples of **incorrect** code for the `{ "allowLiterals": "in-unions-and-intersections" }` option: + ```ts type Foo = {}; - + type Foo = { - name: string, - age: number + name: string; + age: number; }; - + type Foo = { - name: string, - age: number, - walk: (miles: number) => void + name: string; + age: number; + walk: (miles: number) => void; }; ``` Examples of **correct** code for the `{ "allowLiterals": "in-unions-and-intersections" }` option: + ```ts type Foo = { name: string } | { age: number }; - + type Foo = { name: string } & { age: number }; ``` ### allowMappedTypes + This applies to literal types. The setting accepts the following values: -- `true` or `false` (`"always"` or `"never"`) to active or deactivate the feature. -- `"in-unions"`, allows aliasing in union statements, e.g. `type Foo = string | string[];` -- `"in-intersections"`, allows aliasing in intersection statements, e.g. `type Foo = string & string[];` -- `"in-unions-and-intersections"`, allows aliasing in union and/or intersection statements. + +- `true` or `false` (`"always"` or `"never"`) to active or deactivate the feature. +- `"in-unions"`, allows aliasing in union statements, e.g. `type Foo = string | string[];` +- `"in-intersections"`, allows aliasing in intersection statements, e.g. `type Foo = string & string[];` +- `"in-unions-and-intersections"`, allows aliasing in union and/or intersection statements. Examples of **correct** code for the `{ "allowMappedTypes": true }` or `{ "allowMappedTypes": "always" }` options: + ```ts -type Foo = { - readonly [P in keyof T] : T[P] -}; - -type Foo = { - [P in keyof T]? : T[P] -}; - -type Foo = { - readonly [P in keyof T] : T[P] -} | { - readonly [P in keyof U] : U[P] -}; - -type Foo = { - [P in keyof T]? : T[P] -} | { - [P in keyof U]? : U[P] -}; - -type Foo = { - readonly [P in keyof T] : T[P] -} & { - readonly [P in keyof U] : U[P] -}; - -type Foo = { - [P in keyof T]? : T[P] -} & { - [P in keyof U]? : U[P] -}; +type Foo = { readonly [P in keyof T]: T[P] }; + +type Foo = { [P in keyof T]?: T[P] }; + +type Foo = + | { readonly [P in keyof T]: T[P] } + | { readonly [P in keyof U]: U[P] }; + +type Foo = { [P in keyof T]?: T[P] } | { [P in keyof U]?: U[P] }; + +type Foo = { readonly [P in keyof T]: T[P] } & + { readonly [P in keyof U]: U[P] }; + +type Foo = { [P in keyof T]?: T[P] } & { [P in keyof U]?: U[P] }; ``` Examples of **incorrect** code for the `{ "allowMappedTypes": "in-unions" }` option: + ```ts -type Foo = { - readonly [P in keyof T] : T[P] -}; - -type Foo = { - [P in keyof T]? : T[P] -}; - -type Foo = { - readonly [P in keyof T] : T[P] -} & { - readonly [P in keyof U] : U[P] -}; - -type Foo = { - [P in keyof T]? : T[P] -} & { - [P in keyof U]? : U[P] -}; +type Foo = { readonly [P in keyof T]: T[P] }; + +type Foo = { [P in keyof T]?: T[P] }; + +type Foo = { readonly [P in keyof T]: T[P] } & + { readonly [P in keyof U]: U[P] }; + +type Foo = { [P in keyof T]?: T[P] } & { [P in keyof U]?: U[P] }; ``` Examples of **correct** code for the `{ "allowMappedTypes": "in-unions" }` option: + ```ts -type Foo = { - readonly [P in keyof T] : T[P] -} | { - readonly [P in keyof U] : U[P] -}; - -type Foo = { - [P in keyof T]? : T[P] -} | { - [P in keyof U]? : U[P] -}; +type Foo = + | { readonly [P in keyof T]: T[P] } + | { readonly [P in keyof U]: U[P] }; + +type Foo = { [P in keyof T]?: T[P] } | { [P in keyof U]?: U[P] }; ``` Examples of **incorrect** code for the `{ "allowMappedTypes": "in-intersections" }` option: + ```ts -type Foo = { - readonly [P in keyof T] : T[P] -}; - -type Foo = { - [P in keyof T]? : T[P] -}; - -type Foo = { - readonly [P in keyof T] : T[P] -} | { - readonly [P in keyof U] : U[P] -}; - -type Foo = { - [P in keyof T]? : T[P] -} | { - [P in keyof U]? : U[P] -}; +type Foo = { readonly [P in keyof T]: T[P] }; + +type Foo = { [P in keyof T]?: T[P] }; + +type Foo = + | { readonly [P in keyof T]: T[P] } + | { readonly [P in keyof U]: U[P] }; + +type Foo = { [P in keyof T]?: T[P] } | { [P in keyof U]?: U[P] }; ``` Examples of **correct** code for the `{ "allowMappedTypes": "in-intersections" }` option: + ```ts -type Foo = { - readonly [P in keyof T] : T[P] -} & { - readonly [P in keyof U] : U[P] -}; - -type Foo = { - [P in keyof T]? : T[P] -} & { - [P in keyof U]? : U[P] -}; +type Foo = { readonly [P in keyof T]: T[P] } & + { readonly [P in keyof U]: U[P] }; + +type Foo = { [P in keyof T]?: T[P] } & { [P in keyof U]?: U[P] }; ``` Examples of **incorrect** code for the `{ "allowMappedTypes": "in-unions-and-intersections" }` option: + ```ts -type Foo = { - readonly [P in keyof T] : T[P] -}; - -type Foo = { - [P in keyof T]? : T[P] -}; +type Foo = { readonly [P in keyof T]: T[P] }; + +type Foo = { [P in keyof T]?: T[P] }; ``` Examples of **correct** code for the `{ "allowMappedTypes": "in-unions-and-intersections" }` option: + ```ts -type Foo = { - readonly [P in keyof T] : T[P] -} | { - readonly [P in keyof U] : U[P] -}; - -type Foo = { - [P in keyof T]? : T[P] -} | { - [P in keyof U]? : U[P] -}; - -type Foo = { - readonly [P in keyof T] : T[P] -} & { - readonly [P in keyof U] : U[P] -}; - -type Foo = { - [P in keyof T]? : T[P] -} & { - [P in keyof U]? : U[P] -}; +type Foo = + | { readonly [P in keyof T]: T[P] } + | { readonly [P in keyof U]: U[P] }; + +type Foo = { [P in keyof T]?: T[P] } | { [P in keyof U]?: U[P] }; + +type Foo = { readonly [P in keyof T]: T[P] } & + { readonly [P in keyof U]: U[P] }; + +type Foo = { [P in keyof T]?: T[P] } & { [P in keyof U]?: U[P] }; ``` ## When Not To Use It -When you can't express some shape with an interface or you need to use a union, tuple type, +When you can't express some shape with an interface or you need to use a union, tuple type, callback, etc. that would cause the code to be unreadable or impractical. ## Further Reading -* [Advance Types](https://www.typescriptlang.org/docs/handbook/advanced-types.html) +- [Advance Types](https://www.typescriptlang.org/docs/handbook/advanced-types.html) ## Related to -* TSLint: [interface-over-type-literal](https://palantir.github.io/tslint/rules/interface-over-type-literal/) +- TSLint: [interface-over-type-literal](https://palantir.github.io/tslint/rules/interface-over-type-literal/) diff --git a/docs/rules/no-unused-vars.md b/docs/rules/no-unused-vars.md index af0b96f..e090c41 100644 --- a/docs/rules/no-unused-vars.md +++ b/docs/rules/no-unused-vars.md @@ -4,7 +4,7 @@ It ensures that TypeScript-specific constructs, such as implemented interfaces, ## Configuration -***This rule only has an effect when the `no-unused-vars` core rule is enabled.*** +**_This rule only has an effect when the `no-unused-vars` core rule is enabled._** See [the core ESLint docs](https://eslint.org/docs/rules/no-unused-vars) for how to configure the base `no-unused-vars` rule. @@ -16,6 +16,7 @@ See [the core ESLint docs](https://eslint.org/docs/rules/no-unused-vars) for how } } ``` + ## Rule Details The following patterns are considered warnings: diff --git a/docs/rules/no-use-before-define.md b/docs/rules/no-use-before-define.md index fb45321..6396696 100644 --- a/docs/rules/no-use-before-define.md +++ b/docs/rules/no-use-before-define.md @@ -71,29 +71,29 @@ let myVar: StringOrNumber; } ``` -* `functions` (`boolean`) - - The flag which shows whether or not this rule checks function declarations. - If this is `true`, this rule warns every reference to a function before the function declaration. - Otherwise, ignores those references. - Function declarations are hoisted, so it's safe. - Default is `true`. -* `classes` (`boolean`) - - The flag which shows whether or not this rule checks class declarations of upper scopes. - If this is `true`, this rule warns every reference to a class before the class declaration. - Otherwise, ignores those references if the declaration is in upper function scopes. - Class declarations are not hoisted, so it might be danger. - Default is `true`. -* `variables` (`boolean`) - - This flag determines whether or not the rule checks variable declarations in upper scopes. - If this is `true`, the rule warns every reference to a variable before the variable declaration. - Otherwise, the rule ignores a reference if the declaration is in an upper scope, while still reporting the reference if it's in the same scope as the declaration. - Default is `true`. -* `typedefs` (`boolean`, **added** in `eslint-plugin-typescript`) - - The flag which shows whether or not this rule checks type declarations. - If this is `true`, this rule warns every reference to a type before the type declaration. - Otherwise, ignores those references. - Type declarations are hoisted, so it's safe. - Default is `true`. +- `functions` (`boolean`) - + The flag which shows whether or not this rule checks function declarations. + If this is `true`, this rule warns every reference to a function before the function declaration. + Otherwise, ignores those references. + Function declarations are hoisted, so it's safe. + Default is `true`. +- `classes` (`boolean`) - + The flag which shows whether or not this rule checks class declarations of upper scopes. + If this is `true`, this rule warns every reference to a class before the class declaration. + Otherwise, ignores those references if the declaration is in upper function scopes. + Class declarations are not hoisted, so it might be danger. + Default is `true`. +- `variables` (`boolean`) - + This flag determines whether or not the rule checks variable declarations in upper scopes. + If this is `true`, the rule warns every reference to a variable before the variable declaration. + Otherwise, the rule ignores a reference if the declaration is in an upper scope, while still reporting the reference if it's in the same scope as the declaration. + Default is `true`. +- `typedefs` (`boolean`, **added** in `eslint-plugin-typescript`) - + The flag which shows whether or not this rule checks type declarations. + If this is `true`, this rule warns every reference to a type before the type declaration. + Otherwise, ignores those references. + Type declarations are hoisted, so it's safe. + Default is `true`. This rule accepts `"nofunc"` string as an option. `"nofunc"` is the same as `{ "functions": false, "classes": true }`. @@ -118,8 +118,7 @@ Examples of **incorrect** code for the `{ "classes": false }` option: /*eslint-env es6*/ new A(); -class A { -} +class A {} ``` Examples of **correct** code for the `{ "classes": false }` option: @@ -132,8 +131,7 @@ function foo() { return new A(); } -class A { -} +class A {} ``` ### variables @@ -170,5 +168,4 @@ let myVar: StringOrNumber; type StringOrNumber = string | number; ``` - Copied from [the original ESLint rule docs](https://github.com/eslint/eslint/blob/a113cd3/docs/rules/no-use-before-define.md) diff --git a/docs/rules/no-var-requires.md b/docs/rules/no-var-requires.md index d376176..a67c37e 100644 --- a/docs/rules/no-var-requires.md +++ b/docs/rules/no-var-requires.md @@ -25,4 +25,4 @@ If you don't care about TypeScript module syntax, then you will not need this ru ## Compatibility -* TSLint: [no-var-requires](https://palantir.github.io/tslint/rules/no-var-requires/) +- TSLint: [no-var-requires](https://palantir.github.io/tslint/rules/no-var-requires/) diff --git a/docs/rules/prefer-interface.md b/docs/rules/prefer-interface.md index 676f1bd..1692ec9 100644 --- a/docs/rules/prefer-interface.md +++ b/docs/rules/prefer-interface.md @@ -7,14 +7,14 @@ Interfaces are generally preferred over type literals because interfaces can be Examples of **incorrect** code for this rule. ```ts -type T = { x: number; } +type T = { x: number }; ``` Examples of **correct** code for this rule. ```ts type T = string; -type Foo = string | { } +type Foo = string | {}; interface T { x: number; @@ -22,6 +22,7 @@ interface T { ``` ## Options + ```CJSON { "interface-over-type-literal": "error" @@ -30,4 +31,4 @@ interface T { ## Compatibility -* TSLint: [interface-over-type-literal](https://palantir.github.io/tslint/rules/interface-over-type-literal/) +- TSLint: [interface-over-type-literal](https://palantir.github.io/tslint/rules/interface-over-type-literal/) diff --git a/docs/rules/prefer-namespace-keyword.md b/docs/rules/prefer-namespace-keyword.md index 89d64a2..fdfbb17 100644 --- a/docs/rules/prefer-namespace-keyword.md +++ b/docs/rules/prefer-namespace-keyword.md @@ -1,7 +1,7 @@ # Require the use of the `namespace` keyword instead of the `module` keyword to declare custom TypeScript modules. (prefer-namespace-keyword) In an effort to prevent further confusion between custom TypeScript modules and the new ES2015 modules, starting -with TypeScript v1.5 the keyword `namespace` is now the preferred way to declare custom TypeScript modules. +with TypeScript v1.5 the keyword `namespace` is now the preferred way to declare custom TypeScript modules. ## Rule Details @@ -13,10 +13,10 @@ If you are using the ES2015 module syntax, then you will not need this rule. ## Further Reading -* [Modules](https://www.typescriptlang.org/docs/handbook/modules.html) -* [Namespaces](https://www.typescriptlang.org/docs/handbook/namespaces.html) -* [Namespaces and Modules](https://www.typescriptlang.org/docs/handbook/namespaces-and-modules.html) +- [Modules](https://www.typescriptlang.org/docs/handbook/modules.html) +- [Namespaces](https://www.typescriptlang.org/docs/handbook/namespaces.html) +- [Namespaces and Modules](https://www.typescriptlang.org/docs/handbook/namespaces-and-modules.html) ## Compatibility -* TSLint: [no-internal-module](https://palantir.github.io/tslint/rules/no-internal-module/) +- TSLint: [no-internal-module](https://palantir.github.io/tslint/rules/no-internal-module/) diff --git a/docs/rules/type-annotation-spacing.md b/docs/rules/type-annotation-spacing.md index 5e75154..509088f 100644 --- a/docs/rules/type-annotation-spacing.md +++ b/docs/rules/type-annotation-spacing.md @@ -2,6 +2,7 @@ Spacing around type annotations improves readability of the code. Although the most commonly used style guideline for type annotations in TypeScript prescribes adding a space after the colon, but not before it, it is subjective to the preferences of a project. For example: + ```ts // with space after, but not before (default if no option is specified) let foo: string = "bar"; @@ -35,14 +36,18 @@ This rule aims to enforce specific spacing patterns around type annotations and ## Options This rule has an object option: -- `"before": false`, (default for colon) disallows spaces before the colon/arrow. -- `"before": true`, (default for arrow) requires a space before the colon/arrow. -- `"after": true`, (default) requires a space after the colon/arrow. -- `"after": false`, disallows spaces after the colon/arrow. -- `"overrides"`, overrides the default options for type annotations with `colon` (e.g. `const foo: string`) and function types with `arrow` (e.g. `type Foo = () => {}`). + +- `"before": false`, (default for colon) disallows spaces before the colon/arrow. +- `"before": true`, (default for arrow) requires a space before the colon/arrow. +- `"after": true`, (default) requires a space after the colon/arrow. +- `"after": false`, disallows spaces after the colon/arrow. +- `"overrides"`, overrides the default options for type annotations with `colon` (e.g. `const foo: string`) and function types with `arrow` (e.g. `type Foo = () => {}`). ### defaults + Examples of **incorrect** code for this rule with no options at all: + + ```ts let foo:string = "bar"; let foo :string = "bar"; @@ -68,6 +73,8 @@ type Foo = ()=> {}; ``` Examples of **correct** code for this rule with no options at all: + + ```ts let foo: string = "bar"; @@ -81,7 +88,10 @@ type Foo = () => {}; ``` ### after + Examples of **incorrect** code for this rule with `{ "before": false, "after": true }`: + + ```ts let foo:string = "bar"; let foo :string = "bar"; @@ -107,6 +117,8 @@ type Foo = () => {}; ``` Examples of **correct** code for this rule with `{ "before": false, "after": true }`: + + ```ts let foo: string = "bar"; @@ -120,7 +132,10 @@ type Foo = ()=> {}; ``` ### before + Examples of **incorrect** code for this rule with `{ "before": true, "after": true }` options: + + ```ts let foo: string = "bar"; let foo:string = "bar"; @@ -144,6 +159,8 @@ class Foo { ``` Examples of **correct** code for this rule with `{ "before": true, "after": true }` options: + + ```ts let foo : string = "bar"; @@ -155,7 +172,10 @@ class Foo { ``` ### overrides - colon + Examples of **incorrect** code for this rule with `{ "before": false, "after": false, overrides: { colon: { before: true, after: true }} }` options: + + ```ts let foo: string = "bar"; let foo:string = "bar"; @@ -183,6 +203,8 @@ type Foo = { ``` Examples of **correct** code for this rule with `{ "before": true, "after": true, overrides: { colon: { before: true, after: true }} }` options: + + ```ts let foo : string = "bar"; @@ -198,7 +220,10 @@ type Foo = { ``` ### overrides - arrow + Examples of **incorrect** code for this rule with `{ "before": false, "after": false, overrides: { arrow: { before: true, after: true }} }` options: + + ```ts let foo: string = "bar"; let foo : string = "bar"; @@ -234,6 +259,8 @@ type Foo = { ``` Examples of **correct** code for this rule with `{ "before": false, "after": false, overrides: { arrow: { before: true, after: true }} }` options: + + ```ts let foo:string = "bar"; @@ -254,9 +281,9 @@ If you don't want to enforce spacing for your type annotations, you can safely t ## Further Reading -* [TypeScript Type System](https://basarat.gitbooks.io/typescript/docs/types/type-system.html) -* [Type Inference](https://www.typescriptlang.org/docs/handbook/type-inference.html) +- [TypeScript Type System](https://basarat.gitbooks.io/typescript/docs/types/type-system.html) +- [Type Inference](https://www.typescriptlang.org/docs/handbook/type-inference.html) ## Compatibility -* TSLint: [typedef-whitespace](https://palantir.github.io/tslint/rules/typedef-whitespace/) +- TSLint: [typedef-whitespace](https://palantir.github.io/tslint/rules/typedef-whitespace/) diff --git a/package.json b/package.json index f13eafb..0370308 100644 --- a/package.json +++ b/package.json @@ -15,9 +15,10 @@ "lint:fix": "eslint lib/ tests/ tools/ --fix", "docs": "eslint-docs", "docs:check": "eslint-docs check", + "prettier": "prettier **/*.{md,yml,json}", "format-no-write": "prettier-eslint lib/**/*.js tests/**/*.js tools/**/*.js --eslint-config-path=.eslintrc --eslint-ignore --prettier-ignore --eslint-path=node_modules/eslint --config=.prettierrc", - "format": "yarn format-no-write --write", - "format-check": "yarn format-no-write --list-different", + "format": "yarn format-no-write --write && yarn prettier --write", + "format-check": "yarn format-no-write --list-different && yarn prettier --list-different", "test": "mocha tests --recursive --reporter=dot", "recommended:update": "node tools/update-recommended.js" }, diff --git a/yarn.lock b/yarn.lock index 91ea910..c9dd2af 100644 --- a/yarn.lock +++ b/yarn.lock @@ -87,9 +87,9 @@ ajv@^5.2.3, ajv@^5.3.0: json-schema-traverse "^0.3.0" ajv@^6.5.3, ajv@^6.6.1: - version "6.6.1" - resolved "https://registry.yarnpkg.com/ajv/-/ajv-6.6.1.tgz#6360f5ed0d80f232cc2b294c362d5dc2e538dd61" - integrity sha512-ZoJjft5B+EJBjUyu9C9Hc0OZyPZSSlOF+plzouTrg6UlA8f+e/n8NIgBFG/9tppJtpPWfthHakK7juJdNDODww== + version "6.6.2" + resolved "https://registry.yarnpkg.com/ajv/-/ajv-6.6.2.tgz#caceccf474bf3fc3ce3b147443711a24063cc30d" + integrity sha512-FBHEW6Jf5TB9MGBgUUA9XHkTbjXYfAUjY43ACMfmdMRHniyoMHjHjzD50OK8LGDWQwp4rWEsIq5kEqq7rvIM1g== dependencies: fast-deep-equal "^2.0.1" fast-json-stable-stringify "^2.0.0" @@ -523,7 +523,7 @@ cosmiconfig@5.0.6: js-yaml "^3.9.0" parse-json "^4.0.0" -cosmiconfig@^5.0.6: +cosmiconfig@^5.0.7: version "5.0.7" resolved "https://registry.yarnpkg.com/cosmiconfig/-/cosmiconfig-5.0.7.tgz#39826b292ee0d78eda137dfa3173bd1c21a43b04" integrity sha512-PcLqxTKiDmNT6pSpy4N6KtuPwb53W+2tzNvwOZw0WH9N6O0vLIBq0x8aj8Oj75ere4YcGi48bDFCL+3fRJdlNA== @@ -554,9 +554,9 @@ cross-spawn@^6.0.0, cross-spawn@^6.0.5: which "^1.2.9" date-fns@^1.27.2: - version "1.29.0" - resolved "https://registry.yarnpkg.com/date-fns/-/date-fns-1.29.0.tgz#12e609cdcb935127311d04d33334e2960a2a54e6" - integrity sha512-lbTXWZ6M20cWH8N9S6afb0SBm6tMk+uUg6z3MqHPKE9atmsY3kJkTm8vKe93izJ2B2+q5MV990sM2CHgtAZaOw== + version "1.30.1" + resolved "https://registry.yarnpkg.com/date-fns/-/date-fns-1.30.1.tgz#2e71bf0b119153dbb4cc4e88d9ea5acfb50dc05c" + integrity sha512-hBSVCvSmWC+QypYObzwGOd9wqdDpOt+0wl0KbU+R+uuZBS1jN8VsD1ss3irQDknRj5NvxiTF6oj/nDRnN/UQNw== debug@3.1.0: version "3.1.0" @@ -822,9 +822,9 @@ eslint@^4.0.0, eslint@^4.5.0: text-table "~0.2.0" eslint@^5.9.0: - version "5.9.0" - resolved "https://registry.yarnpkg.com/eslint/-/eslint-5.9.0.tgz#b234b6d15ef84b5849c6de2af43195a2d59d408e" - integrity sha512-g4KWpPdqN0nth+goDNICNXGfJF7nNnepthp46CAlJoJtC5K/cLu3NgCM3AHu1CkJ5Hzt9V0Y0PBAO6Ay/gGb+w== + version "5.10.0" + resolved "https://registry.yarnpkg.com/eslint/-/eslint-5.10.0.tgz#24adcbe92bf5eb1fc2d2f2b1eebe0c5e0713903a" + integrity sha512-HpqzC+BHULKlnPwWae9MaVZ5AXJKpkxCVXQHrFaRw3hbDj26V/9ArYM4Rr/SQ8pi6qUPLXSSXC4RBJlyq2Z2OQ== dependencies: "@babel/code-frame" "^7.0.0" ajv "^6.5.3" @@ -835,7 +835,7 @@ eslint@^5.9.0: eslint-scope "^4.0.0" eslint-utils "^1.3.1" eslint-visitor-keys "^1.0.0" - espree "^4.0.0" + espree "^5.0.0" esquery "^1.0.1" esutils "^2.0.2" file-entry-cache "^2.0.0" @@ -845,7 +845,6 @@ eslint@^5.9.0: ignore "^4.0.6" imurmurhash "^0.1.4" inquirer "^6.1.0" - is-resolvable "^1.1.0" js-yaml "^3.12.0" json-stable-stringify-without-jsonify "^1.0.1" levn "^0.3.0" @@ -873,10 +872,10 @@ espree@^3.5.2, espree@^3.5.4: acorn "^5.5.0" acorn-jsx "^3.0.0" -espree@^4.0.0: - version "4.1.0" - resolved "https://registry.yarnpkg.com/espree/-/espree-4.1.0.tgz#728d5451e0fd156c04384a7ad89ed51ff54eb25f" - integrity sha512-I5BycZW6FCVIub93TeVY1s7vjhP9CY6cXCznIRfiig7nRviKZYdRnj/sHEWC6A7WE9RDWOFq9+7OsWSYz8qv2w== +espree@^5.0.0: + version "5.0.0" + resolved "https://registry.yarnpkg.com/espree/-/espree-5.0.0.tgz#fc7f984b62b36a0f543b13fb9cd7b9f4a7f5b65c" + integrity sha512-1MpUfwsdS9MMoN7ZXqAr9e9UKdVHDcvrJpyx7mm1WuQlx/ygErEQBzgi5Nh5qBHIoYweprhtMkTCb9GhcAIcsA== dependencies: acorn "^6.0.2" acorn-jsx "^5.0.0" @@ -1273,11 +1272,11 @@ hosted-git-info@^2.1.4: integrity sha512-7T/BxH19zbcCTa8XkMlbK5lTo1WtgkFi3GvdWEyNuc4Vex7/9Dqbnpsf4JMydcfj9HCg4zUWFTL3Za6lapg5/w== husky@^1.2.0: - version "1.2.0" - resolved "https://registry.yarnpkg.com/husky/-/husky-1.2.0.tgz#d631dda1e4a9ee8ba69a10b0c51a0e2c66e711e5" - integrity sha512-/ib3+iycykXC0tYIxsyqierikVa9DA2DrT32UEirqNEFVqOj1bFMTgP3jAz8HM7FgC/C8pc/BTUa9MV2GEkZaA== + version "1.2.1" + resolved "https://registry.yarnpkg.com/husky/-/husky-1.2.1.tgz#33628f7013e345c1790a4dbe4642ad047f772dee" + integrity sha512-4Ylal3HWhnDvIszuiyLoVrSGI7QLg/ogkNCoHE34c+yZYzb9kBZNrlTOsdw92cGi3cJT8pPb6CdVfxFkLnc8Dg== dependencies: - cosmiconfig "^5.0.6" + cosmiconfig "^5.0.7" execa "^1.0.0" find-up "^3.0.0" get-stdin "^6.0.0" @@ -1551,7 +1550,7 @@ is-regexp@^1.0.0: resolved "https://registry.yarnpkg.com/is-regexp/-/is-regexp-1.0.0.tgz#fd2d883545c46bac5a633e7b9a09e87fa2cb5069" integrity sha1-/S2INUXEa6xaYz57mgnof6LLUGk= -is-resolvable@^1.0.0, is-resolvable@^1.1.0: +is-resolvable@^1.0.0: version "1.1.0" resolved "https://registry.yarnpkg.com/is-resolvable/-/is-resolvable-1.1.0.tgz#fb18f87ce1feb925169c9a407c19318a3206ed88" integrity sha512-qgDYXFSR5WvEfuS5dMj6oTMEbrrSaM0CrFk2Yiq/gXnBvD9pMa2jGXxyhGLfvhZpuMZe18CJpFxAt3CRs42NMg==