Socket
Socket
Sign inDemoInstall

tsd

Package Overview
Dependencies
Maintainers
2
Versions
71
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

tsd - npm Package Compare versions

Comparing version 0.27.0 to 0.28.0

dist/lib/utils/make-diagnostic-with-diff.d.ts

47

dist/cli.js

@@ -26,8 +26,9 @@ #!/usr/bin/env node

Info
--help Display help text
--version Display version info
--help Display help text
--version Display version info
Options
--typings -t Type definition file to test [Default: "types" property in package.json]
--files -f Glob of files to test [Default: '/path/test-d/**/*.test-d.ts' or '.tsx']
--typings -t Type definition file to test [Default: "types" property in package.json]
--files -f Glob of files to test [Default: '/path/test-d/**/*.test-d.ts' or '.tsx']
--show-diff Show type error diffs [Default: don't show]

@@ -54,14 +55,33 @@ Examples

},
showDiff: {
type: 'boolean',
},
},
});
/**
* 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, { isError = true } = {}) => {
if (isError) {
console.error(message);
process.exit(1);
}
else {
console.log(message);
process.exit(0);
}
};
(() => __awaiter(void 0, void 0, void 0, function* () {
var _a;
var _a, _b;
try {
const cwd = cli.input.length > 0 ? cli.input[0] : process.cwd();
const typingsFile = cli.flags.typings;
const testFiles = cli.flags.files;
const options = { cwd, typingsFile, testFiles };
const diagnostics = yield (0, lib_1.default)(options);
const { typings: typingsFile, files: testFiles, showDiff } = cli.flags;
const diagnostics = yield (0, lib_1.default)({ cwd, typingsFile, testFiles });
if (diagnostics.length > 0) {
throw new Error((0, formatter_1.default)(diagnostics));
const hasErrors = diagnostics.some(diagnostic => diagnostic.severity === 'error');
const formattedDiagnostics = (0, formatter_1.default)(diagnostics, showDiff);
exit(formattedDiagnostics, { isError: hasErrors });
}

@@ -71,8 +91,5 @@ }

const potentialError = error;
const errorMessage = (_a = potentialError === null || potentialError === void 0 ? void 0 : potentialError.stack) !== null && _a !== void 0 ? _a : potentialError === null || potentialError === void 0 ? void 0 : potentialError.message;
if (errorMessage) {
console.error(`Error running tsd: ${errorMessage}`);
}
process.exit(1);
const errorMessage = (_b = (_a = potentialError === null || potentialError === void 0 ? void 0 : potentialError.stack) !== null && _a !== void 0 ? _a : potentialError === null || potentialError === void 0 ? void 0 : potentialError.message) !== null && _b !== void 0 ? _b : 'tsd unexpectedly crashed.';
exit(`Error running tsd:\n${errorMessage}`);
}
}))();
import tsd from './lib';
export * from './lib/assertions/assert';
export { default as formatter } from './lib/formatter';
export default tsd;

@@ -20,4 +20,7 @@ "use strict";

Object.defineProperty(exports, "__esModule", { value: true });
exports.formatter = void 0;
const lib_1 = __importDefault(require("./lib"));
__exportStar(require("./lib/assertions/assert"), exports);
var formatter_1 = require("./lib/formatter");
Object.defineProperty(exports, "formatter", { enumerable: true, get: function () { return __importDefault(formatter_1).default; } });
exports.default = lib_1.default;

@@ -24,8 +24,14 @@ "use strict";

const expectedType = checker.getTypeFromTypeNode(node.typeArguments[0]);
const argumentType = checker.getTypeAtLocation(node.arguments[0]);
if (checker.isTypeAssignableTo(argumentType, expectedType)) {
const receivedType = checker.getTypeAtLocation(node.arguments[0]);
if (checker.isTypeAssignableTo(receivedType, expectedType)) {
/**
* The argument type is assignable to the expected type, we don't want this so add a diagnostic.
*/
diagnostics.push((0, utils_1.makeDiagnostic)(node, `Argument of type \`${checker.typeToString(argumentType)}\` is assignable to parameter of type \`${checker.typeToString(expectedType)}\`.`));
diagnostics.push((0, utils_1.makeDiagnosticWithDiff)({
message: 'Argument of type `{receivedType}` is assignable to parameter of type `{expectedType}`.',
expectedType,
receivedType,
checker,
node,
}));
}

@@ -32,0 +38,0 @@ }

@@ -26,8 +26,8 @@ "use strict";

// Retrieve the argument type. This is the type to be checked.
const argumentType = checker.getTypeAtLocation(node.arguments[0]);
if (!checker.isTypeAssignableTo(argumentType, expectedType)) {
const receivedType = checker.getTypeAtLocation(node.arguments[0]);
if (!checker.isTypeAssignableTo(receivedType, expectedType)) {
// The argument type is not assignable to the expected type. TypeScript will catch this for us.
continue;
}
if (!checker.isTypeAssignableTo(expectedType, argumentType)) {
if (!checker.isTypeAssignableTo(expectedType, receivedType)) {
/**

@@ -37,5 +37,11 @@ * The expected type is not assignable to the argument type, but the argument type is

*/
diagnostics.push((0, utils_1.makeDiagnostic)(node, `Parameter type \`${checker.typeToString(expectedType)}\` is declared too wide for argument type \`${checker.typeToString(argumentType)}\`.`));
diagnostics.push((0, utils_1.makeDiagnosticWithDiff)({
message: 'Parameter type `{expectedType}` is declared too wide for argument type `{receivedType}`.',
expectedType,
receivedType,
checker,
node,
}));
}
else if (!checker.isTypeIdenticalTo(expectedType, argumentType)) {
else if (!checker.isTypeIdenticalTo(expectedType, receivedType)) {
/**

@@ -45,3 +51,9 @@ * The expected type and argument type are assignable in both directions. We still have to check

*/
diagnostics.push((0, utils_1.makeDiagnostic)(node, `Parameter type \`${checker.typeToString(expectedType)}\` is not identical to argument type \`${checker.typeToString(argumentType)}\`.`));
diagnostics.push((0, utils_1.makeDiagnosticWithDiff)({
message: 'Parameter type `{expectedType}` is not identical to argument type `{receivedType}`.',
expectedType,
receivedType,
checker,
node,
}));
}

@@ -48,0 +60,0 @@ }

@@ -70,3 +70,9 @@ "use strict";

}
diagnostics.push((0, utils_1.makeDiagnostic)(node, `Documentation comment \`${docComment}\` for expression \`${expression}\` does not include expected \`${expectedDocComment}\`.`));
diagnostics.push((0, utils_1.makeDiagnosticWithDiff)({
message: `Documentation comment \`{receivedType}\` for expression \`${expression}\` does not include expected \`{expectedType}\`.`,
expectedType: expectedDocComment,
receivedType: docComment,
checker,
node,
}));
}

@@ -73,0 +79,0 @@ return diagnostics;

@@ -17,3 +17,3 @@ "use strict";

const module = (_c = combinedCompilerOptions.module) !== null && _c !== void 0 ? _c : typescript_1.ModuleKind.CommonJS;
return Object.assign(Object.assign({ directory: 'test-d' }, pkgConfig), { compilerOptions: Object.assign(Object.assign({ strict: true, jsx: typescript_1.JsxEmit.React, lib: parseRawLibs(['es2020', 'dom', 'dom.iterable'], cwd), module, target: typescript_1.ScriptTarget.ES2020, esModuleInterop: true }, combinedCompilerOptions), { moduleResolution: module === typescript_1.ModuleKind.NodeNext ?
return Object.assign(Object.assign({ directory: 'test-d' }, pkgConfig), { compilerOptions: Object.assign(Object.assign({ strict: true, jsx: typescript_1.JsxEmit.React, lib: parseRawLibs(['es2020', 'dom', 'dom.iterable'], cwd), module, target: typescript_1.ScriptTarget.ES2020, esModuleInterop: true, noUnusedLocals: false }, combinedCompilerOptions), { moduleResolution: module === typescript_1.ModuleKind.NodeNext ?
typescript_1.ModuleResolutionKind.NodeNext :

@@ -20,0 +20,0 @@ module === typescript_1.ModuleKind.Node16 ?

@@ -6,5 +6,6 @@ import { Diagnostic } from './interfaces';

* @param diagnostics - List of TypeScript diagnostics.
* @param showDiff - Display difference between expected and received types.
* @returns Beautiful diagnostics output
*/
declare const _default: (diagnostics: Diagnostic[]) => string;
declare const _default: (diagnostics: Diagnostic[], showDiff?: boolean) => string;
export default _default;

@@ -7,2 +7,3 @@ "use strict";

const eslint_formatter_pretty_1 = __importDefault(require("eslint-formatter-pretty"));
const jest_diff_1 = require("jest-diff");
/**

@@ -12,5 +13,6 @@ * Format the TypeScript diagnostics to a human readable output.

* @param diagnostics - List of TypeScript diagnostics.
* @param showDiff - Display difference between expected and received types.
* @returns Beautiful diagnostics output
*/
exports.default = (diagnostics) => {
exports.default = (diagnostics, showDiff = false) => {
const fileMap = new Map();

@@ -28,3 +30,15 @@ for (const diagnostic of diagnostics) {

}
entry.errorCount++;
if (showDiff && diagnostic.diff) {
let difference = (0, jest_diff_1.diffStringsUnified)(diagnostic.diff.expected, diagnostic.diff.received, { omitAnnotationLines: true });
if (difference) {
difference = difference.split('\n').map(line => ` ${line}`).join('\n');
diagnostic.message = `${diagnostic.message}\n\n${difference}`;
}
}
if (diagnostic.severity === 'error') {
entry.errorCount++;
}
else if (diagnostic.severity === 'warning') {
entry.warningCount++;
}
entry.messages.push(diagnostic);

@@ -31,0 +45,0 @@ }

@@ -56,2 +56,6 @@ import { CompilerOptions } from '@tsd/typescript';

column?: number;
diff?: {
expected: string;
received: string;
};
}

@@ -58,0 +62,0 @@ export interface Location {

import makeDiagnostic from './make-diagnostic';
import makeDiagnosticWithDiff from './make-diagnostic-with-diff';
import getJSONPropertyPosition from './get-json-property-position';
import * as tsutils from './typescript';
export { getJSONPropertyPosition, makeDiagnostic, tsutils };
export { getJSONPropertyPosition, makeDiagnostic, makeDiagnosticWithDiff, tsutils };

@@ -29,5 +29,7 @@ "use strict";

Object.defineProperty(exports, "__esModule", { value: true });
exports.tsutils = exports.makeDiagnostic = exports.getJSONPropertyPosition = void 0;
exports.tsutils = exports.makeDiagnosticWithDiff = exports.makeDiagnostic = exports.getJSONPropertyPosition = void 0;
const make_diagnostic_1 = __importDefault(require("./make-diagnostic"));
exports.makeDiagnostic = make_diagnostic_1.default;
const make_diagnostic_with_diff_1 = __importDefault(require("./make-diagnostic-with-diff"));
exports.makeDiagnosticWithDiff = make_diagnostic_with_diff_1.default;
const get_json_property_position_1 = __importDefault(require("./get-json-property-position"));

@@ -34,0 +36,0 @@ exports.getJSONPropertyPosition = get_json_property_position_1.default;

{
"name": "tsd",
"version": "0.27.0",
"version": "0.28.0",
"description": "Check TypeScript type definitions",

@@ -42,5 +42,6 @@ "license": "MIT",

"dependencies": {
"@tsd/typescript": "~4.9.5",
"@tsd/typescript": "~5.0.2",
"eslint-formatter-pretty": "^4.1.0",
"globby": "^11.0.1",
"jest-diff": "^29.0.3",
"meow": "^9.0.0",

@@ -64,2 +65,3 @@ "path-exists": "^4.0.0",

"react": "^16.9.0",
"resolve-from": "^5.0.0",
"rxjs": "^6.5.3",

@@ -66,0 +68,0 @@ "typescript": "~4.9.5"

@@ -221,5 +221,8 @@ # tsd ![CI](https://github.com/SamVerschueren/tsd/workflows/CI/badge.svg)

"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
}

@@ -241,3 +244,3 @@ ```

*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.*

@@ -273,2 +276,10 @@ ### Via the CLI

You can also make use of the CLI's formatter to generate the same formatting output when running `tsd` programmatically.
```ts
import tsd, {formatter} from 'tsd';
const formattedDiagnostics = formatter(await tsd());
```
### tsd(options?)

@@ -275,0 +286,0 @@

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc