Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Chore: add test for eslint v9 #2368

Merged
merged 14 commits into from
Jan 16, 2024
Merged
Show file tree
Hide file tree
Changes from 10 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
14 changes: 13 additions & 1 deletion .github/workflows/CI.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,18 @@ jobs:
name: Test
strategy:
matrix:
node: [17, 18, 20]
node: [18, 20]
os: [ubuntu-latest]
eslint: [8]
include:
# On next ESLint version
- eslint: ^9.0.0-0
node: 20
os: ubuntu-latest
# On old Node version
- eslint: 8
node: 17
os: ubuntu-latest

runs-on: ${{ matrix.os }}
steps:
Expand All @@ -39,6 +49,8 @@ jobs:
node-version: ${{ matrix.node }}
- name: Install Packages
run: npm install --legacy-peer-deps
- name: Install ESLint v${{ matrix.eslint }}
run: npm install --save-dev eslint@${{ matrix.eslint }} --legacy-peer-deps
- name: Test
run: npm test

Expand Down
1 change: 1 addition & 0 deletions lib/utils/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -673,6 +673,7 @@ module.exports = {
*/
wrapCoreRule,
wrapStylisticOrCoreRule,
getCoreRule,
/**
* Checks whether the given value is defined.
* @template T
Expand Down
156 changes: 151 additions & 5 deletions tests/eslint-compat.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,66 @@
// @ts-check
const eslint = require('eslint')
const semver = require('semver')

let ESLint = eslint.ESLint
let Linter = eslint.Linter
let RuleTester = eslint.RuleTester
if (semver.lt(eslint.Linter.version, '9.0.0-0')) {
ESLint = eslint.ESLint ? getESLintClassForV8() : getESLintClassForV6()
Linter = getLinterClassForV8()
RuleTester = getRuleTesterClassForV8()
}

module.exports = {
ESLint: eslint.ESLint || getESLintClassForV6(),
RuleTester: eslint.RuleTester
ESLint,
RuleTester,
Linter
}

/** @returns {typeof eslint.ESLint} */
function getESLintClassForV8(BaseESLintClass = eslint.ESLint) {
return class ESLintForV8 extends BaseESLintClass {
static get version() {
return BaseESLintClass.version
}
constructor(options) {
super(adjustOptions(options))
}
}

// eslint-disable-next-line unicorn/consistent-function-scoping
function adjustOptions(options) {
const newOptions = {
...options,
useEslintrc: false
}
if (newOptions.overrideConfig) {
newOptions.overrideConfig = { ...newOptions.overrideConfig }
let plugins
if (newOptions.overrideConfig.plugins) {
plugins = newOptions.overrideConfig.plugins
delete newOptions.overrideConfig.plugins
}
newOptions.overrideConfig = processCompatibleConfig(
newOptions.overrideConfig
)
if (plugins) {
newOptions.overrideConfig.plugins = Object.keys(plugins)
newOptions.plugins = plugins
}

// adjust
delete newOptions.overrideConfig.files
delete newOptions.overrideConfig.processor
}
return newOptions
}
}
/** @returns {typeof eslint.ESLint} */
function getESLintClassForV6() {
class ESLintForV6 {
static get version() {
// @ts-expect-error
return eslint.CLIEngine.version
}

Expand All @@ -26,7 +77,7 @@ function getESLintClassForV6() {
plugins: pluginsMap,
...otherOptions
} = options || {}
/** @type {eslint.CLIEngine.Options} */

const newOptions = {
fix: Boolean(fix),
reportUnusedDisableDirectives: reportUnusedDisableDirectives
Expand All @@ -47,6 +98,8 @@ function getESLintClassForV6() {
: undefined,
...overrideConfig
}

// @ts-expect-error
this.engine = new eslint.CLIEngine(newOptions)

for (const [name, plugin] of Object.entries(pluginsMap || {})) {
Expand All @@ -59,12 +112,105 @@ function getESLintClassForV6() {
* @returns {ReturnType<eslint.ESLint['lintText']>}
*/
async lintText(...params) {
const result = this.engine.executeOnText(params[0], params[1].filePath)
const result = this.engine.executeOnText(params[0], params[1]?.filePath)
return result.results
}
}

/** @type {typeof eslint.ESLint} */
const eslintClass = /** @type {any} */ (ESLintForV6)
return eslintClass
return getESLintClassForV8(eslintClass)
}

/** @returns {typeof eslint.Linter} */
function getLinterClassForV8() {
return class LinterForV8 extends eslint.Linter {
static get version() {
return eslint.Linter.version
}
verify(code, config, option) {
return super.verify(code, processCompatibleConfig(config, this), option)
}
}
}

function getRuleTesterClassForV8() {
return class RuleTesterForV8 extends eslint.RuleTester {
constructor(options) {
const defineRules = []
super(
processCompatibleConfig(options, {
defineRule(...args) {
defineRules.push(args)
}
})
)
for (const args of defineRules) {
// @ts-expect-error
this.linter.defineRule(...args)
}
}
run(name, rule, tests) {
super.run(name, rule, {
valid: (tests.valid || []).map((test) =>
typeof test === 'string' ? test : adjustOptions(test)
),
invalid: (tests.invalid || []).map((test) => adjustOptions(test))
})
}
}
// eslint-disable-next-line unicorn/consistent-function-scoping
function adjustOptions(test) {
return processCompatibleConfig(test)
}
}

function processCompatibleConfig(config, linter) {
const newConfig = { ...config }
if (newConfig.languageOptions) {
const languageOptions = newConfig.languageOptions
delete newConfig.languageOptions
newConfig.parserOptions = {
...newConfig.parserOptions,
...languageOptions,
...languageOptions.parserOptions
}
if (languageOptions.globals) {
newConfig.globals = {
...newConfig.globals,
...languageOptions.globals
}
}
if (languageOptions.parser) {
newConfig.parser = getParserName(languageOptions.parser)
if (!languageOptions.parserOptions?.parser) {
delete newConfig.parserOptions.parser
}
linter?.defineParser?.(newConfig.parser, require(newConfig.parser))
}
}
if (newConfig.plugins) {
const plugins = newConfig.plugins
delete newConfig.plugins
for (const [pluginName, plugin] of Object.entries(plugins)) {
for (const [ruleName, rule] of Object.entries(plugin.rules || {})) {
linter.defineRule(`${pluginName}/${ruleName}`, rule)
}
}
}
newConfig.env = { ...newConfig.env, es6: true }
return newConfig
}

function getParserName(parser) {
const name = parser.meta?.name || parser.name
if (name === 'typescript-eslint/parser') {
return require.resolve('@typescript-eslint/parser')
} else if (
name == null &&
// @ts-expect-error
parser === require('@typescript-eslint/parser')
)
return require.resolve('@typescript-eslint/parser')
return require.resolve(name)
}
2 changes: 1 addition & 1 deletion tests/fixtures/script-indent/jsx-01.vue
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<!--{ "parserOptions": { "ecmaFeatures": { "jsx": true } } }-->
<!--{ "languageOptions": { "parserOptions": {"ecmaFeatures": { "jsx": true }} } }-->
<script>
// Not yet supported.
const jsx = <
Expand Down
2 changes: 1 addition & 1 deletion tests/fixtures/script-indent/jsx-02.vue
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<!--{ "parserOptions": { "ecmaFeatures": { "jsx": true } } }-->
<!--{ "languageOptions": { "parserOptions": {"ecmaFeatures": { "jsx": true }} } }-->
<script>
// Not yet supported.
const jsx = <
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<!--{"parserOptions": {"parser":"@typescript-eslint/parser"}, "requirements": { "@typescript-eslint/parser": ">=5.44.1-alpha.15"}}-->
<!--{"languageOptions": {"parserOptions": {"parser":"@typescript-eslint/parser"}}, "requirements": { "@typescript-eslint/parser": ">=5.44.1-alpha.15"}}-->
<script lang="ts">
abstract class Foo {
abstract accessor
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<!--{"parserOptions": {"parser":"@typescript-eslint/parser"}}-->
<!--{"languageOptions": {"parserOptions": {"parser":"@typescript-eslint/parser"}}}-->
<script lang="ts">
abstract class A {
abstract a;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<!--{"parserOptions": {"parser":"@typescript-eslint/parser"}}-->
<!--{"languageOptions": {"parserOptions": {"parser":"@typescript-eslint/parser"}}}-->
<script lang="ts">
abstract class A {
a
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<!--{"parserOptions": {"parser":"@typescript-eslint/parser"}}-->
<!--{"languageOptions": {"parserOptions": {"parser":"@typescript-eslint/parser"}}}-->
<script lang="ts">
abstract class A {
abstract a ();
Expand Down
2 changes: 1 addition & 1 deletion tests/fixtures/script-indent/ts-accessor-property-01.vue
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<!--{"parserOptions": {"parser":"@typescript-eslint/parser"}, "requirements": { "@typescript-eslint/parser": ">=5.44.1-alpha.15"}}-->
<!--{"languageOptions": {"parserOptions": {"parser":"@typescript-eslint/parser"}}, "requirements": { "@typescript-eslint/parser": ">=5.44.1-alpha.15"}}-->
<script lang="ts">
class Foo {
accessor
Expand Down
2 changes: 1 addition & 1 deletion tests/fixtures/script-indent/ts-accessor-property-02.vue
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<!--{"parserOptions": {"parser":"@typescript-eslint/parser"}, "requirements": { "@typescript-eslint/parser": ">=5.44.1-alpha.15"}}-->
<!--{"languageOptions": {"parserOptions": {"parser":"@typescript-eslint/parser"}}, "requirements": { "@typescript-eslint/parser": ">=5.44.1-alpha.15"}}-->
<script lang="ts">
class Foo {
accessor
Expand Down
2 changes: 1 addition & 1 deletion tests/fixtures/script-indent/ts-accessor-property-03.vue
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<!--{"parserOptions": {"parser":"@typescript-eslint/parser"}, "requirements": { "@typescript-eslint/parser": ">=5.44.1-alpha.15"}}-->
<!--{"languageOptions": {"parserOptions": {"parser":"@typescript-eslint/parser"}}, "requirements": { "@typescript-eslint/parser": ">=5.44.1-alpha.15"}}-->
<script lang="ts">
class Foo {
declare accessor
Expand Down
2 changes: 1 addition & 1 deletion tests/fixtures/script-indent/ts-accessor-property-04.vue
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<!--{"parserOptions": {"parser":"@typescript-eslint/parser"}, "requirements": { "@typescript-eslint/parser": ">=5.44.1-alpha.15"}}-->
<!--{"languageOptions": {"parserOptions": {"parser":"@typescript-eslint/parser"}}, "requirements": { "@typescript-eslint/parser": ">=5.44.1-alpha.15"}}-->
<script lang="ts">
class Foo {
override accessor
Expand Down
2 changes: 1 addition & 1 deletion tests/fixtures/script-indent/ts-accessor-property-05.vue
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<!--{"parserOptions": {"parser":"@typescript-eslint/parser"}, "requirements": { "@typescript-eslint/parser": ">=5.44.1-alpha.15"}}-->
<!--{"languageOptions": {"parserOptions": {"parser":"@typescript-eslint/parser"}}, "requirements": { "@typescript-eslint/parser": ">=5.44.1-alpha.15"}}-->
<script lang="ts">
class Foo {
accessor
Expand Down
2 changes: 1 addition & 1 deletion tests/fixtures/script-indent/ts-as-expression-01.vue
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<!--{"parserOptions": {"parser":"@typescript-eslint/parser"}}-->
<!--{"languageOptions": {"parserOptions": {"parser":"@typescript-eslint/parser"}}}-->
<script lang="ts">
var foo =
bar as
Expand Down
2 changes: 1 addition & 1 deletion tests/fixtures/script-indent/ts-call-expression-01.vue
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<!--{"parserOptions": {"parser":"@typescript-eslint/parser"}}-->
<!--{"languageOptions": {"parserOptions": {"parser":"@typescript-eslint/parser"}}}-->
<script lang="ts">
foo
<
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<!--{"parserOptions": {"parser":"@typescript-eslint/parser"}}-->
<!--{"languageOptions": {"parserOptions": {"parser":"@typescript-eslint/parser"}}}-->
<script lang="ts">
interface Foo {
(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<!--{"parserOptions": {"parser":"@typescript-eslint/parser"}}-->
<!--{"languageOptions": {"parserOptions": {"parser":"@typescript-eslint/parser"}}}-->
<script lang="ts">
type Foo = {
(
Expand Down
2 changes: 1 addition & 1 deletion tests/fixtures/script-indent/ts-class-declaration-01.vue
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<!--{"parserOptions": {"parser":"@typescript-eslint/parser"}}-->
<!--{"languageOptions": {"parserOptions": {"parser":"@typescript-eslint/parser"}}}-->
<script lang="ts">
class
Foo
Expand Down
2 changes: 1 addition & 1 deletion tests/fixtures/script-indent/ts-class-declaration-02.vue
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<!--{"parserOptions": {"parser":"@typescript-eslint/parser"}}-->
<!--{"languageOptions": {"parserOptions": {"parser":"@typescript-eslint/parser"}}}-->
<script lang="ts">
class
Foo
Expand Down
2 changes: 1 addition & 1 deletion tests/fixtures/script-indent/ts-class-declaration-03.vue
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<!--{"parserOptions": {"parser":"@typescript-eslint/parser"}}-->
<!--{"languageOptions": {"parserOptions": {"parser":"@typescript-eslint/parser"}}}-->
<script lang="ts">
class
Foo
Expand Down
2 changes: 1 addition & 1 deletion tests/fixtures/script-indent/ts-class-declaration-04.vue
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<!--{"parserOptions": {"parser":"@typescript-eslint/parser"}}-->
<!--{"languageOptions": {"parserOptions": {"parser":"@typescript-eslint/parser"}}}-->
<script lang="ts">
class
Foo
Expand Down
2 changes: 1 addition & 1 deletion tests/fixtures/script-indent/ts-class-declaration-05.vue
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<!--{"parserOptions": {"parser":"@typescript-eslint/parser"}}-->
<!--{"languageOptions": {"parserOptions": {"parser":"@typescript-eslint/parser"}}}-->
<script lang="ts">
class
Foo
Expand Down
2 changes: 1 addition & 1 deletion tests/fixtures/script-indent/ts-class-declaration-06.vue
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<!--{"parserOptions": {"parser":"@typescript-eslint/parser"}}-->
<!--{"languageOptions": {"parserOptions": {"parser":"@typescript-eslint/parser"}}}-->
<script lang="ts">
class
Foo
Expand Down
2 changes: 1 addition & 1 deletion tests/fixtures/script-indent/ts-class-declaration-07.vue
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<!--{"parserOptions": {"parser":"@typescript-eslint/parser"}}-->
<!--{"languageOptions": {"parserOptions": {"parser":"@typescript-eslint/parser"}}}-->
<script lang="ts">
import {Component, Prop, Vue} from "vue-property-decorator";

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<!--{"parserOptions": {"parser":"@typescript-eslint/parser"}}-->
<!--{"languageOptions": {"parserOptions": {"parser":"@typescript-eslint/parser"}}}-->
<script>
class Counter {
get
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<!--{"parserOptions": {"parser":"@typescript-eslint/parser"}}-->
<!--{"languageOptions": {"parserOptions": {"parser":"@typescript-eslint/parser"}}}-->
<script>
class Counter {
#x
Expand Down
2 changes: 1 addition & 1 deletion tests/fixtures/script-indent/ts-class-property-01.vue
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<!--{"parserOptions": {"parser":"@typescript-eslint/parser"}}-->
<!--{"languageOptions": {"parserOptions": {"parser":"@typescript-eslint/parser"}}}-->
<script lang="ts">
class A {
a;
Expand Down
2 changes: 1 addition & 1 deletion tests/fixtures/script-indent/ts-class-property-02.vue
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<!--{"parserOptions": {"parser":"@typescript-eslint/parser"}}-->
<!--{"languageOptions": {"parserOptions": {"parser":"@typescript-eslint/parser"}}}-->
<script lang="ts">
class A {
a
Expand Down
2 changes: 1 addition & 1 deletion tests/fixtures/script-indent/ts-class-property-03.vue
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<!--{"parserOptions": {"parser":"@typescript-eslint/parser"}}-->
<!--{"languageOptions": {"parserOptions": {"parser":"@typescript-eslint/parser"}}}-->
<script lang="ts">
class A {
a: number
Expand Down
2 changes: 1 addition & 1 deletion tests/fixtures/script-indent/ts-conditional-type-01.vue
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<!--{"parserOptions": {"parser":"@typescript-eslint/parser"}}-->
<!--{"languageOptions": {"parserOptions": {"parser":"@typescript-eslint/parser"}}}-->
<script lang="ts">
type X = A extends B
? C
Expand Down
Loading
Loading