Skip to content

Commit

Permalink
feat(eslint-plugin): apply final v6 changes to configs (#7110)
Browse files Browse the repository at this point in the history
* feat(eslint-plugin): apply final v6 changes to configs

* Ah, fixed new lint rule violations internally

* no-mixed-enums strict

* Apply suggestions from code review

Co-authored-by: Brad Zacher <brad.zacher@gmail.com>

* Fix up build & lint issues

* Never mind, prefer nullish coalescing

---------

Co-authored-by: Brad Zacher <brad.zacher@gmail.com>
  • Loading branch information
JoshuaKGoldberg and bradzacher committed Jun 18, 2023
1 parent a4967f2 commit c13ce0b
Show file tree
Hide file tree
Showing 36 changed files with 468 additions and 63 deletions.
1 change: 0 additions & 1 deletion .eslintrc.js
Expand Up @@ -85,7 +85,6 @@ module.exports = {
'@typescript-eslint/no-non-null-assertion': 'off',
'@typescript-eslint/no-var-requires': 'off',
'@typescript-eslint/prefer-for-of': 'error',
'@typescript-eslint/prefer-nullish-coalescing': 'error',
'@typescript-eslint/prefer-optional-chain': 'error',
'@typescript-eslint/unbound-method': 'off',
'@typescript-eslint/prefer-as-const': 'error',
Expand Down
5 changes: 1 addition & 4 deletions packages/ast-spec/tests/util/serialize-error.ts
Expand Up @@ -2,10 +2,7 @@ import { codeFrameColumns } from '@babel/code-frame';

import { TSError } from './parsers/typescript-estree-import';

export function serializeError(
error: unknown,
contents: string,
): unknown | string {
export function serializeError(error: unknown, contents: string): unknown {
if (!(error instanceof TSError)) {
return error;
}
Expand Down
Expand Up @@ -13,7 +13,9 @@ export = {
'@typescript-eslint/ban-types': 'error',
'no-array-constructor': 'off',
'@typescript-eslint/no-array-constructor': 'error',
'@typescript-eslint/no-base-to-string': 'error',
'@typescript-eslint/no-duplicate-enum-values': 'error',
'@typescript-eslint/no-duplicate-type-constituents': 'error',
'@typescript-eslint/no-extra-non-null-assertion': 'error',
'@typescript-eslint/no-floating-promises': 'error',
'@typescript-eslint/no-for-in-array': 'error',
Expand All @@ -25,12 +27,15 @@ export = {
'@typescript-eslint/no-misused-promises': 'error',
'@typescript-eslint/no-namespace': 'error',
'@typescript-eslint/no-non-null-asserted-optional-chain': 'error',
'@typescript-eslint/no-redundant-type-constituents': 'error',
'@typescript-eslint/no-this-alias': 'error',
'@typescript-eslint/no-unnecessary-type-assertion': 'error',
'@typescript-eslint/no-unnecessary-type-constraint': 'error',
'@typescript-eslint/no-unsafe-argument': 'error',
'@typescript-eslint/no-unsafe-assignment': 'error',
'@typescript-eslint/no-unsafe-call': 'error',
'@typescript-eslint/no-unsafe-declaration-merging': 'error',
'@typescript-eslint/no-unsafe-enum-comparison': 'error',
'@typescript-eslint/no-unsafe-member-access': 'error',
'@typescript-eslint/no-unsafe-return': 'error',
'no-unused-vars': 'off',
Expand Down
1 change: 1 addition & 0 deletions packages/eslint-plugin/src/configs/recommended.ts
Expand Up @@ -21,6 +21,7 @@ export = {
'@typescript-eslint/no-non-null-asserted-optional-chain': 'error',
'@typescript-eslint/no-this-alias': 'error',
'@typescript-eslint/no-unnecessary-type-constraint': 'error',
'@typescript-eslint/no-unsafe-declaration-merging': 'error',
'no-unused-vars': 'off',
'@typescript-eslint/no-unused-vars': 'error',
'@typescript-eslint/no-var-requires': 'error',
Expand Down
2 changes: 2 additions & 0 deletions packages/eslint-plugin/src/configs/strict-type-checked.ts
Expand Up @@ -15,6 +15,7 @@ export = {
'@typescript-eslint/no-array-constructor': 'error',
'@typescript-eslint/no-base-to-string': 'error',
'@typescript-eslint/no-duplicate-enum-values': 'error',
'@typescript-eslint/no-duplicate-type-constituents': 'error',
'@typescript-eslint/no-dynamic-delete': 'error',
'@typescript-eslint/no-explicit-any': 'error',
'@typescript-eslint/no-extra-non-null-assertion': 'error',
Expand All @@ -34,6 +35,7 @@ export = {
'@typescript-eslint/no-non-null-asserted-nullish-coalescing': 'error',
'@typescript-eslint/no-non-null-asserted-optional-chain': 'error',
'@typescript-eslint/no-non-null-assertion': 'error',
'@typescript-eslint/no-redundant-type-constituents': 'error',
'@typescript-eslint/no-this-alias': 'error',
'no-throw-literal': 'off',
'@typescript-eslint/no-throw-literal': 'error',
Expand Down
Expand Up @@ -19,6 +19,7 @@ export = {
'dot-notation': 'off',
'@typescript-eslint/dot-notation': 'error',
'@typescript-eslint/no-confusing-non-null-assertion': 'error',
'@typescript-eslint/no-confusing-void-expression': 'error',
'no-empty-function': 'off',
'@typescript-eslint/no-empty-function': 'error',
'@typescript-eslint/no-empty-interface': 'error',
Expand Down
1 change: 1 addition & 0 deletions packages/eslint-plugin/src/index.ts
Expand Up @@ -17,6 +17,7 @@ export = {
'disable-type-checked': disableTypeChecked,
'eslint-recommended': eslintRecommended,
recommended,
/** @deprecated - please use "recommended-type-checked" instead. */
'recommended-requiring-type-checking': recommendedTypeChecked,
'recommended-type-checked': recommendedTypeChecked,
strict,
Expand Down
Expand Up @@ -84,6 +84,7 @@ export default util.createRule<Options, MessageIds>({
const symbol = services.getSymbolAtLocation(specifier.exported);
const aliasedSymbol = checker.getAliasedSymbol(symbol!);

// eslint-disable-next-line @typescript-eslint/no-unsafe-enum-comparison
if (!aliasedSymbol || aliasedSymbol.escapedName === 'unknown') {
return undefined;
}
Expand Down
4 changes: 2 additions & 2 deletions packages/eslint-plugin/src/rules/no-base-to-string.ts
Expand Up @@ -5,7 +5,7 @@ import * as ts from 'typescript';
import * as util from '../util';

enum Usefulness {
Always,
Always = 'always',
Never = 'will',
Sometimes = 'may',
}
Expand All @@ -23,7 +23,7 @@ export default util.createRule<Options, MessageIds>({
docs: {
description:
'Require `.toString()` to only be called on objects which provide useful information when stringified',
recommended: 'strict',
recommended: 'recommended',
requiresTypeChecking: true,
},
messages: {
Expand Down
Expand Up @@ -28,6 +28,7 @@ export default util.createRule<Options, MessageId>({
docs: {
description:
'Require expressions of type void to appear in statement position',
recommended: 'stylistic',
requiresTypeChecking: true,
},
messages: {
Expand Down
Expand Up @@ -73,6 +73,7 @@ export default util.createRule<Options, MessageIds>({
docs: {
description:
'Disallow duplicate constituents of union or intersection types',
recommended: 'recommended',
requiresTypeChecking: true,
},
fixable: 'code',
Expand Down
3 changes: 2 additions & 1 deletion packages/eslint-plugin/src/rules/no-empty-interface.ts
@@ -1,3 +1,4 @@
import { ScopeType } from '@typescript-eslint/scope-manager';
import type { TSESLint } from '@typescript-eslint/utils';
import { AST_NODE_TYPES } from '@typescript-eslint/utils';

Expand Down Expand Up @@ -84,7 +85,7 @@ export default util.createRule<Options, MessageIds>({

const isInAmbientDeclaration = !!(
util.isDefinitionFile(filename) &&
scope.type === 'tsModule' &&
scope.type === ScopeType.tsModule &&
scope.block.declare
);

Expand Down
1 change: 1 addition & 0 deletions packages/eslint-plugin/src/rules/no-implied-eval.ts
Expand Up @@ -77,6 +77,7 @@ export default util.createRule({
return true;
}

// eslint-disable-next-line @typescript-eslint/no-unsafe-enum-comparison
if (symbol && symbol.escapedName === FUNCTION_CONSTRUCTOR) {
const declarations = symbol.getDeclarations() ?? [];
for (const declaration of declarations) {
Expand Down
3 changes: 2 additions & 1 deletion packages/eslint-plugin/src/rules/no-redeclare.ts
@@ -1,3 +1,4 @@
import { ScopeType } from '@typescript-eslint/scope-manager';
import type { TSESLint, TSESTree } from '@typescript-eslint/utils';
import { AST_NODE_TYPES } from '@typescript-eslint/utils';

Expand Down Expand Up @@ -255,7 +256,7 @@ export default util.createRule<Options, MessageIds>({

// Node.js or ES modules has a special scope.
if (
scope.type === 'global' &&
scope.type === ScopeType.global &&
scope.childScopes[0] &&
// The special scope's block is the Program node.
scope.block === scope.childScopes[0].block
Expand Down
Expand Up @@ -87,6 +87,7 @@ function describeLiteralType(type: ts.Type): string {
}

if (type.isLiteral()) {
// eslint-disable-next-line @typescript-eslint/no-base-to-string
return type.value.toString();
}

Expand Down Expand Up @@ -182,6 +183,7 @@ export default util.createRule({
docs: {
description:
'Disallow members of unions and intersections that do nothing or override type information',
recommended: 'recommended',
requiresTypeChecking: true,
},
messages: {
Expand Down
2 changes: 1 addition & 1 deletion packages/eslint-plugin/src/rules/no-shadow.ts
Expand Up @@ -365,7 +365,7 @@ export default util.createRule<Options, MessageIds>({
): TSESLint.Scope.Scope | null {
const upper = scope.upper;

if (upper?.type === 'function-expression-name') {
if (upper?.type === ScopeType.functionExpressionName) {
return upper.upper;
}
return upper;
Expand Down
Expand Up @@ -10,7 +10,7 @@ export default util.createRule({
type: 'problem',
docs: {
description: 'Disallow unsafe declaration merging',
recommended: 'strict',
recommended: 'recommended',
requiresTypeChecking: false,
},
messages: {
Expand Down
Expand Up @@ -42,7 +42,7 @@ export default util.createRule({
type: 'suggestion',
docs: {
description: 'Disallow comparing an enum value with a non-enum value',
recommended: 'strict',
recommended: 'recommended',
requiresTypeChecking: true,
},
messages: {
Expand Down
6 changes: 3 additions & 3 deletions packages/eslint-plugin/src/rules/prefer-nullish-coalescing.ts
Expand Up @@ -63,9 +63,9 @@ export default util.createRule<Options, MessageIds>({
},
defaultOptions: [
{
ignoreConditionalTests: true,
ignoreTernaryTests: true,
ignoreMixedLogicalExpressions: true,
ignoreConditionalTests: false,
ignoreTernaryTests: false,
ignoreMixedLogicalExpressions: false,
allowRuleToRunWithoutStrictNullChecksIKnowWhatIAmDoing: false,
},
],
Expand Down
Expand Up @@ -13,7 +13,7 @@ export default util.createRule<Options, MessageIds>({
name: 'require-array-sort-compare',
defaultOptions: [
{
ignoreStringArrays: false,
ignoreStringArrays: true,
},
],

Expand Down
7 changes: 6 additions & 1 deletion packages/eslint-plugin/src/rules/restrict-plus-operands.ts
Expand Up @@ -72,19 +72,24 @@ export default util.createRule<Options, MessageIds>({
},
defaultOptions: [
{
allowAny: true,
allowBoolean: true,
allowNullish: true,
allowNumberAndString: true,
allowRegExp: true,
skipCompoundAssignments: false,
},
],
create(
context,
[
{
skipCompoundAssignments,
allowAny,
allowBoolean,
allowNullish,
allowNumberAndString,
allowRegExp,
skipCompoundAssignments,
},
],
) {
Expand Down
Expand Up @@ -71,7 +71,11 @@ export default util.createRule<Options, MessageId>({
},
defaultOptions: [
{
allowAny: true,
allowBoolean: true,
allowNullish: true,
allowNumber: true,
allowRegExp: true,
},
],
create(context, [options]) {
Expand Down
1 change: 1 addition & 0 deletions packages/eslint-plugin/src/rules/unbound-method.ts
Expand Up @@ -271,6 +271,7 @@ function checkMethod(
const firstParam = decl.parameters[0];
const firstParamIsThis =
firstParam?.name.kind === ts.SyntaxKind.Identifier &&
// eslint-disable-next-line @typescript-eslint/no-unsafe-enum-comparison
firstParam?.name.escapedText === 'this';
const thisArgIsVoid =
firstParamIsThis &&
Expand Down
8 changes: 6 additions & 2 deletions packages/eslint-plugin/src/util/collectUnusedVariables.ts
@@ -1,4 +1,8 @@
import { ImplicitLibVariable, Visitor } from '@typescript-eslint/scope-manager';
import {
ImplicitLibVariable,
ScopeType,
Visitor,
} from '@typescript-eslint/scope-manager';
import type { TSESTree } from '@typescript-eslint/utils';
import {
AST_NODE_TYPES,
Expand Down Expand Up @@ -97,7 +101,7 @@ class UnusedVarsVisitor<
const scope = this.#scopeManager.acquire(node, inner);

if (scope) {
if (scope.type === 'function-expression-name') {
if (scope.type === ScopeType.functionExpressionName) {
return scope.childScopes[0] as T;
}
return scope as T;
Expand Down
Expand Up @@ -138,19 +138,37 @@ ruleTester.run('require-array-sort-compare', rule, {
},
{
code: `
function f(a: string[]) {
function f(a: number[]) {
a.sort();
}
`,
errors: [{ messageId: 'requireCompare' }],
},
{
code: `
function f(a: number[]) {
a.sort();
}
`,
errors: [{ messageId: 'requireCompare' }],
options: [{ ignoreStringArrays: false }],
},
{
code: `
function f(a: number | number[]) {
if (Array.isArray(a)) a.sort();
}
`,
errors: [{ messageId: 'requireCompare' }],
},
{
code: `
function f(a: string | string[]) {
if (Array.isArray(a)) a.sort();
}
`,
errors: [{ messageId: 'requireCompare' }],
options: [{ ignoreStringArrays: false }],
},
{
code: `
Expand Down Expand Up @@ -179,32 +197,32 @@ ruleTester.run('require-array-sort-compare', rule, {
// optional chain
{
code: `
function f(a: string[]) {
function f(a: number[]) {
a?.sort();
}
`,
errors: [{ messageId: 'requireCompare' }],
},
{
code: `
['foo', 'bar', 'baz'].sort();
[1, 2, 3].sort();
`,
errors: [{ messageId: 'requireCompare' }],
},
{
code: `
function getString() {
return 'foo';
function getNumber() {
return 1;
}
[getString(), getString()].sort();
[getNumber(), getNumber()].sort();
`,
errors: [{ messageId: 'requireCompare' }],
},
{
code: `
const foo = 'foo';
const bar = 'bar';
const baz = 'baz';
const foo = 1;
const bar = 2;
const baz = 3;
[foo, bar, baz].sort();
`,
errors: [{ messageId: 'requireCompare' }],
Expand Down

0 comments on commit c13ce0b

Please sign in to comment.