Skip to content

Commit a428f82

Browse files
authored
fix(#185): merge parserOptions from langaugeOptions (#187)
1 parent 14fc608 commit a428f82

12 files changed

+303
-237
lines changed

.changeset/thirty-houses-rest.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"eslint-plugin-import-x": patch
3+
---
4+
5+
Attach `ecmaVersion` and `sourceType` to `parserOptions` during parse

package.json

+10-10
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
"repository": "git+https://github.com/un-ts/eslint-plugin-import-x",
66
"author": "JounQin <[email protected]> (https://www.1stG.me)",
77
"license": "MIT",
8-
"packageManager": "[email protected].19",
8+
"packageManager": "[email protected].22",
99
"engines": {
1010
"node": "^18.18.0 || ^20.9.0 || >=21.1.0"
1111
},
@@ -82,22 +82,24 @@
8282
"@total-typescript/ts-reset": "^0.5.1",
8383
"@types/debug": "^4.1.12",
8484
"@types/doctrine": "^0.0.9",
85-
"@types/eslint": "^9.6.0",
8685
"@types/eslint8.56": "npm:@types/eslint@^8.56.11",
87-
"@types/eslint9": "npm:@types/eslint@^9.6.0",
86+
"@types/eslint": "^9.6.1",
87+
"@types/eslint9": "npm:@types/eslint@^9.6.1",
8888
"@types/is-glob": "^4.0.4",
8989
"@types/jest": "^29.5.12",
9090
"@types/json-schema": "^7.0.15",
9191
"@types/klaw-sync": "^6.0.5",
9292
"@types/node": "^20.11.30",
93-
"@typescript-eslint/eslint-plugin": "^8.1.0",
94-
"@typescript-eslint/parser": "^8.1.0",
95-
"@typescript-eslint/rule-tester": "^8.1.0",
93+
"@typescript-eslint/eslint-plugin": "^8.15.0",
94+
"@typescript-eslint/parser": "^8.15.0",
95+
"@typescript-eslint/rule-tester": "^8.15.0",
9696
"@unts/patch-package": "^8.0.0",
9797
"cross-env": "^7.0.3",
9898
"enhanced-resolve": "^5.16.0",
9999
"escope": "^4.0.0",
100-
"eslint": "^9.9.0",
100+
"eslint8.56": "npm:eslint@^8.56.0",
101+
"eslint": "^9.15.0",
102+
"eslint9": "npm:eslint@^9.15.0",
101103
"eslint-config-prettier": "^9.1.0",
102104
"eslint-doc-generator": "^1.7.1",
103105
"eslint-import-resolver-typescript": "^3.6.1",
@@ -109,9 +111,7 @@
109111
"eslint-plugin-json": "^3.1.0",
110112
"eslint-plugin-n": "^16.6.2",
111113
"eslint-plugin-prettier": "^5.1.3",
112-
"eslint-plugin-unicorn": "^51.0.1",
113-
"eslint8.56": "npm:eslint@^8.56.0",
114-
"eslint9": "npm:eslint@^9.8.0",
114+
"eslint-plugin-unicorn": "^56.0.1",
115115
"hermes-eslint": "^0.23.1",
116116
"jest": "^29.7.0",
117117
"klaw-sync": "^6.0.0",

src/rules/consistent-type-specifier-style.ts

+5-4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import type { TSESLint, TSESTree } from '@typescript-eslint/utils'
22

3-
import { createRule } from '../utils'
3+
import { createRule, getValue } from '../utils'
44

55
function isComma(token: TSESTree.Token): token is TSESTree.PunctuatorToken {
66
return token.type === 'Punctuator' && token.value === ','
@@ -34,10 +34,11 @@ function getImportText(
3434
}
3535

3636
const names = specifiers.map(s => {
37-
if (s.imported.name === s.local.name) {
38-
return s.imported.name
37+
const importedName = getValue(s.imported)
38+
if (importedName === s.local.name) {
39+
return importedName
3940
}
40-
return `${s.imported.name} as ${s.local.name}`
41+
return `${importedName} as ${s.local.name}`
4142
})
4243
// insert a fresh top-level import
4344
return `import ${kind} {${names.join(', ')}} from ${sourceString};`

src/rules/no-default-export.ts

+3-7
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { createRule } from '../utils'
1+
import { createRule, getValue } from '../utils'
22
import sourceType from '../utils/source-type'
33

44
export = createRule({
@@ -37,11 +37,7 @@ export = createRule({
3737

3838
ExportNamedDeclaration(node) {
3939
for (const specifier of node.specifiers.filter(
40-
specifier =>
41-
(specifier.exported.name ||
42-
('value' in specifier.exported && specifier.exported.value)) ===
43-
'default',
44-
)) {
40+
specifier => getValue(specifier.exported) === 'default')) {
4541
const { loc } = sourceCode.getFirstTokens(node)[1] || {}
4642
// @ts-expect-error - experimental parser type
4743
if (specifier.type === 'ExportDefaultSpecifier') {
@@ -55,7 +51,7 @@ export = createRule({
5551
node,
5652
messageId: 'noAliasDefault',
5753
data: {
58-
local: specifier.local.name,
54+
local: getValue(specifier.local),
5955
},
6056
loc,
6157
})

src/rules/no-deprecated.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import type { TSESTree } from '@typescript-eslint/utils'
22
import type { Tag } from 'doctrine'
33

44
import type { ModuleNamespace } from '../utils'
5-
import { ExportMap, createRule, declaredScope } from '../utils'
5+
import { ExportMap, createRule, declaredScope, getValue } from '../utils'
66

77
function message(deprecation: Tag) {
88
return {
@@ -93,7 +93,7 @@ export = createRule({
9393
}
9494

9595
case 'ImportSpecifier': {
96-
imported = im.imported.name
96+
imported = getValue(im.imported)
9797
local = im.local.name
9898
break
9999
}

src/rules/no-named-export.ts

+2-4
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { createRule } from '../utils'
1+
import { createRule, getValue } from '../utils'
22
import sourceType from '../utils/source-type'
33

44
export = createRule({
@@ -32,9 +32,7 @@ export = createRule({
3232
}
3333

3434
const someNamed = node.specifiers.some(
35-
specifier =>
36-
(specifier.exported.name ||
37-
('value' in specifier.exported && specifier.exported.value)) !==
35+
specifier => getValue(specifier.exported) !==
3836
'default',
3937
)
4038
if (someNamed) {

src/rules/no-rename-default.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import path from 'node:path'
88

99
import type { TSESTree } from '@typescript-eslint/utils'
1010

11-
import { createRule, ExportMap } from '../utils'
11+
import { createRule, ExportMap, getValue } from '../utils'
1212
import type { ModuleOptions } from '../utils'
1313

1414
type Options = ModuleOptions & {
@@ -86,7 +86,7 @@ export = createRule<[Options?], MessageId>({
8686
return
8787
}
8888
case 'ExportSpecifier': {
89-
return targetNode.local.name
89+
return getValue(targetNode.local)
9090
}
9191
case 'FunctionDeclaration': {
9292
return targetNode.id?.name
@@ -171,7 +171,7 @@ export = createRule<[Options?], MessageId>({
171171
return
172172
}
173173

174-
if (node.imported.name !== 'default') {
174+
if (getValue(node.imported) !== 'default') {
175175
return
176176
}
177177

@@ -322,7 +322,7 @@ function getDefaultExportNode(
322322
}
323323
case 'ExportNamedDeclaration': {
324324
return defaultExportNode.specifiers.find(
325-
specifier => specifier.exported.name === 'default',
325+
specifier => getValue(specifier.exported) === 'default',
326326
)
327327
}
328328
default: {

src/rules/prefer-default-export.ts

+2-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import type { TSESTree } from '@typescript-eslint/utils'
22

3-
import { createRule } from '../utils'
3+
import { createRule, getValue } from '../utils'
44

55
type Options = {
66
target?: 'single' | 'any'
@@ -70,8 +70,7 @@ export = createRule<[Options?], MessageId>({
7070

7171
ExportSpecifier(node) {
7272
if (
73-
(node.exported.name ||
74-
('value' in node.exported && node.exported.value)) === 'default'
73+
getValue(node.exported) === 'default'
7574
) {
7675
hasDefaultExport = true
7776
} else {

src/utils/declared-scope.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
import type { TSESTree } from '@typescript-eslint/utils'
2+
import type { ScopeType } from '@typescript-eslint/scope-manager';
23

34
import type { RuleContext } from '../types'
45

56
export function declaredScope(
67
context: RuleContext,
78
node: TSESTree.Node,
89
name: string,
9-
) {
10+
): ScopeType | undefined {
1011
const references = context.sourceCode.getScope(node).references
1112
const reference = references.find(x => x.identifier.name === name)
1213
if (!reference || !reference.resolved) {

src/utils/export-map.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -241,7 +241,7 @@ export class ExportMap {
241241
}
242242
}
243243

244-
function addNamespace(object: object, identifier: TSESTree.Identifier) {
244+
function addNamespace(object: object, identifier: TSESTree.Identifier | TSESTree.StringLiteral) {
245245
const nsfn = getNamespace(getValue(identifier))
246246
if (nsfn) {
247247
Object.defineProperty(object, 'namespace', { get: nsfn })
@@ -308,7 +308,7 @@ export class ExportMap {
308308
// else falls through
309309
default: {
310310
if ('local' in s) {
311-
local = s.local.name
311+
local = getValue(s.local)
312312
} else {
313313
throw new Error('Unknown export specifier type')
314314
}
@@ -318,7 +318,7 @@ export class ExportMap {
318318

319319
if ('exported' in s) {
320320
// todo: JSDoc
321-
m.reexports.set(s.exported.name, {
321+
m.reexports.set(getValue(s.exported), {
322322
local,
323323
getImport: () => resolveImport(nsource),
324324
})

src/utils/parse.ts

+5-2
Original file line numberDiff line numberDiff line change
@@ -79,8 +79,7 @@ export function parse(
7979

8080
// ESLint in "flat" mode only sets context.languageOptions.parserOptions
8181
let parserOptions =
82-
('languageOptions' in context && context.languageOptions?.parserOptions) ||
83-
context.parserOptions
82+
context.languageOptions?.parserOptions || context.parserOptions
8483

8584
const parserOrPath = getParser(path, context)
8685

@@ -111,6 +110,10 @@ export function parse(
111110
// https://github.com/import-js/eslint-plugin-import/issues/1408#issuecomment-509298962
112111
parserOptions = withoutProjectParserOptions(parserOptions)
113112

113+
// If this is a flat config, we need to add ecmaVersion and sourceType (if present) from languageOptions
114+
parserOptions.ecmaVersion ??= context.languageOptions?.ecmaVersion
115+
parserOptions.sourceType ??= context.languageOptions?.sourceType
116+
114117
// require the parser relative to the main module (i.e., ESLint)
115118
const parser =
116119
typeof parserOrPath === 'string'

0 commit comments

Comments
 (0)