Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(TS): infer definitions correctly #357

Merged
merged 6 commits into from
Jul 8, 2019
Merged
Show file tree
Hide file tree
Changes from 5 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
2 changes: 1 addition & 1 deletion src/__tests__/__snapshots__/config.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ Object {
"maxObjSize": 450000,
"npmDownloadsEndpoint": "https://api.npmjs.org/downloads",
"npmRegistryEndpoint": "https://replicate.npmjs.com/registry",
"npmRootEndpoint": "https://api.npmjs.org",
"npmRootEndpoint": "https://registry.npmjs.org",
Haroenv marked this conversation as resolved.
Show resolved Hide resolved
"popularDownloadsRatio": 0.005,
"replicateConcurrency": 10,
"seq": null,
Expand Down
15 changes: 15 additions & 0 deletions src/__tests__/__snapshots__/formatPkg.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,9 @@ Are you in trouble? Read through our [contribution guidelines](https://bitbucket
"tags": Object {
"latest": "1.6.1",
},
"types": Object {
"ts": null,
},
"version": "1.6.1",
"versions": Object {
"1.0.0": "2017-01-29T23:47:18.021Z",
Expand Down Expand Up @@ -286,6 +289,9 @@ Object {
"tags": Object {
"latest": "0.0.4",
},
"types": Object {
"ts": null,
},
"version": "0.0.4",
"versions": Object {
"0.0.1": "2017-06-22T16:19:35.102Z",
Expand Down Expand Up @@ -401,6 +407,9 @@ Object {
"tags": Object {
"latest": "4.4.2",
},
"types": Object {
"ts": null,
},
"version": "4.4.2",
"versions": Object {
"4.4.2": "2019-04-10T11:34:01.895Z",
Expand Down Expand Up @@ -478,6 +487,9 @@ index(arr, obj);
"tags": Object {
"latest": "0.0.1",
},
"types": Object {
"ts": null,
},
"version": "0.0.1",
"versions": Object {
"0.0.1": "2012-08-31T16:20:14.894Z",
Expand Down Expand Up @@ -531,6 +543,9 @@ Object {
"readme": Any<String>,
"repository": null,
"tags": undefined,
"types": Object {
"ts": null,
},
"version": "0.0.0",
"versions": Object {},
}
Expand Down
18 changes: 18 additions & 0 deletions src/__tests__/formatPkg.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,24 @@ describe('adds webpack scaffolds', () => {
});
});

it('adds types if included in the package.json', () => {
expect(
formatPkg({
name: 'xxx',
lastPublisher: { name: 'unknown' },
types: './test.dts',
})
).toEqual(expect.objectContaining({ types: { ts: 'included' } }));

expect(
formatPkg({
name: 'xxx',
lastPublisher: { name: 'unknown' },
typings: './test.dts',
})
).toEqual(expect.objectContaining({ types: { ts: 'included' } }));
});

describe('test getRepositoryInfo', () => {
const getRepositoryInfo = formatPkg.__RewireAPI__.__get__(
'getRepositoryInfo'
Expand Down
28 changes: 15 additions & 13 deletions src/__tests__/typescript.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,10 @@ import { validatePackageExists } from '../npm';
import { fileExistsInUnpkg } from '../unpkg.js';

describe('getTypeScriptSupport()', () => {
it('If types or typings are present in pkg.json - return early', async () => {
let typesSupport = await getTypeScriptSupport({
it('If types are already calculated - return early', async () => {
const typesSupport = await getTypeScriptSupport({
name: 'Has Types',
types: './types',
});

expect(typesSupport).toEqual({ types: { ts: 'included' } });

typesSupport = await getTypeScriptSupport({
name: 'Has Types',
typings: './types',
types: { ts: 'included' },
});

expect(typesSupport).toEqual({ types: { ts: 'included' } });
Expand All @@ -24,23 +17,32 @@ describe('getTypeScriptSupport()', () => {
describe('without types/typings', () => {
it('Checks for @types/[name]', async () => {
validatePackageExists.mockResolvedValue(true);
const atTypesSupport = await getTypeScriptSupport({ name: 'my-lib' });
const atTypesSupport = await getTypeScriptSupport({
name: 'my-lib',
types: { ts: null },
});
expect(atTypesSupport).toEqual({ types: { ts: '@types/my-lib' } });
});

it('Checks for a d.ts resolved version of main ', async () => {
validatePackageExists.mockResolvedValue(false);
fileExistsInUnpkg.mockResolvedValue(true);

const typesSupport = await getTypeScriptSupport({ name: 'my-lib' });
const typesSupport = await getTypeScriptSupport({
name: 'my-lib',
types: { ts: null },
});
expect(typesSupport).toEqual({ types: { ts: 'included' } });
});

it('Handles not having and TS types', async () => {
validatePackageExists.mockResolvedValue(false);
fileExistsInUnpkg.mockResolvedValue(false);

const typesSupport = await getTypeScriptSupport({ name: 'my-lib' });
const typesSupport = await getTypeScriptSupport({
name: 'my-lib',
types: { ts: null },
});
expect(typesSupport).toEqual({ types: { ts: null } });
});
});
Expand Down
2 changes: 1 addition & 1 deletion src/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import ms from 'ms';
const defaultConfig = {
npmRegistryEndpoint: 'https://replicate.npmjs.com/registry',
npmDownloadsEndpoint: 'https://api.npmjs.org/downloads',
npmRootEndpoint: 'https://api.npmjs.org',
npmRootEndpoint: 'https://registry.npmjs.org',
jsDelivrHitsEndpoint: 'https://data.jsdelivr.com/v1/stats/packages/month/all',
unpkgRoot: 'https://unpkg.com',
maxObjSize: 450000,
Expand Down
18 changes: 18 additions & 0 deletions src/formatPkg.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ export default function formatPkg(pkg) {
}
: null;

const types = getTypes(pkg);

const owner = getOwner(repository, lastPublisher, author); // always favor the repository owner
const { computedKeywords, computedMetadata } = getComputedData(cleaned);
const keywords = getKeywords(cleaned);
Expand Down Expand Up @@ -92,6 +94,7 @@ export default function formatPkg(pkg) {
lastPublisher,
owners: (cleaned.owners || []).map(formatUser),
bin: cleaned.bin,
types,
lastCrawl: new Date().toISOString(),
_searchInternal: {
concatenatedName,
Expand Down Expand Up @@ -386,3 +389,18 @@ function formatUser(user) {
link: `https://www.npmjs.com/~${encodeURIComponent(user.name)}`,
};
}

function getTypes(pkg) {
// The cheap and simple (+ recommended by TS) way
// of adding a types section to your package.json
if (pkg.types) {
return { ts: 'included' };
}

// Older, but still works way of defining your types
if (pkg.typings) {
return { ts: 'included' };
}

return { ts: null };
}
33 changes: 20 additions & 13 deletions src/typescriptSupport.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,26 @@
import { validatePackageExists } from './npm.js';
import { fileExistsInUnpkg } from './unpkg.js';

/**
* @typedef Package
* @property {string} name
* @property {string} version
* @property {string} [main]
* @property {{ ts: string | null }} [types]
* @property {string} [typings]
*/

/**
* Basically either
* - { types: { ts: null }} for no existing TypeScript support
* - { types: { ts: "@types/module" }} - for definitely typed support
* - { types: { ts: "included" }} - for types shipped with the module
* */
* - { types: { ts: null }} for no existing TypeScript support
* - { types: { ts: "@types/module" }} - for definitely typed support
* - { types: { ts: "included" }} - for types shipped with the module
* @param {Package} pkg
*/
export async function getTypeScriptSupport(pkg) {
// The cheap and simple (+ recommended by TS) way
// of adding a types section to your package.json
if (pkg.types) {
return { types: { ts: 'included' } };
}

// Older, but still works way of defining your types
if (pkg.typings) {
return { types: { ts: 'included' } };
// Already calculated in `formatPkg`
if (pkg.types.ts) {
return { types: pkg.types };
}

// The 2nd most likely is definitely typed
Expand All @@ -41,6 +45,9 @@ export async function getTypeScriptSupport(pkg) {
return { types: { ts: null } };
}

/**
* @param {Array<Package>} pkgs
*/
export function getTSSupport(pkgs) {
return Promise.all(pkgs.map(getTypeScriptSupport));
}