Skip to content
This repository was archived by the owner on Jun 11, 2020. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion src/lib/definition-parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ function getTypingDataForSingleTypesVersion(
): TypingDataFromIndividualTypeScriptVersion {
const tsconfig = fs.readJson("tsconfig.json") as TsConfig;
checkFilesFromTsConfig(packageName, tsconfig, fs.debugPath());
const { types, tests } = allReferencedFiles(tsconfig.files!, fs, packageName, packageDirectory);
const { types, tests, hasNonRelativeImport } = allReferencedFiles(tsconfig.files!, fs, packageName, packageDirectory);
const usedFiles = new Set([...types.keys(), ...tests.keys(), "tsconfig.json", "tslint.json"]);
const otherFiles = ls.indexOf(unusedFilesName) > -1 ? (fs.readFile(unusedFilesName)).split(/\r?\n/g).filter(Boolean) : [];
checkAllFilesUsed(ls, usedFiles, otherFiles, packageName, fs);
Expand All @@ -232,6 +232,14 @@ function getTypingDataForSingleTypesVersion(
),
);

const { paths } = tsconfig.compilerOptions;
if (directoryVersion !== undefined && !(paths && `${packageName}/*` in paths) && hasNonRelativeImport) {
const mapping = JSON.stringify([`${packageName}/v${formatTypingVersion(directoryVersion)}/*`]);
throw new Error(
`${packageName}: Older version ${formatTypingVersion(directoryVersion)} must have a "paths" entry of "${packageName}/*": ${mapping}`,
);
}

const { dependencies, pathMappings } = calculateDependencies(packageName, tsconfig, dependenciesSet, directoryVersion);
const tsconfigPathsForHash = JSON.stringify(tsconfig.compilerOptions.paths);
return {
Expand Down
12 changes: 8 additions & 4 deletions src/lib/module-info.ts
Original file line number Diff line number Diff line change
Expand Up @@ -146,12 +146,13 @@ function withoutExtension(str: string, ext: string): string {
/** Returns a map from filename (path relative to `directory`) to the SourceFile we parsed for it. */
export function allReferencedFiles(
entryFilenames: ReadonlyArray<string>, fs: FS, packageName: string, baseDirectory: string,
): { types: Map<string, ts.SourceFile>, tests: Map<string, ts.SourceFile> } {
): { types: Map<string, ts.SourceFile>, tests: Map<string, ts.SourceFile>, hasNonRelativeImport: boolean } {
const seenReferences = new Set<string>();
const types = new Map<string, ts.SourceFile>();
const tests = new Map<string, ts.SourceFile>();
let hasNonRelativeImport = false;
entryFilenames.forEach(text => recur({ text, exact: true }));
return { types, tests };
return { types, tests, hasNonRelativeImport };

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

const refs = findReferencedFiles(
const { refs, hasNonRelativeImport: result } = findReferencedFiles(
src,
packageName,
path.dirname(resolvedFilename),
normalizeSlashes(path.relative(baseDirectory, fs.debugPath())),
);
refs.forEach(recur);
hasNonRelativeImport = hasNonRelativeImport || result;
}
}

Expand Down Expand Up @@ -210,6 +212,7 @@ interface Reference {
*/
function findReferencedFiles(src: ts.SourceFile, packageName: string, subDirectory: string, baseDirectory: string) {
const refs: Reference[] = [];
let hasNonRelativeImport = false;

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

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