From 47ac3b60a81d22b4320e91749599c2dfb435c7b7 Mon Sep 17 00:00:00 2001 From: Jed Fox Date: Sun, 16 Dec 2018 13:06:43 -0500 Subject: [PATCH 1/7] Run Prettier on the other files in the project --- .github/ISSUE_TEMPLATE/bug-report.md | 11 +- .github/ISSUE_TEMPLATE/feature_request.md | 6 +- .github/no-response.yml | 10 +- .vscode/extensions.json | 5 +- .vscode/settings.json | 7 +- CONTRIBUTING.md | 44 +- README.md | 3 + docs/rules/adjacent-overload-signatures.md | 54 +- docs/rules/array-type.md | 17 +- docs/rules/ban-types.md | 38 +- docs/rules/camelcase.md | 39 +- docs/rules/class-name-casing.md | 24 +- docs/rules/explicit-function-return-type.md | 19 +- docs/rules/explicit-member-accessibility.md | 12 +- docs/rules/generic-type-naming.md | 16 +- docs/rules/interface-name-prefix.md | 4 +- docs/rules/member-delimiter-style.md | 91 ++-- docs/rules/member-naming.md | 4 +- docs/rules/member-ordering.md | 466 +++++++++--------- docs/rules/no-angle-bracket-type-assertion.md | 6 +- docs/rules/no-array-constructor.md | 12 +- docs/rules/no-empty-interface.md | 14 +- docs/rules/no-explicit-any.md | 16 +- docs/rules/no-inferrable-types.md | 13 +- docs/rules/no-misused-new.md | 7 +- docs/rules/no-namespace.md | 39 +- docs/rules/no-non-null-assertion.md | 2 +- .../rules/no-object-literal-type-assertion.md | 3 +- docs/rules/no-parameter-properties.md | 43 +- docs/rules/no-triple-slash-reference.md | 6 +- docs/rules/no-type-alias.md | 455 ++++++++--------- docs/rules/no-unused-vars.md | 3 +- docs/rules/no-use-before-define.md | 53 +- docs/rules/no-var-requires.md | 2 +- docs/rules/prefer-namespace-keyword.md | 10 +- docs/rules/type-annotation-spacing.md | 162 +++--- package.json | 115 ++--- tests/.eslintrc.yml | 4 +- yarn.lock | 8 +- 39 files changed, 932 insertions(+), 911 deletions(-) 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/no-response.yml b/.github/no-response.yml index bdee8b0..168c8f8 100644 --- a/.github/no-response.yml +++ b/.github/no-response.yml @@ -8,8 +8,8 @@ responseRequiredLabel: "awaiting response" # Comment to post when closing an Issue for lack of response. Set to `false` to disable closeComment: > - This issue has been automatically closed because there has been no response - to our request for more information from the original author. With only the - information that is currently in the issue, we don't have enough information - to take action. Please reach out if you have or find the answers we need so - that we can investigate further. + This issue has been automatically closed because there has been no response + to our request for more information from the original author. With only the + information that is currently in the issue, we don't have enough information + to take action. Please reach out if you have or find the answers we need so + that we can investigate further. 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/README.md b/README.md index d33b879..08aeda0 100644 --- a/README.md +++ b/README.md @@ -48,8 +48,10 @@ This guarantees 100% compatibility between the plugin and the parser. + **Key**: :heavy_check_mark: = recommended, :wrench: = fixable + | Name | Description | :heavy_check_mark: | :wrench: | | ---- | ----------- | ------------------ | -------- | | [`typescript/adjacent-overload-signatures`](./docs/rules/adjacent-overload-signatures.md) | Require that member overloads be consecutive (`adjacent-overload-signatures` from TSLint) | | | @@ -81,4 +83,5 @@ This guarantees 100% compatibility between the plugin and the parser. | [`typescript/no-var-requires`](./docs/rules/no-var-requires.md) | Disallows the use of require statements except in import statements (`no-var-requires` from TSLint) | | | | [`typescript/prefer-namespace-keyword`](./docs/rules/prefer-namespace-keyword.md) | Require the use of the `namespace` keyword instead of the `module` keyword to declare custom TypeScript modules. (`no-internal-module` from TSLint) | | :wrench: | | [`typescript/type-annotation-spacing`](./docs/rules/type-annotation-spacing.md) | Require consistent spacing around type annotations (`typedef-whitespace` from TSLint) | | :wrench: | + 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 c3af576..2313890 100644 --- a/docs/rules/camelcase.md +++ b/docs/rules/camelcase.md @@ -15,18 +15,18 @@ 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 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" @@ -35,7 +35,7 @@ Examples of **incorrect** code for this rule with the default `{ "properties": " ```js /*eslint camelcase: "error"*/ -import { no_camelcased } from "external-module" +import { no_camelcased } from "external-module"; var my_favorite_color = "#112C85"; @@ -49,18 +49,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; @@ -77,9 +77,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 }; @@ -92,22 +92,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" @@ -118,7 +117,7 @@ Examples of **correct** code for this rule with the `{ "properties": "never" }` /*eslint 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/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..d37bd8d 100644 --- a/docs/rules/member-delimiter-style.md +++ b/docs/rules/member-delimiter-style.md @@ -2,7 +2,7 @@ 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 { @@ -13,34 +13,35 @@ interface Foo { type Bar = { name: string; greet(): void; -} +}; ``` -- Comma style (JSON style). +- Comma style (JSON style). ```ts interface Foo { - name: string, - greet(): void, + name: string; + greet(): void; } type Bar = { - name: string, - greet(): void, -} + name: string; + greet(): void; +}; ``` -- Linebreak (none) style. +- Linebreak (none) style. + ```ts interface Foo { - name: string - greet(): void + name: string; + greet(): void; } type Bar = { - name: string - greet(): void -} + name: string; + greet(): void; +}; ``` The rule also enforces the presence (or absence) of the delimiter in the last member of the interface and/or type literal. @@ -54,21 +55,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 +95,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` @@ -140,27 +141,27 @@ Examples of **incorrect** code for this rule with the default config: ```ts // missing semicolon delimiter interface Foo { - name: string - greet(): string + name: string; + greet(): string; } // using incorrect delimiter interface Bar { - name: string, - greet(): string, + name: string; + greet(): string; } // missing last member delimiter interface Baz { name: string; - greet(): string + greet(): string; } // incorrect delimiter -type FooBar = { name: string, greet(): string } +type FooBar = { name: string; greet(): string }; // last member should not have delimiter -type FooBar = { name: string; greet(): string; } +type FooBar = { name: string; greet(): string }; ``` Examples of **correct** code for this rule with the default config: @@ -171,16 +172,18 @@ interface Foo { greet(): string; } -interface Foo { name: string } +interface Foo { + name: string; +} type Bar = { name: string; greet(): string; -} +}; -type Bar = { name: string } +type Bar = { name: string }; -type FooBar = { name: string; greet(): string } +type FooBar = { name: string; greet(): string }; ``` ## When Not To Use It 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-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-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-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..6bc2ee0 100644 --- a/docs/rules/type-annotation-spacing.md +++ b/docs/rules/type-annotation-spacing.md @@ -7,25 +7,25 @@ Spacing around type annotations improves readability of the code. Although the m let foo: string = "bar"; // with no spaces -let foo:string = "bar"; +let foo: string = "bar"; // with space before and after -let foo : string = "bar"; +let foo: string = "bar"; // with space before, but not after -let foo :string = "bar"; +let foo: string = "bar"; // with spaces before and after the fat arrow (default if no option is specified) type Foo = (string: name) => string; // with no spaces between the fat arrow -type Foo = (string: name)=>string; +type Foo = (string: name) => string; // with space after, but not before the fat arrow -type Foo = (string: name)=> string; +type Foo = (string: name) => string; // with space before, but not after the fat arrow -type Foo = (string: name) =>string; +type Foo = (string: name) => string; ``` ## Rule Details @@ -35,39 +35,43 @@ 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"; -let foo : string = "bar"; +let foo: string = "bar"; +let foo: string = "bar"; +let foo: string = "bar"; -function foo():string {} -function foo() :string {} -function foo() : string {} +function foo(): string {} +function foo(): string {} +function foo(): string {} class Foo { - name:string; + name: string; } class Foo { - name :string; + name: string; } class Foo { - name : string; + name: string; } -type Foo = ()=> {}; +type Foo = () => {}; ``` Examples of **correct** code for this rule with no options at all: + ```ts let foo: string = "bar"; @@ -81,32 +85,35 @@ type Foo = () => {}; ``` ### after + Examples of **incorrect** code for this rule with `{ "before": false, "after": true }`: + ```ts -let foo:string = "bar"; -let foo :string = "bar"; -let foo : string = "bar"; +let foo: string = "bar"; +let foo: string = "bar"; +let foo: string = "bar"; -function foo():string {} -function foo() :string {} -function foo() : string {} +function foo(): string {} +function foo(): string {} +function foo(): string {} class Foo { - name:string; + name: string; } class Foo { - name :string; + name: string; } class Foo { - name : string; + name: string; } type Foo = () => {}; ``` Examples of **correct** code for this rule with `{ "before": false, "after": true }`: + ```ts let foo: string = "bar"; @@ -116,136 +123,145 @@ class Foo { name: string; } -type Foo = ()=> {}; +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"; -let foo :string = "bar"; +let foo: string = "bar"; +let foo: string = "bar"; function foo(): string {} -function foo():string {} -function foo() :string {} +function foo(): string {} +function foo(): string {} class Foo { name: string; } class Foo { - name:string; + name: string; } class Foo { - name :string; + name: string; } ``` Examples of **correct** code for this rule with `{ "before": true, "after": true }` options: + ```ts -let foo : string = "bar"; +let foo: string = "bar"; -function foo() : string {} +function foo(): string {} class Foo { - name : string; + name: string; } ``` ### 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"; -let foo :string = "bar"; +let foo: string = "bar"; +let foo: string = "bar"; function foo(): string {} -function foo():string {} -function foo() :string {} +function foo(): string {} +function foo(): string {} class Foo { name: string; } class Foo { - name:string; + name: string; } class Foo { - name :string; + name: string; } type Foo = { - name: (name:string) => string; -} + name: (name: string) => string; +}; ``` Examples of **correct** code for this rule with `{ "before": true, "after": true, overrides: { colon: { before: true, after: true }} }` options: + ```ts -let foo : string = "bar"; +let foo: string = "bar"; -function foo() : string {} +function foo(): string {} class Foo { - name : string; + name: string; } type Foo = { - name: (name : string)=>string; -} + name: (name: string) => string; +}; ``` ### 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"; -let foo :string = "bar"; +let foo: string = "bar"; +let foo: string = "bar"; function foo(): string {} -function foo():string {} -function foo() :string {} +function foo(): string {} +function foo(): string {} class Foo { name: string; } class Foo { - name : string; + name: string; } class Foo { - name :string; + name: string; } type Foo = { - name: (name : string)=>string; -} + name: (name: string) => string; +}; type Foo = { - name: (name : string) =>string; -} + name: (name: string) => string; +}; type Foo = { - name: (name : string)=> string; -} + name: (name: string) => string; +}; ``` Examples of **correct** 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"; -function foo():string {} +function foo(): string {} class Foo { - name:string; + name: string; } type Foo = { - name: (name:string) => string; -} + name: (name: string) => string; +}; ``` ## When Not To Use It @@ -254,9 +270,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 5fe410f..2a62f9e 100644 --- a/package.json +++ b/package.json @@ -1,59 +1,60 @@ { - "name": "eslint-plugin-typescript", - "version": "0.14.0", - "description": "TypeScript plugin for ESLint", - "keywords": [ - "eslint", - "eslintplugin", - "eslint-plugin" - ], - "repository": "nzakas/eslint-plugin-typescript", - "author": "Nicholas C. Zakas", - "main": "lib/index.js", - "scripts": { - "lint": "eslint lib/ tests/", - "lint:fix": "eslint lib/ tests/ --fix", - "docs": "eslint-docs", - "docs:check": "eslint-docs check", - "format-no-write": "prettier-eslint lib/**/*.js tests/**/*.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", - "test": "mocha tests --recursive --reporter=dot" - }, - "dependencies": { - "requireindex": "^1.2.0", - "typescript-eslint-parser": "21.0.2" - }, - "devDependencies": { - "eslint": "^5.9.0", - "eslint-config-eslint": "^5.0.1", - "eslint-config-prettier": "^3.3.0", - "eslint-docs": "^0.2.3", - "eslint-plugin-eslint-plugin": "^2.0.0", - "eslint-plugin-node": "^6.0.1", - "eslint-plugin-prettier": "^3.0.0", - "husky": "^1.2.0", - "lint-staged": "^8.1.0", - "mocha": "^5.2.0", - "prettier-eslint-cli": "^4.7.1", - "typescript": "~3.1.1" - }, - "peerDependencies": { - "eslint": ">=4.13.1 < 6" - }, - "lint-staged": { - "*.js": [ - "yarn format", - "git add" - ] - }, - "husky": { - "hooks": { - "pre-commit": "yarn check --verify-tree && yarn lint && yarn format-check && yarn test && yarn docs:check && lint-staged" - } - }, - "engines": { - "node": ">=6" - }, - "license": "MIT" + "name": "eslint-plugin-typescript", + "version": "0.14.0", + "description": "TypeScript plugin for ESLint", + "keywords": [ + "eslint", + "eslintplugin", + "eslint-plugin" + ], + "repository": "nzakas/eslint-plugin-typescript", + "author": "Nicholas C. Zakas", + "main": "lib/index.js", + "scripts": { + "lint": "eslint lib/ tests/", + "lint:fix": "eslint lib/ tests/ --fix", + "docs": "eslint-docs", + "docs:check": "eslint-docs check", + "prettier": "prettier **/*.{md,yml,json}", + "format-no-write": "prettier-eslint *.js lib/**/*.js tests/**/*.js --eslint-config-path=.eslintrc --eslint-ignore --prettier-ignore --eslint-path=node_modules/eslint --config=.prettierrc", + "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" + }, + "dependencies": { + "requireindex": "^1.2.0", + "typescript-eslint-parser": "21.0.2" + }, + "devDependencies": { + "eslint": "^5.9.0", + "eslint-config-eslint": "^5.0.1", + "eslint-config-prettier": "^3.3.0", + "eslint-docs": "^0.2.6", + "eslint-plugin-eslint-plugin": "^2.0.0", + "eslint-plugin-node": "^6.0.1", + "eslint-plugin-prettier": "^3.0.0", + "husky": "^1.2.0", + "lint-staged": "^8.1.0", + "mocha": "^5.2.0", + "prettier-eslint-cli": "^4.7.1", + "typescript": "~3.1.1" + }, + "peerDependencies": { + "eslint": ">=4.13.1 < 6" + }, + "lint-staged": { + "*.js": [ + "yarn format", + "git add" + ] + }, + "husky": { + "hooks": { + "pre-commit": "yarn check --verify-tree && yarn lint && yarn format-check && yarn test && yarn docs:check && lint-staged" + } + }, + "engines": { + "node": ">=6" + }, + "license": "MIT" } diff --git a/tests/.eslintrc.yml b/tests/.eslintrc.yml index f76c64c..0d4dd6c 100644 --- a/tests/.eslintrc.yml +++ b/tests/.eslintrc.yml @@ -1,4 +1,4 @@ env: - mocha: true + mocha: true rules: - node/no-unpublished-require: off # we’re using devDeps here. + node/no-unpublished-require: off # we’re using devDeps here. diff --git a/yarn.lock b/yarn.lock index 46af790..0ccf5a6 100644 --- a/yarn.lock +++ b/yarn.lock @@ -707,10 +707,10 @@ eslint-config-prettier@^3.3.0: dependencies: get-stdin "^6.0.0" -eslint-docs@^0.2.3: - version "0.2.3" - resolved "https://registry.yarnpkg.com/eslint-docs/-/eslint-docs-0.2.3.tgz#71a583443c1d74bfc7c1af31b3249e258037309c" - integrity sha512-6afUYuK65TOPliMul0yIGwCiKWNEqk54RbuNZhJKt1mTVHyG0D2Zbbpa7yTT1/TiCfnUkfQa7TA/l7Jgu998Dw== +eslint-docs@^0.2.6: + version "0.2.6" + resolved "https://registry.yarnpkg.com/eslint-docs/-/eslint-docs-0.2.6.tgz#40bfbf62e22f948f02893e90280abf36ff260293" + integrity sha512-XyuBu/5d51ZecfYY+cantvWDLloo5j38sHJZE3VNU0u4PJCaOYvscWXBX/9ADzXBDJF5TvBEXCqPDnK/WrOiTA== dependencies: chalk "^2.4.1" detect-newline "^2.1.0" From 1067a41d07a5fae0f0f77ddc6655510dacba1f0d Mon Sep 17 00:00:00 2001 From: Jed Fox Date: Sun, 16 Dec 2018 13:33:58 -0500 Subject: [PATCH 2/7] Ignore code with intentionally unstandard formatting in the docs --- docs/rules/member-delimiter-style.md | 51 +++++----- docs/rules/type-annotation-spacing.md | 141 ++++++++++++++------------ 2 files changed, 103 insertions(+), 89 deletions(-) diff --git a/docs/rules/member-delimiter-style.md b/docs/rules/member-delimiter-style.md index d37bd8d..0774de6 100644 --- a/docs/rules/member-delimiter-style.md +++ b/docs/rules/member-delimiter-style.md @@ -4,6 +4,7 @@ Enforces a consistent member delimiter style in interfaces and type literals. Th - Semicolon style (default, preferred in TypeScript). + ```ts interface Foo { name: string; @@ -13,35 +14,37 @@ interface Foo { type Bar = { name: string; greet(): void; -}; +} ``` - Comma style (JSON style). + ```ts interface Foo { - name: string; - greet(): void; + name: string, + greet(): void, } type Bar = { - name: string; - greet(): void; -}; + name: string, + greet(): void, +} ``` - Linebreak (none) style. + ```ts interface Foo { - name: string; - greet(): void; + name: string + greet(): void } type Bar = { - name: string; - greet(): void; -}; + name: string + greet(): void +} ``` The rule also enforces the presence (or absence) of the delimiter in the last member of the interface and/or type literal. @@ -138,52 +141,52 @@ 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 { - name: string; - greet(): string; + name: string + greet(): string } // using incorrect delimiter interface Bar { - name: string; - greet(): string; + name: string, + greet(): string, } // missing last member delimiter interface Baz { name: string; - greet(): string; + greet(): string } // incorrect delimiter -type FooBar = { name: string; greet(): string }; +type FooBar = { name: string, greet(): string } // last member should not have delimiter -type FooBar = { name: string; greet(): string }; +type FooBar = { name: string; greet(): string; } ``` Examples of **correct** code for this rule with the default config: + ```ts interface Foo { name: string; greet(): string; } -interface Foo { - name: string; -} +interface Foo { name: string } type Bar = { name: string; greet(): string; -}; +} -type Bar = { name: string }; +type Bar = { name: string } -type FooBar = { name: string; greet(): string }; +type FooBar = { name: string; greet(): string } ``` ## When Not To Use It diff --git a/docs/rules/type-annotation-spacing.md b/docs/rules/type-annotation-spacing.md index 6bc2ee0..509088f 100644 --- a/docs/rules/type-annotation-spacing.md +++ b/docs/rules/type-annotation-spacing.md @@ -2,30 +2,31 @@ 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"; // with no spaces -let foo: string = "bar"; +let foo:string = "bar"; // with space before and after -let foo: string = "bar"; +let foo : string = "bar"; // with space before, but not after -let foo: string = "bar"; +let foo :string = "bar"; // with spaces before and after the fat arrow (default if no option is specified) type Foo = (string: name) => string; // with no spaces between the fat arrow -type Foo = (string: name) => string; +type Foo = (string: name)=>string; // with space after, but not before the fat arrow -type Foo = (string: name) => string; +type Foo = (string: name)=> string; // with space before, but not after the fat arrow -type Foo = (string: name) => string; +type Foo = (string: name) =>string; ``` ## Rule Details @@ -46,32 +47,34 @@ This rule has an object option: Examples of **incorrect** code for this rule with no options at all: + ```ts -let foo: string = "bar"; -let foo: string = "bar"; -let foo: string = "bar"; +let foo:string = "bar"; +let foo :string = "bar"; +let foo : string = "bar"; -function foo(): string {} -function foo(): string {} -function foo(): string {} +function foo():string {} +function foo() :string {} +function foo() : string {} class Foo { - name: string; + name:string; } class Foo { - name: string; + name :string; } class Foo { - name: string; + name : string; } -type Foo = () => {}; +type Foo = ()=> {}; ``` Examples of **correct** code for this rule with no options at all: + ```ts let foo: string = "bar"; @@ -88,25 +91,26 @@ type Foo = () => {}; Examples of **incorrect** code for this rule with `{ "before": false, "after": true }`: + ```ts -let foo: string = "bar"; -let foo: string = "bar"; -let foo: string = "bar"; +let foo:string = "bar"; +let foo :string = "bar"; +let foo : string = "bar"; -function foo(): string {} -function foo(): string {} -function foo(): string {} +function foo():string {} +function foo() :string {} +function foo() : string {} class Foo { - name: string; + name:string; } class Foo { - name: string; + name :string; } class Foo { - name: string; + name : string; } type Foo = () => {}; @@ -114,6 +118,7 @@ type Foo = () => {}; Examples of **correct** code for this rule with `{ "before": false, "after": true }`: + ```ts let foo: string = "bar"; @@ -123,44 +128,46 @@ class Foo { name: string; } -type Foo = () => {}; +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"; -let foo: string = "bar"; +let foo:string = "bar"; +let foo :string = "bar"; function foo(): string {} -function foo(): string {} -function foo(): string {} +function foo():string {} +function foo() :string {} class Foo { name: string; } class Foo { - name: string; + name:string; } class Foo { - name: string; + name :string; } ``` Examples of **correct** code for this rule with `{ "before": true, "after": true }` options: + ```ts -let foo: string = "bar"; +let foo : string = "bar"; -function foo(): string {} +function foo() : string {} class Foo { - name: string; + name : string; } ``` @@ -168,100 +175,104 @@ class Foo { 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"; -let foo: string = "bar"; +let foo:string = "bar"; +let foo :string = "bar"; function foo(): string {} -function foo(): string {} -function foo(): string {} +function foo():string {} +function foo() :string {} class Foo { name: string; } class Foo { - name: string; + name:string; } class Foo { - name: string; + name :string; } type Foo = { - name: (name: string) => string; -}; + name: (name:string) => string; +} ``` Examples of **correct** code for this rule with `{ "before": true, "after": true, overrides: { colon: { before: true, after: true }} }` options: + ```ts -let foo: string = "bar"; +let foo : string = "bar"; -function foo(): string {} +function foo() : string {} class Foo { - name: string; + name : string; } type Foo = { - name: (name: string) => string; -}; + name: (name : string)=>string; +} ``` ### 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"; -let foo: string = "bar"; +let foo : string = "bar"; +let foo :string = "bar"; function foo(): string {} -function foo(): string {} -function foo(): string {} +function foo():string {} +function foo() :string {} class Foo { name: string; } class Foo { - name: string; + name : string; } class Foo { - name: string; + name :string; } type Foo = { - name: (name: string) => string; -}; + name: (name : string)=>string; +} type Foo = { - name: (name: string) => string; -}; + name: (name : string) =>string; +} type Foo = { - name: (name: string) => string; -}; + name: (name : string)=> string; +} ``` Examples of **correct** 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"; -function foo(): string {} +function foo():string {} class Foo { - name: string; + name:string; } type Foo = { - name: (name: string) => string; -}; + name: (name:string) => string; +} ``` ## When Not To Use It From 1d3dab67acfdbae0b7b824e550e674a5317ccb8a Mon Sep 17 00:00:00 2001 From: Jed Fox Date: Sun, 16 Dec 2018 16:57:18 -0500 Subject: [PATCH 3/7] Remove extraneous Prettier options, allowing fallback to editorconfig --- .github/no-response.yml | 10 ++-- .prettierrc | 11 ---- .prettierrc.yml | 2 + .travis.yml | 16 +++--- package.json | 116 ++++++++++++++++++++-------------------- tests/.eslintrc.yml | 4 +- 6 files changed, 75 insertions(+), 84 deletions(-) delete mode 100644 .prettierrc create mode 100644 .prettierrc.yml diff --git a/.github/no-response.yml b/.github/no-response.yml index 168c8f8..bdee8b0 100644 --- a/.github/no-response.yml +++ b/.github/no-response.yml @@ -8,8 +8,8 @@ responseRequiredLabel: "awaiting response" # Comment to post when closing an Issue for lack of response. Set to `false` to disable closeComment: > - This issue has been automatically closed because there has been no response - to our request for more information from the original author. With only the - information that is currently in the issue, we don't have enough information - to take action. Please reach out if you have or find the answers we need so - that we can investigate further. + This issue has been automatically closed because there has been no response + to our request for more information from the original author. With only the + information that is currently in the issue, we don't have enough information + to take action. Please reach out if you have or find the answers we need so + that we can investigate further. 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/package.json b/package.json index 2a62f9e..34183f4 100644 --- a/package.json +++ b/package.json @@ -1,60 +1,60 @@ { - "name": "eslint-plugin-typescript", - "version": "0.14.0", - "description": "TypeScript plugin for ESLint", - "keywords": [ - "eslint", - "eslintplugin", - "eslint-plugin" - ], - "repository": "nzakas/eslint-plugin-typescript", - "author": "Nicholas C. Zakas", - "main": "lib/index.js", - "scripts": { - "lint": "eslint lib/ tests/", - "lint:fix": "eslint lib/ tests/ --fix", - "docs": "eslint-docs", - "docs:check": "eslint-docs check", - "prettier": "prettier **/*.{md,yml,json}", - "format-no-write": "prettier-eslint *.js lib/**/*.js tests/**/*.js --eslint-config-path=.eslintrc --eslint-ignore --prettier-ignore --eslint-path=node_modules/eslint --config=.prettierrc", - "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" - }, - "dependencies": { - "requireindex": "^1.2.0", - "typescript-eslint-parser": "21.0.2" - }, - "devDependencies": { - "eslint": "^5.9.0", - "eslint-config-eslint": "^5.0.1", - "eslint-config-prettier": "^3.3.0", - "eslint-docs": "^0.2.6", - "eslint-plugin-eslint-plugin": "^2.0.0", - "eslint-plugin-node": "^6.0.1", - "eslint-plugin-prettier": "^3.0.0", - "husky": "^1.2.0", - "lint-staged": "^8.1.0", - "mocha": "^5.2.0", - "prettier-eslint-cli": "^4.7.1", - "typescript": "~3.1.1" - }, - "peerDependencies": { - "eslint": ">=4.13.1 < 6" - }, - "lint-staged": { - "*.js": [ - "yarn format", - "git add" - ] - }, - "husky": { - "hooks": { - "pre-commit": "yarn check --verify-tree && yarn lint && yarn format-check && yarn test && yarn docs:check && lint-staged" - } - }, - "engines": { - "node": ">=6" - }, - "license": "MIT" + "name": "eslint-plugin-typescript", + "version": "0.14.0", + "description": "TypeScript plugin for ESLint", + "keywords": [ + "eslint", + "eslintplugin", + "eslint-plugin" + ], + "repository": "nzakas/eslint-plugin-typescript", + "author": "Nicholas C. Zakas", + "main": "lib/index.js", + "scripts": { + "lint": "eslint lib/ tests/", + "lint:fix": "eslint lib/ tests/ --fix", + "docs": "eslint-docs", + "docs:check": "eslint-docs check", + "prettier": "prettier **/*.{md,yml,json}", + "format-no-write": "prettier-eslint *.js lib/**/*.js tests/**/*.js --eslint-config-path=.eslintrc --eslint-ignore --prettier-ignore --eslint-path=node_modules/eslint --config=.prettierrc", + "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" + }, + "dependencies": { + "requireindex": "^1.2.0", + "typescript-eslint-parser": "21.0.2" + }, + "devDependencies": { + "eslint": "^5.9.0", + "eslint-config-eslint": "^5.0.1", + "eslint-config-prettier": "^3.3.0", + "eslint-docs": "^0.2.6", + "eslint-plugin-eslint-plugin": "^2.0.0", + "eslint-plugin-node": "^6.0.1", + "eslint-plugin-prettier": "^3.0.0", + "husky": "^1.2.0", + "lint-staged": "^8.1.0", + "mocha": "^5.2.0", + "prettier-eslint-cli": "^4.7.1", + "typescript": "~3.1.1" + }, + "peerDependencies": { + "eslint": ">=4.13.1 < 6" + }, + "lint-staged": { + "*.js": [ + "yarn format", + "git add" + ] + }, + "husky": { + "hooks": { + "pre-commit": "yarn check --verify-tree && yarn lint && yarn format-check && yarn test && yarn docs:check && lint-staged" + } + }, + "engines": { + "node": ">=6" + }, + "license": "MIT" } diff --git a/tests/.eslintrc.yml b/tests/.eslintrc.yml index 0d4dd6c..f76c64c 100644 --- a/tests/.eslintrc.yml +++ b/tests/.eslintrc.yml @@ -1,4 +1,4 @@ env: - mocha: true + mocha: true rules: - node/no-unpublished-require: off # we’re using devDeps here. + node/no-unpublished-require: off # we’re using devDeps here. From 14acd6e5aa598991a1ca3e8dc2bd144f9effbbbf Mon Sep 17 00:00:00 2001 From: Jed Fox Date: Sun, 16 Dec 2018 17:08:17 -0500 Subject: [PATCH 4/7] Update .editorconfig --- .editorconfig | 1 + 1 file changed, 1 insertion(+) 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] From 0ee7717066d8c1d2ebfa1191f0b844c387507b73 Mon Sep 17 00:00:00 2001 From: Jed Fox Date: Sun, 16 Dec 2018 17:51:43 -0500 Subject: [PATCH 5/7] Update yarn.lock --- yarn.lock | 47 +++++++++++++++++++++++------------------------ 1 file changed, 23 insertions(+), 24 deletions(-) diff --git a/yarn.lock b/yarn.lock index 0ccf5a6..2ab083a 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" @@ -708,9 +708,9 @@ eslint-config-prettier@^3.3.0: get-stdin "^6.0.0" eslint-docs@^0.2.6: - version "0.2.6" - resolved "https://registry.yarnpkg.com/eslint-docs/-/eslint-docs-0.2.6.tgz#40bfbf62e22f948f02893e90280abf36ff260293" - integrity sha512-XyuBu/5d51ZecfYY+cantvWDLloo5j38sHJZE3VNU0u4PJCaOYvscWXBX/9ADzXBDJF5TvBEXCqPDnK/WrOiTA== + version "0.2.7" + resolved "https://registry.yarnpkg.com/eslint-docs/-/eslint-docs-0.2.7.tgz#f208c3420fa2613f215a8daf5b9d75e9e7aa29ea" + integrity sha512-ylCFv96SW3aaWBrMFA7gai5tYntFXjy25CWNZWlAvamKCl7OYCTUfdUI40eAkO+3taxhGhTwCnIMHnwWwBxeYw== dependencies: chalk "^2.4.1" detect-newline "^2.1.0" @@ -812,9 +812,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" @@ -825,7 +825,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" @@ -835,7 +835,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" @@ -863,10 +862,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" @@ -1263,11 +1262,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" @@ -1536,7 +1535,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== From 0e71e45674c62e0827f1d4f732c0750ad6cdbebd Mon Sep 17 00:00:00 2001 From: Jed Fox Date: Sun, 16 Dec 2018 17:54:25 -0500 Subject: [PATCH 6/7] Re-run Prettier --- ROADMAP.md | 1 - docs/rules/prefer-interface.md | 7 ++++--- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/ROADMAP.md b/ROADMAP.md index f53f7c0..e3ff092 100644 --- a/ROADMAP.md +++ b/ROADMAP.md @@ -352,4 +352,3 @@ [`typescript/no-parameter-properties`]: https://github.com/bradzacher/eslint-plugin-typescript/blob/master/docs/rules/no-parameter-properties.md [`typescript/member-delimiter-style`]: https://github.com/bradzacher/eslint-plugin-typescript/blob/master/docs/rules/member-delimiter-style.md [`typescript/prefer-interface`]: https://github.com/bradzacher/eslint-plugin-typescript/blob/master/docs/rules/prefer-interface.md - 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/) From 76215bbae2af2b09c0fd9964c83f4db0f756f2af Mon Sep 17 00:00:00 2001 From: Brad Zacher Date: Mon, 17 Dec 2018 17:56:04 -0800 Subject: [PATCH 7/7] run format --- docs/rules/no-this-alias.md | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) 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/)