Skip to content

Commit

Permalink
Merge remote-tracking branch 'upstream/master' into prefer-regexp-exec
Browse files Browse the repository at this point in the history
  • Loading branch information
ldrick committed May 10, 2019
2 parents de05e3e + 6eb97d4 commit 80dff66
Show file tree
Hide file tree
Showing 42 changed files with 14,672 additions and 448 deletions.
2 changes: 2 additions & 0 deletions .eslintrc.json
Expand Up @@ -8,6 +8,8 @@
"extends": ["eslint:recommended", "plugin:@typescript-eslint/recommended"],
"rules": {
"comma-dangle": ["error", "always-multiline"],
"curly": ["error", "all"],
"no-dupe-class-members": "off",
"no-mixed-operators": "error",
"no-console": "off",
"no-undef": "off",
Expand Down
5 changes: 3 additions & 2 deletions packages/eslint-plugin-tslint/package.json
Expand Up @@ -19,10 +19,11 @@
},
"license": "MIT",
"scripts": {
"test": "jest --coverage",
"prebuild": "npm run clean",
"build": "tsc -p tsconfig.build.json",
"clean": "rimraf dist/",
"format": "prettier --write \"./**/*.{ts,js,json,md}\" --ignore-path ../../.prettierignore",
"prebuild": "npm run clean",
"test": "jest --coverage",
"typecheck": "tsc --noEmit"
},
"dependencies": {
Expand Down
1 change: 1 addition & 0 deletions packages/eslint-plugin/README.md
Expand Up @@ -134,6 +134,7 @@ Then you should add `airbnb` (or `airbnb-base`) to your `extends` section of `.e
| [`@typescript-eslint/no-extraneous-class`](./docs/rules/no-extraneous-class.md) | Forbids the use of classes as namespaces (`no-unnecessary-class` from TSLint) | | | |
| [`@typescript-eslint/no-for-in-array`](./docs/rules/no-for-in-array.md) | Disallow iterating over an array with a for-in loop (`no-for-in-array` from TSLint) | | | :thought_balloon: |
| [`@typescript-eslint/no-inferrable-types`](./docs/rules/no-inferrable-types.md) | Disallows explicit type declarations for variables or parameters initialized to a number, string, or boolean. (`no-inferrable-types` from TSLint) | :heavy_check_mark: | :wrench: | |
| [`@typescript-eslint/no-magic-numbers`](./docs/rules/no-magic-numbers.md) | Disallows magic numbers. | :heavy_check_mark: | |
| [`@typescript-eslint/no-misused-new`](./docs/rules/no-misused-new.md) | Enforce valid definition of `new` and `constructor`. (`no-misused-new` from TSLint) | :heavy_check_mark: | | |
| [`@typescript-eslint/no-namespace`](./docs/rules/no-namespace.md) | Disallow the use of custom TypeScript modules and namespaces (`no-namespace` from TSLint) | :heavy_check_mark: | | |
| [`@typescript-eslint/no-non-null-assertion`](./docs/rules/no-non-null-assertion.md) | Disallows non-null assertions using the `!` postfix operator (`no-non-null-assertion` from TSLint) | :heavy_check_mark: | | |
Expand Down
42 changes: 31 additions & 11 deletions packages/eslint-plugin/docs/rules/explicit-function-return-type.md
Expand Up @@ -61,12 +61,19 @@ 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
- `allowTypedFunctionExpressions` if true, type annotations are also allowed on the variable
of a function expression rather than on the function directly.
```ts
type Options = {
// if true, only functions which are part of a declaration will be checked
allowExpressions?: boolean;
// if true, type annotations are also allowed on the variable of a function expression rather than on the function directly.
allowTypedFunctionExpressions?: boolean;
};

By default, `allowExpressions: false` and `allowTypedFunctionExpressions: false` are used,
meaning all declarations and expressions _must_ have a return type.
const defaults = {
allowExpressions: false,
allowTypedFunctionExpressions: false,
};
```

### allowExpressions

Expand All @@ -88,6 +95,20 @@ const foo = arr.map(i => i * i);

### allowTypedFunctionExpressions

Examples of **incorrect** code for this rule with `{ allowTypedFunctionExpressions: true }`:

```ts
let arrowFn = () => 'test';

let funcExpr = function() {
return 'test';
};

let objectProp = {
foo: () => 1,
};
```

Examples of additional **correct** code for this rule with `{ allowTypedFunctionExpressions: true }`:

```ts
Expand All @@ -100,21 +121,20 @@ let funcExpr: FuncType = function() {
};

let asTyped = (() => '') as () => string;
let caasTyped = <() => string>(() => '');

interface ObjectType {
foo(): number;
}
let objectProp: ObjectType = {
foo: () => 1,
};

interface ObjectType {
foo(): number;
}

let asObjectProp = {
let objectPropAs = {
foo: () => 1,
} as ObjectType;
let objectPropCast = <ObjectType>{
foo: () => 1,
};
```

## When Not To Use It
Expand Down
99 changes: 81 additions & 18 deletions packages/eslint-plugin/docs/rules/no-inferrable-types.md
Expand Up @@ -9,23 +9,59 @@ and properties where the type can be easily inferred from its value.

## Options

This rule has an options object:
This rule accepts the following options:

```json
{
"ignoreProperties": false,
"ignoreParameters": false
```ts
interface Options {
ignoreParameters?: boolean;
ignoreProperties?: boolean;
}
```

### Default

When none of the options are truthy, the following patterns are valid:
The default options are:

```JSON
{
"ignoreParameters": true,
"ignoreProperties": true,
}
```

With these options, the following patterns are valid:

```ts
const foo = 5;
const bar = true;
const baz = 'str';
const a = 10n;
const a = -10n;
const a = BigInt(10);
const a = -BigInt(10);
const a = false;
const a = true;
const a = Boolean(null);
const a = !0;
const a = 10;
const a = +10;
const a = -10;
const a = Number('1');
const a = +Number('1');
const a = -Number('1');
const a = Infinity;
const a = +Infinity;
const a = -Infinity;
const a = NaN;
const a = +NaN;
const a = -NaN;
const a = null;
const a = /a/;
const a = RegExp('a');
const a = new RegExp('a');
const a = 'str';
const a = `str`;
const a = String(1);
const a = Symbol('a');
const a = undefined;
const a = void someValue;

class Foo {
prop = 5;
Expand All @@ -39,9 +75,36 @@ function fn(a: number, b: boolean, c: string) {}
The following are invalid:

```ts
const foo: number = 5;
const bar: boolean = true;
const baz: string = 'str';
const a: bigint = 10n;
const a: bigint = -10n;
const a: bigint = BigInt(10);
const a: bigint = -BigInt(10);
const a: boolean = false;
const a: boolean = true;
const a: boolean = Boolean(null);
const a: boolean = !0;
const a: number = 10;
const a: number = +10;
const a: number = -10;
const a: number = Number('1');
const a: number = +Number('1');
const a: number = -Number('1');
const a: number = Infinity;
const a: number = +Infinity;
const a: number = -Infinity;
const a: number = NaN;
const a: number = +NaN;
const a: number = -NaN;
const a: null = null;
const a: RegExp = /a/;
const a: RegExp = RegExp('a');
const a: RegExp = new RegExp('a');
const a: string = 'str';
const a: string = `str`;
const a: string = String(1);
const a: symbol = Symbol('a');
const a: undefined = undefined;
const a: undefined = void someValue;

class Foo {
prop: number = 5;
Expand All @@ -50,23 +113,23 @@ class Foo {
function fn(a: number = 5, b: boolean = true) {}
```

### `ignoreProperties`
### `ignoreParameters`

When set to true, the following pattern is considered valid:

```ts
class Foo {
prop: number = 5;
function foo(a: number = 5, b: boolean = true) {
// ...
}
```

### `ignoreParameters`
### `ignoreProperties`

When set to true, the following pattern is considered valid:

```ts
function foo(a: number = 5, b: boolean = true) {
// ...
class Foo {
prop: number = 5;
}
```

Expand Down
44 changes: 44 additions & 0 deletions packages/eslint-plugin/docs/rules/no-magic-numbers.md
@@ -0,0 +1,44 @@
# Disallow Magic Numbers (@typescript-eslint/no-magic-numbers)

'Magic numbers' are numbers that occur multiple times in code without an explicit meaning.
They should preferably be replaced by named constants.

## Rule Details

The `@typescript-eslint/no-magic-numbers` rule extends the `no-magic-numbers` rule from ESLint core, and adds support for handling Typescript specific code that would otherwise trigger the rule.

See the [ESLint documentation](https://eslint.org/docs/rules/no-magic-numbers) for more details on the `no-magic-numbers` rule.

## Rule Changes

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

In addition to the options supported by the `no-magic-numbers` rule in ESLint core, the rule adds the following options:

### ignoreNumericLiteralTypes

A boolean to specify if numbers used in Typescript numeric literal types are considered okay. `false` by default.

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

```ts
/*eslint @typescript-eslint/no-magic-numbers: ["error", { "ignoreNumericLiteralTypes": false }]*/

type SmallPrimes = 2 | 3 | 5 | 7 | 11;
```

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

```ts
/*eslint @typescript-eslint/no-magic-numbers: ["error", { "ignoreNumericLiteralTypes": true }]*/

type SmallPrimes = 2 | 3 | 5 | 7 | 11;
```

<sup>Taken with ❤️ [from ESLint core](https://github.com/eslint/eslint/blob/master/docs/rules/no-magic-numbers.md)</sup>
9 changes: 5 additions & 4 deletions packages/eslint-plugin/package.json
Expand Up @@ -25,13 +25,14 @@
"license": "MIT",
"main": "dist/index.js",
"scripts": {
"build": "tsc -p tsconfig.build.json",
"clean": "rimraf dist/",
"docs": "eslint-docs",
"docs:check": "eslint-docs check",
"test": "jest --coverage",
"recommended:update": "ts-node tools/update-recommended.ts",
"format": "prettier --write \"./**/*.{ts,js,json,md}\" --ignore-path ../../.prettierignore",
"prebuild": "npm run clean",
"build": "tsc -p tsconfig.build.json",
"clean": "rimraf dist/",
"recommended:update": "ts-node tools/update-recommended.ts",
"test": "jest --coverage",
"typecheck": "tsc --noEmit"
},
"dependencies": {
Expand Down

0 comments on commit 80dff66

Please sign in to comment.