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

fix(#185): merge parserOptions from langaugeOptions #187

Merged
merged 4 commits into from
Nov 20, 2024
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
5 changes: 5 additions & 0 deletions .changeset/thirty-houses-rest.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"eslint-plugin-import-x": patch
---

Attach `ecmaVersion` and `sourceType` to `parserOptions` during parse
20 changes: 10 additions & 10 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"repository": "git+https://github.com/un-ts/eslint-plugin-import-x",
"author": "JounQin <admin@1stg.me> (https://www.1stG.me)",
"license": "MIT",
"packageManager": "yarn@1.22.19",
"packageManager": "yarn@1.22.22",
"engines": {
"node": "^18.18.0 || ^20.9.0 || >=21.1.0"
},
Expand Down Expand Up @@ -82,22 +82,24 @@
"@total-typescript/ts-reset": "^0.5.1",
"@types/debug": "^4.1.12",
"@types/doctrine": "^0.0.9",
"@types/eslint": "^9.6.0",
"@types/eslint8.56": "npm:@types/eslint@^8.56.11",
"@types/eslint9": "npm:@types/eslint@^9.6.0",
"@types/eslint": "^9.6.1",
"@types/eslint9": "npm:@types/eslint@^9.6.1",
"@types/is-glob": "^4.0.4",
"@types/jest": "^29.5.12",
"@types/json-schema": "^7.0.15",
"@types/klaw-sync": "^6.0.5",
"@types/node": "^20.11.30",
"@typescript-eslint/eslint-plugin": "^8.1.0",
"@typescript-eslint/parser": "^8.1.0",
"@typescript-eslint/rule-tester": "^8.1.0",
"@typescript-eslint/eslint-plugin": "^8.15.0",
"@typescript-eslint/parser": "^8.15.0",
"@typescript-eslint/rule-tester": "^8.15.0",
"@unts/patch-package": "^8.0.0",
"cross-env": "^7.0.3",
"enhanced-resolve": "^5.16.0",
"escope": "^4.0.0",
"eslint": "^9.9.0",
"eslint8.56": "npm:eslint@^8.56.0",
"eslint": "^9.15.0",
"eslint9": "npm:eslint@^9.15.0",
"eslint-config-prettier": "^9.1.0",
"eslint-doc-generator": "^1.7.1",
"eslint-import-resolver-typescript": "^3.6.1",
Expand All @@ -109,9 +111,7 @@
"eslint-plugin-json": "^3.1.0",
"eslint-plugin-n": "^16.6.2",
"eslint-plugin-prettier": "^5.1.3",
"eslint-plugin-unicorn": "^51.0.1",
"eslint8.56": "npm:eslint@^8.56.0",
"eslint9": "npm:eslint@^9.8.0",
"eslint-plugin-unicorn": "^56.0.1",
"hermes-eslint": "^0.23.1",
"jest": "^29.7.0",
"klaw-sync": "^6.0.0",
Expand Down
9 changes: 5 additions & 4 deletions src/rules/consistent-type-specifier-style.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { TSESLint, TSESTree } from '@typescript-eslint/utils'

import { createRule } from '../utils'
import { createRule, getValue } from '../utils'

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

const names = specifiers.map(s => {
if (s.imported.name === s.local.name) {
return s.imported.name
const importedName = getValue(s.imported)
if (importedName === s.local.name) {
return importedName
}
return `${s.imported.name} as ${s.local.name}`
return `${importedName} as ${s.local.name}`
})
// insert a fresh top-level import
return `import ${kind} {${names.join(', ')}} from ${sourceString};`
Expand Down
10 changes: 3 additions & 7 deletions src/rules/no-default-export.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { createRule } from '../utils'
import { createRule, getValue } from '../utils'
import sourceType from '../utils/source-type'

export = createRule({
Expand Down Expand Up @@ -37,11 +37,7 @@ export = createRule({

ExportNamedDeclaration(node) {
for (const specifier of node.specifiers.filter(
specifier =>
(specifier.exported.name ||
('value' in specifier.exported && specifier.exported.value)) ===
'default',
)) {
specifier => getValue(specifier.exported) === 'default')) {
const { loc } = sourceCode.getFirstTokens(node)[1] || {}
// @ts-expect-error - experimental parser type
if (specifier.type === 'ExportDefaultSpecifier') {
Expand All @@ -55,7 +51,7 @@ export = createRule({
node,
messageId: 'noAliasDefault',
data: {
local: specifier.local.name,
local: getValue(specifier.local),
},
loc,
})
Expand Down
4 changes: 2 additions & 2 deletions src/rules/no-deprecated.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import type { TSESTree } from '@typescript-eslint/utils'
import type { Tag } from 'doctrine'

import type { ModuleNamespace } from '../utils'
import { ExportMap, createRule, declaredScope } from '../utils'
import { ExportMap, createRule, declaredScope, getValue } from '../utils'

function message(deprecation: Tag) {
return {
Expand Down Expand Up @@ -93,7 +93,7 @@ export = createRule({
}

case 'ImportSpecifier': {
imported = im.imported.name
imported = getValue(im.imported)
local = im.local.name
break
}
Expand Down
6 changes: 2 additions & 4 deletions src/rules/no-named-export.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { createRule } from '../utils'
import { createRule, getValue } from '../utils'
import sourceType from '../utils/source-type'

export = createRule({
Expand Down Expand Up @@ -32,9 +32,7 @@ export = createRule({
}

const someNamed = node.specifiers.some(
specifier =>
(specifier.exported.name ||
('value' in specifier.exported && specifier.exported.value)) !==
specifier => getValue(specifier.exported) !==
'default',
)
if (someNamed) {
Expand Down
8 changes: 4 additions & 4 deletions src/rules/no-rename-default.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import path from 'node:path'

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

import { createRule, ExportMap } from '../utils'
import { createRule, ExportMap, getValue } from '../utils'
import type { ModuleOptions } from '../utils'

type Options = ModuleOptions & {
Expand Down Expand Up @@ -86,7 +86,7 @@ export = createRule<[Options?], MessageId>({
return
}
case 'ExportSpecifier': {
return targetNode.local.name
return getValue(targetNode.local)
}
case 'FunctionDeclaration': {
return targetNode.id?.name
Expand Down Expand Up @@ -171,7 +171,7 @@ export = createRule<[Options?], MessageId>({
return
}

if (node.imported.name !== 'default') {
if (getValue(node.imported) !== 'default') {
return
}

Expand Down Expand Up @@ -322,7 +322,7 @@ function getDefaultExportNode(
}
case 'ExportNamedDeclaration': {
return defaultExportNode.specifiers.find(
specifier => specifier.exported.name === 'default',
specifier => getValue(specifier.exported) === 'default',
)
}
default: {
Expand Down
5 changes: 2 additions & 3 deletions src/rules/prefer-default-export.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { TSESTree } from '@typescript-eslint/utils'

import { createRule } from '../utils'
import { createRule, getValue } from '../utils'

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

ExportSpecifier(node) {
if (
(node.exported.name ||
('value' in node.exported && node.exported.value)) === 'default'
getValue(node.exported) === 'default'
) {
hasDefaultExport = true
} else {
Expand Down
3 changes: 2 additions & 1 deletion src/utils/declared-scope.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import type { TSESTree } from '@typescript-eslint/utils'
import type { ScopeType } from '@typescript-eslint/scope-manager';

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

export function declaredScope(
context: RuleContext,
node: TSESTree.Node,
name: string,
) {
): ScopeType | undefined {
const references = context.sourceCode.getScope(node).references
const reference = references.find(x => x.identifier.name === name)
if (!reference || !reference.resolved) {
Expand Down
6 changes: 3 additions & 3 deletions src/utils/export-map.ts
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,7 @@ export class ExportMap {
}
}

function addNamespace(object: object, identifier: TSESTree.Identifier) {
function addNamespace(object: object, identifier: TSESTree.Identifier | TSESTree.StringLiteral) {
const nsfn = getNamespace(getValue(identifier))
if (nsfn) {
Object.defineProperty(object, 'namespace', { get: nsfn })
Expand Down Expand Up @@ -308,7 +308,7 @@ export class ExportMap {
// else falls through
default: {
if ('local' in s) {
local = s.local.name
local = getValue(s.local)
} else {
throw new Error('Unknown export specifier type')
}
Expand All @@ -318,7 +318,7 @@ export class ExportMap {

if ('exported' in s) {
// todo: JSDoc
m.reexports.set(s.exported.name, {
m.reexports.set(getValue(s.exported), {
local,
getImport: () => resolveImport(nsource),
})
Expand Down
7 changes: 5 additions & 2 deletions src/utils/parse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,7 @@ export function parse(

// ESLint in "flat" mode only sets context.languageOptions.parserOptions
let parserOptions =
('languageOptions' in context && context.languageOptions?.parserOptions) ||
context.parserOptions
context.languageOptions?.parserOptions || context.parserOptions

const parserOrPath = getParser(path, context)

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

// If this is a flat config, we need to add ecmaVersion and sourceType (if present) from languageOptions
parserOptions.ecmaVersion ??= context.languageOptions?.ecmaVersion
parserOptions.sourceType ??= context.languageOptions?.sourceType

// require the parser relative to the main module (i.e., ESLint)
const parser =
typeof parserOrPath === 'string'
Expand Down
Loading
Loading