@typescript-eslint/type-utils 8.71.2-alpha.4 → 8.71.2-alpha.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -32,8 +32,12 @@ var __importStar = (this && this.__importStar) || (function () {
32
32
  return result;
33
33
  };
34
34
  })();
35
+ var __importDefault = (this && this.__importDefault) || function (mod) {
36
+ return (mod && mod.__esModule) ? mod : { "default": mod };
37
+ };
35
38
  Object.defineProperty(exports, "__esModule", { value: true });
36
39
  exports.typeDeclaredInPackageDeclarationFile = typeDeclaredInPackageDeclarationFile;
40
+ const node_path_1 = __importDefault(require("node:path"));
37
41
  const tsutils = __importStar(require("ts-api-utils"));
38
42
  const ts = __importStar(require("typescript"));
39
43
  function findParentModuleDeclaration(node) {
@@ -85,17 +89,57 @@ function typeDeclaredInDeclarationFile(packageName, declarationFiles, program) {
85
89
  // Handle scoped packages: if the name starts with @, remove it and replace / with __
86
90
  const typesPackageName = packageName.replace(/^@([^/]+)\//, '$1__');
87
91
  return declarationFiles.some(declaration => {
92
+ if (!program.isSourceFileFromExternalLibrary(declaration)) {
93
+ return false;
94
+ }
88
95
  // A package id name is a path within the package, such as
89
96
  // `typescript/lib/typescript.d.ts` or `@types/semver/classes/semver.d.ts`.
90
- const packageIdName = program.sourceFileToPackageName.get(declaration.path);
97
+ const packageIdName = program.sourceFileToPackageName.get(declaration.path) ??
98
+ resolvePackageIdName(declaration.fileName);
91
99
  if (packageIdName == null) {
92
100
  return false;
93
101
  }
94
- return ((pathIsInPackage(packageIdName, packageName) ||
95
- pathIsInPackage(packageIdName.replace(/^@types\//, ''), typesPackageName)) &&
96
- program.isSourceFileFromExternalLibrary(declaration));
102
+ return (pathIsInPackage(packageIdName, packageName) ||
103
+ pathIsInPackage(packageIdName.replace(/^@types\//, ''), typesPackageName));
97
104
  });
98
105
  }
106
+ /**
107
+ * TypeScript's `sourceFileToPackageName` doesn't include (1) files in linked
108
+ * packages (e.g., workspace packages) that are reached through relative
109
+ * imports, rather than by package name, or (2) any files of packages without
110
+ * `version` in their `package.json`.
111
+ *
112
+ * So we're looking for the nearest `package.json` that has a `name` field,
113
+ * starting from the directory of the declaration file, and using that as the
114
+ * package name.
115
+ */
116
+ function resolvePackageIdName(fileName) {
117
+ const packageRoot = findPackageRoot(node_path_1.default.dirname(fileName));
118
+ if (!packageRoot) {
119
+ return undefined;
120
+ }
121
+ const fileRelativeToPackage = fileName.slice(packageRoot.directory.length);
122
+ return `${packageRoot.name}${fileRelativeToPackage}`;
123
+ }
124
+ const packageRoots = new Map();
125
+ function findPackageRoot(directory) {
126
+ if (!packageRoots.has(directory)) {
127
+ const parentDir = node_path_1.default.dirname(directory);
128
+ const packageName = getPackageName(directory);
129
+ const packageRoot = packageName
130
+ ? { directory, name: packageName }
131
+ : parentDir === directory
132
+ ? undefined
133
+ : findPackageRoot(parentDir);
134
+ packageRoots.set(directory, packageRoot);
135
+ }
136
+ return packageRoots.get(directory);
137
+ }
138
+ function getPackageName(directory) {
139
+ const result = ts.readConfigFile(node_path_1.default.join(directory, 'package.json'), ts.sys.readFile);
140
+ const config = result.config;
141
+ return typeof config?.name === 'string' ? config.name : undefined;
142
+ }
99
143
  function typeDeclaredInPackageDeclarationFile(packageName, symbol, declarations, declarationFiles, program) {
100
144
  return (typeDeclaredInDeclareModule(packageName, declarations) ||
101
145
  typeDeclaredInDeclarationFile(packageName, declarationFiles, program) ||
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@typescript-eslint/type-utils",
3
- "version": "8.71.2-alpha.4",
3
+ "version": "8.71.2-alpha.6",
4
4
  "description": "Type utilities for working with TypeScript + ESLint together",
5
5
  "files": [
6
6
  "dist",
@@ -34,9 +34,9 @@
34
34
  "estree"
35
35
  ],
36
36
  "dependencies": {
37
- "@typescript-eslint/types": "8.71.2-alpha.4",
38
- "@typescript-eslint/typescript-estree": "8.71.2-alpha.4",
39
- "@typescript-eslint/utils": "8.71.2-alpha.4",
37
+ "@typescript-eslint/types": "8.71.2-alpha.6",
38
+ "@typescript-eslint/typescript-estree": "8.71.2-alpha.6",
39
+ "@typescript-eslint/utils": "8.71.2-alpha.6",
40
40
  "debug": "^4.4.3",
41
41
  "ts-api-utils": "^2.5.0"
42
42
  },
@@ -46,7 +46,7 @@
46
46
  },
47
47
  "devDependencies": {
48
48
  "@types/babel__code-frame": "^7.0.6",
49
- "@typescript-eslint/parser": "8.71.2-alpha.4",
49
+ "@typescript-eslint/parser": "8.71.2-alpha.6",
50
50
  "@typescript/native": "npm:typescript@^7.0.2",
51
51
  "@vitest/coverage-v8": "^4.0.18",
52
52
  "ajv": "^6.12.6",