Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

custom-tslint-formatters

Package Overview
Dependencies
Maintainers
1
Versions
12
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

custom-tslint-formatters - npm Package Compare versions

Comparing version 2.2.0 to 2.3.0

163

formatters/groupedFormatter.js

@@ -15,2 +15,83 @@ "use strict";

var chalk_1 = require("chalk");
var logSymbols = require("log-symbols");
var FailureGroup = /** @class */ (function () {
function FailureGroup(filename) {
this.filename = filename;
this.failures = [];
}
FailureGroup.prototype.add = function (failure) {
this.failures.push(failure);
};
Object.defineProperty(FailureGroup.prototype, "warningCount", {
get: function () {
return this.getCountForSeverity('warning');
},
enumerable: true,
configurable: true
});
Object.defineProperty(FailureGroup.prototype, "errorCount", {
get: function () {
return this.getCountForSeverity('error');
},
enumerable: true,
configurable: true
});
Object.defineProperty(FailureGroup.prototype, "fixCount", {
get: function () {
return this.failures.reduce(function (count, failure) { return (failure.hasFix() ? count + 1 : count); }, 0);
},
enumerable: true,
configurable: true
});
FailureGroup.prototype.getCountForSeverity = function (severity) {
return this.failures.reduce(function (count, failure) {
return failure.getRuleSeverity() === severity ? count + 1 : count;
}, 0);
};
return FailureGroup;
}());
var GroupedFailures = /** @class */ (function () {
function GroupedFailures(failures) {
var _this = this;
this.groups = {};
failures.forEach(function (failure) { return _this.addFailure(failure); });
}
GroupedFailures.prototype.addFailure = function (failure) {
var filename = failure.getFileName();
var group = this.groups[filename];
if (!group) {
this.groups[filename] = group = new FailureGroup(filename);
}
group.add(failure);
};
Object.defineProperty(GroupedFailures.prototype, "warningCount", {
get: function () {
return this.reduce(function (count, group) { return count + group.warningCount; }, 0);
},
enumerable: true,
configurable: true
});
Object.defineProperty(GroupedFailures.prototype, "errorCount", {
get: function () {
return this.reduce(function (count, group) { return count + group.errorCount; }, 0);
},
enumerable: true,
configurable: true
});
Object.defineProperty(GroupedFailures.prototype, "fixCount", {
get: function () {
return this.reduce(function (count, group) { return count + group.fixCount; }, 0);
},
enumerable: true,
configurable: true
});
GroupedFailures.prototype.reduce = function (callbackfn, initialValue) {
var _this = this;
return Object.keys(this.groups).reduce(function (previousValue, filename) {
var group = _this.groups[filename];
return group ? callbackfn(previousValue, group) : previousValue;
}, initialValue);
};
return GroupedFailures;
}());
var Formatter = /** @class */ (function (_super) {

@@ -21,29 +102,17 @@ __extends(Formatter, _super);

}
Formatter.prototype.formatFailure = function (failure) {
var _a = failure.getStartPosition().getLineAndCharacter(), line = _a.line, character = _a.character;
var position = line + 1 + ":" + (character + 1);
var message = failure.getFailure();
var ruleName = failure.getRuleName();
var severity = failure.getRuleSeverity();
var positionColor = severity === 'warning' ? chalk_1.default.yellow : chalk_1.default.red;
return " " + positionColor(position) + " " + message + " " + chalk_1.default.dim(ruleName);
Formatter.prototype.sortFailures = function (failures) {
return failures.sort(function (a, b) {
var _a = a.getStartPosition().getLineAndCharacter(), lineA = _a.line, characterA = _a.character;
var _b = b.getStartPosition().getLineAndCharacter(), lineB = _b.line, characterB = _b.character;
var filenameA = a.getFileName();
var filenameB = b.getFileName();
if (filenameA === filenameB) {
return lineA === lineB ? characterA - characterB : lineA - lineB;
}
return filenameA < filenameB ? -1 : filenameA > filenameB ? 1 : 0;
});
};
Formatter.prototype.groupByFile = function (failures) {
return failures.reduce(function (groups, failure) {
var fileName = failure.getFileName();
var fileFailures = groups[fileName] || [];
groups[fileName] = fileFailures.concat([failure]);
return groups;
}, {});
};
Formatter.prototype.format = function (failures) {
var _this = this;
var failuresByFile = this.groupByFile(failures);
return Object.keys(failuresByFile)
.reduce(function (lines, fileName) {
lines.push(chalk_1.default.underline.green(fileName));
var fileFailures = failuresByFile[fileName];
return lines.concat(fileFailures.map(_this.formatFailure), ['\n']);
}, [])
.join('\n');
var failuresByFile = new GroupedFailures(this.sortFailures(failures));
return getDetails(failuresByFile) + "\n" + getSummary(failuresByFile);
};

@@ -53,1 +122,45 @@ return Formatter;

exports.Formatter = Formatter;
function formatFailure(failure) {
var _a = failure.getStartPosition().getLineAndCharacter(), line = _a.line, character = _a.character;
var position = line + 1 + ":" + (character + 1);
var message = failure.getFailure();
var ruleName = failure.getRuleName();
var severity = failure.getRuleSeverity();
var positionColor = severity === 'warning' ? chalk_1.default.yellow : chalk_1.default.red;
var fixHint = failure.hasFix() ? " " + logSymbols.info : '';
return " " + positionColor(position) + " " + message + " " + chalk_1.default.dim(ruleName) + fixHint;
}
function getDetails(failures) {
return failures.reduce(function (current, group) {
var headline = chalk_1.default.underline.green(group.filename);
var formattedFailures = group.failures.map(formatFailure).join('\n');
return current + "\n" + headline + "\n" + formattedFailures + "\n";
}, '');
}
function getSummary(failures) {
var warningCount = failures.warningCount, errorCount = failures.errorCount, fixCount = failures.fixCount;
var issueCount = warningCount + errorCount;
var warnings = getCountText('warning', warningCount);
var errors = getCountText('error', errorCount);
var status = getStatusSymbol(warningCount, errorCount);
var summary = status + " Found " + warnings + " and " + errors + ".";
return issueCount === 0
? summary
: summary + "\n" + getFixableHint(issueCount, fixCount) + "\n";
}
function getFixableHint(issueCount, fixCount) {
var issues = getCountText('issue', issueCount);
return logSymbols.info + " " + fixCount + " out of " + issues + " " + (fixCount === 1 ? 'is' : 'are') + " fixable with the tslint option `--fix`.";
}
function getCountText(word, count) {
return count + " " + (count === 1 ? word : word + "s");
}
function getStatusSymbol(warningCount, errorCount) {
if (errorCount > 0) {
return logSymbols.error;
}
if (warningCount > 0) {
return logSymbols.warning;
}
return logSymbols.success;
}

7

package.json
{
"name": "custom-tslint-formatters",
"version": "2.2.0",
"version": "2.3.0",
"description": "Custom formatters for TSLint.",

@@ -12,2 +12,3 @@ "main": "index.js",

"lint:grouped": "tslint -s formatters -t grouped test/*.ts",
"lint:grouped:ok": "tslint -s formatters -t grouped test/test3.ts",
"lint:vscode": "tslint -s formatters -t vscode test/*.ts",

@@ -35,2 +36,3 @@ "precommit": "lint-staged",

"devDependencies": {
"@types/log-symbols": "^2.0.0",
"@types/node": "^8.9.1",

@@ -43,3 +45,4 @@ "lint-staged": "^6.1.0",

"dependencies": {
"chalk": "^2.3.0"
"chalk": "^2.3.0",
"log-symbols": "^2.2.0"
},

@@ -46,0 +49,0 @@ "lint-staged": {

@@ -11,5 +11,5 @@ Custom TSLint Formatters

Prints a block for each file with the file name as headline.
Prints a block for each file with the file name as headline, followed by a summary.
<img width="603" alt="custom tslint formatter grouped" src="docs/screenshots/grouped.png">
<img width="662" alt="custom tslint formatter grouped" src="docs/screenshots/grouped.png">

@@ -16,0 +16,0 @@ Errors are printed with a red position (`row:column`), warnings are printed with a yellow position.

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