Skip to content

Commit

Permalink
chore: Require eslint 7+ (#124)
Browse files Browse the repository at this point in the history
BREAKING CHANGE: Requires eslint@7
  • Loading branch information
hjylewis authored Mar 22, 2022
1 parent 0c2bb75 commit 158b0ee
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 44 deletions.
52 changes: 9 additions & 43 deletions lib/eslint.js
Original file line number Diff line number Diff line change
@@ -1,40 +1,25 @@
const CLIEngine = require("eslint").CLIEngine;
const ESLint = require("eslint").ESLint;
const chalk = require("chalk");
const EsplintError = require("./EsplintError");
const { merge } = require("lodash");

function creatEslintInstance(eslintConfig) {
if (ESLint) {
const eslint = new ESLint(eslintConfig);
return {
runLint: (files) => newRunLint(eslint, files),
getConfigForFile: (file) => newGetConfigForFile(eslint, file),
};
}

const cli = new CLIEngine(eslintConfig);
const eslint = new ESLint(eslintConfig);
return {
runLint: (files) => runLint(cli, files),
getConfigForFile: (file) => getConfigForFile(cli, file),
runLint: (files) => runLint(eslint, files),
getConfigForFile: (file) => getConfigForFile(eslint, file),
};
}

function overrideRulesConfig(config, rules) {
if (ESLint) {
return merge({}, config.eslint, {
overrideConfig: {
rules,
},
});
} else {
return merge({}, config.eslint, {
return merge({}, config.eslint, {
overrideConfig: {
rules,
});
}
},
});
}

async function newRunLint(eslint, files) {
async function runLint(eslint, files) {
const results = await eslint.lintFiles(files);
const errorResults = ESLint.getErrorResults(results);
const hasErrors = Boolean(errorResults.length);
Expand All @@ -49,29 +34,10 @@ async function newRunLint(eslint, files) {
return results;
}

async function runLint(eslintCli, files) {
const report = eslintCli.executeOnFiles(files);
const errorReport = CLIEngine.getErrorResults(report.results);
const hasErrors = Boolean(errorReport.length);
if (hasErrors) {
const formatter = eslintCli.getFormatter();
throw new EsplintError(
`There were some ESLint errors. Fix them and try again.\n${chalk.reset(
formatter(errorReport)
)}`
);
}
return report.results;
}

function newGetConfigForFile(eslint, file) {
function getConfigForFile(eslint, file) {
return eslint.calculateConfigForFile(file);
}

async function getConfigForFile(cli, file) {
return cli.getConfigForFile(file);
}

function getWarningRules({ rules }) {
return Object.keys(rules).filter((name) => {
const ruleConfig = rules[name];
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
"strip-ansi": "^6.0.0"
},
"peerDependencies": {
"eslint": ">=4.19.1"
"eslint": ">=7.0.0"
},
"dependencies": {
"chalk": "^4.0.0",
Expand Down

0 comments on commit 158b0ee

Please sign in to comment.