Skip to content

Commit

Permalink
Merge pull request #4 from doximity/upgrade-eslint
Browse files Browse the repository at this point in the history
Add support for eslint 8, make eslint a peer dependency, and upgrade deps
  • Loading branch information
davidmyersdev authored Aug 2, 2022
2 parents 6c84cf1 + 67bf58d commit d4579e1
Show file tree
Hide file tree
Showing 14 changed files with 3,014 additions and 3,900 deletions.
14 changes: 14 additions & 0 deletions .eslintrc.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
module.exports = {
env: {
node: true,
},
parser: "@typescript-eslint/parser",
plugins: ["@typescript-eslint"],
extends: [
"eslint:recommended",
"plugin:prettier/recommended",
"plugin:@typescript-eslint/eslint-recommended",
"plugin:@typescript-eslint/recommended",
],
root: true,
};
8 changes: 0 additions & 8 deletions .eslintrc.js

This file was deleted.

7 changes: 4 additions & 3 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,12 @@ Thank you for wanting to contribute back to the eslint-generate-todo repository.
Before contributing code to `eslint-generate-todo`, please read and sign the [Contributor License Agreement](CONTRIBUTOR_LICENSE_AGREEMENT.md).

## Guidelines

We ask that you keep the following guidelines in mind when planning your contribution:

* Whether your contribution is for a bug fix or a feature request, **create an [Issue](https://github.com/doximity/eslint-generate-todo/issues)** to let us know what you are thinking before you fix it. It helps us give a LGTM much faster (with fewer cases of saying no to a PR)
* **For bugs**, if you have already found a fix, feel free to submit a Pull Request referencing the Issue you created. Include the `Fixes #` syntax to link it to the issue you're addressing.
* **For feature requests**, we want to improve upon the library incrementally which means small changes at a time. In order to ensure your PR can be reviewed in a timely manner, please keep PRs small. If you think this is unrealistic, then mention that within the issue and we can discuss it.
- Whether your contribution is for a bug fix or a feature request, **create an [Issue](https://github.com/doximity/eslint-generate-todo/issues)** to let us know what you are thinking before you fix it. It helps us give a LGTM much faster (with fewer cases of saying no to a PR)
- **For bugs**, if you have already found a fix, feel free to submit a Pull Request referencing the Issue you created. Include the `Fixes #` syntax to link it to the issue you're addressing.
- **For feature requests**, we want to improve upon the library incrementally which means small changes at a time. In order to ensure your PR can be reviewed in a timely manner, please keep PRs small. If you think this is unrealistic, then mention that within the issue and we can discuss it.

## Workflow

Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# eslint-generate-todo

Tool for generating eslint todo files

## Installation
Expand Down
5 changes: 5 additions & 0 deletions bin/eslint-generate-todo.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#!/usr/bin/env ts-node

const { parse } = require("../dist/src/bin.js");

parse(process.argv);
6 changes: 3 additions & 3 deletions jest.config.js → jest.config.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
module.exports = {
export default {
roots: ["<rootDir>/src"],
testMatch: ["**/__tests__/**/*.+(ts|js)", "**/?(*.)+(spec|test).+(ts|js)"],
transform: {
"^.+\\.ts$": "ts-jest"
}
"^.+\\.ts$": "ts-jest",
},
};
55 changes: 31 additions & 24 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,23 @@
"version": "0.1.1",
"description": "Tool for generating eslint todo files",
"main": "dist/index.js",
"engines": {
"node": ">=8.0.0"
"exports": {
".": {
"require": "./*.js"
}
},
"scripts": {
"build": "yarn prettier --list-different & yarn lint && tsc",
"build": "yarn prettier --list-different && yarn lint && tsc",
"dev": "nodemon --watch . --ignore dist --ext js,ts,json --exec \"yarn build\"",
"format": "yarn prettier --write",
"lint": "eslint . --ext .js,.ts",
"prettier": "prettier \"{src,bin}/**\"",
"prepack": "tsc",
"prettier": "prettier .",
"release": "np --any-branch",
"test": "jest"
},
"bin": {
"eslint-generate-todo": "./bin/eslint-generate-todo.js"
"eslint-generate-todo": "./bin/eslint-generate-todo.cjs"
},
"files": [
"dist",
Expand All @@ -35,32 +38,36 @@
"eslint-todo"
],
"dependencies": {
"caporal": "^1.3.0",
"eslint": "^6.7.2",
"js-yaml": "^3.13.1"
"caporal": "^1.4.0",
"js-yaml": "^4.1.0"
},
"devDependencies": {
"@types/eslint": "^6.1.3",
"@types/jest": "^24.0.24",
"@types/js-yaml": "^3.12.1",
"@types/node": "^12.12.21",
"@typescript-eslint/eslint-plugin": "^2.12.0",
"@typescript-eslint/parser": "^2.12.0",
"eslint-config-prettier": "^6.7.0",
"eslint-plugin-jest": "^23.1.1",
"eslint-plugin-prettier": "^3.1.2",
"husky": "^3.1.0",
"jest": "^24.9.0",
"nodemon": "^2.0.2",
"np": "^5.2.1",
"@types/eslint": "^8.4.2",
"@types/jest": "^27.5.1",
"@types/js-yaml": "^4.0.5",
"@types/node": "^17.0.35",
"@typescript-eslint/eslint-plugin": "^5.26.0",
"@typescript-eslint/parser": "^5.26.0",
"eslint": "^8.16.0",
"eslint-config-prettier": "^8.5.0",
"eslint-plugin-jest": "^26.2.2",
"eslint-plugin-prettier": "^4.0.0",
"husky": "^8.0.1",
"jest": "^28.1.0",
"nodemon": "^2.0.16",
"np": "^7.6.1",
"precise-commits": "^1.0.2",
"prettier": "^1.19.1",
"ts-jest": "^24.2.0",
"typescript": "^3.7.3"
"prettier": "^2.6.2",
"ts-jest": "^28.0.3",
"ts-node": "^10.8.0",
"typescript": "^4.7.2"
},
"husky": {
"hooks": {
"pre-commit": "precise-commits"
}
},
"peerDependencies": {
"eslint": ">=7.0.0 <9.0.0"
}
}
8 changes: 4 additions & 4 deletions src/.eslintrc.js → src/.eslintrc.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@ module.exports = {
plugins: ["@typescript-eslint"],
extends: [
"plugin:@typescript-eslint/eslint-recommended",
"plugin:@typescript-eslint/recommended"
"plugin:@typescript-eslint/recommended",
],
overrides: [
{
files: ["__tests__", "**/*.spec.{js,ts}"],
plugins: ["jest"],
extends: ["plugin:jest/recommended", "plugin:jest/style"]
}
]
extends: ["plugin:jest/recommended", "plugin:jest/style"],
},
],
};
65 changes: 28 additions & 37 deletions src/__tests__/overrides.spec.ts
Original file line number Diff line number Diff line change
@@ -1,48 +1,39 @@
import { CLIEngine } from "eslint";
import { ESLint } from "eslint";

import {
stripEmptyFiles,
stripNullRuleIds,
mapReportToOverrides
mapReportToOverrides,
} from "../overrides";

function generateLintResult(ruleIds: string[] = []): CLIEngine.LintResult {
const generateLintResult = (ruleIds: string[] = []): ESLint.LintResult => {
return {
errorCount: 0,
fatalErrorCount: 0,
filePath: "foo",
messages: ruleIds.map(ruleId => ({
fixableErrorCount: 0,
fixableWarningCount: 0,
messages: ruleIds.map((ruleId) => ({
column: 0,
line: 0,
ruleId,
message: "",
nodeType: "",
severity: 0,
source: ""
source: "",
})),
errorCount: 0,
suppressedMessages: [],
usedDeprecatedRules: [],
warningCount: 0,
fixableErrorCount: 0,
fixableWarningCount: 0
};
}

function generateReport(
results: CLIEngine.LintResult[] = []
): CLIEngine.LintReport {
return {
results,
errorCount: 0,
warningCount: 0,
fixableErrorCount: 0,
fixableWarningCount: 0
};
}
};

describe("stripEmptyFiles", () => {
test("removes results with no messages", () => {
const results = [
generateLintResult(),
generateLintResult(["foo", "bar"]),
generateLintResult()
generateLintResult(),
];

expect(stripEmptyFiles(results)).toEqual([results[1]]);
Expand All @@ -59,54 +50,54 @@ describe("stripNullRuleIds", () => {

describe("mapReportToOverrides", () => {
test("creates a list of overrides from a report", () => {
const report = generateReport([generateLintResult(["foo", "bar"])]);
const report = [generateLintResult(["foo", "bar"])];

expect(mapReportToOverrides(report, "warn")).toEqual([
{
files: ["foo"],
rules: {
foo: "warn",
bar: "warn"
}
}
bar: "warn",
},
},
]);
});

test("sets the level of the rules", () => {
const report = generateReport([generateLintResult(["foo", "bar"])]);
const report = [generateLintResult(["foo", "bar"])];

expect(mapReportToOverrides(report, "warn")).toEqual([
{
files: ["foo"],
rules: {
foo: "warn",
bar: "warn"
}
}
bar: "warn",
},
},
]);

expect(mapReportToOverrides(report, "off")).toEqual([
{
files: ["foo"],
rules: {
foo: "off",
bar: "off"
}
}
bar: "off",
},
},
]);
});

test("combines duplicate rule violations", () => {
const report = generateReport([generateLintResult(["foo", "foo", "bar"])]);
const report = [generateLintResult(["foo", "foo", "bar"])];

expect(mapReportToOverrides(report, "warn")).toEqual([
{
files: ["foo"],
rules: {
foo: "warn",
bar: "warn"
}
}
bar: "warn",
},
},
]);
});
});
16 changes: 8 additions & 8 deletions bin/eslint-generate-todo.js → src/bin.ts
100755 → 100644
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
#!/usr/bin/env node

const program = require("caporal");
const { execute } = require("../dist/index");
const package = require("../package.json");
import program from "caporal";
import { execute } from "./index";
import { version } from "../package.json";

program
.version(package.version)
.version(version)
.description("Generates a list of disabled eslint rules.")
.argument("[files...]", "Files to validate", program.LIST, ".")
.option(
Expand All @@ -30,11 +28,13 @@ program
const configFile = await execute(args.files, {
level,
format: options.format,
path: options.path
path: options.path,
});

logger.info(`ESLint todo config written to ${configFile}`);
logger.info("You may now extend this config in your ESLint config file.");
});

program.parse(process.argv);
export const parse = (args: string[]) => {
program.parse(args);
};
27 changes: 14 additions & 13 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,25 @@
import fs from "fs";
import util from "util";
import { CLIEngine, Linter } from "eslint";
import { ESLint, Linter } from "eslint";

import { renderAsYAML, renderAsJSON } from "./render";
import { mapReportToOverrides } from "./overrides";

const writeFile = util.promisify(fs.writeFile);

export async function execute(
files: Array<string>,
{
level,
format = "yaml",
path
}: { level: Linter.RuleLevel; format: "yaml" | "json"; path?: string }
): Promise<string> {
const cli = new CLIEngine({});
interface Options {
format: "yaml" | "json";
level: Linter.RuleLevel;
path?: string;
}

const report = cli.executeOnFiles(files) as CLIEngine.LintReport;
const config = { overrides: mapReportToOverrides(report, level) };
export const execute = async (
files: string[],
{ level, format = "yaml", path }: Options
): Promise<string> => {
const cli = new ESLint({});
const results = await cli.lintFiles(files);
const config = { overrides: mapReportToOverrides(results, level) };

const outputPath =
path || `.eslintrc-todo.${format === "yaml" ? "yml" : "json"}`;
Expand All @@ -30,4 +31,4 @@ export async function execute(
);

return outputPath;
}
};
Loading

0 comments on commit d4579e1

Please sign in to comment.