diff --git a/packages/type-utils/tests/isSymbolFromDefaultLibrary.test.ts b/packages/type-utils/tests/isSymbolFromDefaultLibrary.test.ts new file mode 100644 index 00000000000..858aa7d9b53 --- /dev/null +++ b/packages/type-utils/tests/isSymbolFromDefaultLibrary.test.ts @@ -0,0 +1,58 @@ +import { parseForESLint } from '@typescript-eslint/parser'; +import type { TSESTree } from '@typescript-eslint/typescript-estree'; +import path from 'path'; +import type * as ts from 'typescript'; + +import { isSymbolFromDefaultLibrary } from '../src'; +import { expectToHaveParserServices } from './test-utils/expectToHaveParserServices'; + +describe('isSymbolFromDefaultLibrary', () => { + const rootDir = path.join(__dirname, 'fixtures'); + + function getTypes(code: string): { + program: ts.Program; + symbol: ts.Symbol | undefined; + } { + const { services, ast } = parseForESLint(code, { + project: './tsconfig.json', + filePath: path.join(rootDir, 'file.ts'), + tsconfigRootDir: rootDir, + }); + expectToHaveParserServices(services); + const declaration = ast.body[0] as TSESTree.TSTypeAliasDeclaration; + const type = services.getTypeAtLocation(declaration.id); + return { program: services.program, symbol: type.getSymbol() }; + } + + function runTestForAliasDeclaration(code: string, expected: boolean): void { + const { program, symbol } = getTypes(code); + const result = isSymbolFromDefaultLibrary(program, symbol); + expect(result).toBe(expected); + } + + describe('is symbol from default library', () => { + function runTest(code: string): void { + runTestForAliasDeclaration(code, true); + } + + it.each([ + ['type Test = Array;'], + ['type Test = Map;'], + ['type Test = Promise'], + ['type Test = Error'], + ['type Test = Object'], + ])('when code is %s, returns true', runTest); + }); + + describe('is not symbol from default library', () => { + function runTest(code: string): void { + runTestForAliasDeclaration(code, false); + } + + it.each([ + ['const test: Array = [1,2,3];'], + ['type Test = number;'], + ['interface Test { bar: string; };'], + ])('when code is %s, returns false', runTest); + }); +});