Socket
Socket
Sign inDemoInstall

tslint

Package Overview
Dependencies
Maintainers
1
Versions
182
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

tslint - npm Package Compare versions

Comparing version 5.19.0 to 5.20.0

2

lib/linter.js

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

};
Linter.VERSION = "5.19.0";
Linter.VERSION = "5.20.0";
Linter.findConfiguration = configuration_1.findConfiguration;

@@ -249,0 +249,0 @@ Linter.findConfigurationPath = configuration_1.findConfigurationPath;

@@ -140,2 +140,4 @@ "use strict";

return true;
case ts.SyntaxKind.ParenthesizedType:
return isSimpleType(nodeType.type);
case ts.SyntaxKind.TypeReference:

@@ -142,0 +144,0 @@ // TypeReferences must be non-generic or be another Array with a simple type

@@ -28,7 +28,13 @@ "use strict";

Casing["PascalCase"] = "pascal-case";
Casing["Ignored"] = "ignored";
Casing["Ignored"] = "ignore";
Casing["KebabCase"] = "kebab-case";
Casing["SnakeCase"] = "snake-case";
})(Casing || (Casing = {}));
var rules = [Casing.CamelCase, Casing.PascalCase, Casing.KebabCase, Casing.SnakeCase];
var rules = [
Casing.CamelCase,
Casing.Ignored,
Casing.PascalCase,
Casing.KebabCase,
Casing.SnakeCase,
];
var validCasingOptions = new Set(rules);

@@ -35,0 +41,0 @@ function isCorrectCasing(fileName, casing) {

@@ -21,4 +21,7 @@ /**

static metadata: Lint.IRuleMetadata;
static FAILURE_STRING: string;
static FAILURE_STRING_ALLOW_GENERICS: string;
static FAILURE_STRING_NO_GENERICS: string;
static FAILURE_WRONG_GENERIC: (genericName: string) => string;
apply(sourceFile: ts.SourceFile): Lint.RuleFailure[];
private getAllowGenerics;
}

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

var tslib_1 = require("tslib");
var _a, _b, _c;
var tsutils = require("tsutils");
var ts = require("typescript");
var Lint = require("../index");
var OPTION_ALLOW_GENERICS = "allow-generics";
var Rule = /** @class */ (function (_super) {

@@ -29,13 +32,38 @@ tslib_1.__extends(Rule, _super);

Rule.prototype.apply = function (sourceFile) {
return this.applyWithFunction(sourceFile, walk);
return this.applyWithFunction(sourceFile, walk, {
// tslint:disable-next-line:no-object-literal-type-assertion
allowGenerics: this.getAllowGenerics(this.ruleArguments[0]),
});
};
Rule.prototype.getAllowGenerics = function (rawArgument) {
if (rawArgument == undefined) {
return true;
}
var allowGenerics = rawArgument[OPTION_ALLOW_GENERICS];
return allowGenerics instanceof Array ? new Set(allowGenerics) : Boolean(allowGenerics);
};
/* tslint:disable:object-literal-sort-keys */
Rule.metadata = {
ruleName: "invalid-void",
description: Lint.Utils.dedent(templateObject_1 || (templateObject_1 = tslib_1.__makeTemplateObject(["\n Disallows usage of `void` type outside of return type.\n If `void` is used as return type, it shouldn't be a part of intersection/union type."], ["\n Disallows usage of \\`void\\` type outside of return type.\n If \\`void\\` is used as return type, it shouldn't be a part of intersection/union type."]))),
description: Lint.Utils.dedent(templateObject_1 || (templateObject_1 = tslib_1.__makeTemplateObject(["\n Disallows usage of `void` type outside of generic or return types.\n If `void` is used as return type, it shouldn't be a part of intersection/union type."], ["\n Disallows usage of \\`void\\` type outside of generic or return types.\n If \\`void\\` is used as return type, it shouldn't be a part of intersection/union type."]))),
rationale: Lint.Utils.dedent(templateObject_2 || (templateObject_2 = tslib_1.__makeTemplateObject(["\n The `void` type means \"nothing\" or that a function does not return any value,\n in contra with implicit `undefined` type which means that a function returns a value `undefined`.\n So \"nothing\" cannot be mixed with any other types.\n If you need this - use `undefined` type instead."], ["\n The \\`void\\` type means \"nothing\" or that a function does not return any value,\n in contra with implicit \\`undefined\\` type which means that a function returns a value \\`undefined\\`.\n So \"nothing\" cannot be mixed with any other types.\n If you need this - use \\`undefined\\` type instead."]))),
hasFix: false,
optionsDescription: "Not configurable.",
options: null,
optionExamples: [true],
optionsDescription: Lint.Utils.dedent(templateObject_3 || (templateObject_3 = tslib_1.__makeTemplateObject(["\n If `", "` is specified as `false`, then generic types will no longer be allowed to to be `void`.\n Alternately, provide an array of strings for `", "` to exclusively allow generic types by those names."], ["\n If \\`", "\\` is specified as \\`false\\`, then generic types will no longer be allowed to to be \\`void\\`.\n Alternately, provide an array of strings for \\`", "\\` to exclusively allow generic types by those names."])), OPTION_ALLOW_GENERICS, OPTION_ALLOW_GENERICS),
options: {
type: "object",
properties: (_a = {},
_a[OPTION_ALLOW_GENERICS] = {
oneOf: [
{ type: "boolean" },
{ type: "array", items: { type: "string" }, minLength: 1 },
],
},
_a),
additionalProperties: false,
},
optionExamples: [
true,
[true, (_b = {}, _b[OPTION_ALLOW_GENERICS] = false, _b)],
[true, (_c = {}, _c[OPTION_ALLOW_GENERICS] = ["Promise", "PromiseLike"], _c)],
],
type: "maintainability",

@@ -45,3 +73,7 @@ typescriptOnly: true,

/* tslint:enable:object-literal-sort-keys */
Rule.FAILURE_STRING = "void is not a valid type other than return types";
Rule.FAILURE_STRING_ALLOW_GENERICS = "void is only valid as a return type or generic type variable";
Rule.FAILURE_STRING_NO_GENERICS = "void is only valid as a return type";
Rule.FAILURE_WRONG_GENERIC = function (genericName) {
return genericName + " may not have void as a type variable";
};
return Rule;

@@ -70,5 +102,41 @@ }(Lint.Rules.AbstractRule));

function walk(ctx) {
var defaultFailureString = ctx.options.allowGenerics
? Rule.FAILURE_STRING_ALLOW_GENERICS
: Rule.FAILURE_STRING_NO_GENERICS;
var getGenericReferenceName = function (node) {
var rawName = tsutils.isNewExpression(node) ? node.expression : node.typeName;
return tsutils.isIdentifier(rawName) ? rawName.text : rawName.getText(ctx.sourceFile);
};
var getTypeReferenceFailure = function (node) {
if (!(ctx.options.allowGenerics instanceof Set)) {
return ctx.options.allowGenerics ? undefined : defaultFailureString;
}
var genericName = getGenericReferenceName(node);
return ctx.options.allowGenerics.has(genericName)
? undefined
: Rule.FAILURE_WRONG_GENERIC(genericName);
};
var checkTypeReference = function (parent, node) {
var failure = getTypeReferenceFailure(parent);
if (failure !== undefined) {
ctx.addFailureAtNode(node, failure);
}
};
var isParentGenericReference = function (parent, node) {
if (tsutils.isTypeReferenceNode(parent)) {
return true;
}
return (tsutils.isNewExpression(parent) &&
parent.typeArguments !== undefined &&
ts.isTypeNode(node) &&
parent.typeArguments.indexOf(node) !== -1);
};
ts.forEachChild(ctx.sourceFile, function cb(node) {
if (node.kind === ts.SyntaxKind.VoidKeyword && failedKinds.has(node.parent.kind)) {
ctx.addFailureAtNode(node, Rule.FAILURE_STRING);
if (isParentGenericReference(node.parent, node)) {
checkTypeReference(node.parent, node);
}
else {
ctx.addFailureAtNode(node, defaultFailureString);
}
}

@@ -78,2 +146,2 @@ ts.forEachChild(node, cb);

}
var templateObject_1, templateObject_2;
var templateObject_1, templateObject_2, templateObject_3;

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

Rule.prototype.applyWithProgram = function (sourceFile, program) {
return this.applyWithWalker(new Walker(sourceFile, this.ruleName, this.ruleArguments, program.getTypeChecker(), !!program.getCompilerOptions().strictNullChecks));
var compilerOptions = program.getCompilerOptions();
var strictChecksEnabled = !!compilerOptions.strict;
var strictNullChecksEnabled = compilerOptions.strictNullChecks === true;
var strictNullChecksNotDisabled = compilerOptions.strictNullChecks !== false;
return this.applyWithWalker(new Walker(sourceFile, this.ruleName, this.ruleArguments, program.getTypeChecker(), strictNullChecksEnabled || (strictChecksEnabled && strictNullChecksNotDisabled)));
};

@@ -32,0 +36,0 @@ /* tslint:disable:object-literal-sort-keys */

@@ -19,2 +19,6 @@ /**

import * as Lint from "..";
interface Options {
enforceShorthandMethods: boolean;
enforceShorthandProperties: boolean;
}
export declare class Rule extends Lint.Rules.AbstractRule {

@@ -25,3 +29,8 @@ static metadata: Lint.IRuleMetadata;

static SHORTHAND_ASSIGNMENT: string;
static getLonghandPropertyErrorMessage(nodeText: string): string;
static getLonghandMethodErrorMessage(nodeText: string): string;
static getDisallowedShorthandErrorMessage(options: Options): "Shorthand property assignments have been disallowed." | "Shorthand method assignments have been disallowed." | "Shorthand property and method assignments have been disallowed.";
apply(sourceFile: ts.SourceFile): Lint.RuleFailure[];
private parseOptions;
}
export {};

@@ -20,6 +20,9 @@ "use strict";

var tslib_1 = require("tslib");
var _a, _b;
var tsutils_1 = require("tsutils");
var ts = require("typescript");
var Lint = require("..");
var OPTION_NEVER = "never";
var OPTION_VALUE_NEVER = "never";
var OPTION_KEY_PROPERTY = "property";
var OPTION_KEY_METHOD = "method";
var Rule = /** @class */ (function (_super) {

@@ -30,7 +33,45 @@ tslib_1.__extends(Rule, _super);

}
Rule.getLonghandPropertyErrorMessage = function (nodeText) {
return "Expected property shorthand in object literal ('" + nodeText + "').";
};
Rule.getLonghandMethodErrorMessage = function (nodeText) {
return "Expected method shorthand in object literal ('" + nodeText + "').";
};
Rule.getDisallowedShorthandErrorMessage = function (options) {
if (options.enforceShorthandMethods && !options.enforceShorthandProperties) {
return "Shorthand property assignments have been disallowed.";
}
else if (!options.enforceShorthandMethods && options.enforceShorthandProperties) {
return "Shorthand method assignments have been disallowed.";
}
return "Shorthand property and method assignments have been disallowed.";
};
Rule.prototype.apply = function (sourceFile) {
return this.applyWithFunction(sourceFile, this.ruleArguments.indexOf(OPTION_NEVER) === -1
? enforceShorthandWalker
: disallowShorthandWalker);
return this.applyWithFunction(sourceFile, walk, this.parseOptions(this.ruleArguments));
};
Rule.prototype.parseOptions = function (options) {
if (options.indexOf(OPTION_VALUE_NEVER) !== -1) {
return {
enforceShorthandMethods: false,
enforceShorthandProperties: false,
};
}
var optionsObject = options.find(function (el) {
return typeof el === "object" &&
(el[OPTION_KEY_PROPERTY] === OPTION_VALUE_NEVER ||
el[OPTION_KEY_METHOD] === OPTION_VALUE_NEVER);
});
if (optionsObject !== undefined) {
return {
enforceShorthandMethods: !(optionsObject[OPTION_KEY_METHOD] === OPTION_VALUE_NEVER),
enforceShorthandProperties: !(optionsObject[OPTION_KEY_PROPERTY] === OPTION_VALUE_NEVER),
};
}
else {
return {
enforceShorthandMethods: true,
enforceShorthandProperties: true,
};
}
};
/* tslint:disable:object-literal-sort-keys */

@@ -41,8 +82,31 @@ Rule.metadata = {

hasFix: true,
optionsDescription: Lint.Utils.dedent(templateObject_1 || (templateObject_1 = tslib_1.__makeTemplateObject(["\n If the 'never' option is provided, any shorthand object literal syntax will cause a failure."], ["\n If the \\'never\\' option is provided, any shorthand object literal syntax will cause a failure."]))),
optionsDescription: Lint.Utils.dedent(templateObject_1 || (templateObject_1 = tslib_1.__makeTemplateObject(["\n `\"always\"` assumed to be default option, thus with no options provided\n the rule enforces object literal methods and properties shorthands.\n With `\"never\"` option provided, any shorthand object literal syntax causes an error.\n\n The rule can be configured in a more granular way.\n With `{\"property\": \"never\"}` provided (which is equivalent to `{\"property\": \"never\", \"method\": \"always\"}`),\n the rule only flags property shorthand assignments,\n and respectively with `{\"method\": \"never\"}` (equivalent to `{\"property\": \"always\", \"method\": \"never\"}`),\n the rule fails only on method shorthands."], ["\n \\`\"always\"\\` assumed to be default option, thus with no options provided\n the rule enforces object literal methods and properties shorthands.\n With \\`\"never\"\\` option provided, any shorthand object literal syntax causes an error.\n\n The rule can be configured in a more granular way.\n With \\`{\"property\": \"never\"}\\` provided (which is equivalent to \\`{\"property\": \"never\", \"method\": \"always\"}\\`),\n the rule only flags property shorthand assignments,\n and respectively with \\`{\"method\": \"never\"}\\` (equivalent to \\`{\"property\": \"always\", \"method\": \"never\"}\\`),\n the rule fails only on method shorthands."]))),
options: {
type: "string",
enum: [OPTION_NEVER],
oneOf: [
{
type: "string",
enum: [OPTION_VALUE_NEVER],
},
{
type: "object",
properties: (_a = {},
_a[OPTION_KEY_PROPERTY] = {
type: "string",
enum: [OPTION_VALUE_NEVER],
},
_a[OPTION_KEY_METHOD] = {
type: "string",
enum: [OPTION_VALUE_NEVER],
},
_a),
minProperties: 1,
maxProperties: 2,
},
],
},
optionExamples: [true, [true, OPTION_NEVER]],
optionExamples: [
true,
[true, OPTION_VALUE_NEVER],
[true, (_b = {}, _b[OPTION_KEY_PROPERTY] = OPTION_VALUE_NEVER, _b)],
],
type: "style",

@@ -58,10 +122,27 @@ typescriptOnly: false,

exports.Rule = Rule;
function disallowShorthandWalker(ctx) {
function walk(ctx) {
var _a = ctx.options, enforceShorthandMethods = _a.enforceShorthandMethods, enforceShorthandProperties = _a.enforceShorthandProperties;
return ts.forEachChild(ctx.sourceFile, function cb(node) {
if (tsutils_1.isShorthandPropertyAssignment(node)) {
ctx.addFailureAtNode(node.name, Rule.SHORTHAND_ASSIGNMENT, Lint.Replacement.appendText(node.getStart(ctx.sourceFile), node.name.text + ": "));
if (enforceShorthandProperties &&
tsutils_1.isPropertyAssignment(node) &&
node.name.kind === ts.SyntaxKind.Identifier &&
tsutils_1.isIdentifier(node.initializer) &&
node.name.text === node.initializer.text) {
ctx.addFailureAtNode(node, Rule.getLonghandPropertyErrorMessage("{" + node.name.text + "}"), Lint.Replacement.deleteFromTo(node.name.end, node.end));
}
else if (tsutils_1.isMethodDeclaration(node) &&
else if (enforceShorthandMethods &&
tsutils_1.isPropertyAssignment(node) &&
tsutils_1.isFunctionExpression(node.initializer) &&
// allow named function expressions
node.initializer.name === undefined) {
var _a = handleLonghandMethod(node.name, node.initializer, ctx.sourceFile), name = _a[0], fix = _a[1];
ctx.addFailure(node.getStart(ctx.sourceFile), tsutils_1.getChildOfKind(node.initializer, ts.SyntaxKind.OpenParenToken, ctx.sourceFile).pos, Rule.getLonghandMethodErrorMessage("{" + name + "() {...}}"), fix);
}
else if (!enforceShorthandProperties && tsutils_1.isShorthandPropertyAssignment(node)) {
ctx.addFailureAtNode(node.name, Rule.getDisallowedShorthandErrorMessage(ctx.options), Lint.Replacement.appendText(node.getStart(ctx.sourceFile), node.name.text + ": "));
}
else if (!enforceShorthandMethods &&
tsutils_1.isMethodDeclaration(node) &&
node.parent.kind === ts.SyntaxKind.ObjectLiteralExpression) {
ctx.addFailureAtNode(node.name, Rule.SHORTHAND_ASSIGNMENT, fixShorthandMethodDeclaration(node, ctx.sourceFile));
ctx.addFailureAtNode(node.name, Rule.getDisallowedShorthandErrorMessage(ctx.options), fixShorthandMethodDeclaration(node, ctx.sourceFile));
}

@@ -71,21 +152,2 @@ return ts.forEachChild(node, cb);

}
function enforceShorthandWalker(ctx) {
return ts.forEachChild(ctx.sourceFile, function cb(node) {
if (tsutils_1.isPropertyAssignment(node)) {
if (node.name.kind === ts.SyntaxKind.Identifier &&
tsutils_1.isIdentifier(node.initializer) &&
node.name.text === node.initializer.text) {
ctx.addFailureAtNode(node, Rule.LONGHAND_PROPERTY + "('{" + node.name.text + "}').", Lint.Replacement.deleteFromTo(node.name.end, node.end));
}
else if (tsutils_1.isFunctionExpression(node.initializer) &&
// allow named function expressions
node.initializer.name === undefined) {
var _a = handleLonghandMethod(node.name, node.initializer, ctx.sourceFile), name = _a[0], fix = _a[1];
ctx.addFailure(node.getStart(ctx.sourceFile), tsutils_1.getChildOfKind(node.initializer, ts.SyntaxKind.OpenParenToken, ctx.sourceFile)
.pos, Rule.LONGHAND_METHOD + "('{" + name + "() {...}}').", fix);
}
}
return ts.forEachChild(node, cb);
});
}
function fixShorthandMethodDeclaration(node, sourceFile) {

@@ -92,0 +154,0 @@ var isGenerator = node.asteriskToken !== undefined;

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

// side no matter what, because its right side might not follow the rules.
if (options.ignoreRhs || !isBooleanBinaryExpression(left)) {
if (!isBooleanBinaryExpression(left)) {
checkExpression(left, b);

@@ -112,0 +112,0 @@ }

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

var tslib_1 = require("tslib");
var _a, _b;
var tsutils_1 = require("tsutils");

@@ -25,2 +26,3 @@ var ts = require("typescript");

var rule_1 = require("../language/rule/rule");
var OPTION_CHECK_SUPER_CALL = "check-super-calls";
var Rule = /** @class */ (function (_super) {

@@ -32,10 +34,19 @@ tslib_1.__extends(Rule, _super);

Rule.prototype.apply = function (sourceFile) {
return this.applyWithFunction(sourceFile, walk);
var options = {
checkSuperCall: this.ruleArguments.length !== 0 &&
this.ruleArguments[0]["check-super-calls"] === true,
};
return this.applyWithFunction(sourceFile, walk, options);
};
Rule.metadata = {
description: "Prevents blank constructors, as they are redundant.",
optionExamples: [true],
options: null,
optionsDescription: "Not configurable.",
rationale: Lint.Utils.dedent(templateObject_1 || (templateObject_1 = tslib_1.__makeTemplateObject(["\n JavaScript implicitly adds a blank constructor when there isn't one.\n It's not necessary to manually add one in.\n "], ["\n JavaScript implicitly adds a blank constructor when there isn't one.\n It's not necessary to manually add one in.\n "]))),
optionExamples: [true, [true, (_a = {}, _a[OPTION_CHECK_SUPER_CALL] = true, _a)]],
options: {
properties: (_b = {},
_b[OPTION_CHECK_SUPER_CALL] = { type: "boolean" },
_b),
type: "object",
},
optionsDescription: Lint.Utils.dedent(templateObject_1 || (templateObject_1 = tslib_1.__makeTemplateObject(["\n An optional object with the property '", "'.\n This is to check for unnecessary constructor parameters for super call"], ["\n An optional object with the property '", "'.\n This is to check for unnecessary constructor parameters for super call"])), OPTION_CHECK_SUPER_CALL),
rationale: Lint.Utils.dedent(templateObject_2 || (templateObject_2 = tslib_1.__makeTemplateObject(["\n JavaScript implicitly adds a blank constructor when there isn't one.\n It's not necessary to manually add one in.\n "], ["\n JavaScript implicitly adds a blank constructor when there isn't one.\n It's not necessary to manually add one in.\n "]))),
ruleName: "unnecessary-constructor",

@@ -49,5 +60,41 @@ type: "functionality",

exports.Rule = Rule;
var isEmptyConstructor = function (node) {
return node.body !== undefined && node.body.statements.length === 0;
var containsSuper = function (statement, constructorParameters) {
if (tsutils_1.isExpressionStatement(statement) &&
tsutils_1.isCallExpression(statement.expression) &&
ts.SyntaxKind.SuperKeyword === statement.expression.expression.kind) {
var superArguments = statement.expression.arguments;
if (superArguments.length < constructorParameters.length) {
return true;
}
if (superArguments.length === constructorParameters.length) {
if (constructorParameters.length === 0) {
return true;
}
for (var _i = 0, constructorParameters_1 = constructorParameters; _i < constructorParameters_1.length; _i++) {
var constructorParameter = constructorParameters_1[_i];
for (var _a = 0, superArguments_1 = superArguments; _a < superArguments_1.length; _a++) {
var superArgument = superArguments_1[_a];
if (constructorParameter.name.kind !== superArgument.kind) {
return false;
}
}
}
return true;
}
}
return false;
};
var isEmptyOrContainsOnlySuper = function (node, options) {
if (node.body !== undefined) {
var checkSuperCall = options.checkSuperCall;
if (node.body.statements.length === 0) {
return true;
}
if (checkSuperCall) {
return (node.body.statements.length === 1 &&
containsSuper(node.body.statements[0], node.parameters));
}
}
return false;
};
var containsConstructorParameter = function (node) {

@@ -69,3 +116,3 @@ // If this has any parameter properties

if (tsutils_1.isConstructorDeclaration(node) &&
isEmptyConstructor(node) &&
isEmptyOrContainsOnlySuper(node, context.options) &&
!containsConstructorParameter(node) &&

@@ -90,2 +137,2 @@ !containsDecorator(node) &&

}
var templateObject_1;
var templateObject_1, templateObject_2;
{
"name": "tslint",
"version": "5.19.0",
"version": "5.20.0",
"description": "An extensible static analysis linter for the TypeScript language",

@@ -36,3 +36,3 @@ "bin": {

"commander": "^2.12.1",
"diff": "^3.2.0",
"diff": "^4.0.1",
"glob": "^7.1.1",

@@ -39,0 +39,0 @@ "js-yaml": "^3.13.1",

Sorry, the diff of this file is too big to display

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