Skip to content

Commit

Permalink
Merge pull request #204 from ext/feature/jest-dependencies
Browse files Browse the repository at this point in the history
feat: allow jest dependencies when using jest keyword
  • Loading branch information
ext committed Feb 28, 2024
2 parents e594bbe + f9b7c9d commit 749a46c
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 1 deletion.
29 changes: 29 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,35 @@ If your `package.json` contains the `"eslint"` keyword the ESLint packages can b
}
```

### Jest

If your `package.json` contains the `"jest"` keyword the Jest packages can be included as dependencies, e.g. if you publish a sharable config including a plugin you must include `"jest"` as a keyword.

**OK**:

```json
{
"name": "awesome-jest-config",
"version": "1.0.0",
"keywords": ["jest"],
"dependencies": {
"babel-jest": "^29.0.0"
}
}
```

**Fail**:

```json
{
"name": "eslint-config-myfancyconfig",
"version": "1.0.0",
"dependencies": {
"babel-jest": "^29.0.0"
}
}
```

### Prettier

If your `package.json` contains the `"prettier"` keyword the Prettier packages can be included as dependencies, e.g. if you publish a sharable config including a plugin you must include `"prettier"` as a keyword.
Expand Down
19 changes: 19 additions & 0 deletions src/rules/disallowed-dependency.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,11 @@ describe("package list", () => {
"html-validate",
"html-validate-foobar",
"jest",
"jest-cli",
"jest-transform-stub",
"babel-jest",
"ts-jest",
"@jest/core",
"mocha",
"prettier",
"prettier-config-foobar",
Expand Down Expand Up @@ -76,6 +81,20 @@ it("should allow eslint-* if package keywords includes eslint", () => {
expect(isDisallowedDependency(eslintPkg, "@scope/eslint-plugin-foobar")).toBeFalsy();
});

it("should allow jest-* if package keywords includes jest", () => {
expect.assertions(6);
const eslintPkg: PackageJson = {
...pkg,
keywords: ["jest"],
};
expect(isDisallowedDependency(eslintPkg, "jest")).toBeFalsy();
expect(isDisallowedDependency(eslintPkg, "jest-cli")).toBeFalsy();
expect(isDisallowedDependency(eslintPkg, "jest-transform-stub")).toBeFalsy();
expect(isDisallowedDependency(eslintPkg, "babel-jest")).toBeFalsy();
expect(isDisallowedDependency(eslintPkg, "ts-jest")).toBeFalsy();
expect(isDisallowedDependency(eslintPkg, "@jest/core")).toBeFalsy();
});

it("should allow prettier-* if package keywords includes prettier", () => {
expect.assertions(5);
const eslintPkg: PackageJson = {
Expand Down
14 changes: 13 additions & 1 deletion src/rules/disallowed-dependency.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ const disallowedDependencies: RegExp[] = [
prefix("gulp"),
prefix("html-validate"),
prefix("jasmine"),
prefix("jest"),
prefix("mocha"),
prefix("nyc"),
prefix("protractor"),
Expand All @@ -49,6 +48,14 @@ const disallowedEslint: RegExp[] = [
scopedPrefix("eslint-plugin"),
];

const disallowedJest: RegExp[] = [
exact("jest"),
exact("babel-jest"),
exact("ts-jest"),
prefix("jest-"),
scope("@jest"),
];

const disallowedPrettier: RegExp[] = [
exact("prettier"),
prefix("prettier-"),
Expand Down Expand Up @@ -81,6 +88,11 @@ export function isDisallowedDependency(pkg: PackageJson, dependency: string): bo
return true;
}

/* jest-* is allowed only if keywords includes "jest" */
if (!keywords.includes("jest") && match(disallowedJest, dependency)) {
return true;
}

/* prettier-* is allowed only if keywords includes "prettier" */
if (!keywords.includes("prettier") && match(disallowedPrettier, dependency)) {
return true;
Expand Down

0 comments on commit 749a46c

Please sign in to comment.