Thanks to visit codestin.com
Credit goes to github.com

Skip to content

chore: enabled most of strict-type-checked internally #7339

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 11 additions & 3 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,8 @@ module.exports = {
extends: [
'eslint:recommended',
'plugin:eslint-plugin/recommended',
'plugin:@typescript-eslint/recommended-type-checked',
'plugin:@typescript-eslint/strict-type-checked',
'plugin:@typescript-eslint/stylistic-type-checked',
// TODO: consider enabling strict-type-checked
],
parserOptions: {
sourceType: 'module',
Expand Down Expand Up @@ -53,7 +52,10 @@ module.exports = {
// make sure we're not leveraging any deprecated APIs
'deprecation/deprecation': 'error',

// TODO(#7138): Investigate enabling these soon ✨
// TODO(#7338): Investigate enabling these soon ✨
'@typescript-eslint/consistent-indexed-object-style': 'off',
'@typescript-eslint/no-unnecessary-condition': 'off',
'@typescript-eslint/no-dynamic-delete': 'off',
'@typescript-eslint/prefer-nullish-coalescing': 'off',

// TODO(#7130): Investigate changing these in or removing these from presets
Expand Down Expand Up @@ -85,6 +87,12 @@ module.exports = {
'@typescript-eslint/no-explicit-any': 'error',
'@typescript-eslint/no-non-null-assertion': 'off',
'@typescript-eslint/no-var-requires': 'off',
'@typescript-eslint/prefer-literal-enum-member': [
'error',
{
allowBitwiseExpressions: true,
},
],
'@typescript-eslint/unbound-method': 'off',
'@typescript-eslint/restrict-template-expressions': [
'error',
Expand Down
1 change: 1 addition & 0 deletions packages/ast-spec/tests/ast-node-types.type-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,5 @@ type TakesString<T extends Record<string, string>> = T;
type _Test =
// forcing the test onto a new line so it isn't covered by the expect error
// If there are any enum members that don't have a corresponding TSESTree.Node, then this line will error with "Type 'string | number | symbol' is not assignable to type 'string'."
// eslint-disable-next-line @typescript-eslint/no-invalid-void-type
TakesString<AllKeys> | void;
2 changes: 1 addition & 1 deletion packages/eslint-plugin/src/rules/default-param-last.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ export default createRule({
* @private
*/
function isOptionalParam(node: TSESTree.Parameter): boolean {
return 'optional' in node && node.optional === true;
return 'optional' in node && node.optional;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ export default createRule<Options, MessageIds>({
case AST_NODE_TYPES.Property: {
if (
parent.key.type === AST_NODE_TYPES.Identifier &&
parent.computed === false
!parent.computed
) {
funcName = parent.key.name;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ enum Selectors {
type SelectorsString = keyof typeof Selectors;

enum MetaSelectors {
/* eslint-disable @typescript-eslint/prefer-literal-enum-member */
default = -1,
variableLike = 0 |
Selectors.variable |
Expand Down Expand Up @@ -79,6 +80,7 @@ enum MetaSelectors {
Selectors.classProperty |
Selectors.objectLiteralProperty |
Selectors.typeProperty,
/* eslint-enable @typescript-eslint/prefer-literal-enum-member */
}
type MetaSelectorsString = keyof typeof MetaSelectors;
type IndividualAndMetaSelectorsString = MetaSelectorsString | SelectorsString;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ function parseOptions(context: Context): ParsedOptions {
const result = getEnumNames(Selectors).reduce((acc, k) => {
acc[k] = createValidator(k, context, normalizedOptions);
return acc;
// eslint-disable-next-line @typescript-eslint/prefer-reduce-type-parameter
}, {} as ParsedOptions);

return result;
Expand Down
2 changes: 1 addition & 1 deletion packages/eslint-plugin/src/rules/no-empty-function.ts
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ export default createRule<Options, MessageIds>({
isAllowedOverrideMethods &&
isBodyEmpty(node) &&
node.parent?.type === AST_NODE_TYPES.MethodDefinition &&
node.parent.override === true
node.parent.override
);
}

Expand Down
5 changes: 1 addition & 4 deletions packages/eslint-plugin/src/rules/no-namespace.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,7 @@ export default createRule<Options, MessageIds>({
const filename = context.getFilename();

function isDeclaration(node: TSESTree.Node): boolean {
if (
node.type === AST_NODE_TYPES.TSModuleDeclaration &&
node.declare === true
) {
if (node.type === AST_NODE_TYPES.TSModuleDeclaration && node.declare) {
return true;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,7 @@ function isDefinitionWithAssignment(definition: Definition): boolean {
}

const variableDeclarator = definition.node;
return (
variableDeclarator.definite === true || variableDeclarator.init != null
);
return variableDeclarator.definite || variableDeclarator.init != null;
}

export default createRule({
Expand Down
3 changes: 1 addition & 2 deletions packages/eslint-plugin/src/rules/no-redeclare.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,7 @@ export default createRule<Options, MessageIds>({
node?: TSESTree.Comment | TSESTree.Identifier;
loc?: TSESTree.SourceLocation;
},
void,
unknown
void
> {
if (
options?.builtinGlobals &&
Expand Down
2 changes: 1 addition & 1 deletion packages/eslint-plugin/src/rules/no-shadow.ts
Original file line number Diff line number Diff line change
Expand Up @@ -284,7 +284,7 @@ export default createRule<Options, MessageIds>({
* @returns Whether or not the variable name is allowed.
*/
function isAllowed(variable: TSESLint.Scope.Variable): boolean {
return options.allow!.indexOf(variable.name) !== -1;
return options.allow!.includes(variable.name);
}

/**
Expand Down
2 changes: 1 addition & 1 deletion packages/eslint-plugin/src/rules/no-unsafe-assignment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ export default createRule({
}

let key: string;
if (receiverProperty.computed === false) {
if (!receiverProperty.computed) {
key =
receiverProperty.key.type === AST_NODE_TYPES.Identifier
? receiverProperty.key.name
Expand Down
1 change: 1 addition & 0 deletions packages/eslint-plugin/src/rules/prefer-regexp-exec.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/* eslint-disable @typescript-eslint/prefer-literal-enum-member */
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

enum ArgumentType {
  Other = 0,
  String = 1 << 0,
  RegExp = 1 << 1,
  Both = String | RegExp,
}

🤮

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Doesn't it make you feel like a pro? 😎

import type { TSESTree } from '@typescript-eslint/utils';
import { AST_NODE_TYPES } from '@typescript-eslint/utils';
import * as tsutils from 'ts-api-utils';
Expand Down
2 changes: 1 addition & 1 deletion packages/eslint-plugin/src/util/collectUnusedVariables.ts
Original file line number Diff line number Diff line change
Expand Up @@ -323,7 +323,7 @@ class UnusedVarsVisitor<

protected TSModuleDeclaration(node: TSESTree.TSModuleDeclaration): void {
// -- global augmentation can be in any file, and they do not need exports
if (node.global === true) {
if (node.global) {
this.markVariableAsUsed('global', node.parent);
}
}
Expand Down
1 change: 1 addition & 0 deletions packages/eslint-plugin/src/util/getOperatorPrecedence.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/* eslint-disable @typescript-eslint/prefer-literal-enum-member -- the enums come from TS so to make merging upstream easier we purposely avoid adding literal values. */
import type { TSESTree } from '@typescript-eslint/utils';
import { AST_NODE_TYPES } from '@typescript-eslint/utils';
import { SyntaxKind } from 'typescript';
Expand Down
6 changes: 2 additions & 4 deletions packages/eslint-plugin/tools/generate-configs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,10 +74,8 @@ async function main(): Promise<void> {
);
const EXTENDS = ['./configs/base', './configs/eslint-recommended'];

type RuleEntry = [
string,
TSESLint.RuleModule<string, readonly unknown[], TSESLint.RuleListener>,
];
// TODO: migrate to import { RuleModule } from '@typescript-eslint/utils/ts-eslint'
type RuleEntry = [string, TSESLint.RuleModule<string, readonly unknown[]>];
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TODO - should really migrate to import { RuleModule } from '@typescript-eslint/utils/ts-eslint'
That was the entire reason for adding the export maps


const allRuleEntries: RuleEntry[] = Object.entries(rules).sort((a, b) =>
a[0].localeCompare(b[0]),
Expand Down
2 changes: 1 addition & 1 deletion packages/rule-tester/src/RuleTester.ts
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,7 @@ export class RuleTester extends TestFramework {

// convenience iterator to make it easy to loop all tests without a concat
const allTestsIterator = {
*[Symbol.iterator](): Generator<ValidTestCase<TOptions>, void, unknown> {
*[Symbol.iterator](): Generator<ValidTestCase<TOptions>, void> {
for (const testCase of normalizedTests.valid) {
yield testCase;
}
Expand Down
1 change: 1 addition & 0 deletions packages/rule-tester/src/TestFramework.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ let OVERRIDE_IT_SKIP: Maybe<RuleTesterTestFrameworkFunctionBase> = null;
* allows the user to manually supply functions in case they want to roll their
* own tooling
*/
// eslint-disable-next-line @typescript-eslint/no-extraneous-class
export abstract class TestFramework {
/**
* Runs a function after all the tests in this file have completed.
Expand Down
2 changes: 1 addition & 1 deletion packages/rule-tester/tests/RuleTester.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ beforeEach(() => {
jest.clearAllMocks();
});

const NOOP_RULE: RuleModule<'error', []> = {
const NOOP_RULE: RuleModule<'error'> = {
meta: {
messages: {
error: 'error',
Expand Down
2 changes: 1 addition & 1 deletion packages/scope-manager/src/definition/DefinitionBase.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ abstract class DefinitionBase<
TType extends DefinitionType,
TNode extends TSESTree.Node,
TParent extends TSESTree.Node | null,
TName extends TSESTree.Node = TSESTree.BindingName,
TName extends TSESTree.Node,
> {
/**
* A unique ID for this instance - primarily used to help debugging and testing
Expand Down
6 changes: 1 addition & 5 deletions packages/scope-manager/tests/util/getSpecificNode.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,13 @@
import type { AST_NODE_TYPES, TSESTree } from '@typescript-eslint/types';
import { simpleTraverse } from '@typescript-eslint/typescript-estree';

function getSpecificNode<
TSelector extends AST_NODE_TYPES,
TNode extends Extract<TSESTree.Node, { type: TSelector }>,
>(ast: TSESTree.Node, selector: TSelector): TNode;
function getSpecificNode<
TSelector extends AST_NODE_TYPES,
TNode extends Extract<TSESTree.Node, { type: TSelector }>,
>(
ast: TSESTree.Node,
selector: TSelector,
cb: (node: TNode) => boolean | null | undefined,
cb?: (node: TNode) => boolean | null | undefined,
): TNode;
function getSpecificNode<
TSelector extends AST_NODE_TYPES,
Expand Down
12 changes: 10 additions & 2 deletions packages/scope-manager/tests/util/serializers/DefinitionBase.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,17 @@
import type { TSESTree } from '@typescript-eslint/types';

import { DefinitionBase } from '../../../src/definition/DefinitionBase';
import { createSerializer } from './baseSerializer';

// hacking around the fact that you can't use abstract classes generically
// eslint-disable-next-line @typescript-eslint/no-explicit-any
class DefinitionInstance extends DefinitionBase<any, any, any> {
class DefinitionInstance extends DefinitionBase<
/* eslint-disable @typescript-eslint/no-explicit-any */
any,
any,
any,
/* eslint-enable @typescript-eslint/no-explicit-any */
TSESTree.BindingName
> {
isTypeDefinition = false;
isVariableDefinition = false;
}
Expand Down
2 changes: 1 addition & 1 deletion packages/typescript-estree/src/convert.ts
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ export class Converter {
const isType =
result.type === AST_NODE_TYPES.TSInterfaceDeclaration ||
result.type === AST_NODE_TYPES.TSTypeAliasDeclaration;
const isDeclare = 'declare' in result && result.declare === true;
const isDeclare = 'declare' in result && result.declare;
return this.createNode<TSESTree.ExportNamedDeclaration>(node, {
type: AST_NODE_TYPES.ExportNamedDeclaration,
// @ts-expect-error - TODO, narrow the types here
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ export const DEFAULT_TSCONFIG_CACHE_DURATION_SECONDS = 30;
const ZERO_HR_TIME: [number, number] = [0, 0];

export interface CacheLike<Key, Value> {
get(key: Key): Value | void;
get(key: Key): Value | undefined;
set(key: Key, value: Value): this;
}

Expand Down
4 changes: 2 additions & 2 deletions packages/typescript-estree/tests/lib/parse.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -362,7 +362,7 @@ describe('parseAndGenerateServices', () => {
filePath: join(PROJECT_DIR, filePath),
});
} catch (error) {
throw alignErrorPath(error as Error);
alignErrorPath(error as Error);
}
};

Expand Down Expand Up @@ -499,7 +499,7 @@ describe('parseAndGenerateServices', () => {
filePath: join(PROJECT_DIR, filePath),
});
} catch (error) {
throw alignErrorPath(error as Error);
alignErrorPath(error as Error);
}
};

Expand Down
2 changes: 2 additions & 0 deletions packages/utils/src/eslint-utils/getParserServices.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import type {
const ERROR_MESSAGE =
'You have used a rule which requires parserServices to be generated. You must therefore provide a value for the "parserOptions.project" property for @typescript-eslint/parser.';

/* eslint-disable @typescript-eslint/unified-signatures */
/**
* Try to retrieve type-aware parser service from context.
* This **_will_** throw if it is not available.
Expand Down Expand Up @@ -83,5 +84,6 @@ function getParserServices(

return context.parserServices;
}
/* eslint-enable @typescript-eslint/unified-signatures */

export { getParserServices };
1 change: 0 additions & 1 deletion packages/utils/src/json-schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ export type JSONSchema4TypeExtended =
| JSONSchema4Object;

// Workaround for infinite type recursion
// eslint-disable-next-line @typescript-eslint/consistent-indexed-object-style
export interface JSONSchema4Object {
[key: string]: JSONSchema4TypeExtended;
}
Expand Down
20 changes: 4 additions & 16 deletions packages/utils/src/ts-eslint/SourceCode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ declare class TokenStore {
*/
getTokensAfter<T extends SourceCode.CursorWithCountOptions>(
node: TSESTree.Node | TSESTree.Token,
options?: T,
options?: T | number,
): SourceCode.ReturnTypeFromOptions<T>[];
/**
* Gets the `count` tokens that precedes a given node or token.
Expand All @@ -196,31 +196,19 @@ declare class TokenStore {
*/
getTokensBefore<T extends SourceCode.CursorWithCountOptions>(
node: TSESTree.Node | TSESTree.Token,
options?: T,
): SourceCode.ReturnTypeFromOptions<T>[];
/**
* Gets all of the tokens between two non-overlapping nodes.
* @param left Node before the desired token range.
* @param right Node after the desired token range.
* @param options The option object. If this is a function then it's `options.filter`.
* @returns Tokens between left and right.
*/
getTokensBetween<T extends SourceCode.CursorWithCountOptions>(
left: TSESTree.Node | TSESTree.Token,
right: TSESTree.Node | TSESTree.Token,
padding?: T,
options?: T | number,
): SourceCode.ReturnTypeFromOptions<T>[];
/**
* Gets all of the tokens between two non-overlapping nodes.
* @param left Node before the desired token range.
* @param right Node after the desired token range.
* @param padding Number of extra tokens on either side of center.
* @param options The option object. If this is a number then it's `options.count`. If this is a function then it's `options.filter`.
* @returns Tokens between left and right.
*/
getTokensBetween<T extends SourceCode.CursorWithCountOptions>(
left: TSESTree.Node | TSESTree.Token,
right: TSESTree.Node | TSESTree.Token,
padding?: number,
options?: T | number,
): SourceCode.ReturnTypeFromOptions<T>[];
}

Expand Down
1 change: 1 addition & 0 deletions packages/website-eslint/src/mock/eslint.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/* eslint-disable @typescript-eslint/no-extraneous-class */
class RuleTester {}
class SourceCode {}

Expand Down
2 changes: 1 addition & 1 deletion packages/website/plugins/generated-rule-docs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ const eslintPluginDirectory = path.resolve(
path.join(__dirname, '../../eslint-plugin'),
);

function nodeIsParent(node: unist.Node<unist.Data>): node is unist.Parent {
function nodeIsParent(node: unist.Node): node is unist.Parent {
return 'children' in node;
}

Expand Down
2 changes: 1 addition & 1 deletion packages/website/src/hooks/useHistorySelector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { useHistory } from '@docusaurus/router';
import type * as H from 'history';
import { useSyncExternalStore } from 'react';

export type HistorySelector<T> = (history: H.History<H.LocationState>) => T;
export type HistorySelector<T> = (history: H.History) => T;

export function useHistorySelector<T>(
selector: HistorySelector<T>,
Expand Down