Skip to content
Merged
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
22 changes: 11 additions & 11 deletions packages/definitions-parser/src/lib/definition-parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -350,7 +350,7 @@ to refer to @types/P if it relies on old versions of P's types.
In this case, please make a pull request to microsoft/DefinitelyTyped-tools adding @types/P to \`packages/definitions-parser/allowedPackageJsonDependencies.txt\`.`
: `Dependency ${dependencyName} not in the allowed dependencies list.
If you are depending on another \`@types\` package, do *not* add it to a \`package.json\`. Path mapping should make the import work.
For namespaced dependencies you then have to add a \`paths\` mapping from \`@namespace/library\` to \`namespace__library\` in \`tsconfig.json\`.
For namespaced dependencies you then have to add a \`paths\` mapping from \`@namespace/*\` to \`namespace__*\` in \`tsconfig.json\`.
If this is an external library that provides typings, please make a pull request to microsoft/DefinitelyTyped-tools adding it to \`packages/definitions-parser/allowedPackageJsonDependencies.txt\`.`;
throw new Error(`In ${path}: ${msg}`);
}
Expand Down Expand Up @@ -422,22 +422,13 @@ function calculateDependencies(
const pathMappings: { [packageName: string]: TypingVersion } = {};

for (const dependencyName of Object.keys(paths)) {
// Might have a path mapping for "foo/*" to support subdirectories
const rootDirectory = withoutEnd(dependencyName, "/*");
if (rootDirectory !== undefined) {
if (!(rootDirectory in paths)) {
throw new Error(`In ${packageName}: found path mapping for ${dependencyName} but not for ${rootDirectory}`);
}
continue;
}

const pathMappingList = paths[dependencyName];
if (pathMappingList.length !== 1) {
throw new Error(`In ${packageName}: Path mapping for ${dependencyName} may only have 1 entry.`);
}
const pathMapping = pathMappingList[0];

// Path mapping may be for "@foo/bar" -> "foo__bar".
// Path mapping may be for "@foo/*" -> "foo__*".
const scopedPackageName = unmangleScopedPackage(pathMapping);
if (scopedPackageName !== undefined) {
if (dependencyName !== scopedPackageName) {
Expand All @@ -446,6 +437,15 @@ function calculateDependencies(
continue;
}

// Might have a path mapping for "foo/*" to support subdirectories
const rootDirectory = withoutEnd(dependencyName, "/*");
if (rootDirectory !== undefined) {
if (!(rootDirectory in paths)) {
throw new Error(`In ${packageName}: found path mapping for ${dependencyName} but not for ${rootDirectory}`);
}
continue;
}

const pathMappingVersion = parseDependencyVersionFromPath(dependencyName, dependencyName, pathMapping);
if (dependencyName === packageName) {
if (directoryVersion === undefined) {
Expand Down
21 changes: 21 additions & 0 deletions packages/definitions-parser/src/mocks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,27 @@ console.log(jQuery);
);
jquery.set("tsconfig.json", tsconfig(["jquery-tests.ts"]));

const scoped = dt.pkgDir("wordpress__plugins");
scoped.set(
"index.d.ts",
`// Type definitions for @wordpress/plguins
// Project: https://github.com/WordPress/gutenberg/tree/master/packages/plugins/README.md
// Definitions by: Derek Sifford <https://github.com/dsifford>
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
`
);
scoped.set(
"tsconfig.json",
JSON.stringify({
compilerOptions: {
paths: {
"@wordpress/*": ["wordpress__*"]
}
},
files: ["index.d.ts"]
})
);

return dt;
}

Expand Down
5 changes: 5 additions & 0 deletions packages/definitions-parser/test/definition-parser.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,4 +104,9 @@ describe(getTypingInfo, () => {
});
});
});

it("allows wildcard scope path mappings", () => {
const dt = createMockDT();
return expect(getTypingInfo("wordpress__plugins", dt.pkgFS("wordpress__plugins"))).resolves.toBeDefined();
});
});
2 changes: 1 addition & 1 deletion packages/definitions-parser/test/parse-definitions.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ testo({
const log = quietLoggerWithErrors()[0];
const defs = await parseDefinitions(createMockDT().fs, undefined, log);
expect(defs.allNotNeeded().length).toBe(1);
expect(defs.allTypings().length).toBe(5);
expect(defs.allTypings().length).toBe(6);
const j = defs.tryGetLatestVersion("jquery");
expect(j).toBeDefined();
expect(j!.fullNpmName).toContain("types");
Expand Down