Skip to content

Commit 4ed5159

Browse files
committed
Parse the same files as the compiler
1 parent f65467b commit 4ed5159

2 files changed

Lines changed: 24 additions & 5 deletions

File tree

packages/definitions-parser/src/lib/definition-parser.ts

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -264,7 +264,12 @@ function getTypingDataForSingleTypesVersion(
264264
): TypingDataFromIndividualTypeScriptVersion {
265265
const tsconfig = fs.readJson("tsconfig.json") as TsConfig;
266266
checkFilesFromTsConfig(packageName, tsconfig, fs.debugPath());
267-
const { types, tests } = allReferencedFiles(tsconfig.files!, fs, packageName, packageDirectory);
267+
const { types, tests, hasNonRelativeImports } = allReferencedFiles(
268+
tsconfig.files!,
269+
fs,
270+
packageName,
271+
packageDirectory
272+
);
268273
const usedFiles = new Set([...types.keys(), ...tests.keys(), "tsconfig.json", "tslint.json"]);
269274
const otherFiles =
270275
ls.indexOf(unusedFilesName) > -1
@@ -289,6 +294,16 @@ function getTypingDataForSingleTypesVersion(
289294
filter(getTestDependencies(packageName, types, tests.keys(), dependenciesSet, fs), m => !declaredModulesSet.has(m))
290295
);
291296

297+
const { paths } = tsconfig.compilerOptions;
298+
if (directoryVersion !== undefined && !(paths && `${packageName}/*` in paths) && hasNonRelativeImports) {
299+
const mapping = JSON.stringify([`${packageName}/v${formatTypingVersion(directoryVersion)}/*`]);
300+
throw new Error(
301+
`${packageName}: Older version ${formatTypingVersion(
302+
directoryVersion
303+
)} must have a "paths" entry of "${packageName}/*": ${mapping}`
304+
);
305+
}
306+
292307
const { dependencies, pathMappings } = calculateDependencies(
293308
packageName,
294309
tsconfig,

packages/definitions-parser/src/lib/module-info.ts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -147,12 +147,13 @@ export function allReferencedFiles(
147147
fs: FS,
148148
packageName: string,
149149
baseDirectory: string
150-
): { types: Map<string, ts.SourceFile>; tests: Map<string, ts.SourceFile> } {
150+
): { types: Map<string, ts.SourceFile>; tests: Map<string, ts.SourceFile>; hasNonRelativeImports: boolean } {
151151
const seenReferences = new Set<string>();
152152
const types = new Map<string, ts.SourceFile>();
153153
const tests = new Map<string, ts.SourceFile>();
154+
let hasNonRelativeImports = false;
154155
entryFilenames.forEach(text => recur({ text, exact: true }));
155-
return { types, tests };
156+
return { types, tests, hasNonRelativeImports };
156157

157158
function recur({ text, exact }: Reference): void {
158159
if (seenReferences.has(text)) {
@@ -170,13 +171,14 @@ export function allReferencedFiles(
170171
tests.set(resolvedFilename, src);
171172
}
172173

173-
const refs = findReferencedFiles(
174+
const { refs, hasNonRelativeImports: result } = findReferencedFiles(
174175
src,
175176
packageName,
176177
path.dirname(resolvedFilename),
177178
normalizeSlashes(path.relative(baseDirectory, fs.debugPath()))
178179
);
179180
refs.forEach(recur);
181+
hasNonRelativeImports = hasNonRelativeImports || result;
180182
}
181183
}
182184
}
@@ -215,6 +217,7 @@ interface Reference {
215217
*/
216218
function findReferencedFiles(src: ts.SourceFile, packageName: string, subDirectory: string, baseDirectory: string) {
217219
const refs: Reference[] = [];
220+
let hasNonRelativeImports = false;
218221

219222
for (const ref of src.referencedFiles) {
220223
// Any <reference path="foo"> is assumed to be local
@@ -235,9 +238,10 @@ function findReferencedFiles(src: ts.SourceFile, packageName: string, subDirecto
235238
}
236239
if (ref.startsWith(packageName + "/")) {
237240
addReference({ text: convertToRelativeReference(ref), exact: false });
241+
hasNonRelativeImports = true;
238242
}
239243
}
240-
return refs;
244+
return { refs, hasNonRelativeImports };
241245

242246
function addReference(ref: Reference): void {
243247
// `path.normalize` may add windows slashes

0 commit comments

Comments
 (0)