diff --git a/packages/eslint-plugin/README.md b/packages/eslint-plugin/README.md index ce069a2aff6d..46f50c752c99 100644 --- a/packages/eslint-plugin/README.md +++ b/packages/eslint-plugin/README.md @@ -140,7 +140,6 @@ Then you should add `airbnb` (or `airbnb-base`) to your `extends` section of `.e | [`@typescript-eslint/member-delimiter-style`](./docs/rules/member-delimiter-style.md) | Require a specific member delimiter style for interfaces and type literals | :heavy_check_mark: | :wrench: | | | [`@typescript-eslint/member-naming`](./docs/rules/member-naming.md) | Enforces naming conventions for class members by visibility | | | | | [`@typescript-eslint/member-ordering`](./docs/rules/member-ordering.md) | Require a consistent member declaration order | | | | -| [`@typescript-eslint/no-angle-bracket-type-assertion`](./docs/rules/no-angle-bracket-type-assertion.md) | Enforces the use of `as Type` assertions instead of `` assertions | :heavy_check_mark: | | | | [`@typescript-eslint/no-array-constructor`](./docs/rules/no-array-constructor.md) | Disallow generic `Array` constructors | :heavy_check_mark: | :wrench: | | | [`@typescript-eslint/no-empty-function`](./docs/rules/no-empty-function.md) | Disallow empty functions | | | | | [`@typescript-eslint/no-empty-interface`](./docs/rules/no-empty-interface.md) | Disallow the declaration of empty interfaces | :heavy_check_mark: | | | @@ -155,7 +154,6 @@ Then you should add `airbnb` (or `airbnb-base`) to your `extends` section of `.e | [`@typescript-eslint/no-misused-promises`](./docs/rules/no-misused-promises.md) | Avoid using promises in places not designed to handle them | | | :thought_balloon: | | [`@typescript-eslint/no-namespace`](./docs/rules/no-namespace.md) | Disallow the use of custom TypeScript modules and namespaces | :heavy_check_mark: | | | | [`@typescript-eslint/no-non-null-assertion`](./docs/rules/no-non-null-assertion.md) | Disallows non-null assertions using the `!` postfix operator | :heavy_check_mark: | | | -| [`@typescript-eslint/no-object-literal-type-assertion`](./docs/rules/no-object-literal-type-assertion.md) | Forbids an object literal to appear in a type assertion expression | :heavy_check_mark: | | | | [`@typescript-eslint/no-parameter-properties`](./docs/rules/no-parameter-properties.md) | Disallow the use of parameter properties in class constructors | :heavy_check_mark: | | | | [`@typescript-eslint/no-require-imports`](./docs/rules/no-require-imports.md) | Disallows invocation of `require()` | | | | | [`@typescript-eslint/no-this-alias`](./docs/rules/no-this-alias.md) | Disallow aliasing `this` | | | | diff --git a/packages/eslint-plugin/ROADMAP.md b/packages/eslint-plugin/ROADMAP.md index 60cdbe7de24f..d37a0fce2b10 100644 --- a/packages/eslint-plugin/ROADMAP.md +++ b/packages/eslint-plugin/ROADMAP.md @@ -26,7 +26,7 @@ | [`no-namespace`] | ✅ | [`@typescript-eslint/no-namespace`] | | [`no-non-null-assertion`] | ✅ | [`@typescript-eslint/no-non-null-assertion`] | | [`no-parameter-reassignment`] | ✅ | [`no-param-reassign`][no-param-reassign] | -| [`no-reference`] | ✅ | [`@typescript-eslint/no-triple-slash-reference`] | +| [`no-reference`] | ✅ | [`@typescript-eslint/triple-slash-reference`] | | [`no-unnecessary-type-assertion`] | ✅ | [`@typescript-eslint/no-unnecessary-type-assertion`] | | [`no-var-requires`] | ✅ | [`@typescript-eslint/no-var-requires`] | | [`only-arrow-functions`] | 🔌 | [`prefer-arrow/prefer-arrow-functions`] | @@ -70,7 +70,7 @@ | [`no-invalid-this`] | 🌟 | [`no-invalid-this`][no-invalid-this] | | [`no-misused-new`] | ✅ | [`@typescript-eslint/no-misused-new`] | | [`no-null-keyword`] | 🔌 | [`no-null/no-null`] (doesn’t handle `null` type) | -| [`no-object-literal-type-assertion`] | ✅ | [`@typescript-eslint/no-object-literal-type-assertion`] | +| [`no-object-literal-type-assertion`] | ✅ | [`@typescript-eslint/consistent-type-assertions`] | | [`no-return-await`] | 🌟 | [`no-return-await`][no-return-await] | | [`no-shadowed-variable`] | 🌟 | [`no-shadow`][no-shadow] | | [`no-sparse-arrays`] | 🌟 | [`no-sparse-arrays`][no-sparse-arrays] | @@ -148,13 +148,13 @@ | [`import-spacing`] | 🔌 | Use [Prettier] | | [`increment-decrement`] | 🌟 | [`no-plusplus`][no-plusplus] | | [`interface-name`] | ✅ | [`@typescript-eslint/interface-name-prefix`] | -| [`interface-over-type-literal`] | ✅ | [`@typescript-eslint/prefer-interface`] | +| [`interface-over-type-literal`] | ✅ | [`@typescript-eslint/consistent-type-definitions`] | | [`jsdoc-format`] | 🌓 | [`valid-jsdoc`][valid-jsdoc] or [`eslint-plugin-jsdoc`][plugin:jsdoc] | | [`match-default-export-name`] | 🛑 | N/A | | [`newline-before-return`] | 🌟 | [`padding-line-between-statements`][padding-line-between-statements] [1] | | [`newline-per-chained-call`] | 🌟 | [`newline-per-chained-call`][newline-per-chained-call] | | [`new-parens`] | 🌟 | [`new-parens`][new-parens] | -| [`no-angle-bracket-type-assertion`] | ✅ | [`@typescript-eslint/no-angle-bracket-type-assertion`] | +| [`no-angle-bracket-type-assertion`] | ✅ | [`@typescript-eslint/consistent-type-assertions`] | | [`no-boolean-literal-compare`] | 🛑 | N/A | | [`no-consecutive-blank-lines`] | 🌟 | [`no-multiple-empty-lines`][no-multiple-empty-lines] | | [`no-irregular-whitespace`] | 🌟 | [`no-irregular-whitespace`][no-irregular-whitespace] with `skipStrings: false` | @@ -577,6 +577,8 @@ Relevant plugins: [`chai-expect-keywords`](https://github.com/gavinaiken/eslint- [`@typescript-eslint/await-thenable`]: https://github.com/typescript-eslint/typescript-eslint/blob/master/packages/eslint-plugin/docs/rules/await-thenable.md [`@typescript-eslint/ban-types`]: https://github.com/typescript-eslint/typescript-eslint/blob/master/packages/eslint-plugin/docs/rules/ban-types.md [`@typescript-eslint/ban-ts-ignore`]: https://github.com/typescript-eslint/typescript-eslint/blob/master/packages/eslint-plugin/docs/rules/ban-ts-ignore.md +[`@typescript-eslint/consistent-type-assertions`]: https://github.com/typescript-eslint/typescript-eslint/blob/master/packages/eslint-plugin/docs/rules/consistent-type-assertions.md +[`@typescript-eslint/consistent-type-definitions`]: https://github.com/typescript-eslint/typescript-eslint/blob/master/packages/eslint-plugin/docs/rules/consistent-type-definitions.md [`@typescript-eslint/explicit-member-accessibility`]: https://github.com/typescript-eslint/typescript-eslint/blob/master/packages/eslint-plugin/docs/rules/explicit-member-accessibility.md [`@typescript-eslint/member-ordering`]: https://github.com/typescript-eslint/typescript-eslint/blob/master/packages/eslint-plugin/docs/rules/member-ordering.md [`@typescript-eslint/no-explicit-any`]: https://github.com/typescript-eslint/typescript-eslint/blob/master/packages/eslint-plugin/docs/rules/no-explicit-any.md @@ -586,14 +588,13 @@ Relevant plugins: [`chai-expect-keywords`](https://github.com/gavinaiken/eslint- [`@typescript-eslint/promise-function-async`]: https://github.com/typescript-eslint/typescript-eslint/blob/master/packages/eslint-plugin/docs/rules/promise-function-async.md [`@typescript-eslint/no-namespace`]: https://github.com/typescript-eslint/typescript-eslint/blob/master/packages/eslint-plugin/docs/rules/no-namespace.md [`@typescript-eslint/no-non-null-assertion`]: https://github.com/typescript-eslint/typescript-eslint/blob/master/packages/eslint-plugin/docs/rules/no-non-null-assertion.md -[`@typescript-eslint/no-triple-slash-reference`]: https://github.com/typescript-eslint/typescript-eslint/blob/master/packages/eslint-plugin/docs/rules/no-triple-slash-reference.md +[`@typescript-eslint/triple-slash-reference`]: https://github.com/typescript-eslint/typescript-eslint/blob/master/packages/eslint-plugin/docs/rules/triple-slash-reference.md [`@typescript-eslint/unbound-method`]: https://github.com/typescript-eslint/typescript-eslint/blob/master/packages/eslint-plugin/docs/rules/unbound-method.md [`@typescript-eslint/no-unnecessary-type-assertion`]: https://github.com/typescript-eslint/typescript-eslint/blob/master/packages/eslint-plugin/docs/rules/no-unnecessary-type-assertion.md [`@typescript-eslint/no-var-requires`]: https://github.com/typescript-eslint/typescript-eslint/blob/master/packages/eslint-plugin/docs/rules/no-var-requires.md [`@typescript-eslint/type-annotation-spacing`]: https://github.com/typescript-eslint/typescript-eslint/blob/master/packages/eslint-plugin/docs/rules/type-annotation-spacing.md [`@typescript-eslint/unified-signatures`]: https://github.com/typescript-eslint/typescript-eslint/blob/master/packages/eslint-plugin/docs/rules/unified-signatures.md [`@typescript-eslint/no-misused-new`]: https://github.com/typescript-eslint/typescript-eslint/blob/master/packages/eslint-plugin/docs/rules/no-misused-new.md -[`@typescript-eslint/no-object-literal-type-assertion`]: https://github.com/typescript-eslint/typescript-eslint/blob/master/packages/eslint-plugin/docs/rules/no-object-literal-type-assertion.md [`@typescript-eslint/no-this-alias`]: https://github.com/typescript-eslint/typescript-eslint/blob/master/packages/eslint-plugin/docs/rules/no-this-alias.md [`@typescript-eslint/no-extraneous-class`]: https://github.com/typescript-eslint/typescript-eslint/blob/master/packages/eslint-plugin/docs/rules/no-extraneous-class.md [`@typescript-eslint/no-unused-vars`]: https://github.com/typescript-eslint/typescript-eslint/blob/master/packages/eslint-plugin/docs/rules/no-unused-vars.md @@ -604,11 +605,9 @@ Relevant plugins: [`chai-expect-keywords`](https://github.com/gavinaiken/eslint- [`@typescript-eslint/array-type`]: https://github.com/typescript-eslint/typescript-eslint/blob/master/packages/eslint-plugin/docs/rules/array-type.md [`@typescript-eslint/class-name-casing`]: https://github.com/typescript-eslint/typescript-eslint/blob/master/packages/eslint-plugin/docs/rules/class-name-casing.md [`@typescript-eslint/interface-name-prefix`]: https://github.com/typescript-eslint/typescript-eslint/blob/master/packages/eslint-plugin/docs/rules/interface-name-prefix.md -[`@typescript-eslint/no-angle-bracket-type-assertion`]: https://github.com/typescript-eslint/typescript-eslint/blob/master/packages/eslint-plugin/docs/rules/no-angle-bracket-type-assertion.md [`@typescript-eslint/no-parameter-properties`]: https://github.com/typescript-eslint/typescript-eslint/blob/master/packages/eslint-plugin/docs/rules/no-parameter-properties.md [`@typescript-eslint/member-delimiter-style`]: https://github.com/typescript-eslint/typescript-eslint/blob/master/packages/eslint-plugin/docs/rules/member-delimiter-style.md [`@typescript-eslint/prefer-for-of`]: https://github.com/typescript-eslint/typescript-eslint/blob/master/packages/eslint-plugin/docs/rules/prefer-for-of.md -[`@typescript-eslint/prefer-interface`]: https://github.com/typescript-eslint/typescript-eslint/blob/master/packages/eslint-plugin/docs/rules/prefer-interface.md [`@typescript-eslint/no-array-constructor`]: https://github.com/typescript-eslint/typescript-eslint/blob/master/packages/eslint-plugin/docs/rules/no-array-constructor.md [`@typescript-eslint/prefer-function-type`]: https://github.com/typescript-eslint/typescript-eslint/blob/master/packages/eslint-plugin/docs/rules/prefer-function-type.md [`@typescript-eslint/prefer-readonly`]: https://github.com/typescript-eslint/typescript-eslint/blob/master/packages/eslint-plugin/docs/rules/prefer-readonly.md diff --git a/packages/eslint-plugin/docs/rules/no-angle-bracket-type-assertion.md b/packages/eslint-plugin/docs/rules/no-angle-bracket-type-assertion.md deleted file mode 100644 index 68cf1591a75f..000000000000 --- a/packages/eslint-plugin/docs/rules/no-angle-bracket-type-assertion.md +++ /dev/null @@ -1,32 +0,0 @@ -# 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. - -## Rule Details - -This rule aims to standardise the use of type assertion style across the codebase - -The following patterns are considered warnings: - -```ts -const foo = bar; -``` - -The following patterns are not warnings: - -```ts -const foo = bar as Foo; -``` - -## When Not To Use It - -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) - -## Compatibility - -- TSLint: [no-angle-bracket-type-assertion](https://palantir.github.io/tslint/rules/no-angle-bracket-type-assertion/) diff --git a/packages/eslint-plugin/docs/rules/no-object-literal-type-assertion.md b/packages/eslint-plugin/docs/rules/no-object-literal-type-assertion.md deleted file mode 100644 index 044c68aa57fd..000000000000 --- a/packages/eslint-plugin/docs/rules/no-object-literal-type-assertion.md +++ /dev/null @@ -1,33 +0,0 @@ -# Forbids an object literal to appear in a type assertion expression (no-object-literal-type-assertion) - -Always prefer `const x: T = { ... };` to `const x = { ... } as T;`. Casting to `any` and `unknown` is still allowed, and const assertions (`as const`) are still allowed. - -## Rule Details - -Examples of **incorrect** code for this rule. - -```ts -const x = { ... } as T; -``` - -Examples of **correct** code for this rule. - -```ts -const x: T = { ... }; -const y = { ... } as any; -const z = { ... } as unknown; -``` - -## Options - -```cjson -{ - "@typescript-eslint/no-object-literal-type-assertion": ["error", { - allowAsParameter: false // Allow type assertion in call and new expression, default false - }] -} -``` - -## Compatibility - -- TSLint: [no-object-literal-type-assertion](https://palantir.github.io/tslint/rules/no-object-literal-type-assertion/) diff --git a/packages/eslint-plugin/docs/rules/no-triple-slash-reference.md b/packages/eslint-plugin/docs/rules/no-triple-slash-reference.md deleted file mode 100644 index 409ff6093ca1..000000000000 --- a/packages/eslint-plugin/docs/rules/no-triple-slash-reference.md +++ /dev/null @@ -1,43 +0,0 @@ -# Disallow `/// ` comments (no-triple-slash-reference) - -Triple-slash reference directive comments should not be used anymore. Use `import` instead. - -Before TypeScript adopted ES6 Module syntax, -triple-slash reference directives were used to specify dependencies. -Now that we have `import`, triple-slash reference directives are discouraged for specifying dependencies -in favor of `import`. - -A triple-slash reference directive is a comment beginning with three slashes followed by a path to the module being imported: -`/// `. -ES6 Modules handle this now: -`import animal from "./Animal"` - -## DEPRECATED - this rule has been deprecated in favour of [`triple-slash-reference`](./triple-slash-reference.md) - -## Rule Details - -Does not allow the use of `/// ` comments. - -The following patterns are considered warnings: - -```ts -/// -``` - -The following patterns are not warnings: - -```ts -import Animal from 'Animal'; -``` - -## When Not To Use It - -If you use `/// ` style imports. - -## Further Reading - -- 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/) diff --git a/packages/eslint-plugin/docs/rules/prefer-interface.md b/packages/eslint-plugin/docs/rules/prefer-interface.md deleted file mode 100644 index 8b995e6b06c7..000000000000 --- a/packages/eslint-plugin/docs/rules/prefer-interface.md +++ /dev/null @@ -1,36 +0,0 @@ -# Prefer an interface declaration over a type literal (type T = { ... }) (prefer-interface)\ - -Interfaces are generally preferred over type literals because interfaces can be implemented, extended and merged. - -## DEPRECATED - this rule has been deprecated in favour of [`consistent-type-definitions`](./consistent-type-definitions.md) - -## Rule Details - -Examples of **incorrect** code for this rule. - -```ts -type T = { x: number }; -``` - -Examples of **correct** code for this rule. - -```ts -type T = string; -type Foo = string | {}; - -interface T { - x: number; -} -``` - -## Options - -```CJSON -{ - "interface-over-type-literal": "error" -} -``` - -## Compatibility - -- TSLint: [interface-over-type-literal](https://palantir.github.io/tslint/rules/interface-over-type-literal/) diff --git a/packages/eslint-plugin/src/configs/all.json b/packages/eslint-plugin/src/configs/all.json index cf3f61c69cd6..8109d62c3094 100644 --- a/packages/eslint-plugin/src/configs/all.json +++ b/packages/eslint-plugin/src/configs/all.json @@ -21,7 +21,6 @@ "@typescript-eslint/member-delimiter-style": "error", "@typescript-eslint/member-naming": "error", "@typescript-eslint/member-ordering": "error", - "@typescript-eslint/no-angle-bracket-type-assertion": "error", "no-array-constructor": "off", "@typescript-eslint/no-array-constructor": "error", "@typescript-eslint/no-empty-function": "error", @@ -39,7 +38,6 @@ "@typescript-eslint/no-misused-promises": "error", "@typescript-eslint/no-namespace": "error", "@typescript-eslint/no-non-null-assertion": "error", - "@typescript-eslint/no-object-literal-type-assertion": "error", "@typescript-eslint/no-parameter-properties": "error", "@typescript-eslint/no-require-imports": "error", "@typescript-eslint/no-this-alias": "error", diff --git a/packages/eslint-plugin/src/configs/recommended.json b/packages/eslint-plugin/src/configs/recommended.json index d26fd25d01c1..e8685fbbe154 100644 --- a/packages/eslint-plugin/src/configs/recommended.json +++ b/packages/eslint-plugin/src/configs/recommended.json @@ -13,7 +13,6 @@ "@typescript-eslint/indent": "error", "@typescript-eslint/interface-name-prefix": "error", "@typescript-eslint/member-delimiter-style": "error", - "@typescript-eslint/no-angle-bracket-type-assertion": "error", "no-array-constructor": "off", "@typescript-eslint/no-array-constructor": "error", "@typescript-eslint/no-empty-interface": "error", @@ -22,15 +21,12 @@ "@typescript-eslint/no-misused-new": "error", "@typescript-eslint/no-namespace": "error", "@typescript-eslint/no-non-null-assertion": "error", - "@typescript-eslint/no-object-literal-type-assertion": "error", "@typescript-eslint/no-parameter-properties": "error", - "@typescript-eslint/no-triple-slash-reference": "error", "no-unused-vars": "off", "@typescript-eslint/no-unused-vars": "warn", "no-use-before-define": "off", "@typescript-eslint/no-use-before-define": "error", "@typescript-eslint/no-var-requires": "error", - "@typescript-eslint/prefer-interface": "error", "@typescript-eslint/prefer-namespace-keyword": "error", "@typescript-eslint/type-annotation-spacing": "error" } diff --git a/packages/eslint-plugin/src/rules/index.ts b/packages/eslint-plugin/src/rules/index.ts index 482806640756..cf59df039ab7 100644 --- a/packages/eslint-plugin/src/rules/index.ts +++ b/packages/eslint-plugin/src/rules/index.ts @@ -15,7 +15,6 @@ import interfaceNamePrefix from './interface-name-prefix'; import memberDelimiterStyle from './member-delimiter-style'; import memberNaming from './member-naming'; import memberOrdering from './member-ordering'; -import noAngleBracketTypeAssertion from './no-angle-bracket-type-assertion'; import noArrayConstructor from './no-array-constructor'; import noEmptyFunction from './no-empty-function'; import noEmptyInterface from './no-empty-interface'; @@ -30,11 +29,9 @@ import noMisusedNew from './no-misused-new'; import noMisusedPromises from './no-misused-promises'; import noNamespace from './no-namespace'; import noNonNullAssertion from './no-non-null-assertion'; -import noObjectLiteralTypeAssertion from './no-object-literal-type-assertion'; import noParameterProperties from './no-parameter-properties'; import noRequireImports from './no-require-imports'; import noThisAlias from './no-this-alias'; -import noTripleSlashReference from './no-triple-slash-reference'; import noTypeAlias from './no-type-alias'; import noUnnecessaryQualifier from './no-unnecessary-qualifier'; import noUnnecessaryTypeAssertion from './no-unnecessary-type-assertion'; @@ -45,7 +42,6 @@ import noVarRequires from './no-var-requires'; import preferForOf from './prefer-for-of'; import preferFunctionType from './prefer-function-type'; import preferIncludes from './prefer-includes'; -import preferInterface from './prefer-interface'; import preferNamespaceKeyword from './prefer-namespace-keyword'; import preferReadonly from './prefer-readonly'; import preferRegexpExec from './prefer-regexp-exec'; @@ -79,7 +75,6 @@ export default { 'member-delimiter-style': memberDelimiterStyle, 'member-naming': memberNaming, 'member-ordering': memberOrdering, - 'no-angle-bracket-type-assertion': noAngleBracketTypeAssertion, 'no-array-constructor': noArrayConstructor, 'no-empty-function': noEmptyFunction, 'no-empty-interface': noEmptyInterface, @@ -94,11 +89,9 @@ export default { 'no-misused-promises': noMisusedPromises, 'no-namespace': noNamespace, 'no-non-null-assertion': noNonNullAssertion, - 'no-object-literal-type-assertion': noObjectLiteralTypeAssertion, 'no-parameter-properties': noParameterProperties, 'no-require-imports': noRequireImports, 'no-this-alias': noThisAlias, - 'no-triple-slash-reference': noTripleSlashReference, 'no-type-alias': noTypeAlias, 'no-unnecessary-qualifier': noUnnecessaryQualifier, 'no-unnecessary-type-assertion': noUnnecessaryTypeAssertion, @@ -109,7 +102,6 @@ export default { 'prefer-for-of': preferForOf, 'prefer-function-type': preferFunctionType, 'prefer-includes': preferIncludes, - 'prefer-interface': preferInterface, 'prefer-namespace-keyword': preferNamespaceKeyword, 'prefer-readonly': preferReadonly, 'prefer-regexp-exec': preferRegexpExec, diff --git a/packages/eslint-plugin/src/rules/no-angle-bracket-type-assertion.ts b/packages/eslint-plugin/src/rules/no-angle-bracket-type-assertion.ts deleted file mode 100644 index 82848a07196f..000000000000 --- a/packages/eslint-plugin/src/rules/no-angle-bracket-type-assertion.ts +++ /dev/null @@ -1,34 +0,0 @@ -import * as util from '../util'; - -export default util.createRule({ - name: 'no-angle-bracket-type-assertion', - meta: { - type: 'problem', - docs: { - description: - 'Enforces the use of `as Type` assertions instead of `` assertions', - category: 'Stylistic Issues', - recommended: 'error', - }, - messages: { - preferAs: - "Prefer 'as {{cast}}' instead of '<{{cast}}>' when doing type assertions.", - }, - schema: [], - }, - defaultOptions: [], - create(context) { - const sourceCode = context.getSourceCode(); - return { - TSTypeAssertion(node) { - context.report({ - node, - messageId: 'preferAs', - data: { - cast: sourceCode.getText(node.typeAnnotation), - }, - }); - }, - }; - }, -}); diff --git a/packages/eslint-plugin/src/rules/no-object-literal-type-assertion.ts b/packages/eslint-plugin/src/rules/no-object-literal-type-assertion.ts deleted file mode 100644 index f40896553370..000000000000 --- a/packages/eslint-plugin/src/rules/no-object-literal-type-assertion.ts +++ /dev/null @@ -1,91 +0,0 @@ -import { - AST_NODE_TYPES, - TSESTree, -} from '@typescript-eslint/experimental-utils'; -import * as util from '../util'; - -type Options = [ - { - allowAsParameter?: boolean; - } -]; -type MessageIds = 'unexpectedTypeAssertion'; - -export default util.createRule({ - name: 'no-object-literal-type-assertion', - meta: { - type: 'problem', - docs: { - description: - 'Forbids an object literal to appear in a type assertion expression', - category: 'Stylistic Issues', - recommended: 'error', - }, - messages: { - unexpectedTypeAssertion: - 'Type assertion on object literals is forbidden, use a type annotation instead.', - }, - schema: [ - { - type: 'object', - additionalProperties: false, - properties: { - allowAsParameter: { - type: 'boolean', - }, - }, - }, - ], - }, - defaultOptions: [ - { - allowAsParameter: false, - }, - ], - create(context, [{ allowAsParameter }]) { - /** - * Check whatever node should be reported - * @param node the node to be evaluated. - */ - function checkType(node: TSESTree.TypeNode): boolean { - switch (node.type) { - case AST_NODE_TYPES.TSAnyKeyword: - case AST_NODE_TYPES.TSUnknownKeyword: - return false; - case AST_NODE_TYPES.TSTypeReference: - // Ignore `as const` and `` (#166) - return ( - node.typeName.type === AST_NODE_TYPES.Identifier && - node.typeName.name !== 'const' - ); - default: - return true; - } - } - - return { - 'TSTypeAssertion, TSAsExpression'( - node: TSESTree.TSTypeAssertion | TSESTree.TSAsExpression, - ) { - if ( - allowAsParameter && - node.parent && - (node.parent.type === AST_NODE_TYPES.NewExpression || - node.parent.type === AST_NODE_TYPES.CallExpression) - ) { - return; - } - - if ( - checkType(node.typeAnnotation) && - node.expression.type === AST_NODE_TYPES.ObjectExpression - ) { - context.report({ - node, - messageId: 'unexpectedTypeAssertion', - }); - } - }, - }; - }, -}); diff --git a/packages/eslint-plugin/src/rules/no-triple-slash-reference.ts b/packages/eslint-plugin/src/rules/no-triple-slash-reference.ts deleted file mode 100644 index 49f164201664..000000000000 --- a/packages/eslint-plugin/src/rules/no-triple-slash-reference.ts +++ /dev/null @@ -1,42 +0,0 @@ -import * as util from '../util'; - -export default util.createRule({ - name: 'no-triple-slash-reference', - meta: { - type: 'suggestion', - docs: { - description: 'Disallow `/// ` comments', - category: 'Best Practices', - recommended: 'error', - }, - schema: [], - deprecated: true, - replacedBy: ['triple-slash-reference'], - messages: { - noTripleSlashReference: 'Do not use a triple slash reference.', - }, - }, - defaultOptions: [], - create(context) { - const referenceRegExp = /^\/\s* { - if (comment.type !== 'Line') { - return; - } - if (referenceRegExp.test(comment.value)) { - context.report({ - node: comment, - messageId: 'noTripleSlashReference', - }); - } - }); - }, - }; - }, -}); diff --git a/packages/eslint-plugin/src/rules/prefer-interface.ts b/packages/eslint-plugin/src/rules/prefer-interface.ts deleted file mode 100644 index 555e9646b2ba..000000000000 --- a/packages/eslint-plugin/src/rules/prefer-interface.ts +++ /dev/null @@ -1,64 +0,0 @@ -import { TSESLint, TSESTree } from '@typescript-eslint/experimental-utils'; -import * as util from '../util'; - -export default util.createRule({ - name: 'prefer-interface', - meta: { - type: 'suggestion', - docs: { - description: - 'Prefer an interface declaration over a type literal (type T = { ... })', - category: 'Stylistic Issues', - recommended: 'error', - }, - fixable: 'code', - messages: { - interfaceOverType: 'Use an interface instead of a type literal.', - }, - schema: [], - deprecated: true, - replacedBy: ['consistent-type-definitions'], - }, - defaultOptions: [], - create(context) { - const sourceCode = context.getSourceCode(); - - return { - // VariableDeclaration with kind type has only one VariableDeclarator - "TSTypeAliasDeclaration[typeAnnotation.type='TSTypeLiteral']"( - node: TSESTree.TSTypeAliasDeclaration, - ) { - context.report({ - node: node.id, - messageId: 'interfaceOverType', - fix(fixer) { - const typeNode = node.typeParameters || node.id; - const fixes: TSESLint.RuleFix[] = []; - - const firstToken = sourceCode.getFirstToken(node); - if (firstToken) { - fixes.push(fixer.replaceText(firstToken, 'interface')); - fixes.push( - fixer.replaceTextRange( - [typeNode.range[1], node.typeAnnotation.range[0]], - ' ', - ), - ); - } - - const afterToken = sourceCode.getTokenAfter(node.typeAnnotation); - if ( - afterToken && - afterToken.type === 'Punctuator' && - afterToken.value === ';' - ) { - fixes.push(fixer.remove(afterToken)); - } - - return fixes; - }, - }); - }, - }; - }, -}); diff --git a/packages/eslint-plugin/tests/rules/no-angle-bracket-type-assertion.test.ts b/packages/eslint-plugin/tests/rules/no-angle-bracket-type-assertion.test.ts deleted file mode 100644 index 1d8bd4b23212..000000000000 --- a/packages/eslint-plugin/tests/rules/no-angle-bracket-type-assertion.test.ts +++ /dev/null @@ -1,164 +0,0 @@ -import rule from '../../src/rules/no-angle-bracket-type-assertion'; -import { RuleTester } from '../RuleTester'; - -const ruleTester = new RuleTester({ - parser: '@typescript-eslint/parser', -}); - -ruleTester.run('no-angle-bracket-type-assertion', rule, { - valid: [ - ` -interface Foo { - bar : number; - bas : string; -} - -class Generic implements Foo {} - -const foo = {} as Foo; -const bar = new Generic() as Foo; - `, - 'const array : Array = [];', - ` -class A {} -class B extends A {} - -const b : B = new B(); -const a : A = b as A; - `, - ` -type A = { - num: number -}; - -const b = { - num: 5 -}; - -const a: A = b as A; - `, - 'const a : number = 5 as number', - ` -const a : number = 5; -const b : number = a as number; - `, - 'const a : Array = [1] as Array;', - ], - invalid: [ - { - code: ` -interface Foo { - bar : number; - bas : string; -} - -class Generic implements Foo {} - -const foo = {}; -const bar = new Generic(); - `, - errors: [ - { - messageId: 'preferAs', - data: { - cast: 'Foo', - }, - line: 9, - column: 13, - }, - { - messageId: 'preferAs', - data: { - cast: 'Foo', - }, - line: 10, - column: 13, - }, - ], - }, - { - code: 'const a : number = 5', - errors: [ - { - messageId: 'preferAs', - data: { - cast: 'number', - }, - line: 1, - column: 20, - }, - ], - }, - { - code: ` -const a : number = 5; -const b : number = a; - `, - errors: [ - { - messageId: 'preferAs', - data: { - cast: 'number', - }, - line: 3, - column: 20, - }, - ], - }, - { - code: 'const a : Array = >[1];', - errors: [ - { - messageId: 'preferAs', - data: { - cast: 'Array', - }, - line: 1, - column: 27, - }, - ], - }, - { - code: ` -class A {} -class B extends A {} - -const b : B = new B(); -const a : A = b; - `, - errors: [ - { - messageId: 'preferAs', - data: { - cast: 'A', - }, - line: 6, - column: 15, - }, - ], - }, - { - code: ` -type A = { - num: number -}; - -const b = { - num: 5 -}; - -const a: A = b; - `, - errors: [ - { - messageId: 'preferAs', - data: { - cast: 'A', - }, - line: 10, - column: 14, - }, - ], - }, - ], -}); diff --git a/packages/eslint-plugin/tests/rules/no-object-literal-type-assertion.test.ts b/packages/eslint-plugin/tests/rules/no-object-literal-type-assertion.test.ts deleted file mode 100644 index 336df4c29a1b..000000000000 --- a/packages/eslint-plugin/tests/rules/no-object-literal-type-assertion.test.ts +++ /dev/null @@ -1,100 +0,0 @@ -import rule from '../../src/rules/no-object-literal-type-assertion'; -import { RuleTester } from '../RuleTester'; - -const ruleTester = new RuleTester({ - parser: '@typescript-eslint/parser', - parserOptions: { - ecmaVersion: 6, - sourceType: 'module', - ecmaFeatures: { - jsx: false, - }, - }, -}); - -ruleTester.run('no-object-literal-type-assertion', rule, { - valid: [ - ` x;`, - `x as T;`, - `const foo = bar;`, - `const foo: baz = bar;`, - `const x: T = {};`, - `const foo = { bar: { } };`, - // Allow cast to 'any' - `const foo = {} as any;`, - `const foo = {};`, - // Allow cast to 'unknown' - `const foo = {} as unknown;`, - `const foo = {};`, - `const foo = {} as const;`, - `const foo = {};`, - { - code: `print({ bar: 5 } as Foo)`, - options: [ - { - allowAsParameter: true, - }, - ], - }, - { - code: `new print({ bar: 5 } as Foo)`, - options: [ - { - allowAsParameter: true, - }, - ], - }, - ], - invalid: [ - { - code: ` ({});`, - errors: [ - { - messageId: 'unexpectedTypeAssertion', - line: 1, - column: 1, - }, - ], - }, - { - code: `({}) as T;`, - errors: [ - { - messageId: 'unexpectedTypeAssertion', - line: 1, - column: 1, - }, - ], - }, - { - code: `const x = {} as T;`, - errors: [ - { - messageId: 'unexpectedTypeAssertion', - line: 1, - column: 11, - }, - ], - }, - { - code: `print({ bar: 5 } as Foo)`, - errors: [ - { - messageId: 'unexpectedTypeAssertion', - line: 1, - column: 7, - }, - ], - }, - { - code: `new print({ bar: 5 } as Foo)`, - errors: [ - { - messageId: 'unexpectedTypeAssertion', - line: 1, - column: 11, - }, - ], - }, - ], -}); diff --git a/packages/eslint-plugin/tests/rules/no-triple-slash-reference.test.ts b/packages/eslint-plugin/tests/rules/no-triple-slash-reference.test.ts deleted file mode 100644 index f120d5262432..000000000000 --- a/packages/eslint-plugin/tests/rules/no-triple-slash-reference.test.ts +++ /dev/null @@ -1,46 +0,0 @@ -import rule from '../../src/rules/no-triple-slash-reference'; -import { RuleTester } from '../RuleTester'; - -const ruleTester = new RuleTester({ - parser: '@typescript-eslint/parser', -}); - -ruleTester.run('no-triple-slash-reference', rule, { - valid: [ - `/// `, - `/// `, - `/// `, - '/// Non-reference triple-slash comment', - "// ", - `/* -/// -let a -*/`, - ], - invalid: [ - { - code: '/// ', - errors: [ - { - messageId: 'noTripleSlashReference', - line: 1, - column: 1, - }, - ], - }, - { - code: ` -/// -let a - `, - parser: '@typescript-eslint/parser', - errors: [ - { - messageId: 'noTripleSlashReference', - line: 2, - column: 1, - }, - ], - }, - ], -}); diff --git a/packages/eslint-plugin/tests/rules/prefer-interface.test.ts b/packages/eslint-plugin/tests/rules/prefer-interface.test.ts deleted file mode 100644 index ea09c3eb91f6..000000000000 --- a/packages/eslint-plugin/tests/rules/prefer-interface.test.ts +++ /dev/null @@ -1,73 +0,0 @@ -import rule from '../../src/rules/prefer-interface'; -import { RuleTester } from '../RuleTester'; - -const ruleTester = new RuleTester({ - parser: '@typescript-eslint/parser', -}); - -ruleTester.run('interface-over-type-literal', rule, { - valid: [ - `var foo = { };`, - `type U = string;`, - `type V = { x: number; } | { y: string; };`, - ` -type Record = { - [K in T]: U; -} - `, - ], - invalid: [ - { - code: `type T = { x: number; }`, - output: `interface T { x: number; }`, - errors: [ - { - messageId: 'interfaceOverType', - line: 1, - column: 6, - }, - ], - }, - { - code: `type T={ x: number; }`, - output: `interface T { x: number; }`, - errors: [ - { - messageId: 'interfaceOverType', - line: 1, - column: 6, - }, - ], - }, - { - code: `type T= { x: number; }`, - output: `interface T { x: number; }`, - errors: [ - { - messageId: 'interfaceOverType', - line: 1, - column: 6, - }, - ], - }, - { - code: ` -export type W = { - x: T, -}; -`, - output: ` -export interface W { - x: T, -} -`, - errors: [ - { - messageId: 'interfaceOverType', - line: 2, - column: 13, - }, - ], - }, - ], -});