Skip to content

Commit

Permalink
feat: Resolve getAncestors and getScope calls in eslint v9 (#466)
Browse files Browse the repository at this point in the history
Co-authored-by: 唯然 <[email protected]>
  • Loading branch information
scagood and aladdin-add authored May 27, 2024
1 parent 7d33c30 commit c0c716b
Show file tree
Hide file tree
Showing 27 changed files with 220 additions and 55 deletions.
2 changes: 1 addition & 1 deletion __tests__/always-return.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
'use strict'

const rule = require('../rules/always-return')
const RuleTester = require('eslint').RuleTester
const { RuleTester } = require('./rule-tester')
const ruleTester = new RuleTester({
parserOptions: {
ecmaVersion: 11,
Expand Down
2 changes: 1 addition & 1 deletion __tests__/avoid-new.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
'use strict'

const rule = require('../rules/avoid-new')
const RuleTester = require('eslint').RuleTester
const { RuleTester } = require('./rule-tester')
const ruleTester = new RuleTester({
parserOptions: {
ecmaVersion: 6,
Expand Down
13 changes: 7 additions & 6 deletions __tests__/catch-or-return.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
'use strict'

const rule = require('../rules/catch-or-return')
const RuleTester = require('eslint').RuleTester
const ruleTester = new RuleTester()
const { RuleTester } = require('./rule-tester')
const ruleTester = new RuleTester({
parserOptions: {
ecmaVersion: 6,
},
})

const catchMessage = 'Expected catch() or return'
const doneMessage = 'Expected done() or return'
Expand All @@ -19,10 +23,7 @@ ruleTester.run('catch-or-return', rule, {
'frank.then(to).finally(fn).catch(jail)',

// arrow function use case
{
code: 'postJSON("/smajobber/api/reportJob.json")\n\t.then(()=>this.setState())\n\t.catch(()=>this.setState())',
parserOptions: { ecmaVersion: 6 },
},
'postJSON("/smajobber/api/reportJob.json")\n\t.then(()=>this.setState())\n\t.catch(()=>this.setState())',

// return
'function a() { return frank().then(go) }',
Expand Down
2 changes: 1 addition & 1 deletion __tests__/no-callback-in-promise.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
'use strict'

const rule = require('../rules/no-callback-in-promise')
const RuleTester = require('eslint').RuleTester
const { RuleTester } = require('./rule-tester')
const ruleTester = new RuleTester({
parserOptions: {
ecmaVersion: 6,
Expand Down
2 changes: 1 addition & 1 deletion __tests__/no-multiple-resolved.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
'use strict'

const rule = require('../rules/no-multiple-resolved')
const RuleTester = require('eslint').RuleTester
const { RuleTester } = require('./rule-tester')
const ruleTester = new RuleTester({
parserOptions: {
ecmaVersion: 2020,
Expand Down
2 changes: 1 addition & 1 deletion __tests__/no-native.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
'use strict'

const rule = require('../rules/no-native')
const RuleTester = require('eslint').RuleTester
const { RuleTester } = require('./rule-tester')
const parserOptions = {
ecmaVersion: 6,
sourceType: 'module',
Expand Down
2 changes: 1 addition & 1 deletion __tests__/no-nesting.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
'use strict'

const rule = require('../rules/no-nesting')
const RuleTester = require('eslint').RuleTester
const { RuleTester } = require('./rule-tester')
const ruleTester = new RuleTester({
parserOptions: {
ecmaVersion: 6,
Expand Down
2 changes: 1 addition & 1 deletion __tests__/no-new-statics.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
'use strict'

const rule = require('../rules/no-new-statics')
const RuleTester = require('eslint').RuleTester
const { RuleTester } = require('./rule-tester')
const ruleTester = new RuleTester()

ruleTester.run('no-new-statics', rule, {
Expand Down
2 changes: 1 addition & 1 deletion __tests__/no-promise-in-callback.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
'use strict'

const rule = require('../rules/no-promise-in-callback')
const RuleTester = require('eslint').RuleTester
const { RuleTester } = require('./rule-tester')
const ruleTester = new RuleTester({
parserOptions: {
ecmaVersion: 6,
Expand Down
2 changes: 1 addition & 1 deletion __tests__/no-return-in-finally.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict'

const RuleTester = require('eslint').RuleTester
const { RuleTester } = require('./rule-tester')
const rule = require('../rules/no-return-in-finally')
const ruleTester = new RuleTester({
parserOptions: {
Expand Down
2 changes: 1 addition & 1 deletion __tests__/no-return-wrap.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
'use strict'

const rule = require('../rules/no-return-wrap')
const RuleTester = require('eslint').RuleTester
const { RuleTester } = require('./rule-tester')
const ruleTester = new RuleTester({
parserOptions: {
ecmaVersion: 6,
Expand Down
2 changes: 1 addition & 1 deletion __tests__/param-names.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
'use strict'

const rule = require('../rules/param-names')
const RuleTester = require('eslint').RuleTester
const { RuleTester } = require('./rule-tester')
const ruleTester = new RuleTester({
parserOptions: {
ecmaVersion: 6,
Expand Down
2 changes: 1 addition & 1 deletion __tests__/prefer-await-to-callbacks.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
'use strict'

const rule = require('../rules/prefer-await-to-callbacks')
const RuleTester = require('eslint').RuleTester
const { RuleTester } = require('./rule-tester')
const ruleTester = new RuleTester({
parserOptions: {
ecmaVersion: 8,
Expand Down
2 changes: 1 addition & 1 deletion __tests__/prefer-await-to-then.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
'use strict'

const rule = require('../rules/prefer-await-to-then')
const RuleTester = require('eslint').RuleTester
const { RuleTester } = require('./rule-tester')
const ruleTester = new RuleTester({
parserOptions: {
ecmaVersion: 8,
Expand Down
68 changes: 68 additions & 0 deletions __tests__/rule-tester.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
/**
* @fileoverview Helpers for tests.
* @author 唯然<[email protected]>
*/
'use strict'
const { version } = require('eslint/package.json')
const { RuleTester } = require('eslint')
const globals = require('globals')

const majorVersion = Number.parseInt(version.split('.')[0], 10)

function convertConfig(config) {
if (config instanceof Object === false) {
return config
}

if (config.languageOptions == null) {
config.languageOptions = {}
}

if (config.parserOptions) {
Object.assign(config.languageOptions, config.parserOptions)
delete config.parserOptions
}

if (typeof config.parser === 'string') {
config.languageOptions.parser = require(config.parser)
delete config.parser
}

if (config.globals instanceof Object) {
config.languageOptions.globals = config.globals
delete config.globals
}

if (config.env instanceof Object) {
if (config.languageOptions.globals == null) {
config.languageOptions.globals = {}
}

for (const key in config.env) {
Object.assign(config.languageOptions.globals, globals[key])
}

delete config.env
}

delete config.parserOptions
delete config.parser

return config
}

exports.RuleTester = function (config = {}) {
if (majorVersion <= 8) {
return new RuleTester(config)
}

const ruleTester = new RuleTester(convertConfig(config))
const $run = ruleTester.run.bind(ruleTester)
ruleTester.run = function (name, rule, tests) {
tests.valid = tests.valid.map(convertConfig)
tests.invalid = tests.invalid.map(convertConfig)

$run(name, rule, tests)
}
return ruleTester
}
2 changes: 1 addition & 1 deletion __tests__/valid-params.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
'use strict'

const rule = require('../rules/valid-params')
const RuleTester = require('eslint').RuleTester
const { RuleTester } = require('./rule-tester')
const ruleTester = new RuleTester({
parserOptions: {
ecmaVersion: 6,
Expand Down
77 changes: 62 additions & 15 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 11 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,15 @@
"lines": 100,
"statements": 100
}
}
},
"collectCoverageFrom": [
"rules/*.js",
"rules/*/*.js",
"!rules/lib/eslint-compat.js"
],
"testPathIgnorePatterns": [
"__tests__/rule-tester.js"
]
},
"devDependencies": {
"@typescript-eslint/parser": "^5.45.0",
Expand All @@ -67,6 +75,7 @@
"eslint-plugin-jest": "^26.9.0",
"eslint-plugin-node": "^11.1.0",
"eslint-plugin-prettier": "^4.2.1",
"globals": "^14.0.0",
"husky": "^7.0.4",
"jest": "^28.1.3",
"lint-staged": "^12.5.0",
Expand All @@ -75,7 +84,7 @@
"typescript": "^4.9.3"
},
"peerDependencies": {
"eslint": "^7.0.0 || ^8.0.0"
"eslint": "^7.0.0 || ^8.0.0 || ^9.0.0"
},
"engines": {
"node": "^12.22.0 || ^14.17.0 || >=16.0.0"
Expand Down
Loading

0 comments on commit c0c716b

Please sign in to comment.