New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

dtslint

Package Overview
Dependencies
Maintainers
1
Versions
104
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

dtslint - npm Package Compare versions

Comparing version 2.0.6 to 3.0.0

176

bin/rules/npmNamingRule.js
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const critic = require("dts-critic");
const dts_critic_1 = require("dts-critic");
const Lint = require("tslint");
const util_1 = require("../util");
const defaultOptions = {
mode: dts_critic_1.Mode.NameOnly,
};
class Rule extends Lint.Rules.AbstractRule {
apply(sourceFile) {
return this.applyWithFunction(sourceFile, walk);
return this.applyWithFunction(sourceFile, walk, parseOptions(this.ruleArguments));
}

@@ -15,7 +18,131 @@ }

description: "Ensure that package name and DefinitelyTyped header match npm package info.",
optionsDescription: "Not configurable.",
options: null,
optionsDescription: `An object with a \`mode\` property should be provided.
If \`mode\` is '${dts_critic_1.Mode.Code}', then option \`errors\` can be provided.
\`errors\` should be an array specifying which code checks should be enabled or disabled.`,
options: {
oneOf: [
{
type: "object",
properties: {
"mode": {
type: "string",
enum: [dts_critic_1.Mode.NameOnly],
},
"single-line": {
description: "Whether to print error messages in a single line. Used for testing.",
type: "boolean",
},
"required": ["mode"],
},
},
{
type: "object",
properties: {
"mode": {
type: "string",
enum: [dts_critic_1.Mode.Code],
},
"errors": {
type: "array",
items: {
type: "array",
items: [
{ description: "Name of the check.",
type: "string",
enum: [dts_critic_1.ErrorKind.NeedsExportEquals, dts_critic_1.ErrorKind.NoDefaultExport],
},
{
description: "Whether the check is enabled or disabled.",
type: "boolean",
},
],
minItems: 2,
maxItems: 2,
},
},
"single-line": {
description: "Whether to print error messages in a single line. Used for testing.",
type: "boolean",
},
"required": ["mode"],
},
},
],
},
optionExamples: [
true,
[true, { mode: dts_critic_1.Mode.NameOnly }],
[true, { mode: dts_critic_1.Mode.Code, errors: [[dts_critic_1.ErrorKind.NeedsExportEquals, true], [dts_critic_1.ErrorKind.NoDefaultExport, false]] }],
],
type: "functionality",
typescriptOnly: true,
};
function parseOptions(args) {
if (args.length === 0) {
return defaultOptions;
}
const arg = args[0];
if (arg == null) {
return defaultOptions;
}
if (!arg.mode || typeof arg.mode !== "string") {
return defaultOptions;
}
const mode = dts_critic_1.parseMode(arg.mode);
if (!mode) {
return defaultOptions;
}
const singleLine = !!arg["single-line"];
switch (mode) {
case dts_critic_1.Mode.NameOnly:
return { mode, singleLine };
case dts_critic_1.Mode.Code:
if (!arg.errors || !Array.isArray(arg.errors)) {
return { mode, errors: new Map(), singleLine };
}
return { mode, errors: parseEnabledErrors(arg.errors), singleLine };
}
}
function parseEnabledErrors(errors) {
const enabledChecks = new Map();
for (const tuple of errors) {
if (Array.isArray(tuple)
&& tuple.length === 2
&& typeof tuple[0] === "string"
&& typeof tuple[1] === "boolean") {
const error = dts_critic_1.parseExportErrorKind(tuple[0]);
if (error) {
enabledChecks.set(error, tuple[1]);
}
}
}
return enabledChecks;
}
function tslintDisableOption(error) {
switch (error) {
case dts_critic_1.ErrorKind.NoMatchingNpmPackage:
case dts_critic_1.ErrorKind.NoMatchingNpmVersion:
case dts_critic_1.ErrorKind.NonNpmHasMatchingPackage:
return `false`;
case dts_critic_1.ErrorKind.NoDefaultExport:
case dts_critic_1.ErrorKind.NeedsExportEquals:
case dts_critic_1.ErrorKind.JsSignatureNotInDts:
case dts_critic_1.ErrorKind.JsPropertyNotInDts:
case dts_critic_1.ErrorKind.DtsSignatureNotInJs:
case dts_critic_1.ErrorKind.DtsPropertyNotInJs:
return JSON.stringify([true, { mode: dts_critic_1.Mode.Code, errors: [[error, false]] }]);
}
}
function errorMessage(error, opts) {
const message = error.message +
`\nIf you won't fix this error now or you think this error is wrong,
you can disable this check by adding the following options to your project's tslint.json file under "rules":
"npm-naming": ${tslintDisableOption(error.kind)}
`;
if (opts.singleLine) {
return message.replace(/(\r\n|\n|\r|\t)/gm, " ");
}
return message;
}
function walk(ctx) {

@@ -32,21 +159,28 @@ const { sourceFile } = ctx;

try {
critic(sourceFile.fileName);
const errors = dts_critic_1.dtsCritic(sourceFile.fileName, /* sourcePath */ undefined, ctx.options);
for (const error of errors) {
switch (error.kind) {
case dts_critic_1.ErrorKind.NoMatchingNpmPackage:
case dts_critic_1.ErrorKind.NoMatchingNpmVersion:
case dts_critic_1.ErrorKind.NonNpmHasMatchingPackage:
lookFor("// Type definitions for", errorMessage(error, ctx.options));
break;
case dts_critic_1.ErrorKind.DtsPropertyNotInJs:
case dts_critic_1.ErrorKind.DtsSignatureNotInJs:
case dts_critic_1.ErrorKind.JsPropertyNotInDts:
case dts_critic_1.ErrorKind.JsSignatureNotInDts:
case dts_critic_1.ErrorKind.NeedsExportEquals:
case dts_critic_1.ErrorKind.NoDefaultExport:
if (error.position) {
ctx.addFailureAt(error.position.start, error.position.length, util_1.failure(Rule.metadata.ruleName, errorMessage(error, ctx.options)));
}
else {
ctx.addFailure(0, 1, util_1.failure(Rule.metadata.ruleName, errorMessage(error, ctx.options)));
}
break;
}
}
}
catch (e) {
// TODO: dts-critic should
// 1. not really be using exceptions, but lists
// 2. export an error code enum
// 3. add an errorCode property to the exception (or return value)
if (e.message.indexOf("d.ts file must have a matching npm package") > -1 ||
e.message.indexOf("must match a version that exists on npm") > -1 ||
e.message.indexOf("The non-npm package") > -1) {
lookFor("// Type definitions for", e.message);
}
else if (e.message.indexOf("export default") > -1) {
lookFor("export default", e.message);
}
else {
// should be unused!
ctx.addFailureAt(0, 1, e.message);
}
// We're ignoring exceptions.
}

@@ -53,0 +187,0 @@ }

8

package.json
{
"name": "dtslint",
"version": "2.0.6",
"version": "3.0.0",
"description": "Runs tests on TypeScript definition files",

@@ -31,3 +31,3 @@ "files": [

"definitelytyped-header-parser": "3.8.2",
"dts-critic": "^2.2.4",
"dts-critic": "^3.0.0",
"fs-extra": "^6.0.1",

@@ -40,9 +40,9 @@ "strip-json-comments": "^2.0.1",

"@types/fs-extra": "^5.0.2",
"@types/node": "^7.10.7",
"@types/node": "12.0.x",
"@types/strip-json-comments": "^0.0.28"
},
"engines": {
"node": ">=6.10.0"
"node": ">=12.0.0"
},
"license": "MIT"
}

Sorry, the diff of this file is not supported yet

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