Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JoshuaKGoldberg/2abf653072478ee076522a4913adb618 to your computer and use it in GitHub Desktop.
Save JoshuaKGoldberg/2abf653072478ee076522a4913adb618 to your computer and use it in GitHub Desktop.
Remove unused variables from gulp tslint
// Folder prefix for file names if running from a different path than gulp
const fileNamePrefix = "";
// Relies on an "input.txt" to exist in the same directory
// That "input.txt" should be a listing of just the tslint failures
const inputFileName = "input.txt";
const fs = require("fs");
const lines = fs
.readFileSync(inputFileName)
.toString()
.split("\n")
.map(line => line.trim());
const errorLines = lines
.filter(line => line.indexOf(": unused variable: ") !== -1)
// Filter out gulp tslint summary messages
.filter(line => line.indexOf("Failed to lint:") === -1);
const errorsInfo = errorLines
// Remove any preceding '>', just in case
.map(line => line.substring(line.indexOf(">") + 1))
.map(originalErrorLine => {
const parenthesisIndex = originalErrorLine.indexOf(") ") + 2;
const bracketIndex = originalErrorLine.indexOf("[", parenthesisIndex);
const fileName = fileNamePrefix + originalErrorLine.substring(parenthesisIndex, bracketIndex);
const commaIndex = originalErrorLine.indexOf(",", bracketIndex);
const lineNumber = parseInt(originalErrorLine.substring(bracketIndex + 1, commaIndex)) - 1;
return { fileName, lineNumber, originalErrorLine };
})
.reverse();
const errorFileNames = errorsInfo.map(errorInfo => errorInfo.fileName);
errorsInfo.forEach(errorInfo => {
const fileContents = fs.readFileSync(errorInfo.fileName).toString();
const fileLines = fileContents.split("\r\n");
const changedLinesBefore = fileLines
.slice(0, errorInfo.lineNumber)
var changedLinesAfter = fileLines
.slice(errorInfo.lineNumber + 1);
const changedLines = changedLinesBefore.concat(changedLinesAfter);
fs.writeFileSync(errorInfo.fileName, changedLines.join("\r\n"));
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment