Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions .changeset/some-moments-trade.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
---
"eslint-plugin-import-x": patch
---

First step toward ESLint 10 support:

- `sourceType` determination now prefers `context.languageOptions` when possible
- Ensure `context.parserOptions` no longer results in crashes with ESLint 10
- Merge `getParser` and `getParserPath` implementations into one `getParserOrPath`
43 changes: 19 additions & 24 deletions src/utils/parse.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import path from 'node:path'
import nodePath from 'node:path'

// import { withoutProjectParserOptions } from '@typescript-eslint/typescript-estree'
import type { TSESLint, TSESTree } from '@typescript-eslint/utils'
Expand Down Expand Up @@ -81,7 +81,7 @@ export function parse(
let parserOptions =
context.languageOptions?.parserOptions || context.parserOptions

const parserOrPath = getParser(path, context)
const parserOrPath = getParserOrPath(path, context)

if (!parserOrPath) {
throw new Error('parserPath or languageOptions.parser is required!')
Expand Down Expand Up @@ -165,19 +165,30 @@ export function parse(
throw new Error('Parser must expose a `parse` or `parseForESLint` method')
}

function getParser(path: string, context: ChildContext | RuleContext) {
const parserPath = getParserPath(path, context)

if (parserPath) {
return parserPath
function getParserOrPath(path: string, context: ChildContext | RuleContext) {
const parsers = context.settings['import-x/parsers']
if (parsers != null) {
const extension = nodePath.extname(path) as FileExtension
for (const parserPath in parsers) {
if (parsers[parserPath].includes(extension)) {
// use this alternate parser
log('using alt parser:', parserPath)
return parserPath
}
}
}
// default to use ESLint parser, only exists in eslintrc
if ('parserPath' in context && context.parserPath) {
log('using context.parserPath:', context.parserPath)
return context.parserPath
}

const parser = 'languageOptions' in context && context.languageOptions?.parser

if (
parser &&
typeof parser !== 'string' &&
(('parse' in parser && typeof parse === 'function') ||
(('parse' in parser && typeof parser.parse === 'function') ||
('parseForESLint' in parser &&
typeof parser.parseForESLint === 'function'))
) {
Expand All @@ -186,19 +197,3 @@ function getParser(path: string, context: ChildContext | RuleContext) {

return null
}

function getParserPath(filepath: string, context: ChildContext | RuleContext) {
const parsers = context.settings['import-x/parsers']
if (parsers != null) {
const extension = path.extname(filepath) as FileExtension
for (const parserPath in parsers) {
if (parsers[parserPath].includes(extension)) {
// use this alternate parser
log('using alt parser:', parserPath)
return parserPath
}
}
}
// default to use ESLint parser
return context.parserPath
}
20 changes: 16 additions & 4 deletions src/utils/source-type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,22 @@ export function sourceType<
MessageIds extends string,
Options extends readonly unknown[],
>(context: TSESLint.RuleContext<MessageIds, Options>) {
if ('sourceType' in context.parserOptions) {
return context.parserOptions.sourceType
}
if ('languageOptions' in context && context.languageOptions) {
return context.languageOptions.sourceType
if (
'parserOptions' in context.languageOptions &&
context.languageOptions.parserOptions?.sourceType
) {
return context.languageOptions.parserOptions.sourceType
}
if (
'sourceType' in context.languageOptions &&
context.languageOptions.sourceType
) {
return context.languageOptions.sourceType
}
}
// For backwards compatibility with earlier ESLint versions without `languageOptions`.
if ('parserOptions' in context && context.parserOptions) {
return context.parserOptions.sourceType
}
}
Loading