@typescript-eslint/type-utils
Advanced tools
Comparing version
import type { JSONSchema4 } from '@typescript-eslint/utils/json-schema'; | ||
import type * as ts from 'typescript'; | ||
interface FileSpecifier { | ||
export interface FileSpecifier { | ||
from: 'file'; | ||
@@ -8,7 +8,7 @@ name: string[] | string; | ||
} | ||
interface LibSpecifier { | ||
export interface LibSpecifier { | ||
from: 'lib'; | ||
name: string[] | string; | ||
} | ||
interface PackageSpecifier { | ||
export interface PackageSpecifier { | ||
from: 'package'; | ||
@@ -21,3 +21,2 @@ name: string[] | string; | ||
export declare function typeMatchesSpecifier(type: ts.Type, specifier: TypeOrValueSpecifier, program: ts.Program): boolean; | ||
export {}; | ||
//# sourceMappingURL=TypeOrValueSpecifier.d.ts.map |
@@ -25,11 +25,10 @@ "use strict"; | ||
}; | ||
var __importDefault = (this && this.__importDefault) || function (mod) { | ||
return (mod && mod.__esModule) ? mod : { "default": mod }; | ||
}; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.typeOrValueSpecifierSchema = void 0; | ||
exports.typeMatchesSpecifier = typeMatchesSpecifier; | ||
const typescript_estree_1 = require("@typescript-eslint/typescript-estree"); | ||
const path_1 = __importDefault(require("path")); | ||
const tsutils = __importStar(require("ts-api-utils")); | ||
const specifierNameMatches_1 = require("./typeOrValueSpecifiers/specifierNameMatches"); | ||
const typeDeclaredInFile_1 = require("./typeOrValueSpecifiers/typeDeclaredInFile"); | ||
const typeDeclaredInLib_1 = require("./typeOrValueSpecifiers/typeDeclaredInLib"); | ||
const typeDeclaredInPackageDeclarationFile_1 = require("./typeOrValueSpecifiers/typeDeclaredInPackageDeclarationFile"); | ||
exports.typeOrValueSpecifierSchema = { | ||
@@ -126,42 +125,2 @@ oneOf: [ | ||
}; | ||
function specifierNameMatches(type, name) { | ||
if (typeof name === 'string') { | ||
name = [name]; | ||
} | ||
if (name.some(item => item === type.intrinsicName)) { | ||
return true; | ||
} | ||
const symbol = type.aliasSymbol ?? type.getSymbol(); | ||
if (symbol === undefined) { | ||
return false; | ||
} | ||
return name.some(item => item === symbol.escapedName); | ||
} | ||
function typeDeclaredInFile(relativePath, declarationFiles, program) { | ||
if (relativePath === undefined) { | ||
const cwd = (0, typescript_estree_1.getCanonicalFileName)(program.getCurrentDirectory()); | ||
return declarationFiles.some(declaration => (0, typescript_estree_1.getCanonicalFileName)(declaration.fileName).startsWith(cwd)); | ||
} | ||
const absolutePath = (0, typescript_estree_1.getCanonicalFileName)(path_1.default.join(program.getCurrentDirectory(), relativePath)); | ||
return declarationFiles.some(declaration => (0, typescript_estree_1.getCanonicalFileName)(declaration.fileName) === absolutePath); | ||
} | ||
function typeDeclaredInPackage(packageName, declarationFiles, program) { | ||
// Handle scoped packages - if the name starts with @, remove it and replace / with __ | ||
const typesPackageName = packageName.replace(/^@([^/]+)\//, '$1__'); | ||
const matcher = new RegExp(`${packageName}|${typesPackageName}`); | ||
return declarationFiles.some(declaration => { | ||
const packageIdName = program.sourceFileToPackageName.get(declaration.path); | ||
return (packageIdName !== undefined && | ||
matcher.test(packageIdName) && | ||
program.isSourceFileFromExternalLibrary(declaration)); | ||
}); | ||
} | ||
function typeDeclaredInLib(declarationFiles, program) { | ||
// Assertion: The type is not an error type. | ||
// Intrinsic type (i.e. string, number, boolean, etc) - Treat it as if it's from lib. | ||
if (declarationFiles.length === 0) { | ||
return true; | ||
} | ||
return declarationFiles.some(declaration => program.isSourceFileDefaultLibrary(declaration)); | ||
} | ||
function typeMatchesSpecifier(type, specifier, program) { | ||
@@ -172,20 +131,19 @@ if (tsutils.isIntrinsicErrorType(type)) { | ||
if (typeof specifier === 'string') { | ||
return specifierNameMatches(type, specifier); | ||
return (0, specifierNameMatches_1.specifierNameMatches)(type, specifier); | ||
} | ||
if (!specifierNameMatches(type, specifier.name)) { | ||
if (!(0, specifierNameMatches_1.specifierNameMatches)(type, specifier.name)) { | ||
return false; | ||
} | ||
const symbol = type.getSymbol() ?? type.aliasSymbol; | ||
const declarationFiles = symbol | ||
?.getDeclarations() | ||
?.map(declaration => declaration.getSourceFile()) ?? []; | ||
const declarations = symbol?.getDeclarations() ?? []; | ||
const declarationFiles = declarations.map(declaration => declaration.getSourceFile()); | ||
switch (specifier.from) { | ||
case 'file': | ||
return typeDeclaredInFile(specifier.path, declarationFiles, program); | ||
return (0, typeDeclaredInFile_1.typeDeclaredInFile)(specifier.path, declarationFiles, program); | ||
case 'lib': | ||
return typeDeclaredInLib(declarationFiles, program); | ||
return (0, typeDeclaredInLib_1.typeDeclaredInLib)(declarationFiles, program); | ||
case 'package': | ||
return typeDeclaredInPackage(specifier.package, declarationFiles, program); | ||
return (0, typeDeclaredInPackageDeclarationFile_1.typeDeclaredInPackageDeclarationFile)(specifier.package, declarations, declarationFiles, program); | ||
} | ||
} | ||
//# sourceMappingURL=TypeOrValueSpecifier.js.map |
{ | ||
"name": "@typescript-eslint/type-utils", | ||
"version": "8.0.0-alpha.45", | ||
"version": "8.0.0-alpha.46", | ||
"description": "Type utilities for working with TypeScript + ESLint together", | ||
@@ -49,4 +49,4 @@ "files": [ | ||
"dependencies": { | ||
"@typescript-eslint/typescript-estree": "8.0.0-alpha.45", | ||
"@typescript-eslint/utils": "8.0.0-alpha.45", | ||
"@typescript-eslint/typescript-estree": "8.0.0-alpha.46", | ||
"@typescript-eslint/utils": "8.0.0-alpha.46", | ||
"debug": "^4.3.4", | ||
@@ -57,3 +57,3 @@ "ts-api-utils": "^1.3.0" | ||
"@jest/types": "29.6.3", | ||
"@typescript-eslint/parser": "8.0.0-alpha.45", | ||
"@typescript-eslint/parser": "8.0.0-alpha.46", | ||
"ajv": "^6.12.6", | ||
@@ -60,0 +60,0 @@ "downlevel-dts": "*", |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
110170
6.75%83
23.88%1583
4.28%+ Added
+ Added
+ Added
+ Added
+ Added
- Removed
- Removed
- Removed
- Removed
- Removed