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 0.2.0 to 0.3.0

bin/rules/noConstEnumRule.js

10

bin/checks.js

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

}
for (const key in pkgJson) {
for (const key in pkgJson) { // tslint:disable-line forin
switch (key) {
case "private":
case "dependencies":
// "private" checked above, "dependencies" checked by types-publisher
case "license":
// "private" checked above, "dependencies" / "license" checked by types-publisher
break;

@@ -64,3 +65,3 @@ default:

}
for (const key in options) {
for (const key in options) { // tslint:disable-line forin
switch (key) {

@@ -72,2 +73,5 @@ case "lib":

case "strictFunctionTypes":
case "esModuleInterop":
case "allowSyntheticDefaultImports":
// Allow any value
break;

@@ -74,0 +78,0 @@ case "target":

@@ -12,3 +12,2 @@ #!/usr/bin/env node

Object.defineProperty(exports, "__esModule", { value: true });
const child_process_1 = require("child_process");
const definitelytyped_header_parser_1 = require("definitelytyped-header-parser");

@@ -23,4 +22,5 @@ const fs_promise_1 = require("fs-promise");

const args = process.argv.slice(2);
let noLint = false;
let dirPath = process.cwd();
let onlyTestTsNext = false;
let shouldListen = false;
for (const arg of args) {

@@ -36,5 +36,10 @@ switch (arg) {

return;
case "--noLint":
noLint = true;
case "--onlyTestTsNext":
onlyTestTsNext = true;
break;
// Only for use by types-publisher.
// Listens for { path, onlyTestTsNext } messages and ouputs { path, status }.
case "--listen":
shouldListen = true;
break;
default:

@@ -51,24 +56,44 @@ if (arg.startsWith("--")) {

: arg;
dirPath = dirPath === undefined ? path : path_1.join(dirPath, path);
dirPath = path_1.join(dirPath, path);
}
}
yield installer_1.installAll();
yield runTests(dirPath, noLint);
if (shouldListen) {
listen(dirPath);
}
else {
yield runTests(dirPath, onlyTestTsNext);
}
});
}
function usage() {
console.error("Usage: dtslint [--version] [--noLint] [--installAll]");
console.error("Usage: dtslint [--version] [--installAll]");
console.error("Args:");
console.error(" --version Print version and exit.");
console.error(" --noLint Just run 'tsc'. (Not recommended.)");
console.error(" --installAll Cleans and installs all TypeScript versions.");
console.error(" --version Print version and exit.");
console.error(" --installAll Cleans and installs all TypeScript versions.");
console.error(" --onlyTestTsNext Only run with `typescript@next`, not with the minimum version.");
}
function runTests(dirPath, noLint) {
function listen(dirPath) {
process.on("message", (message) => {
const { path, onlyTestTsNext } = message;
runTests(path_1.join(dirPath, path), onlyTestTsNext)
.catch(e => e.stack)
.then(maybeError => {
process.send({ path, status: maybeError === undefined ? "OK" : maybeError });
})
.catch(e => console.error(e.stack));
});
}
function runTests(dirPath, onlyTestTsNext) {
return __awaiter(this, void 0, void 0, function* () {
const text = yield fs_promise_1.readFile(path_1.join(dirPath, "index.d.ts"), "utf-8");
// If this *is* on DefinitelyTyped, types-publisher will fail if it can't parse the header.
const dt = text.includes("// Type definitions for");
if (dt) {
// Someone may have copied text from DefinitelyTyped to their type definition and included a header,
// so assert that we're really on DefinitelyTyped.
assertPathIsInDefinitelyTyped(dirPath);
}
const minVersion = getTypeScriptVersion(text);
if (!noLint) {
yield lint_1.checkTslintJson(dirPath, dt);
}
yield lint_1.checkTslintJson(dirPath, dt);
if (dt) {

@@ -78,3 +103,3 @@ yield checks_1.checkPackageJson(dirPath);

yield checks_1.checkTsconfig(dirPath, dt);
const err = yield test(dirPath, noLint, minVersion);
const err = yield lint_1.lint(dirPath, minVersion, onlyTestTsNext);
if (err) {

@@ -85,2 +110,12 @@ throw new Error(err);

}
function assertPathIsInDefinitelyTyped(dirPath) {
const parent = path_1.dirname(dirPath);
const types = /^v\d+$/.test(path_1.basename(dirPath)) ? path_1.dirname(parent) : parent;
const dt = path_1.dirname(types);
if (path_1.basename(dt) !== "DefinitelyTyped" || path_1.basename(types) !== "types") {
throw new Error("Since this type definition includes a header (a comment starting with `// Type definitions for`), "
+ "assumed this was a DefinitelyTyped package.\n"
+ "But it is not in a `DefinitelyTyped/types/xxx` directory.");
}
}
function getTypeScriptVersion(text) {

@@ -98,31 +133,2 @@ const searchString = "// TypeScript Version: ";

}
function test(dirPath, noLint, minVersion) {
return __awaiter(this, void 0, void 0, function* () {
if (noLint) {
for (const tsVersion of ["next", minVersion]) {
// Special for old DefinitelyTyped packages that aren't linted yet.
const err = yield execScript("node " + installer_1.tscPath(tsVersion), dirPath);
if (err !== undefined && err.trim() !== "error TS5023: Unknown compiler option 'strictFunctionTypes'.") {
return `Error in TypeScript@${tsVersion}: ${err}`;
}
}
return undefined;
}
else {
return lint_1.lint(dirPath, minVersion);
}
});
}
function execScript(cmd, cwd) {
return new Promise(resolve => {
child_process_1.exec(cmd, { encoding: "utf8", cwd }, (err, stdout, stderr) => {
if (err) {
resolve(stdout + stderr);
}
else {
resolve(undefined);
}
});
});
}
if (!module.parent) {

@@ -129,0 +135,0 @@ main().catch(err => {

@@ -45,6 +45,2 @@ "use strict";

exports.typeScriptPath = typeScriptPath;
function tscPath(version) {
return path.join(typeScriptPath(version), "lib", "tsc.js");
}
exports.tscPath = tscPath;
function installDir(version) {

@@ -51,0 +47,0 @@ return path.join(installsDir, version);

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

const util_1 = require("./util");
function lint(dirPath, minVersion) {
function lint(dirPath, minVersion, onlyTestTsNext) {
return __awaiter(this, void 0, void 0, function* () {

@@ -28,5 +28,11 @@ const lintConfigPath = getConfigPath(dirPath);

const linter = new tslint_1.Linter(lintOptions, program);
const config = yield getLintConfig(lintConfigPath, tsconfigPath, minVersion);
const config = yield getLintConfig(lintConfigPath, tsconfigPath, minVersion, onlyTestTsNext);
for (const filename of program.getRootFileNames()) {
const contents = yield fs_promise_1.readFile(filename, "utf-8");
const err = testNoTsIgnore(contents) || testNoTslintDisables(contents);
if (err) {
const { pos, message } = err;
const place = program.getSourceFile(filename).getLineAndCharacterOfPosition(pos);
return `At ${filename}:${JSON.stringify(place)}: ${message}`;
}
linter.lint(filename, contents, config);

@@ -39,2 +45,23 @@ }

exports.lint = lint;
function testNoTsIgnore(text) {
const tsIgnore = "ts-ignore";
const pos = text.indexOf(tsIgnore);
return pos === -1 ? undefined : { pos, message: "'ts-ignore' is forbidden." };
}
function testNoTslintDisables(text) {
const tslintDisable = "tslint:disable";
let lastIndex = 0;
while (true) {
const pos = text.indexOf(tslintDisable, lastIndex);
if (pos === -1) {
return undefined;
}
const end = pos + tslintDisable.length;
if (text.charAt(end) !== "-") {
const message = "'tslint:disable' is forbidden. ('tslint:disable-line' and 'tslint:disable-next-line' are allowed.)";
return { pos, message };
}
lastIndex = end;
}
}
function checkTslintJson(dirPath, dt) {

@@ -61,3 +88,3 @@ return __awaiter(this, void 0, void 0, function* () {

}
function getLintConfig(expectedConfigPath, tsconfigPath, minVersion) {
function getLintConfig(expectedConfigPath, tsconfigPath, minVersion, onlyTestTsNext) {
return __awaiter(this, void 0, void 0, function* () {

@@ -76,2 +103,3 @@ const configPath = (yield fs_promise_1.exists(expectedConfigPath)) ? expectedConfigPath : path_1.join(__dirname, "..", "dtslint.json");

olderInstalls: definitelytyped_header_parser_1.TypeScriptVersion.range(minVersion).map(versionName => ({ versionName, path: installer_1.typeScriptPath(versionName) })),
onlyTestTsNext,
};

@@ -78,0 +106,0 @@ expectRule.ruleArguments = [expectOptions];

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

const nextFailures = getFailures("next", options.tsNextPath, /*nextHigherVersion*/ undefined);
if (nextFailures.length) {
if (options.onlyTestTsNext || nextFailures.length) {
return nextFailures;

@@ -229,3 +229,3 @@ }

}
const type = checker.getTypeAtLocation(node);
const type = checker.getTypeAtLocation(getNodeForExpectType(node, ts));
const actual = checker.typeToString(type, /*enclosingDeclaration*/ undefined, ts.TypeFormatFlags.NoTruncation);

@@ -241,2 +241,14 @@ if (actual !== expected) {

}
function getNodeForExpectType(node, ts) {
if (node.kind === ts.SyntaxKind.VariableStatement) { // ts2.0 doesn't have `isVariableStatement`
const { declarationList: { declarations } } = node;
if (declarations.length === 1) {
const { initializer } = declarations[0];
if (initializer) {
return initializer;
}
}
}
return node;
}
function lineOfPosition(pos, sourceFile) {

@@ -243,0 +255,0 @@ return sourceFile.getLineAndCharacterOfPosition(pos).line;

@@ -102,2 +102,5 @@ "use strict";

const jsdoc = tag.parent;
if (jsdoc.kind === ts.SyntaxKind.JSDocTypeLiteral) {
return undefined;
}
if (jsdoc.comment === undefined && jsdoc.tags.length === 1) {

@@ -104,0 +107,0 @@ // This is the only tag -- remove the whole comment

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

apply(sourceFile) {
if (sourceFile.statements.length) {
const { statements, referencedFiles, typeReferenceDirectives } = sourceFile;
if (statements.length + referencedFiles.length + typeReferenceDirectives.length !== 0) {
return [];

@@ -12,0 +13,0 @@ }

@@ -22,4 +22,4 @@ "use strict";

function walk(ctx) {
eachModuleStatement(ctx.sourceFile, statement => {
if (isVariableStatement(statement)) {
util_1.eachModuleStatement(ctx.sourceFile, statement => {
if (ts.isVariableStatement(statement)) {
for (const varDecl of statement.declarationList.declarations) {

@@ -33,32 +33,2 @@ if (varDecl.type !== undefined && varDecl.type.kind === ts.SyntaxKind.FunctionType) {

}
function isVariableStatement(node) {
return node.kind === ts.SyntaxKind.VariableStatement;
}
function eachModuleStatement(sourceFile, action) {
if (!sourceFile.isDeclarationFile) {
return;
}
for (const node of sourceFile.statements) {
if (isModuleDeclaration(node)) {
let { body } = node;
if (!body) {
return;
}
while (body.kind === ts.SyntaxKind.ModuleDeclaration) {
body = body.body;
}
if (body.kind === ts.SyntaxKind.ModuleBlock) {
for (const statement of body.statements) {
action(statement);
}
}
}
else {
action(node);
}
}
}
function isModuleDeclaration(node) {
return node.kind === ts.SyntaxKind.ModuleDeclaration;
}
//# sourceMappingURL=preferDeclareFunctionRule.js.map

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

case ts.SyntaxKind.CallExpression:
case ts.SyntaxKind.TypeParameter:// Allow f<T = void>
case ts.SyntaxKind.TypeParameter: // Allow f<T = void>
return true;

@@ -41,0 +41,0 @@ default:

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

const stripJsonComments = require("strip-json-comments");
const ts = require("typescript");
function readJson(path) {

@@ -39,2 +40,32 @@ return __awaiter(this, void 0, void 0, function* () {

exports.getCommonDirectoryName = getCommonDirectoryName;
function eachModuleStatement(sourceFile, action) {
if (!sourceFile.isDeclarationFile) {
return;
}
for (const node of sourceFile.statements) {
if (ts.isModuleDeclaration(node)) {
const statements = getModuleDeclarationStatements(node);
if (statements) {
for (const statement of statements) {
action(statement);
}
}
}
else {
action(node);
}
}
}
exports.eachModuleStatement = eachModuleStatement;
function getModuleDeclarationStatements(node) {
let { body } = node;
if (!body) {
return undefined;
}
while (body.kind === ts.SyntaxKind.ModuleDeclaration) {
body = body.body;
}
return ts.isModuleBlock(body) ? body.statements : undefined;
}
exports.getModuleDeclarationStatements = getModuleDeclarationStatements;
//# sourceMappingURL=util.js.map

@@ -8,4 +8,4 @@ {

"no-self-import": true,
//TODO: "no-outside-dependencies": true,
"no-implicit-dependencies": false,
"no-redundant-jsdoc": false,

@@ -12,0 +12,0 @@ "no-redundant-jsdoc-2": true

@@ -9,3 +9,5 @@ {

"no-bad-reference": true,
"no-const-enum": true,
"no-dead-reference": true,
"no-import-default-of-export-equals": true,
"no-padding": true,

@@ -87,2 +89,3 @@ "no-redundant-undefined": true,

"newline-before-return": false,
"newline-per-chained-call": false,
"no-angle-bracket-type-assertion": false,

@@ -93,2 +96,3 @@ "no-bitwise": false,

"no-empty": false,
"no-implicit-dependencies": false, // See https://github.com/palantir/tslint/issues/3364
"no-inferred-empty-object-type": false,

@@ -95,0 +99,0 @@ "no-magic-numbers": false,

{
"name": "dtslint",
"version": "0.2.0",
"version": "0.3.0",
"description": "Runs tests on TypeScript definition files",

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

"build": "tsc",
"lint": "tslint --project tsconfig.json --type-check --format stylish",
"lint": "tslint --project tsconfig.json --format stylish",
"test": "node test/test.js"

@@ -31,3 +31,3 @@ },

"strip-json-comments": "^2.0.1",
"tslint": "^5.4.2",
"tslint": "^5.9.1",
"typescript": "next"

@@ -34,0 +34,0 @@ },

@@ -42,2 +42,3 @@ `dtslint` tests a TypeScript declaration file for style and correctness.

"strictNullChecks": true,
"noEmit": true,

@@ -65,3 +66,3 @@ // If the library is an external module (uses `export`), this allows your test file to import "mylib" instead of "./index".

{
"extends": "dtslint/dtslint.json",
"extends": "dtslint/dtslint.json", // Or "dtslint/dt.json" if on DefinitelyTyped
"rules": {

@@ -136,3 +137,3 @@ "semicolon": false,

Use `npm run test` to run all tests.
To run a single test: `node node_modules/tslint/bin/tslint --rulesDirectory bin/rules --test test/expect`.
To run a single test: `node node_modules/tslint/bin/tslint --rules-dir bin/rules --test test/expect`.

@@ -139,0 +140,0 @@

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

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