Skip to content
This repository was archived by the owner on Jun 11, 2020. It is now read-only.

Commit 5583c10

Browse files
committed
Reference the same files as the compiler
1 parent dd69e3b commit 5583c10

File tree

2 files changed

+17
-5
lines changed

2 files changed

+17
-5
lines changed

src/lib/definition-parser.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -212,7 +212,7 @@ function getTypingDataForSingleTypesVersion(
212212
): TypingDataFromIndividualTypeScriptVersion {
213213
const tsconfig = fs.readJson("tsconfig.json") as TsConfig;
214214
checkFilesFromTsConfig(packageName, tsconfig, fs.debugPath());
215-
const { types, tests } = allReferencedFiles(tsconfig.files!, fs, packageName, packageDirectory);
215+
const { types, tests, hasNonRelativeReference } = allReferencedFiles(tsconfig.files!, fs, packageName, packageDirectory);
216216
const usedFiles = new Set([...types.keys(), ...tests.keys(), "tsconfig.json", "tslint.json"]);
217217
const otherFiles = ls.indexOf(unusedFilesName) > -1 ? (fs.readFile(unusedFilesName)).split(/\r?\n/g).filter(Boolean) : [];
218218
checkAllFilesUsed(ls, usedFiles, otherFiles, packageName, fs);
@@ -232,6 +232,14 @@ function getTypingDataForSingleTypesVersion(
232232
),
233233
);
234234

235+
const { paths } = tsconfig.compilerOptions;
236+
if (directoryVersion !== undefined && !(paths && `${packageName}/*` in paths) && hasNonRelativeReference) {
237+
const mapping = JSON.stringify([`${packageName}/v${formatTypingVersion(directoryVersion)}/*`]);
238+
throw new Error(
239+
`${packageName}: Older version ${formatTypingVersion(directoryVersion)} must have a "paths" entry of "${packageName}/*": ${mapping}`,
240+
);
241+
}
242+
235243
const { dependencies, pathMappings } = calculateDependencies(packageName, tsconfig, dependenciesSet, directoryVersion);
236244
const tsconfigPathsForHash = JSON.stringify(tsconfig.compilerOptions.paths);
237245
return {

src/lib/module-info.ts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -146,12 +146,13 @@ function withoutExtension(str: string, ext: string): string {
146146
/** Returns a map from filename (path relative to `directory`) to the SourceFile we parsed for it. */
147147
export function allReferencedFiles(
148148
entryFilenames: ReadonlyArray<string>, fs: FS, packageName: string, baseDirectory: string,
149-
): { types: Map<string, ts.SourceFile>, tests: Map<string, ts.SourceFile> } {
149+
): { types: Map<string, ts.SourceFile>, tests: Map<string, ts.SourceFile>, hasNonRelativeReference: boolean } {
150150
const seenReferences = new Set<string>();
151151
const types = new Map<string, ts.SourceFile>();
152152
const tests = new Map<string, ts.SourceFile>();
153+
let hasNonRelativeReference = false;
153154
entryFilenames.forEach(text => recur({ text, exact: true }));
154-
return { types, tests };
155+
return { types, tests, hasNonRelativeReference };
155156

156157
function recur({ text, exact }: Reference): void {
157158
if (seenReferences.has(text)) {
@@ -168,13 +169,14 @@ export function allReferencedFiles(
168169
tests.set(resolvedFilename, src);
169170
}
170171

171-
const refs = findReferencedFiles(
172+
const { refs, hasNonRelativeReference: result } = findReferencedFiles(
172173
src,
173174
packageName,
174175
path.dirname(resolvedFilename),
175176
normalizeSlashes(path.relative(baseDirectory, fs.debugPath())),
176177
);
177178
refs.forEach(recur);
179+
hasNonRelativeReference = hasNonRelativeReference || result;
178180
}
179181
}
180182

@@ -210,6 +212,7 @@ interface Reference {
210212
*/
211213
function findReferencedFiles(src: ts.SourceFile, packageName: string, subDirectory: string, baseDirectory: string) {
212214
const refs: Reference[] = [];
215+
let hasNonRelativeReference = false;
213216

214217
for (const ref of src.referencedFiles) {
215218
// Any <reference path="foo"> is assumed to be local
@@ -232,7 +235,7 @@ function findReferencedFiles(src: ts.SourceFile, packageName: string, subDirecto
232235
addReference({ text: convertToRelativeReference(ref), exact: false });
233236
}
234237
}
235-
return refs;
238+
return { refs, hasNonRelativeReference };
236239

237240
function addReference(ref: Reference): void {
238241
// `path.normalize` may add windows slashes
@@ -256,6 +259,7 @@ function findReferencedFiles(src: ts.SourceFile, packageName: string, subDirecto
256259

257260
/** boring/foo -> ./foo when subDirectory === '.'; ../foo when it's === 'x'; ../../foo when it's 'x/y' */
258261
function convertToRelativeReference(name: string) {
262+
hasNonRelativeReference = true;
259263
const relative = "." + "/..".repeat(subDirectory === "." ? 0 : subDirectory.split("/").length);
260264
return relative + name.slice(packageName.length);
261265
}

0 commit comments

Comments
 (0)