Skip to content

Commit

Permalink
fix(#144): read sourceType w/ both flat/legacy config (#181)
Browse files Browse the repository at this point in the history
  • Loading branch information
SukkaW authored Nov 11, 2024
1 parent e01dce0 commit cc4ee65
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 3 deletions.
5 changes: 5 additions & 0 deletions .changeset/ten-lamps-sell.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"eslint-plugin-import-x": patch
---

Fix #144, read proper `sourceType` for both eslint flat config and legacy eslintrc config.
3 changes: 2 additions & 1 deletion src/rules/no-default-export.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { createRule } from '../utils'
import sourceType from '../utils/source-type'

export = createRule({
name: 'no-default-export',
Expand All @@ -18,7 +19,7 @@ export = createRule({
defaultOptions: [],
create(context) {
// ignore non-modules
if (context.parserOptions.sourceType !== 'module') {
if (sourceType(context) !== 'module') {
return {}
}

Expand Down
3 changes: 2 additions & 1 deletion src/rules/no-named-export.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { createRule } from '../utils'
import sourceType from '../utils/source-type'

export = createRule({
name: 'no-named-export',
Expand All @@ -16,7 +17,7 @@ export = createRule({
defaultOptions: [],
create(context) {
// ignore non-modules
if (context.parserOptions.sourceType !== 'module') {
if (sourceType(context) !== 'module') {
return {}
}

Expand Down
3 changes: 2 additions & 1 deletion src/rules/unambiguous.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
*/

import { createRule, isUnambiguousModule } from '../utils'
import sourceType from '../utils/source-type'

export = createRule({
name: 'unambiguous',
Expand All @@ -21,7 +22,7 @@ export = createRule({
defaultOptions: [],
create(context) {
// ignore non-modules
if (context.parserOptions.sourceType !== 'module') {
if (sourceType(context) !== 'module') {
return {}
}

Expand Down
10 changes: 10 additions & 0 deletions src/utils/source-type.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import type { RuleContext } from '../types'

export default function sourceType(context: RuleContext) {
if ('sourceType' in context.parserOptions) {
return context.parserOptions.sourceType
}
if ('languageOptions' in context && context.languageOptions) {
return context.languageOptions.sourceType
}
}
19 changes: 19 additions & 0 deletions test/rules/no-default-export.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,25 @@ ruleTester.run('no-default-export', rule, {
},
],
}),
test({
code: "function foo() { return 'foo'; }\nexport default foo;",
filename: 'foo.ts',
errors: [
{
type: 'ExportDefaultDeclaration',
messageId: 'preferNamed',
},
],
languageOptions: {
parserOptions: {
ecmaversion: 'latest',
sourceType: 'module',
},
},
settings: {
'import-x/resolver': { typescript: true },
},
}),
test({
code: 'export default from "foo.js"',
languageOptions: { parser: require(parsers.BABEL) },
Expand Down

0 comments on commit cc4ee65

Please sign in to comment.