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

Commit 5ace83b

Browse files
committed
Parse the same files as the compiler
1 parent dd69e3b commit 5ace83b

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, hasNonRelativeImport } = 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) && hasNonRelativeImport) {
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>, hasNonRelativeImport: 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 hasNonRelativeImport = false;
153154
entryFilenames.forEach(text => recur({ text, exact: true }));
154-
return { types, tests };
155+
return { types, tests, hasNonRelativeImport };
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, hasNonRelativeImport: result } = findReferencedFiles(
172173
src,
173174
packageName,
174175
path.dirname(resolvedFilename),
175176
normalizeSlashes(path.relative(baseDirectory, fs.debugPath())),
176177
);
177178
refs.forEach(recur);
179+
hasNonRelativeImport = hasNonRelativeImport || 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 hasNonRelativeImport = false;
213216

214217
for (const ref of src.referencedFiles) {
215218
// Any <reference path="foo"> is assumed to be local
@@ -230,9 +233,10 @@ function findReferencedFiles(src: ts.SourceFile, packageName: string, subDirecto
230233
}
231234
if (ref.startsWith(packageName + "/")) {
232235
addReference({ text: convertToRelativeReference(ref), exact: false });
236+
hasNonRelativeImport = true;
233237
}
234238
}
235-
return refs;
239+
return { refs, hasNonRelativeImport };
236240

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

0 commit comments

Comments
 (0)