Skip to content

Commit

Permalink
feat: consume new scope analysis package
Browse files Browse the repository at this point in the history
  • Loading branch information
bradzacher committed Jun 28, 2020
1 parent ad91323 commit fbe16ac
Show file tree
Hide file tree
Showing 55 changed files with 2,327 additions and 165,739 deletions.
1 change: 1 addition & 0 deletions .eslintrc.js
Expand Up @@ -216,6 +216,7 @@ module.exports = {
'packages/eslint-plugin-internal/tests/rules/**/*.test.ts',
'packages/eslint-plugin-tslint/tests/rules/**/*.test.ts',
'packages/eslint-plugin/tests/rules/**/*.test.ts',
'packages/eslint-plugin/tests/eslint-rules/**/*.test.ts',
],
rules: {
'@typescript-eslint/internal/plugin-test-formatting': 'error',
Expand Down
1 change: 0 additions & 1 deletion packages/eslint-plugin/README.md
Expand Up @@ -144,7 +144,6 @@ Pro Tip: For larger codebases you may want to consider splitting our linting int
| [`@typescript-eslint/no-unsafe-call`](./docs/rules/no-unsafe-call.md) | Disallows calling an any type value | :heavy_check_mark: | | :thought_balloon: |
| [`@typescript-eslint/no-unsafe-member-access`](./docs/rules/no-unsafe-member-access.md) | Disallows member access on any typed variables | :heavy_check_mark: | | :thought_balloon: |
| [`@typescript-eslint/no-unsafe-return`](./docs/rules/no-unsafe-return.md) | Disallows returning any from a function | :heavy_check_mark: | | :thought_balloon: |
| [`@typescript-eslint/no-unused-vars-experimental`](./docs/rules/no-unused-vars-experimental.md) | Disallow unused variables and arguments | | | |
| [`@typescript-eslint/no-var-requires`](./docs/rules/no-var-requires.md) | Disallows the use of require statements except in import statements | :heavy_check_mark: | | |
| [`@typescript-eslint/prefer-as-const`](./docs/rules/prefer-as-const.md) | Prefer usage of `as const` over literal type | :heavy_check_mark: | :wrench: | |
| [`@typescript-eslint/prefer-for-of`](./docs/rules/prefer-for-of.md) | Prefer a ‘for-of’ loop over a standard ‘for’ loop if the index is only used to access the array being iterated | | | |
Expand Down
50 changes: 50 additions & 0 deletions packages/eslint-plugin/docs/rules/no-shadow.md
@@ -0,0 +1,50 @@
# Disallow variable declarations from shadowing variables declared in the outer scope (`no-shadow`)

## Rule Details

This rule extends the base [`eslint/no-shadow`](https://eslint.org/docs/rules/no-shadow) rule.
It adds support for TypeScript's `this` parameters, and adds options for TypeScript features.

## How to use

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

## Options

See [`eslint/no-shadow` options](https://eslint.org/docs/rules/no-shadow#options).
This rule adds the following options:

```ts
interface Options extends BaseNoShadowOptions {
ignoreTypeValueShadow?: boolean;
}

const defaultOptions: Options = {
...baseNoShadowDefaultOptions,
ignoreTypeValueShadow: true,
};
```

### `ignoreTypeValueShadow`

When set to `true`, the rule will ignore when you name a type and a variable with the same name.

Examples of **correct** code with `{ ignoreTypeValueShadow: true }`:

```ts
type Foo = number;
const Foo = 1;

interface Bar {
prop: number;
}
const Bar = 'test';
```

<sup>Taken with ❤️ [from ESLint core](https://github.com/eslint/eslint/blob/master/docs/rules/no-shadow.md)</sup>
2 changes: 0 additions & 2 deletions packages/eslint-plugin/docs/rules/no-unused-vars.md
@@ -1,7 +1,5 @@
# Disallow unused variables (`no-unused-vars`)

## PLEASE READ THIS ISSUE BEFORE USING THIS RULE [#1856](https://github.com/typescript-eslint/typescript-eslint/issues/1856)

## Rule Details

This rule extends the base [`eslint/no-unused-vars`](https://eslint.org/docs/rules/no-unused-vars) rule.
Expand Down
51 changes: 31 additions & 20 deletions packages/eslint-plugin/docs/rules/no-use-before-define.md
Expand Up @@ -23,42 +23,34 @@ See [`eslint/no-use-before-define` options](https://eslint.org/docs/rules/no-use
This rule adds the following options:

```ts
interface Options extends BaseNoMagicNumbersOptions {
interface Options extends BaseNoUseBeforeDefineOptions {
enums?: boolean;
typedefs?: boolean;
ignoreTypeReferences?: boolean;
}

const defaultOptions: Options = {
...baseNoMagicNumbersDefaultOptions,
...baseNoUseBeforeDefineDefaultOptions,
enums: true,
typedefs: true,
ignoreTypeReferences: true,
};
```

### `enums`

The flag which shows whether or not this rule checks enum declarations of upper scopes.
If this is `true`, this rule warns every reference to a enum before the enum declaration.
Otherwise, ignores those references.
If this is `false`, this rule will ignore references to enums, when the reference is in a child scope.

Examples of **incorrect** code for the `{ "enums": true }` option:
Examples of **incorrect** code for the `{ "enums": false }` option:

```ts
/*eslint no-use-before-define: ["error", { "enums": true }]*/
/*eslint no-use-before-define: ["error", { "enums": false }]*/

function foo() {
return Foo.FOO;
}

class Test {
foo() {
return Foo.FOO;
}
}
const x = Foo.FOO;

enum Foo {
FOO,
BAR,
}
```

Expand All @@ -78,10 +70,8 @@ enum Foo {

### `typedefs`

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.
If this is `false`, this rule will ignore references to types.

Examples of **correct** code for the `{ "typedefs": false }` option:

Expand All @@ -92,4 +82,25 @@ 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)
### `ignoreTypeReferences`

If this is `true`, this rule ignores all type references, such as in type annotations and assertions.
If this is `false`, this will will check all type references.

Examples of **correct** code for the `{ "ignoreTypeReferences": true }` option:

```ts
/*eslint no-use-before-define: ["error", { "ignoreTypeReferences": true }]*/

let var1: StringOrNumber;
type StringOrNumber = string | number;

let var2: Enum;
enum Enum {}
```

### Other Options

See [`eslint/no-use-before-define` options](https://eslint.org/docs/rules/no-use-before-define#options).

<sup>Taken with ❤️ [from ESLint core](https://github.com/eslint/eslint/blob/master/docs/rules/no-use-before-define.md)</sup>
1 change: 1 addition & 0 deletions packages/eslint-plugin/package.json
Expand Up @@ -43,6 +43,7 @@
},
"dependencies": {
"@typescript-eslint/experimental-utils": "3.4.0",
"@typescript-eslint/scope-manager": "3.4.0",
"debug": "^4.1.1",
"functional-red-black-tree": "^1.0.1",
"regexpp": "^3.0.0",
Expand Down
1 change: 0 additions & 1 deletion packages/eslint-plugin/src/configs/all.ts
Expand Up @@ -90,7 +90,6 @@ export = {
'@typescript-eslint/no-unused-expressions': 'error',
'no-unused-vars': 'off',
'@typescript-eslint/no-unused-vars': 'error',
'@typescript-eslint/no-unused-vars-experimental': 'error',
'no-use-before-define': 'off',
'@typescript-eslint/no-use-before-define': 'error',
'no-useless-constructor': 'off',
Expand Down
12 changes: 2 additions & 10 deletions packages/eslint-plugin/src/rules/no-empty-interface.ts
@@ -1,8 +1,5 @@
import * as util from '../util';
import {
AST_NODE_TYPES,
TSESLint,
} from '@typescript-eslint/experimental-utils';
import { TSESLint } from '@typescript-eslint/experimental-utils';

type Options = [
{
Expand Down Expand Up @@ -81,12 +78,7 @@ export default util.createRule<Options, MessageIds>({
let useAutoFix = true;
if (util.isDefinitionFile(filename)) {
const scope = context.getScope();
if (
scope.block.parent &&
scope.block.parent.type ===
AST_NODE_TYPES.TSModuleDeclaration &&
scope.block.parent.declare
) {
if (scope.type === 'tsModule' && scope.block.declare) {
useAutoFix = false;
}
}
Expand Down

0 comments on commit fbe16ac

Please sign in to comment.