Socket
Socket
Sign inDemoInstall

eslint-plugin-diff

Package Overview
Dependencies
Maintainers
1
Versions
46
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

eslint-plugin-diff - npm Package Compare versions

Comparing version 1.0.13 to 1.0.14

3

dist/git.d.ts
import { Range } from "./Range";
declare const getDiffForFile: (filePath: string, staged?: boolean) => string;
declare const getDiffFileList: () => string[];
declare const getGitFileList: () => string[];
declare const hasCleanIndex: (filePath: string) => boolean;
declare const getUntrackedFileList: (staged?: boolean) => string[];
declare const getRangesForDiff: (diff: string) => Range[];
export { getDiffFileList, getDiffForFile, getRangesForDiff, getGitFileList, getUntrackedFileList, hasCleanIndex, };
export { getDiffFileList, getDiffForFile, getRangesForDiff, getUntrackedFileList, hasCleanIndex, };

@@ -26,74 +26,45 @@ "use strict";

Object.defineProperty(exports, "__esModule", { value: true });
exports.hasCleanIndex = exports.getUntrackedFileList = exports.getGitFileList = exports.getRangesForDiff = exports.getDiffForFile = exports.getDiffFileList = void 0;
exports.hasCleanIndex = exports.getUntrackedFileList = exports.getRangesForDiff = exports.getDiffForFile = exports.getDiffFileList = void 0;
const child_process = __importStar(require("child_process"));
const path = __importStar(require("path"));
const Range_1 = require("./Range");
const RUNNING_INSIDE_VSCODE = process.env.VSCODE_CLI !== undefined;
const COMMAND = "git";
const sanitizeFilePath = (filePath) => JSON.stringify(path.resolve(filePath));
const diffCacheKey = (filePath, staged) => JSON.stringify([path.resolve(filePath), staged]);
const diffCache = new Map();
const setCachedDiff = (filePath, staged, diff) => void diffCache.set(diffCacheKey(filePath, staged), diff);
const getCachedDiff = (filePath, staged) => diffCache.get(diffCacheKey(filePath, staged));
const getDiffForFile = (filePath, staged = false) => {
var _a;
let diff = getCachedDiff(filePath, staged);
if (RUNNING_INSIDE_VSCODE || diff === undefined) {
const args = [
"diff",
"--diff-algorithm=histogram",
"--diff-filter=ACM",
"-M100%",
"--relative",
staged && "--staged",
"--unified=0",
JSON.stringify((_a = process.env.ESLINT_PLUGIN_DIFF_COMMIT) !== null && _a !== void 0 ? _a : "HEAD"),
"--",
sanitizeFilePath(filePath),
].reduce((acc, cur) => (typeof cur === "string" ? [...acc, cur] : acc), []);
const result = child_process.execFileSync(COMMAND, args).toString();
setCachedDiff(filePath, staged, result);
diff = result;
}
return diff;
const args = [
"diff",
"--diff-algorithm=histogram",
"--diff-filter=ACM",
"-M100%",
"--relative",
staged && "--staged",
"--unified=0",
JSON.stringify((_a = process.env.ESLINT_PLUGIN_DIFF_COMMIT) !== null && _a !== void 0 ? _a : "HEAD"),
"--",
sanitizeFilePath(filePath),
].reduce((acc, cur) => (typeof cur === "string" ? [...acc, cur] : acc), []);
return child_process.execFileSync(COMMAND, args).toString();
};
exports.getDiffForFile = getDiffForFile;
let diffFileListCache;
const getDiffFileList = () => {
var _a;
if (RUNNING_INSIDE_VSCODE || diffFileListCache === undefined) {
const args = [
"diff",
"--diff-algorithm=histogram",
"--diff-filter=ACM",
"-M100%",
"--name-only",
"--relative",
"--staged",
JSON.stringify((_a = process.env.ESLINT_PLUGIN_DIFF_COMMIT) !== null && _a !== void 0 ? _a : "HEAD"),
];
diffFileListCache = child_process
.execFileSync(COMMAND, args)
.toString()
.trim()
.split("\n")
.map((filePath) => path.resolve(filePath));
}
return diffFileListCache;
const args = [
"diff",
"--diff-algorithm=histogram",
"--diff-filter=ACM",
"-M100%",
"--name-only",
"--relative",
"--staged",
JSON.stringify((_a = process.env.ESLINT_PLUGIN_DIFF_COMMIT) !== null && _a !== void 0 ? _a : "HEAD"),
];
return child_process
.execFileSync(COMMAND, args)
.toString()
.trim()
.split("\n")
.map((filePath) => path.resolve(filePath));
};
exports.getDiffFileList = getDiffFileList;
let gitFileListCache;
const getGitFileList = () => {
if (RUNNING_INSIDE_VSCODE || gitFileListCache === undefined) {
const args = ["ls-files"];
gitFileListCache = child_process
.execFileSync(COMMAND, args)
.toString()
.trim()
.split("\n")
.map((filePath) => path.resolve(filePath));
}
return gitFileListCache;
};
exports.getGitFileList = getGitFileList;
const hasCleanIndex = (filePath) => {

@@ -118,16 +89,13 @@ const args = [

exports.hasCleanIndex = hasCleanIndex;
let untrackedFileListCache;
const getUntrackedFileList = (staged = false) => {
if (staged) {
untrackedFileListCache = [];
return [];
}
else if (RUNNING_INSIDE_VSCODE || untrackedFileListCache === undefined) {
const args = ["ls-files", "--exclude-standard", "--others"];
untrackedFileListCache = child_process
.execFileSync(COMMAND, args)
.toString()
.trim()
.split("\n")
.map((filePath) => path.resolve(filePath));
}
const args = ["ls-files", "--exclude-standard", "--others"];
const untrackedFileListCache = child_process
.execFileSync(COMMAND, args)
.toString()
.trim()
.split("\n")
.map((filePath) => path.resolve(filePath));
return untrackedFileListCache;

@@ -134,0 +102,0 @@ };

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

}
const diff = (0, git_1.getDiffForFile)(filename, staged);
return messages

@@ -47,3 +48,3 @@ .map((message) => {

}
const isLineWithinSomeRange = (0, git_1.getRangesForDiff)((0, git_1.getDiffForFile)(filename, staged)).some(isLineWithinRange(line));
const isLineWithinSomeRange = (0, git_1.getRangesForDiff)(diff).some(isLineWithinRange(line));
return isLineWithinSomeRange;

@@ -50,0 +51,0 @@ });

{
"name": "eslint-plugin-diff",
"version": "1.0.13",
"version": "1.0.14",
"description": "Run ESLint on your changes only",

@@ -5,0 +5,0 @@ "keywords": [

# eslint-plugin-diff ![](https://img.shields.io/npm/dt/eslint-plugin-diff?style=flat-square&logo=npm&logoColor=white)
Run ESLint on your changed lines only. Now with CI support!
Run ESLint on your changed lines only.
## What problem does it solve?
Introducing new ESLint rules (or updating 3rd party configs) in a large codebase can be tedious. This plugin allows your team to ease into new rules by only linting all new/modified code, eventually migrating to the new/updated rules.
The feedback your developers get in a pull-request should be focused on the changes they've made, but traditional setups don't allow for this. With this plugin you can run ESLint on your changed lines only, making all warnings and errors **relevant to you**, and at the same time avoiding becoming overwhelmed with linter errors.
It can be used in CI environments as well.
### 💰 Your company's budget is precious
When updating your linter or its dependencies, you often get new linter warnings and errors in your code, which can lead to a huge increase of the cost of your project if you try to fix all of them. This plugin allows you to run ESLint on only the changed lines of your code, so the new errors won't get triggered on the code other developers have already manually reviewed and approved.
### 🚀 Your team's velocity is important
Having a healthy and high-quality code-base is a pre-requisite for high velocity and having too many errors in your linter's output can get overwhelming, oftentimes disheartening the developers, at the cost of the quality of the code. Having a linter that runs on only the changed lines of your code will ensure your developers don't get overwhelmed, ensuring your code-base will remain healthy, and your team productive.
### 🧠 Your developers' focus is vital
Let's face it – Developers are bombarded with errors and notifications about systems being broken, code being wrong and people requiring their attention. If a linter has too much output, it becomes a chore for your developers just to assess whether or not their changes actually caused an issue, or if it's just old code they haven't even touched. With this plugin, all the linter output your developers see will be related to whatever they have personally changed, requiring much less focus on parsing the linter's output.
### How does it solve it?
When creating pull-requests, this plugin will enable you to run ESLint on only the changed lines of your pull-request, increasing the focus of your code review. This is a great way to reduce the amount of time spent on code review while still maintaining a high quality code base and increase the quality of your feedback.
As an added bonus, it also makes introducing new ESLint rules (or updating 3rd party configs) in a large codebase trivial, because you avoid becoming blocked by new ESLint issues in already-approved code.
## Installation
```sh
$ yarn add -D eslint-plugin-diff
```
Install the plugin and extend your ESLint config.
## Usage
### Install
If you want to define which commit or commit-range to diff between (useful in CI), you can set the environment variable `ESLINT_PLUGIN_DIFF_COMMIT` (otherwise the plugin will default to `HEAD`):
```sh
$ ESLINT_PLUGIN_DIFF_COMMIT="a8fdc20..5a9f19c" yarn run eslint .
# or
$ ESLINT_PLUGIN_DIFF_COMMIT="${GITHUB_SHA}.." yarn run eslint .
yarn add -D eslint eslint-plugin-diff
```
See [git's official documentation on the syntax](https://git-scm.com/docs/git-diff#Documentation/git-diff.txt-emgitdiffemltoptionsgtltcommitgt--ltpathgt82308203)
### Extend config
It's recommended to use [`"plugin:diff/diff"` (see config)](#config-diff--diff-all-changes-since-head-staged-and-unstaged).
Extend your ESLint config with `"plugin:diff/diff"`:
### Config `diff` — Diff all changes since HEAD (staged and unstaged)
Extend your config in **`.eslintrc`**:
```json

@@ -41,16 +47,29 @@ {

_Equivalent to `git diff HEAD`_
You can also choose `"plugin:diff/staged"` if you prefer to lint only staged
files.
### Config `staged` — Diff staged changes only
## CI Setup
> **Useful for pre-commit hooks, e.g. when running ESLint with lint-staged**
To lint all the changes of a PR, you only have to set
`ESLINT_PLUGIN_DIFF_COMMIT` before running ESLint.
Extend your config in **`.eslintrc`**:
### For GitHub Actions
```json
{
"extends": ["plugin:diff/staged"]
}
```sh
export ESLINT_PLUGIN_DIFF_COMMIT="origin/$GITHUB_BASE_REF";
npx --no-install eslint --ext .js,.ts,.tsx .
```
_Equivalent to `git diff HEAD --staged`_
### For BitBucket Pipelines
```sh
export ESLINT_PLUGIN_DIFF_COMMIT="origin/$BITBUCKET_PR_DESTINATION_BRANCH";
npx --no-install eslint --ext .js,.ts,.tsx .
```
## Note
- You can use any valid commit syntax for `ESLINT_PLUGIN_DIFF_COMMIT`. See [git's official documentation on the syntax](https://git-scm.com/docs/git-diff#Documentation/git-diff.txt-emgitdiffemltoptionsgtltcommitgt--ltpathgt82308203)
- You can choose to lint all changes (using `"plugin:diff/diff"`) or staged changes only (using `"plugin:diff/staged"`).
- We recommend using `"plugin:diff/diff"`, which is equivalent to running `git diff HEAD`.
- `"plugin:diff/staged"` is equivalent to running `git diff HEAD --staged`
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