Skip to content

Commit

Permalink
Merge branch 'main' into patch-20
Browse files Browse the repository at this point in the history
  • Loading branch information
JoshuaKGoldberg committed Jun 11, 2022
2 parents 2e7a2d5 + dc1f930 commit 055f196
Show file tree
Hide file tree
Showing 36 changed files with 1,163 additions and 178 deletions.
2 changes: 2 additions & 0 deletions .cspell.json
Expand Up @@ -73,8 +73,10 @@
"linebreaks",
"lzstring",
"markdownlint",
"metastring",
"necroing",
"nocheck",
"noninteractive",
"nullish",
"OOM",
"OOMs",
Expand Down
3 changes: 3 additions & 0 deletions .prettierignore
Expand Up @@ -14,6 +14,9 @@ packages/eslint-plugin/src/configs/*.json
CONTRIBUTORS.md
packages/ast-spec/src/*/*/fixtures/_error_/*/fixture.ts

# Syntax not yet supported
packages/scope-manager/tests/fixtures/type-declaration/type-query-with-parameters.ts

# Ignore CHANGELOG.md files to avoid issues with automated release job
CHANGELOG.md

Expand Down
34 changes: 18 additions & 16 deletions docs/linting/MONOREPO.md
Expand Up @@ -18,22 +18,24 @@ Earlier in our docs on [typed linting](./TYPED_LINTING.md), we showed you how to

For example, this is how we specify all of our `tsconfig.json` within this repo.

```diff title=".eslintrc.js"
module.exports = {
root: true,
parser: '@typescript-eslint/parser',
parserOptions: {
tsconfigRootDir: __dirname,
- project: ['./tsconfig.json'],
+ project: ['./tsconfig.eslint.json', './packages/*/tsconfig.json'],
},
plugins: ['@typescript-eslint'],
extends: [
'eslint:recommended',
'plugin:@typescript-eslint/recommended',
'plugin:@typescript-eslint/recommended-requiring-type-checking',
],
};
```js title=".eslintrc.js"
module.exports = {
root: true,
parser: '@typescript-eslint/parser',
parserOptions: {
tsconfigRootDir: __dirname,
// Remove this line
project: ['./tsconfig.json'],
// Add this line
project: ['./tsconfig.eslint.json', './packages/*/tsconfig.json'],
},
plugins: ['@typescript-eslint'],
extends: [
'eslint:recommended',
'plugin:@typescript-eslint/recommended',
'plugin:@typescript-eslint/recommended-requiring-type-checking',
],
};
```

If you're looking for an example of what the `.eslintrc.js`, and referenced `tsconfig.json` might look like in a real example, look no further than this very repo. We're a multi-package monorepo that uses one `tsconfig.json` per package, that also uses typed linting.
Expand Down
82 changes: 42 additions & 40 deletions docs/linting/README.md
Expand Up @@ -136,19 +136,18 @@ If you use [`prettier`](https://www.npmjs.com/package/prettier), there is also a

Using this config by adding it to the end of your `extends`:

```diff title=".eslintrc.js"
module.exports = {
root: true,
parser: '@typescript-eslint/parser',
plugins: [
'@typescript-eslint',
],
extends: [
'eslint:recommended',
'plugin:@typescript-eslint/recommended',
+ 'prettier',
],
};
```js title=".eslintrc.js"
module.exports = {
root: true,
parser: '@typescript-eslint/parser',
plugins: ['@typescript-eslint'],
extends: [
'eslint:recommended',
'plugin:@typescript-eslint/recommended',
// Add this line
'prettier',
],
};
```

### Community Configs
Expand All @@ -163,19 +162,20 @@ A few popular all-in-one configs are:
To use one of these complete config packages, you would replace the `extends` with the package name.
For example:

```diff title=".eslintrc.js"
module.exports = {
root: true,
parser: '@typescript-eslint/parser',
plugins: [
'@typescript-eslint',
],
extends: [
- 'eslint:recommended',
- 'plugin:@typescript-eslint/recommended',
+ 'airbnb-typescript',
],
};
```js title=".eslintrc.js"
module.exports = {
root: true,
parser: '@typescript-eslint/parser',
plugins: ['@typescript-eslint'],
extends: [
// Removed lines start
'eslint:recommended',
'plugin:@typescript-eslint/recommended',
// Removed lines end
// Add this line
'airbnb-typescript',
],
};
```

<!-- markdownlint-disable MD044 -->
Expand All @@ -196,20 +196,22 @@ Below are just a few examples:
Every plugin that is out there includes documentation on the various configurations and rules they offer.
A typical plugin might be used like:

```diff title=".eslintrc.js"
module.exports = {
root: true,
parser: '@typescript-eslint/parser',
plugins: [
'@typescript-eslint',
+ 'jest',
],
extends: [
'eslint:recommended',
'plugin:@typescript-eslint/recommended',
+ 'plugin:jest/recommended',
],
};
```js title=".eslintrc.js"
module.exports = {
root: true,
parser: '@typescript-eslint/parser',
plugins: [
'@typescript-eslint',
// Add this line
'jest',
],
extends: [
'eslint:recommended',
'plugin:@typescript-eslint/recommended',
// Add this line
'plugin:jest/recommended',
],
};
```

<!-- markdownlint-disable MD044 -->
Expand Down
15 changes: 9 additions & 6 deletions docs/linting/TROUBLESHOOTING.md
Expand Up @@ -42,12 +42,15 @@ See our docs on [type aware linting](./TYPED_LINTING.md#i-get-errors-telling-me-

You can use `parserOptions.extraFileExtensions` to specify an array of non-TypeScript extensions to allow, for example:

```diff
parserOptions: {
tsconfigRootDir: __dirname,
project: ['./tsconfig.json'],
+ extraFileExtensions: ['.vue'],
},
```js title=".eslintrc.js"
module.exports = {
parserOptions: {
tsconfigRootDir: __dirname,
project: ['./tsconfig.json'],
// Add this line
extraFileExtensions: ['.vue'],
},
};
```

## One of my lint rules isn't working correctly on a pure JavaScript file
Expand Down
33 changes: 18 additions & 15 deletions docs/linting/TYPED_LINTING.md
Expand Up @@ -8,21 +8,24 @@ Under the hood, the typescript-eslint parser uses TypeScript's compiler APIs to

To tap into TypeScript's additional powers, there are two small changes you need to make to your config file:

```diff title=".eslintrc.js"
module.exports = {
root: true,
parser: '@typescript-eslint/parser',
+ parserOptions: {
+ tsconfigRootDir: __dirname,
+ project: ['./tsconfig.json'],
+ },
plugins: ['@typescript-eslint'],
extends: [
'eslint:recommended',
'plugin:@typescript-eslint/recommended',
+ 'plugin:@typescript-eslint/recommended-requiring-type-checking',
],
};
```js title=".eslintrc.js"
module.exports = {
root: true,
parser: '@typescript-eslint/parser',
// Added lines start
parserOptions: {
tsconfigRootDir: __dirname,
project: ['./tsconfig.json'],
},
// Added lines end
plugins: ['@typescript-eslint'],
extends: [
'eslint:recommended',
'plugin:@typescript-eslint/recommended',
// Add this line
'plugin:@typescript-eslint/recommended-requiring-type-checking',
],
};
```

In more detail:
Expand Down
1 change: 1 addition & 0 deletions packages/eslint-plugin/README.md
Expand Up @@ -102,6 +102,7 @@ Pro Tip: For larger codebases you may want to consider splitting our linting int
| [`@typescript-eslint/ban-tslint-comment`](./docs/rules/ban-tslint-comment.md) | Disallow `// tslint:<rule-flag>` comments | :lock: | :wrench: | |
| [`@typescript-eslint/ban-types`](./docs/rules/ban-types.md) | Disallow certain types | :white_check_mark: | :wrench: | |
| [`@typescript-eslint/class-literal-property-style`](./docs/rules/class-literal-property-style.md) | Enforce that literals on classes are exposed in a consistent style | :lock: | :wrench: | |
| [`@typescript-eslint/consistent-generic-constructors`](./docs/rules/consistent-generic-constructors.md) | Enforce specifying generic type arguments on type annotation or constructor name of a constructor call | :lock: | :wrench: | |
| [`@typescript-eslint/consistent-indexed-object-style`](./docs/rules/consistent-indexed-object-style.md) | Require or disallow the `Record` type | :lock: | :wrench: | |
| [`@typescript-eslint/consistent-type-assertions`](./docs/rules/consistent-type-assertions.md) | Enforce consistent usage of type assertions | :lock: | | |
| [`@typescript-eslint/consistent-type-definitions`](./docs/rules/consistent-type-definitions.md) | Enforce type definitions to consistently use either `interface` or `type` | :lock: | :wrench: | |
Expand Down
1 change: 1 addition & 0 deletions packages/eslint-plugin/docs/rules/README.md
Expand Up @@ -24,6 +24,7 @@ See [Configs](/docs/linting/configs) for how to enable recommended rules using c
| [`@typescript-eslint/ban-tslint-comment`](./ban-tslint-comment.md) | Disallow `// tslint:<rule-flag>` comments | :lock: | :wrench: | |
| [`@typescript-eslint/ban-types`](./ban-types.md) | Disallow certain types | :white_check_mark: | :wrench: | |
| [`@typescript-eslint/class-literal-property-style`](./class-literal-property-style.md) | Enforce that literals on classes are exposed in a consistent style | :lock: | :wrench: | |
| [`@typescript-eslint/consistent-generic-constructors`](./consistent-generic-constructors.md) | Enforce specifying generic type arguments on type annotation or constructor name of a constructor call | :lock: | :wrench: | |
| [`@typescript-eslint/consistent-indexed-object-style`](./consistent-indexed-object-style.md) | Require or disallow the `Record` type | :lock: | :wrench: | |
| [`@typescript-eslint/consistent-type-assertions`](./consistent-type-assertions.md) | Enforce consistent usage of type assertions | :lock: | | |
| [`@typescript-eslint/consistent-type-definitions`](./consistent-type-definitions.md) | Enforce type definitions to consistently use either `interface` or `type` | :lock: | :wrench: | |
Expand Down
@@ -0,0 +1,82 @@
# `consistent-generic-constructors`

Enforces specifying generic type arguments on type annotation or constructor name of a constructor call.

When constructing a generic class, you can specify the type arguments on either the left-hand side (as a type annotation) or the right-hand side (as part of the constructor call):

```ts
// Left-hand side
const map: Map<string, number> = new Map();

// Right-hand side
const map = new Map<string, number>();
```

This rule ensures that type arguments appear consistently on one side of the declaration.

## Options

```jsonc
{
"rules": {
"@typescript-eslint/consistent-generic-constructors": [
"error",
"constructor"
]
}
}
```

This rule takes a string option:

- If it's set to `constructor` (default), type arguments that **only** appear on the type annotation are disallowed.
- If it's set to `type-annotation`, type arguments that **only** appear on the constructor are disallowed.

## Rule Details

The rule never reports when there are type parameters on both sides, or neither sides of the declaration. It also doesn't report if the names of the type annotation and the constructor don't match.

### `constructor`

<!--tabs-->

#### ❌ Incorrect

```ts
const map: Map<string, number> = new Map();
const set: Set<string> = new Set();
```

#### ✅ Correct

```ts
const map = new Map<string, number>();
const map: Map<string, number> = new MyMap();
const set = new Set<string>();
const set = new Set();
const set: Set<string> = new Set<string>();
```

### `type-annotation`

<!--tabs-->

#### ❌ Incorrect

```ts
const map = new Map<string, number>();
const set = new Set<string>();
```

#### ✅ Correct

```ts
const map: Map<string, number> = new Map();
const set: Set<string> = new Set();
const set = new Set();
const set: Set<string> = new Set<string>();
```

## When Not To Use It

You can turn this rule off if you don't want to enforce one kind of generic constructor style over the other.
Expand Up @@ -158,10 +158,9 @@ Examples of code for this rule with `{ allowDirectConstAssertionInArrowFunctions

```ts
export const func = (value: number) => ({ type: 'X', value });
export const foo = () =>
({
bar: true,
} as const);
export const foo = () => ({
bar: true,
});
export const bar = () => 1;
```

Expand Down
1 change: 1 addition & 0 deletions packages/eslint-plugin/src/configs/all.ts
Expand Up @@ -18,6 +18,7 @@ export = {
'@typescript-eslint/comma-dangle': 'error',
'comma-spacing': 'off',
'@typescript-eslint/comma-spacing': 'error',
'@typescript-eslint/consistent-generic-constructors': 'error',
'@typescript-eslint/consistent-indexed-object-style': 'error',
'@typescript-eslint/consistent-type-assertions': 'error',
'@typescript-eslint/consistent-type-definitions': 'error',
Expand Down
1 change: 1 addition & 0 deletions packages/eslint-plugin/src/configs/strict.ts
Expand Up @@ -9,6 +9,7 @@ export = {
'@typescript-eslint/ban-tslint-comment': 'warn',
'@typescript-eslint/class-literal-property-style': 'warn',
'@typescript-eslint/consistent-indexed-object-style': 'warn',
'@typescript-eslint/consistent-generic-constructors': 'warn',
'@typescript-eslint/consistent-type-assertions': 'warn',
'@typescript-eslint/consistent-type-definitions': 'warn',
'dot-notation': 'off',
Expand Down

0 comments on commit 055f196

Please sign in to comment.