diff --git a/packages/eslint-plugin/src/util/index.ts b/packages/eslint-plugin/src/util/index.ts index 4c028489523..454837f5baa 100644 --- a/packages/eslint-plugin/src/util/index.ts +++ b/packages/eslint-plugin/src/util/index.ts @@ -12,7 +12,8 @@ export * from './nullThrows'; export * from './objectIterators'; export * from './propertyTypes'; export * from './requiresQuoting'; -export * from './types'; +// TODO: remove in next major release +export * from '@typescript-eslint/experimental-utils/dist/eslint-utils/types'; // this is done for convenience - saves migrating all of the old rules const { applyDefault, deepMerge, isObjectNotArray, getParserServices } = diff --git a/packages/experimental-utils/package.json b/packages/experimental-utils/package.json index 823bab80838..6b383ca19ec 100644 --- a/packages/experimental-utils/package.json +++ b/packages/experimental-utils/package.json @@ -43,14 +43,17 @@ "@typescript-eslint/scope-manager": "5.8.0", "@typescript-eslint/types": "5.8.0", "@typescript-eslint/typescript-estree": "5.8.0", + "debug": "^4.3.1", "eslint-scope": "^5.1.1", - "eslint-utils": "^3.0.0" + "eslint-utils": "^3.0.0", + "tsutils": "^3.21.0" }, "peerDependencies": { "eslint": "^6.0.0 || ^7.0.0 || ^8.0.0" }, "devDependencies": { - "typescript": "*" + "typescript": "*", + "@typescript-eslint/parser": "^5.8.0" }, "funding": { "type": "opencollective", diff --git a/packages/experimental-utils/src/eslint-utils/index.ts b/packages/experimental-utils/src/eslint-utils/index.ts index bbbe8df2709..2381db1d373 100644 --- a/packages/experimental-utils/src/eslint-utils/index.ts +++ b/packages/experimental-utils/src/eslint-utils/index.ts @@ -5,3 +5,4 @@ export * from './InferTypesFromRule'; export * from './RuleCreator'; export * from './RuleTester'; export * from './deepMerge'; +export * from './types'; diff --git a/packages/eslint-plugin/src/util/types.ts b/packages/experimental-utils/src/eslint-utils/types.ts similarity index 98% rename from packages/eslint-plugin/src/util/types.ts rename to packages/experimental-utils/src/eslint-utils/types.ts index 089620e702a..dbe0c1a5825 100644 --- a/packages/eslint-plugin/src/util/types.ts +++ b/packages/experimental-utils/src/eslint-utils/types.ts @@ -1,7 +1,4 @@ -import { - AST_NODE_TYPES, - TSESTree, -} from '@typescript-eslint/experimental-utils'; +import { AST_NODE_TYPES, TSESTree } from '@typescript-eslint/types'; import debug from 'debug'; import { isCallExpression, @@ -19,7 +16,7 @@ import { } from 'tsutils'; import * as ts from 'typescript'; -const log = debug('typescript-eslint:eslint-plugin:utils:types'); +const log = debug('typescript-eslint:experimental-utils:eslint-utils:types'); /** * Checks if the given type is either an array type, diff --git a/packages/eslint-plugin/tests/util/isUnsafeAssignment.test.ts b/packages/experimental-utils/tests/eslint-utils/isUnsafeAssignment.test.ts similarity index 96% rename from packages/eslint-plugin/tests/util/isUnsafeAssignment.test.ts rename to packages/experimental-utils/tests/eslint-utils/isUnsafeAssignment.test.ts index 1d4946a964d..2470f2a8614 100644 --- a/packages/eslint-plugin/tests/util/isUnsafeAssignment.test.ts +++ b/packages/experimental-utils/tests/eslint-utils/isUnsafeAssignment.test.ts @@ -1,9 +1,13 @@ import * as ts from 'typescript'; -import { TSESTree } from '@typescript-eslint/experimental-utils'; import { parseForESLint } from '@typescript-eslint/parser'; import path from 'path'; -import { getFixturesRootDir } from '../RuleTester'; -import { isUnsafeAssignment } from '../../src/util/types'; +import { TSESTree, ESLintUtils } from '../../src'; + +import isUnsafeAssignment = ESLintUtils.isUnsafeAssignment; + +function getFixturesRootDir(): string { + return path.join(__dirname, '../../../eslint-plugin/tests/fixtures'); +} describe('isUnsafeAssignment', () => { const rootDir = getFixturesRootDir(); diff --git a/packages/experimental-utils/typings/typescript.d.ts b/packages/experimental-utils/typings/typescript.d.ts new file mode 100644 index 00000000000..73304155ee7 --- /dev/null +++ b/packages/experimental-utils/typings/typescript.d.ts @@ -0,0 +1,33 @@ +import 'typescript'; + +declare module 'typescript' { + interface TypeChecker { + // internal TS APIs + + /** + * @returns `true` if the given type is an array type: + * - `Array` + * - `ReadonlyArray` + * - `foo[]` + * - `readonly foo[]` + */ + isArrayType(type: Type): type is TypeReference; + /** + * @returns `true` if the given type is a tuple type: + * - `[foo]` + * - `readonly [foo]` + */ + isTupleType(type: Type): type is TupleTypeReference; + /** + * Return the type of the given property in the given type, or undefined if no such property exists + */ + getTypeOfPropertyOfType(type: Type, propertyName: string): Type | undefined; + } + + interface Type { + /** + * If the type is `any`, and this is set to "error", then TS was unable to resolve the type + */ + intrinsicName?: string; + } +}