Skip to content

Commit

Permalink
refactor: use newer ESLint syntax (#87)
Browse files Browse the repository at this point in the history
  • Loading branch information
MichaelDeBoey authored Jul 31, 2023
1 parent 9a6a975 commit e006c94
Show file tree
Hide file tree
Showing 11 changed files with 26 additions and 32 deletions.
11 changes: 5 additions & 6 deletions scripts/lib/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,23 +6,22 @@

const fs = require("fs")
const path = require("path")
const { CLIEngine } = require("eslint")
const linter = new CLIEngine({ fix: true })
const { ESLint } = require("eslint")
const linter = new ESLint({ fix: true })

/**
* Format a given text.
* @param {string} text The text to format.
* @returns {string} The formatted text.
* @returns {Promise<string>} The formatted text.
*/
function format(text) {
const lintResult = linter.executeOnText(text)
return lintResult.results[0].output || text
return linter.lintText(text).then(([{ output }]) => output || text)
}

/**
* Create the index file content of a given directory.
* @param {string} dirPath The path to the directory to create index.
* @returns {string} The index file content.
* @returns {Promise<string>} The index file content.
*/
function createIndex(dirPath) {
const dirName = path.basename(dirPath)
Expand Down
4 changes: 3 additions & 1 deletion scripts/update.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,7 @@ for (const dirPath of [
path.resolve(__dirname, "../lib/rules"),
path.resolve(__dirname, "../lib/utils"),
]) {
fs.writeFileSync(`${dirPath}.js`, createIndex(dirPath))
createIndex(dirPath).then(content =>
fs.writeFileSync(`${dirPath}.js`, content)
)
}
7 changes: 3 additions & 4 deletions tests/lib/rules/disable-enable-pair.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@
"use strict"

const semver = require("semver")
const eslintVersion = require("eslint/package").version
const RuleTester = require("eslint").RuleTester
const { Linter, RuleTester } = require("eslint")
const rule = require("../../../lib/rules/disable-enable-pair")
const tester = new RuleTester()

Expand Down Expand Up @@ -81,7 +80,7 @@ var foo = 1
options: [{ allowWholeFile: true }],
},
// -- description
...(semver.satisfies(eslintVersion, ">=7.0.0")
...(semver.satisfies(Linter.version, ">=7.0.0")
? [
`
/*eslint-disable no-undef -- description*/
Expand Down Expand Up @@ -211,7 +210,7 @@ console.log();
],
},
// -- description
...(semver.satisfies(eslintVersion, ">=7.0.0")
...(semver.satisfies(Linter.version, ">=7.0.0")
? [
{
code: `
Expand Down
5 changes: 2 additions & 3 deletions tests/lib/rules/no-aggregating-enable.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@
"use strict"

const semver = require("semver")
const eslintVersion = require("eslint/package").version
const RuleTester = require("eslint").RuleTester
const { Linter, RuleTester } = require("eslint")
const rule = require("../../../lib/rules/no-aggregating-enable")
const tester = new RuleTester()

Expand Down Expand Up @@ -67,7 +66,7 @@ tester.run("no-aggregating-enable", rule, {
],
},
// -- description
...(semver.satisfies(eslintVersion, ">=7.0.0")
...(semver.satisfies(Linter.version, ">=7.0.0")
? [
{
code: `
Expand Down
5 changes: 2 additions & 3 deletions tests/lib/rules/no-duplicate-disable.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@
"use strict"

const semver = require("semver")
const eslintVersion = require("eslint/package").version
const RuleTester = require("eslint").RuleTester
const { Linter, RuleTester } = require("eslint")
const rule = require("../../../lib/rules/no-duplicate-disable")
const tester = new RuleTester()

Expand Down Expand Up @@ -123,7 +122,7 @@ tester.run("no-duplicate-disable", rule, {
],
},
// -- description
...(semver.satisfies(eslintVersion, ">=7.0.0")
...(semver.satisfies(Linter.version, ">=7.0.0")
? [
{
code: `
Expand Down
3 changes: 1 addition & 2 deletions tests/lib/rules/no-restricted-disable.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
"use strict"

const semver = require("semver")
const eslintVersion = require("eslint/package").version
const { Linter, RuleTester } = require("eslint")
const rule = require("../../../lib/rules/no-restricted-disable")
const coreRules = new Linter().getRules()
Expand Down Expand Up @@ -157,7 +156,7 @@ tester.run("no-restricted-disable", rule, {
],
},
// -- description
...(semver.satisfies(eslintVersion, ">=7.0.0")
...(semver.satisfies(Linter.version, ">=7.0.0")
? [
{
code: "/*eslint-disable -- description*/",
Expand Down
5 changes: 2 additions & 3 deletions tests/lib/rules/no-unlimited-disable.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@
"use strict"

const semver = require("semver")
const eslintVersion = require("eslint/package").version
const RuleTester = require("eslint").RuleTester
const { Linter, RuleTester } = require("eslint")
const rule = require("../../../lib/rules/no-unlimited-disable")
const tester = new RuleTester()

Expand Down Expand Up @@ -95,7 +94,7 @@ tester.run("no-unlimited-disable", rule, {
],
},
// -- description
...(semver.satisfies(eslintVersion, ">=7.0.0")
...(semver.satisfies(Linter.version, ">=7.0.0")
? [
{
code: "/*eslint-disable -- description */",
Expand Down
6 changes: 3 additions & 3 deletions tests/lib/rules/no-unused-disable.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ const path = require("path")
const spawn = require("cross-spawn")
const rimraf = require("rimraf")
const semver = require("semver")
const eslintVersion = require("eslint/package").version
const { Linter } = require("eslint")

/**
* Run eslint CLI command with a given source code.
Expand Down Expand Up @@ -169,7 +169,7 @@ var foo = 3 /*eslint-disable-line no-shadow*/
}
`,
// -- description
...(semver.satisfies(eslintVersion, ">=7.0.0")
...(semver.satisfies(Linter.version, ">=7.0.0")
? [
`/*eslint no-undef:error*/
var a = b //eslint-disable-line -- description`,
Expand Down Expand Up @@ -835,7 +835,7 @@ var a = b /*eslint-disable-line no-undef*/`,
reportUnusedDisableDirectives: true,
},
// -- description
...(semver.satisfies(eslintVersion, ">=7.0.0")
...(semver.satisfies(Linter.version, ">=7.0.0")
? [
{
code: `/*eslint no-undef:off*/
Expand Down
5 changes: 2 additions & 3 deletions tests/lib/rules/no-unused-enable.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@
"use strict"

const semver = require("semver")
const eslintVersion = require("eslint/package").version
const RuleTester = require("eslint").RuleTester
const { Linter, RuleTester } = require("eslint")
const rule = require("../../../lib/rules/no-unused-enable")
const tester = new RuleTester()

Expand Down Expand Up @@ -87,7 +86,7 @@ var a = b
],
},
// -- description
...(semver.satisfies(eslintVersion, ">=7.0.0")
...(semver.satisfies(Linter.version, ">=7.0.0")
? [
{
code: "/*eslint-enable -- description*/",
Expand Down
2 changes: 1 addition & 1 deletion tests/lib/rules/no-use.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
*/
"use strict"

const RuleTester = require("eslint").RuleTester
const { RuleTester } = require("eslint")
const rule = require("../../../lib/rules/no-use")
const tester = new RuleTester()

Expand Down
5 changes: 2 additions & 3 deletions tests/lib/rules/require-description.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,11 @@
"use strict"

const semver = require("semver")
const eslintVersion = require("eslint/package").version
const RuleTester = require("eslint").RuleTester
const { Linter, RuleTester } = require("eslint")
const rule = require("../../../lib/rules/require-description")
const tester = new RuleTester()

if (!semver.satisfies(eslintVersion, ">=7.0.0")) {
if (!semver.satisfies(Linter.version, ">=7.0.0")) {
// This rule can only be used with ESLint v7.x or later.
return
}
Expand Down

0 comments on commit e006c94

Please sign in to comment.