Skip to content

Commit

Permalink
Merge master into parser-error-messages
Browse files Browse the repository at this point in the history
  • Loading branch information
bradzacher committed Aug 19, 2019
2 parents 6a8b2ba + c68e033 commit d15f7f1
Show file tree
Hide file tree
Showing 24 changed files with 1,553 additions and 101 deletions.
2 changes: 2 additions & 0 deletions .gitattributes
@@ -1,5 +1,7 @@
* text=auto
*.js eol=lf
*.json eol=lf
*.md eol=lf
*.ts eol=lf
*.tsx eol=lf
*.yml eol=lf
37 changes: 37 additions & 0 deletions .github/ISSUE_TEMPLATE.md
@@ -0,0 +1,37 @@
---
name: catch-all template
about: Provides some general structure to issues that people choose to log outside the normal flow
title: ''
labels: triage
assignees: ''
---

**What were you trying to do?**

```json
{
"rules": {
"@typescript-eslint/<rule>": ["<setting>"]
}
}
```

```ts
// Put your code here
```

**What did you expect to happen?**

**What actually happened?**

**Versions**

| package | version |
| --------------------------------------- | ------- |
| `@typescript-eslint/eslint-plugin` | `X.Y.Z` |
| `@typescript-eslint/parser` | `X.Y.Z` |
| `@typescript-eslint/typescript-estree` | `X.Y.Z` |
| `@typescript-eslint/experimental-utils` | `X.Y.Z` |
| `TypeScript` | `X.Y.Z` |
| `node` | `X.Y.Z` |
| `npm` | `X.Y.Z` |
2 changes: 1 addition & 1 deletion .github/ISSUE_TEMPLATE/eslint-plugin-typescript.md
Expand Up @@ -22,7 +22,7 @@ Are you opening an issue because the rule you're trying to use is not found?
1) Check the releases log: https://github.com/typescript-eslint/typescript-eslint/releases
- If the rule isn't listed there, then chances are it hasn't been released to the main npm tag yet.
2) Try installing the `canary` tag: `npm i @typescript-eslint/eslint-plugin@canary`.
- The canary tag is built for every commit to master, so it contains the bleeding edge build.
- The canary tag is built for every commit to master, so it contains the bleeding edge build.
3) If ESLint still can't find the rule, then consider reporting an issue.
-->

Expand Down
20 changes: 20 additions & 0 deletions .github/ISSUE_TEMPLATE/typescript-eslint-utils.md
@@ -0,0 +1,20 @@
---
name: '@typescript-eslint/experimental-utils'
about: Report an issue with the `@typescript-eslint/experimental-utils` package
title: ''
labels: 'package: utils, triage'
assignees: ''
---

**What did you expect to happen?**

**What actually happened?**

**Versions**

| package | version |
| --------------------------------------- | ------- |
| `@typescript-eslint/experimental-utils` | `X.Y.Z` |
| `TypeScript` | `X.Y.Z` |
| `node` | `X.Y.Z` |
| `npm` | `X.Y.Z` |
3 changes: 3 additions & 0 deletions .gitignore
Expand Up @@ -60,3 +60,6 @@ jspm_packages/
.DS_Store
.idea
dist

# Editor-specific metadata folders
.vs
3 changes: 2 additions & 1 deletion packages/eslint-plugin/README.md
Expand Up @@ -65,7 +65,7 @@ You can also use [eslint:recommended](https://eslint.org/docs/rules/) (the set o

As of version 2 of this plugin, _by design_, none of the rules in the main `recommended` config require type-checking in order to run. This means that they are more lightweight and faster to run.

Some highly valuable rules simply require type-checking in order to be implemented correctly, however, so we provide an additional config you can extend from called `recommended-requiring-type-checking`. You wou apply this _in addition_ to the recommended configs previously mentioned, e.g.:
Some highly valuable rules simply require type-checking in order to be implemented correctly, however, so we provide an additional config you can extend from called `recommended-requiring-type-checking`. You would apply this _in addition_ to the recommended configs previously mentioned, e.g.:

```json
{
Expand Down Expand Up @@ -191,6 +191,7 @@ Then you should add `airbnb` (or `airbnb-base`) to your `extends` section of `.e
| [`@typescript-eslint/prefer-regexp-exec`](./docs/rules/prefer-regexp-exec.md) | Prefer RegExp#exec() over String#match() if no global flag is provided | :heavy_check_mark: | | :thought_balloon: |
| [`@typescript-eslint/prefer-string-starts-ends-with`](./docs/rules/prefer-string-starts-ends-with.md) | Enforce the use of `String#startsWith` and `String#endsWith` instead of other equivalent methods of checking substrings | :heavy_check_mark: | :wrench: | :thought_balloon: |
| [`@typescript-eslint/promise-function-async`](./docs/rules/promise-function-async.md) | Requires any function or method that returns a Promise to be marked async | | | :thought_balloon: |
| [`@typescript-eslint/quotes`](./docs/rules/quotes.md) | Enforce the consistent use of either backticks, double, or single quotes | | :wrench: | |
| [`@typescript-eslint/require-array-sort-compare`](./docs/rules/require-array-sort-compare.md) | Enforce giving `compare` argument to `Array#sort` | | | :thought_balloon: |
| [`@typescript-eslint/require-await`](./docs/rules/require-await.md) | Disallow async functions which have no `await` expression | :heavy_check_mark: | | :thought_balloon: |
| [`@typescript-eslint/restrict-plus-operands`](./docs/rules/restrict-plus-operands.md) | When adding two variables, operands must both be of type number or of type string | | | :thought_balloon: |
Expand Down
76 changes: 76 additions & 0 deletions packages/eslint-plugin/docs/rules/no-type-alias.md
Expand Up @@ -86,6 +86,7 @@ or more of the following you may pass an object with the options set as follows:
- `allowCallbacks` set to `"always"` will allow you to use type aliases with callbacks (Defaults to `"never"`)
- `allowLiterals` set to `"always"` will allow you to use type aliases with literal objects (Defaults to `"never"`)
- `allowMappedTypes` set to `"always"` will allow you to use type aliases as mapping tools (Defaults to `"never"`)
- `allowTupleTypes` set to `"always"` will allow you to use type aliases with tuples (Defaults to `"never"`)

### allowAliases

Expand Down Expand Up @@ -453,6 +454,81 @@ type Foo<T, U> = { readonly [P in keyof T]: T[P] } &
type Foo<T, U> = { [P in keyof T]?: T[P] } & { [P in keyof U]?: U[P] };
```

### allowTupleTypes

This applies to tuple types (`type Foo = [number]`).

The setting accepts the following options:

- `"always"` or `"never"` to active or deactivate the feature.
- `"in-unions"`, allows tuples in union statements, e.g. `type Foo = [string] | [string, string];`
- `"in-intersections"`, allows tuples in intersection statements, e.g. `type Foo = [string] & [string, string];`
- `"in-unions-and-intersections"`, allows tuples in union and/or intersection statements.

Examples of **correct** code for the `{ "allowTupleTypes": "always" }` options:

```ts
type Foo = [number];

type Foo = [number] | [number, number];

type Foo = [number] & [number, number];

type Foo = [number] | [number, number] & [string, string];
```

Examples of **incorrect** code for the `{ "allowTupleTypes": "in-unions" }` option:

```ts
type Foo = [number];

type Foo = [number] & [number, number];

type Foo = [string] & [number];
```

Examples of **correct** code for the `{ "allowTupleTypes": "in-unions" }` option:

```ts
type Foo = [number] | [number, number];

type Foo = [string] | [number];
```

Examples of **incorrect** code for the `{ "allowTupleTypes": "in-intersections" }` option:

```ts
type Foo = [number];

type Foo = [number] | [number, number];

type Foo = [string] | [number];
```

Examples of **correct** code for the `{ "allowTupleTypes": "in-intersections" }` option:

```ts
type Foo = [number] & [number, number];

type Foo = [string] & [number];
```

Examples of **incorrect** code for the `{ "allowTupleTypes": "in-unions-and-intersections" }` option:

```ts
type Foo = [number];

type Foo = [string];
```

Examples of **correct** code for the `{ "allowLiterals": "in-unions-and-intersections" }` option:

```ts
type Foo = [number] & [number, number];

type Foo = [string] | [number];
```

## When Not To Use It

When you can't express some shape with an interface or you need to use a union, tuple type,
Expand Down
22 changes: 22 additions & 0 deletions packages/eslint-plugin/docs/rules/quotes.md
@@ -0,0 +1,22 @@
# Enforce the consistent use of either backticks, double, or single quotes

## Rule Details

This rule extends the base [eslint/quotes](https://eslint.org/docs/rules/quotes) rule.
It supports all options and features of the base rule.

## How to use

```cjson
{
// note you must disable the base rule as it can report incorrect errors
"quotes": "off",
"@typescript-eslint/quotes": ["error"]
}
```

## Options

See [eslint/quotes options](https://eslint.org/docs/rules/quotes#options).

<sup>Taken with ❤️ [from ESLint core](https://github.com/eslint/eslint/blob/master/docs/rules/quotes.md)</sup>
2 changes: 1 addition & 1 deletion packages/eslint-plugin/package.json
Expand Up @@ -56,7 +56,7 @@
"typescript": "*"
},
"peerDependencies": {
"@typescript-eslint/parser": "^2.0.0-alpha.0",
"@typescript-eslint/parser": "^2.0.0",
"eslint": "^5.0.0 || ^6.0.0"
}
}
2 changes: 2 additions & 0 deletions packages/eslint-plugin/src/configs/all.json
Expand Up @@ -62,6 +62,8 @@
"@typescript-eslint/prefer-regexp-exec": "error",
"@typescript-eslint/prefer-string-starts-ends-with": "error",
"@typescript-eslint/promise-function-async": "error",
"quotes": "off",
"@typescript-eslint/quotes": "error",
"@typescript-eslint/require-array-sort-compare": "error",
"require-await": "off",
"@typescript-eslint/require-await": "error",
Expand Down
38 changes: 38 additions & 0 deletions packages/eslint-plugin/src/rules/explicit-function-return-type.ts
Expand Up @@ -9,6 +9,7 @@ type Options = [
allowExpressions?: boolean;
allowTypedFunctionExpressions?: boolean;
allowHigherOrderFunctions?: boolean;
allowDirectConstAssertionInArrowFunctions?: boolean;
},
];
type MessageIds = 'missingReturnType';
Expand Down Expand Up @@ -39,6 +40,9 @@ export default util.createRule<Options, MessageIds>({
allowHigherOrderFunctions: {
type: 'boolean',
},
allowDirectConstAssertionInArrowFunctions: {
type: 'boolean',
},
},
additionalProperties: false,
},
Expand All @@ -49,6 +53,7 @@ export default util.createRule<Options, MessageIds>({
allowExpressions: false,
allowTypedFunctionExpressions: true,
allowHigherOrderFunctions: true,
allowDirectConstAssertionInArrowFunctions: true,
},
],
create(context, [options]) {
Expand Down Expand Up @@ -203,6 +208,30 @@ export default util.createRule<Options, MessageIds>({
);
}

/**
* Checks if a function belongs to:
* `() => ({ action: 'xxx' }) as const`
*/
function returnsConstAssertionDirectly(
node: TSESTree.ArrowFunctionExpression,
): boolean {
const { body } = node;
if (body.type === AST_NODE_TYPES.TSAsExpression) {
const { typeAnnotation } = body;
if (typeAnnotation.type === AST_NODE_TYPES.TSTypeReference) {
const { typeName } = typeAnnotation;
if (
typeName.type === AST_NODE_TYPES.Identifier &&
typeName.name === 'const'
) {
return true;
}
}
}

return false;
}

/**
* Checks if a function declaration/expression has a return type.
*/
Expand Down Expand Up @@ -263,6 +292,15 @@ export default util.createRule<Options, MessageIds>({
}
}

// https://github.com/typescript-eslint/typescript-eslint/issues/653
if (
node.type === AST_NODE_TYPES.ArrowFunctionExpression &&
options.allowDirectConstAssertionInArrowFunctions &&
returnsConstAssertionDirectly(node)
) {
return;
}

checkFunctionReturnType(node);
}

Expand Down
2 changes: 2 additions & 0 deletions packages/eslint-plugin/src/rules/index.ts
Expand Up @@ -48,6 +48,7 @@ import preferReadonly from './prefer-readonly';
import preferRegexpExec from './prefer-regexp-exec';
import preferStringStartsEndsWith from './prefer-string-starts-ends-with';
import promiseFunctionAsync from './promise-function-async';
import quotes from './quotes';
import requireArraySortCompare from './require-array-sort-compare';
import requireAwait from './require-await';
import restrictPlusOperands from './restrict-plus-operands';
Expand Down Expand Up @@ -112,6 +113,7 @@ export default {
'prefer-regexp-exec': preferRegexpExec,
'prefer-string-starts-ends-with': preferStringStartsEndsWith,
'promise-function-async': promiseFunctionAsync,
quotes: quotes,
'require-array-sort-compare': requireArraySortCompare,
'require-await': requireAwait,
'restrict-plus-operands': restrictPlusOperands,
Expand Down

0 comments on commit d15f7f1

Please sign in to comment.