eslint-plugin-diff
Advanced tools
Comparing version 1.0.3 to 1.0.4
@@ -5,2 +5,5 @@ export declare const diff = "diff --git a/fixme.js b/fixme.js\nindex 4886604..83c3014 100644\n--- a/fixme.js\n+++ b/fixme.js\n@@ -1,0 +2,2 @@ if (new Date().getTime()) console.log(\"curly\");\n+if (new Date().getTime()) console.log(\"curly\");\n+if (new Date().getTime()) console.log(\"curly\");"; | ||
export declare const includingOnlyRemovals = "diff --git a/dirty.js b/dirty.js\nindex cb3c131..874b8f9 100644\n--- a/dirty.js\n+++ b/dirty.js\n@@ -1,0 +2,2 @@ if (new Date().getTime()) console.log(\"curly\");\n+if (new Date().getTime()) console.log(\"curly\");\n+if (new Date().getTime()) console.log(\"curly\");\n@@ -17,2 +16,0 @@ import { a } from \"../components/a\";\n-import { b } from \"../context/b\";\n-import { c } from \"../context/c\";"; | ||
export declare const filenamesAB = "a/dirty.js\nb/dirty.js\n"; | ||
export declare const filenamesA = "a/dirty.js\n"; | ||
export declare const diffFileList = "file1\nfile2\nfile3\n"; | ||
//# sourceMappingURL=diff.d.ts.map |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.includingOnlyRemovals = exports.hunks = exports.staged = exports.diff = void 0; | ||
exports.diffFileList = exports.filenamesA = exports.filenamesAB = exports.includingOnlyRemovals = exports.hunks = exports.staged = exports.diff = void 0; | ||
exports.diff = `diff --git a/fixme.js b/fixme.js | ||
@@ -48,2 +48,8 @@ index 4886604..83c3014 100644 | ||
-import { c } from "../context/c";`; | ||
exports.filenamesAB = `a/dirty.js | ||
b/dirty.js | ||
`; | ||
exports.filenamesA = `a/dirty.js | ||
`; | ||
exports.diffFileList = "file1\nfile2\nfile3\n"; | ||
//# sourceMappingURL=diff.js.map |
@@ -1,4 +0,4 @@ | ||
import { Linter } from "eslint"; | ||
import type { Linter } from "eslint"; | ||
declare const postprocessArguments: [Linter.LintMessage[][], string]; | ||
export { postprocessArguments }; | ||
//# sourceMappingURL=postprocessArguments.d.ts.map |
import { Range } from "./Range"; | ||
declare const getDiffForFile: (filePath: string, staged?: boolean) => string; | ||
declare const getDiffFileList: (staged?: boolean) => string[]; | ||
declare const getRangesForDiff: (diff: string) => Range[]; | ||
export { getDiffForFile, getRangesForDiff }; | ||
export { getDiffForFile, getRangesForDiff, getDiffFileList }; | ||
export type { Range }; | ||
//# sourceMappingURL=git.d.ts.map |
@@ -22,3 +22,3 @@ "use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.getRangesForDiff = exports.getDiffForFile = void 0; | ||
exports.getDiffFileList = exports.getRangesForDiff = exports.getDiffForFile = void 0; | ||
const child_process = __importStar(require("child_process")); | ||
@@ -28,6 +28,31 @@ const path = __importStar(require("path")); | ||
const sanitizeFilePath = (filePath) => JSON.stringify(path.resolve(filePath)); | ||
const getDiffForFile = (filePath, staged = false) => child_process | ||
.execSync(`git diff --diff-filter=ACM --unified=0 HEAD ${staged ? " --staged" : ""} -- ${sanitizeFilePath(filePath)}`) | ||
.toString(); | ||
const diffCacheKey = (filePath, staged) => JSON.stringify([path.resolve(filePath), staged]); | ||
const setCachedDiff = (filePath, staged, diff) => void diffCache.set(diffCacheKey(filePath, staged), diff); | ||
const getCachedDiff = (filePath, staged) => diffCache.get(diffCacheKey(filePath, staged)); | ||
const diffCache = new Map(); | ||
const getDiffForFile = (filePath, staged = false) => { | ||
let diff = getCachedDiff(filePath, staged); | ||
if (diff === undefined) { | ||
const result = child_process | ||
.execSync(`git diff --diff-filter=ACM --unified=0 HEAD ${staged ? " --staged" : ""} -- ${sanitizeFilePath(filePath)}`) | ||
.toString(); | ||
setCachedDiff(filePath, staged, result); | ||
diff = result; | ||
} | ||
return diff; | ||
}; | ||
exports.getDiffForFile = getDiffForFile; | ||
let diffFileListCache; | ||
const getDiffFileList = (staged = false) => { | ||
if (diffFileListCache === undefined) { | ||
diffFileListCache = child_process | ||
.execSync(`git diff --diff-filter=ACM HEAD --name-only ${staged ? "--staged" : ""}`) | ||
.toString() | ||
.trim() | ||
.split("\n") | ||
.map((filePath) => path.resolve(filePath)); | ||
} | ||
return diffFileListCache; | ||
}; | ||
exports.getDiffFileList = getDiffFileList; | ||
const isHunkHeader = (input) => { | ||
@@ -38,3 +63,3 @@ const hunkHeaderRE = new RegExp(/^@@ .* @@/g); | ||
const getRangeForChangedLines = (line) => { | ||
var _a, _b, _c, _d; | ||
var _a; | ||
const rangeRE = new RegExp(/^@@ .* \+(?<start>\d+)(?<linesCountDelimiter>,(?<linesCount>\d+))? @@/); | ||
@@ -52,7 +77,7 @@ const range = rangeRE.exec(line); | ||
} | ||
const linesCount = ((_b = range.groups) === null || _b === void 0 ? void 0 : _b.linesCountDelimiter) && ((_c = range.groups) === null || _c === void 0 ? void 0 : _c.linesCount) | ||
const linesCount = range.groups.linesCountDelimiter && range.groups.linesCount | ||
? parseInt(range.groups.linesCount) | ||
: 1; | ||
const hasAddedLines = linesCount !== 0; | ||
const start = parseInt((_d = range.groups) === null || _d === void 0 ? void 0 : _d.start); | ||
const start = parseInt(range.groups.start); | ||
const end = start + linesCount; | ||
@@ -59,0 +84,0 @@ return hasAddedLines ? new Range_1.Range(start, end) : null; |
@@ -21,2 +21,5 @@ "use strict"; | ||
}; | ||
var __importDefault = (this && this.__importDefault) || function (mod) { | ||
return (mod && mod.__esModule) ? mod : { "default": mod }; | ||
}; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
@@ -27,2 +30,3 @@ const child_process = __importStar(require("child_process")); | ||
const diff_1 = require("./__fixtures__/diff"); | ||
const path_1 = __importDefault(require("path")); | ||
jest.mock("child_process"); | ||
@@ -44,3 +48,21 @@ const mockedChildProcess = utils_1.mocked(child_process, true); | ||
}); | ||
it("should hit the cached diff of a file", () => { | ||
jest.mock("child_process").resetAllMocks(); | ||
mockedChildProcess.execSync.mockReturnValueOnce(Buffer.from(diff_1.hunks)); | ||
const diffFromFileA = git_1.getDiffForFile("./mockfileCache.js"); | ||
const diffFromFileB = git_1.getDiffForFile("./mockfileCache.js"); | ||
expect(mockedChildProcess.execSync).toHaveBeenCalledTimes(1); | ||
expect(diffFromFileA).toEqual(diffFromFileB); | ||
mockedChildProcess.execSync.mockReturnValueOnce(Buffer.from(diff_1.hunks)); | ||
git_1.getDiffForFile("./mockfileMiss.js"); | ||
expect(mockedChildProcess.execSync).toHaveBeenCalledTimes(2); | ||
}); | ||
it("should get the list of staged files", () => { | ||
jest.mock("child_process").resetAllMocks(); | ||
mockedChildProcess.execSync.mockReturnValue(Buffer.from(diff_1.diffFileList)); | ||
const fileList = git_1.getDiffFileList(); | ||
expect(mockedChildProcess.execSync).toHaveBeenCalled(); | ||
expect(fileList).toEqual(["file1", "file2", "file3"].map((p) => path_1.default.resolve(p))); | ||
}); | ||
}); | ||
//# sourceMappingURL=git.test.js.map |
@@ -1,3 +0,7 @@ | ||
import { Linter } from "eslint"; | ||
import type { Linter } from "eslint"; | ||
declare const diff: { | ||
preprocess: (text: string, filename: string) => { | ||
text: string; | ||
filename: string; | ||
}[]; | ||
postprocess: (messages: Linter.LintMessage[][], filename: string) => Linter.LintMessage[]; | ||
@@ -14,2 +18,6 @@ supportsAutofix: boolean; | ||
declare const staged: { | ||
preprocess: (text: string, filename: string) => { | ||
text: string; | ||
filename: string; | ||
}[]; | ||
postprocess: (messages: Linter.LintMessage[][], filename: string) => Linter.LintMessage[]; | ||
@@ -16,0 +24,0 @@ supportsAutofix: boolean; |
@@ -8,4 +8,5 @@ "use strict"; | ||
const diff = { | ||
preprocess: (text, filename) => git_1.getDiffFileList().includes(filename) ? [{ text, filename }] : [], | ||
postprocess: (messages, filename) => messages | ||
.map((message) => message.filter((message) => git_1.getRangesForDiff(git_1.getDiffForFile(filename)).some(isLineWithinRange(message.line)))) | ||
.map((message) => message.filter(({ line }) => git_1.getRangesForDiff(git_1.getDiffForFile(filename)).some(isLineWithinRange(line)))) | ||
.flat(), | ||
@@ -26,4 +27,5 @@ supportsAutofix: true, | ||
const staged = { | ||
preprocess: (text, filename) => git_1.getDiffFileList(STAGED).includes(filename) ? [{ text, filename }] : [], | ||
postprocess: (messages, filename) => messages | ||
.map((message) => message.filter((message) => git_1.getRangesForDiff(git_1.getDiffForFile(filename, STAGED)).some(isLineWithinRange(message.line)))) | ||
.map((message) => message.filter(({ line }) => git_1.getRangesForDiff(git_1.getDiffForFile(filename, STAGED)).some(isLineWithinRange(line)))) | ||
.flat(), | ||
@@ -30,0 +32,0 @@ supportsAutofix: true, |
{ | ||
"name": "eslint-plugin-diff", | ||
"version": "1.0.3", | ||
"version": "1.0.4", | ||
"description": "Run ESLint on your changes only", | ||
@@ -5,0 +5,0 @@ "keywords": [ |
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
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
36644
461
43