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: support ESLint v9 #61

Closed
wants to merge 1 commit into from
Closed
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
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
"watch": "yarn test --watch"
},
"peerDependencies": {
"eslint": "^7.2.0 || ^8"
"eslint": "^7.2.0 || ^8 || ^9.0.0-0"
},
"dependencies": {
"@typescript-eslint/utils": "^5.62.0",
Expand Down Expand Up @@ -92,7 +92,7 @@
"cross-env": "^7.0.3",
"enhanced-resolve": "^5.16.0",
"escope": "^4.0.0",
"eslint": "^7.2.0 || ^8",
"eslint": "^8",
"eslint-config-prettier": "^9.1.0",
"eslint-doc-generator": "^1.7.0",
"eslint-import-resolver-typescript": "^3.6.1",
Expand Down
10 changes: 7 additions & 3 deletions src/rules/namespace.ts
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,10 @@ export = createRule<[Options], MessageId>({
return
}

if (declaredScope(context, dereference.object.name) !== 'module') {
if (
declaredScope(context, dereference.object.name, dereference) !==
'module'
) {
return
}

Expand Down Expand Up @@ -249,7 +252,8 @@ export = createRule<[Options], MessageId>({
}
},

VariableDeclarator({ id, init }) {
VariableDeclarator(node) {
const { id, init } = node
if (init == null) {
return
}
Expand All @@ -261,7 +265,7 @@ export = createRule<[Options], MessageId>({
}

// check for redefinition in intermediate scopes
if (declaredScope(context, init.name) !== 'module') {
if (declaredScope(context, init.name, node) !== 'module') {
return
}

Expand Down
10 changes: 8 additions & 2 deletions src/rules/newline-after-import.ts
Original file line number Diff line number Diff line change
Expand Up @@ -267,14 +267,20 @@
requireCalls.push(node)
}
},
'Program:exit'() {
'Program:exit'(node) {
log(
'exit processing for',
context.getPhysicalFilename
? context.getPhysicalFilename()
: context.getFilename(),
)
const scopeBody = getScopeBody(context.getScope())

// For ESLint v9
const scope = (context as any)?.sourceCode?.getScope

Check warning on line 279 in src/rules/newline-after-import.ts

View workflow job for this annotation

GitHub Actions / Lint and Test with Node.js 20 and ESLint 8 on ubuntu-latest

Unexpected any. Specify a different type
? (context as any).sourceCode.getScope(node)

Check warning on line 280 in src/rules/newline-after-import.ts

View workflow job for this annotation

GitHub Actions / Lint and Test with Node.js 20 and ESLint 8 on ubuntu-latest

Unexpected any. Specify a different type

Check warning on line 280 in src/rules/newline-after-import.ts

View check run for this annotation

Codecov / codecov/patch

src/rules/newline-after-import.ts#L280

Added line #L280 was not covered by tests
: context.getScope()

const scopeBody = getScopeBody(scope)

log('got scope:', scopeBody)

Expand Down
9 changes: 8 additions & 1 deletion src/rules/no-amd.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
* Rule to prefer imports to AMD
*/

import { TSESLint } from '@typescript-eslint/utils'

Check failure on line 5 in src/rules/no-amd.ts

View workflow job for this annotation

GitHub Actions / Lint and Test with Node.js 20 and ESLint 8 on ubuntu-latest

All imports in the declaration are only used as types. Use `import type`

Check failure on line 5 in src/rules/no-amd.ts

View workflow job for this annotation

GitHub Actions / Lint and Test with Node.js 20 and ESLint 8 on ubuntu-latest

There should be at least one empty line between import groups
import { createRule } from '../utils'

type MessageId = 'amd'
Expand All @@ -23,7 +24,13 @@
create(context) {
return {
CallExpression(node) {
if (context.getScope().type !== 'module') {
// For ESLint v9
const scope: TSESLint.Scope.Scope = (context as any)?.sourceCode

Check warning on line 28 in src/rules/no-amd.ts

View workflow job for this annotation

GitHub Actions / Lint and Test with Node.js 20 and ESLint 8 on ubuntu-latest

Unexpected any. Specify a different type
?.getScope
? (context as any).sourceCode.getScope(node)

Check warning on line 30 in src/rules/no-amd.ts

View workflow job for this annotation

GitHub Actions / Lint and Test with Node.js 20 and ESLint 8 on ubuntu-latest

Unexpected any. Specify a different type

Check warning on line 30 in src/rules/no-amd.ts

View check run for this annotation

Codecov / codecov/patch

src/rules/no-amd.ts#L30

Added line #L30 was not covered by tests
: context.getScope()

if (scope.type !== 'module') {
return
}

Expand Down
12 changes: 9 additions & 3 deletions src/rules/no-commonjs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -122,9 +122,15 @@

// exports.
if ('name' in node.object && node.object.name === 'exports') {
const isInScope = context
.getScope()
.variables.some(variable => variable.name === 'exports')
// For ESLint v9
const scope: TSESLint.Scope.Scope = (context as any)?.sourceCode

Check warning on line 126 in src/rules/no-commonjs.ts

View workflow job for this annotation

GitHub Actions / Lint and Test with Node.js 20 and ESLint 8 on ubuntu-latest

Unexpected any. Specify a different type
?.getScope
? (context as any).sourceCode.getScope(node)

Check warning on line 128 in src/rules/no-commonjs.ts

View workflow job for this annotation

GitHub Actions / Lint and Test with Node.js 20 and ESLint 8 on ubuntu-latest

Unexpected any. Specify a different type

Check warning on line 128 in src/rules/no-commonjs.ts

View check run for this annotation

Codecov / codecov/patch

src/rules/no-commonjs.ts#L128

Added line #L128 was not covered by tests
: context.getScope()

const isInScope = scope.variables.some(
variable => variable.name === 'exports',
)
if (!isInScope) {
context.report({ node, messageId: 'export' })
}
Expand Down
7 changes: 5 additions & 2 deletions src/rules/no-deprecated.ts
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ export = createRule({
return
}

if (declaredScope(context, node.name) !== 'module') {
if (declaredScope(context, node.name, node) !== 'module') {
return
}
context.report({
Expand All @@ -165,7 +165,10 @@ export = createRule({
return
}

if (declaredScope(context, dereference.object.name) !== 'module') {
if (
declaredScope(context, dereference.object.name, dereference) !==
'module'
) {
return
}

Expand Down
12 changes: 10 additions & 2 deletions src/rules/no-mutable-exports.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,14 +48,22 @@

return {
ExportDefaultDeclaration(node) {
const scope = context.getScope()
// For ESLint v9
const scope: TSESLint.Scope.Scope = (context as any)?.sourceCode

Check warning on line 52 in src/rules/no-mutable-exports.ts

View workflow job for this annotation

GitHub Actions / Lint and Test with Node.js 20 and ESLint 8 on ubuntu-latest

Unexpected any. Specify a different type
?.getScope
? (context as any).sourceCode.getScope(node)

Check warning on line 54 in src/rules/no-mutable-exports.ts

View workflow job for this annotation

GitHub Actions / Lint and Test with Node.js 20 and ESLint 8 on ubuntu-latest

Unexpected any. Specify a different type

Check warning on line 54 in src/rules/no-mutable-exports.ts

View check run for this annotation

Codecov / codecov/patch

src/rules/no-mutable-exports.ts#L54

Added line #L54 was not covered by tests
: context.getScope()

if ('name' in node.declaration) {
checkDeclarationsInScope(scope, node.declaration.name)
}
},
ExportNamedDeclaration(node) {
const scope = context.getScope()
// For ESLint v9
const scope: TSESLint.Scope.Scope = (context as any)?.sourceCode

Check warning on line 63 in src/rules/no-mutable-exports.ts

View workflow job for this annotation

GitHub Actions / Lint and Test with Node.js 20 and ESLint 8 on ubuntu-latest

Unexpected any. Specify a different type
?.getScope
? (context as any).sourceCode.getScope(node)

Check warning on line 65 in src/rules/no-mutable-exports.ts

View workflow job for this annotation

GitHub Actions / Lint and Test with Node.js 20 and ESLint 8 on ubuntu-latest

Unexpected any. Specify a different type

Check warning on line 65 in src/rules/no-mutable-exports.ts

View check run for this annotation

Codecov / codecov/patch

src/rules/no-mutable-exports.ts#L65

Added line #L65 was not covered by tests
: context.getScope()

if (node.declaration) {
checkDeclaration(node.declaration)
Expand Down
8 changes: 7 additions & 1 deletion src/rules/no-namespace.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,13 @@
return
}

const scopeVariables = context.getScope().variables
// For ESLint v9
const scope: TSESLint.Scope.Scope = (context as any)?.sourceCode
?.getScope
? (context as any).sourceCode.getScope(node)

Check warning on line 65 in src/rules/no-namespace.ts

View check run for this annotation

Codecov / codecov/patch

src/rules/no-namespace.ts#L65

Added line #L65 was not covered by tests
: context.getScope()

const scopeVariables = scope.variables
const namespaceVariable = scopeVariables.find(
variable => variable.defs[0].node === node,
)!
Expand Down
14 changes: 12 additions & 2 deletions src/utils/declared-scope.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,17 @@
import { TSESTree, TSESLint } from '@typescript-eslint/utils'

Check failure on line 1 in src/utils/declared-scope.ts

View workflow job for this annotation

GitHub Actions / Lint and Test with Node.js 20 and ESLint 8 on ubuntu-latest

All imports in the declaration are only used as types. Use `import type`

Check failure on line 1 in src/utils/declared-scope.ts

View workflow job for this annotation

GitHub Actions / Lint and Test with Node.js 20 and ESLint 8 on ubuntu-latest

There should be at least one empty line between import groups
import type { RuleContext } from '../types'

export function declaredScope(context: RuleContext, name: string) {
const references = context.getScope().references
export function declaredScope(
context: RuleContext,
name: string,
node: TSESTree.Node,
) {
// For ESLint v9
const scope: TSESLint.Scope.Scope = (context as any)?.sourceCode?.getScope
? (context as any).sourceCode.getScope(node)

Check warning on line 11 in src/utils/declared-scope.ts

View check run for this annotation

Codecov / codecov/patch

src/utils/declared-scope.ts#L11

Added line #L11 was not covered by tests
: context.getScope()

const references = scope.references
const reference = references.find(x => x.identifier.name === name)
if (!reference || !reference.resolved) {
return
Expand Down
2 changes: 1 addition & 1 deletion yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -3535,7 +3535,7 @@ eslint-visitor-keys@^3.3.0, eslint-visitor-keys@^3.4.1, eslint-visitor-keys@^3.4
resolved "https://registry.yarnpkg.com/eslint-visitor-keys/-/eslint-visitor-keys-3.4.3.tgz#0cd72fe8550e3c2eae156a96a4dddcd1c8ac5800"
integrity sha512-wpc+LXeiyiisxPlEkUzU6svyS1frIO3Mgxj1fdy7Pm8Ygzguax2N3Fa/D/ag1WqbOprdI+uY6wMUl8/a2G+iag==

"eslint@^7.2.0 || ^8":
eslint@^8:
version "8.57.0"
resolved "https://registry.yarnpkg.com/eslint/-/eslint-8.57.0.tgz#c786a6fd0e0b68941aaf624596fb987089195668"
integrity sha512-dZ6+mexnaTIbSBZWgou51U6OmzIhYM2VcNdtiTtI7qPNZm35Akpr0f6vtw3w1Kmn5PYo+tZVfh13WrhpS6oLqQ==
Expand Down
Loading