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

Move to ESM, remove index.d.ts requirement #186

Open
wants to merge 19 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 10 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
1 change: 0 additions & 1 deletion .eslintignore

This file was deleted.

10 changes: 0 additions & 10 deletions .eslintrc

This file was deleted.

68 changes: 38 additions & 30 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "tsd",
"version": "0.27.0",
"version": "0.28.1",
"description": "Check TypeScript type definitions",
"license": "MIT",
"repository": "SamVerschueren/tsd",
Expand All @@ -9,20 +9,17 @@
"email": "[email protected]",
"url": "https://github.com/SamVerschueren"
},
"type": "module",
"exports": "./dist/index.js",
"types": "./dist/index.d.ts",
"bin": "./dist/cli.js",
"engines": {
"node": ">=14.16"
"node": ">=16"
},
"scripts": {
"prepublishOnly": "npm run build",
"pretest": "npm run build && cpy \"./**/**\" \"../../../dist/test/fixtures/\" --parents --cwd=source/test/fixtures",
"test": "npm run lint && ava",
"build": "npm run clean && tsc --project tsconfig.tsd.json && chmod +x dist/cli.js",
"clean": "del-cli dist",
"lint": "eslint \"source/**/*\"",
"lint:fix": "eslint --fix \"source/**/*\""
"test": "xo && tsc --project tsconfig.tsd.json --noEmit && ava",
"build": "del-cli dist && tsc --project tsconfig.tsd.json && chmod +x dist/cli.js"
},
"files": [
"dist/**/*.js",
Expand All @@ -39,41 +36,52 @@
"typedefinitions"
],
"dependencies": {
"@tsd/typescript": "~4.9.5",
"eslint-formatter-pretty": "^4.1.0",
"globby": "^11.0.1",
"jest-diff": "^29.0.3",
"meow": "^9.0.0",
"path-exists": "^4.0.0",
"read-pkg-up": "^7.0.0"
"@tsd/typescript": "~5.0.3",
"eslint-formatter-pretty": "^5.0.0",
"globby": "^13.1.3",
"jest-diff": "^29.5.0",
"meow": "^11.0.0",
"p-map": "^5.5.0",
"path-exists": "^5.0.0",
"read-pkg-up": "^9.1.0"
},
"devDependencies": {
"@ava/typescript": "^1.1.1",
"@types/node": "^14.18.21",
"@types/common-tags": "^1.8.1",
"@types/node": "^16",
"@types/react": "^16.9.2",
"@typescript-eslint/eslint-plugin": "^4.26.0",
"@typescript-eslint/parser": "^4.26.0",
"ava": "^3.8.2",
"cpy-cli": "^3.0.0",
"ava": "^5.2.0",
"common-tags": "^1.8.2",
"del-cli": "^3.0.0",
"eslint": "^7.27.0",
"eslint-config-xo": "^0.36.0",
"eslint-config-xo-typescript": "^0.41.1",
"execa": "^5.0.0",
"execa": "^7.1.1",
"react": "^16.9.0",
"resolve-from": "^5.0.0",
"rxjs": "^6.5.3",
"typescript": "~4.9.5"
"tsx": "^3.12.6",
"typescript": "~5.0.3",
"xo": "^0.53.1"
},
"ava": {
"timeout": "2m",
"files": [
"source/test/**/*",
"!source/test/fixtures/**/*"
],
"typescript": {
"rewritePaths": {
"source/": "dist/"
}
"extensions": {
"ts": "module"
},
"nodeArguments": [
"--loader=tsx"
],
"environmentVariables": {
"ESBK_TSCONFIG_PATH": "tsconfig.tsd.json"
}
},
"xo": {
"ignores": [
"source/test/fixtures"
],
"rules": {
"no-bitwise": "off"
}
}
}
11 changes: 7 additions & 4 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -218,9 +218,12 @@ By default, `tsd` applies the following configuration:
"dom.iterable"
],
"module": "commonjs",
// The following option is set and is not overridable.
// It is set to `nodenext` if `module` is `nodenext`, `node16` if `module` is `node16` or `node` otherwise.
"moduleResolution": "node" | "node16" | "nodenext"
"esModuleInterop": true,
"noUnusedLocals": false,
// The following options are set and are not overridable.
// Set to `nodenext` if `module` is `nodenext`, `node16` if `module` is `node16` or `node` otherwise.
"moduleResolution": "node" | "node16" | "nodenext",
"skipLibCheck": false
}
```

Expand All @@ -237,7 +240,7 @@ These options will be overridden if a `tsconfig.json` file is found in your proj
}
```

*Default options will apply if you don't override them explicitly.* You can't override the `moduleResolution` option.
*Default options will apply if you don't override them explicitly. You can't override the `moduleResolution` or `skipLibCheck` options.*

### Via the CLI

Expand Down
57 changes: 38 additions & 19 deletions source/cli.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
#!/usr/bin/env node
import process from 'node:process';
import meow from 'meow';
import formatter from './lib/formatter';
import tsd from './lib';
import {TsdError} from './lib/interfaces.js';
import formatter from './lib/formatter.js';
import tsd from './lib/index.js';

const cli = meow(`
Usage
Expand All @@ -28,6 +30,7 @@ const cli = meow(`
index.test-d.ts
✖ 10:20 Argument of type string is not assignable to parameter of type number.
`, {
importMeta: import.meta,
flags: {
typings: {
type: 'string',
Expand All @@ -44,26 +47,42 @@ const cli = meow(`
},
});

(async () => {
try {
const cwd = cli.input.length > 0 ? cli.input[0] : process.cwd();
const {typings: typingsFile, files: testFiles, showDiff} = cli.flags;
/**
* Displays a message and exits, conditionally erroring.
*
* @param message The message to display.
* @param isError Whether or not to fail on exit.
*/
const exit = (message: string, {isError = true}: {isError?: boolean} = {}) => {
if (isError) {
console.error(message);
process.exit(1);
} else {
console.log(message);
process.exit(0);
}
};

const options = {cwd, typingsFile, testFiles};
try {
const cwd = cli.input.at(0) ?? process.cwd();
const {typings: typingsFile, files: testFiles, showDiff} = cli.flags;

const diagnostics = await tsd(options);
const diagnostics = await tsd({cwd, typingsFile, testFiles});

if (diagnostics.length > 0) {
throw new Error(formatter(diagnostics, showDiff));
}
} catch (error: unknown) {
const potentialError = error as Error | undefined;
const errorMessage = potentialError?.stack ?? potentialError?.message;
if (diagnostics.length > 0) {
const hasErrors = diagnostics.some(diagnostic => diagnostic.severity === 'error');
const formattedDiagnostics = formatter(diagnostics, showDiff);

if (errorMessage) {
console.error(`Error running tsd: ${errorMessage}`);
}
exit(formattedDiagnostics, {isError: hasErrors});
}
} catch (error: unknown) {
const potentialError = error as Error | undefined;

process.exit(1);
if (potentialError instanceof TsdError) {
exit(potentialError.message);
}
})();

const errorMessage = potentialError?.stack ?? potentialError?.message ?? 'tsd unexpectedly crashed.';

exit(`Error running tsd:\n${errorMessage}`);
}
9 changes: 3 additions & 6 deletions source/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
import tsd from './lib';
import formatter from './lib/formatter';

export * from './lib/assertions/assert';
export {formatter};
export default tsd;
export * from './lib/assertions/assert.js';
export {default as formatter} from './lib/formatter.js';
export {default} from './lib/index.js';
24 changes: 10 additions & 14 deletions source/lib/assertions/assert.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
/* eslint-disable @typescript-eslint/no-unused-vars */

/**
* Asserts that the type of `expression` is identical to type `T`.
*
* @param expression - Expression that should be identical to type `T`.
*/
// @ts-expect-error
// @ts-expect-error: "expression is never read"
export const expectType = <T>(expression: T) => {
// Do nothing, the TypeScript compiler handles this for us
};
Expand All @@ -15,7 +13,7 @@ export const expectType = <T>(expression: T) => {
*
* @param expression - Expression that should not be identical to type `T`.
*/
// @ts-expect-error
// @ts-expect-error: "expression is never read"
export const expectNotType = <T>(expression: any) => {
// eslint-disable-next-line no-warning-comments
// TODO Use a `not T` type when possible https://github.com/microsoft/TypeScript/pull/29317
Expand All @@ -27,7 +25,7 @@ export const expectNotType = <T>(expression: any) => {
*
* @param expression - Expression that should be assignable to type `T`.
*/
// @ts-expect-error
// @ts-expect-error: "expression is never read"
export const expectAssignable = <T>(expression: T) => {
// Do nothing, the TypeScript compiler handles this for us
};
Expand All @@ -37,7 +35,7 @@ export const expectAssignable = <T>(expression: T) => {
*
* @param expression - Expression that should not be assignable to type `T`.
*/
// @ts-expect-error
// @ts-expect-error: "expression is never read"
export const expectNotAssignable = <T>(expression: any) => {
// Do nothing, the TypeScript compiler handles this for us
};
Expand All @@ -47,7 +45,7 @@ export const expectNotAssignable = <T>(expression: any) => {
*
* @param expression - Expression that should throw an error.
*/
// @ts-expect-error
// @ts-expect-error: "expression is never read"
export const expectError = <T = any>(expression: T) => {
// Do nothing, the TypeScript compiler handles this for us
};
Expand All @@ -57,7 +55,7 @@ export const expectError = <T = any>(expression: T) => {
*
* @param expression - Expression that should be marked as `@deprecated`.
*/
// @ts-expect-error
// @ts-expect-error: "expression is never read"
export const expectDeprecated = (expression: any) => {
// Do nothing, the TypeScript compiler handles this for us
};
Expand All @@ -67,7 +65,7 @@ export const expectDeprecated = (expression: any) => {
*
* @param expression - Expression that should not be marked as `@deprecated`.
*/
// @ts-expect-error
// @ts-expect-error: "expression is never read"
export const expectNotDeprecated = (expression: any) => {
// Do nothing, the TypeScript compiler handles this for us
};
Expand All @@ -79,16 +77,14 @@ export const expectNotDeprecated = (expression: any) => {
*
* @param expression - Expression that should be `never`.
*/
export const expectNever = (expression: never): never => {
return expression;
};
export const expectNever = (expression: never): never => expression;

/**
* Prints the type of `expression` as a warning.
*
* @param expression - Expression whose type should be printed as a warning.
*/
// @ts-expect-error
// @ts-expect-error: "expression is never read"
export const printType = (expression: any) => {
// Do nothing, the TypeScript compiler handles this for us
};
Expand All @@ -98,7 +94,7 @@ export const printType = (expression: any) => {
*
* @param expression - Expression whose documentation comment should include string literal type `T`.
*/
// @ts-expect-error
// @ts-expect-error: "expression is never read"
export const expectDocCommentIncludes = <T>(expression: any) => {
// Do nothing, the TypeScript compiler handles this for us
};
6 changes: 3 additions & 3 deletions source/lib/assertions/handlers/assignability.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {CallExpression, TypeChecker} from '@tsd/typescript';
import {Diagnostic} from '../../interfaces';
import {makeDiagnosticWithDiff} from '../../utils';
import type {CallExpression, TypeChecker} from '@tsd/typescript';
import type {Diagnostic} from '../../interfaces.js';
import {makeDiagnosticWithDiff} from '../../utils/index.js';

/**
* Asserts that the argument of the assertion is not assignable to the generic type of the assertion.
Expand Down
Loading