Skip to content
This repository has been archived by the owner on Oct 16, 2024. It is now read-only.

Add support for ESLint v9 #79

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
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
15 changes: 15 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,21 @@ jobs:
fail-fast: false
matrix:
include:
- name: 'Test with Eslint v8'
cmd: |
npm i eslint@^8 \
@typescript-eslint/utils@^6 \
@typescript-eslint/parser@^6 \
@typescript-eslint/rule-tester@^6 \
@typescript-eslint/eslint-plugin@^6 \
cat package-lock.json | grep -A 1 \
-e "\"node_modules/eslint\": {" \
-e "\"node_modules/@typescript-eslint/utils\": {" \
-e "\"node_modules/@typescript-eslint/parser\": {" \
-e "\"node_modules/@typescript-eslint/rule-tester\": {" \
-e "\"node_modules/@typescript-eslint/eslint-plugin\": {" \
test: npm run test
build: npm run build
- name: 'Test with Eslint v7'
cmd: |
npm i eslint@^7 \
Expand Down
1,056 changes: 526 additions & 530 deletions package-lock.json

Large diffs are not rendered by default.

12 changes: 6 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,12 @@
"semantic-release": "semantic-release"
},
"dependencies": {
"@typescript-eslint/utils": "^6.0.0",
"@typescript-eslint/utils": "^6.18.0",
"tslib": "^2.3.1",
"tsutils": "^3.21.0"
},
"peerDependencies": {
"eslint": "^7.0.0 || ^8.0.0",
"eslint": "^7.0.0 || ^8.0.0 || ^9.0.0 || ^9.0.0-alpha.0",
"typescript": "^4.2.4 || ^5.0.0"
},
"devDependencies": {
Expand All @@ -43,12 +43,12 @@
"@semantic-release/release-notes-generator": "^10.0.3",
"@types/jest": "^29.5.0",
"@types/node": "^18.15.11",
"@typescript-eslint/eslint-plugin": "^6.0.0",
"@typescript-eslint/rule-tester": "^6.0.0",
"@typescript-eslint/parser": "^6.0.0",
"@typescript-eslint/eslint-plugin": "^6.18.0",
"@typescript-eslint/rule-tester": "^6.18.0",
"@typescript-eslint/parser": "^6.18.0",
"commitizen": "^4.2.4",
"cz-conventional-changelog": "^3.3.0",
"eslint": "^8.0.0",
"eslint": "^9.0.0-alpha.0",
"husky": "^8.0.3",
"jest": "^29.5.0",
"lint-staged": "^13.2.0",
Expand Down
26 changes: 11 additions & 15 deletions src/rules/deprecation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import {
} from '@typescript-eslint/utils';
import { isReassignmentTarget } from 'tsutils';
import * as ts from 'typescript';
import { stringifyJSDocTagInfoText } from '../utils/stringifyJSDocTagInfoText';
import { getSourceAncestors, stringifyJSDocTag } from '../utils';

const createRule = ESLintUtils.RuleCreator(
() => 'https://github.com/gund/eslint-plugin-deprecation',
Expand Down Expand Up @@ -85,9 +85,9 @@ function createRuleForIdentifier(
}

// - Inside an import
const isInsideImport = context
.getAncestors()
.some((anc) => anc.type.includes('Import'));
const isInsideImport = getSourceAncestors(id, context).some((anc) =>
anc.type.includes('Import'),
);

if (isInsideImport) {
return;
Expand All @@ -109,11 +109,6 @@ function createRuleForIdentifier(
};
}

function getParent(context: TSESLint.RuleContext<MessageIds, Options>) {
const ancestors = context.getAncestors();
return ancestors.length > 0 ? ancestors[ancestors.length - 1] : undefined;
}

// Unfortunately need to keep some state because identifiers like foo in
// `const { foo } = bar` will be processed twice.
let lastProcessedDuplicateName: string | undefined;
Expand All @@ -122,9 +117,13 @@ function isDeclaration(
id: TSESTree.Identifier | TSESTree.JSXIdentifier,
context: TSESLint.RuleContext<MessageIds, Options>,
) {
const parent = getParent(context);
const parent = getSourceAncestors(id, context).at(-1);

switch (parent?.type) {
if (!parent) {
return false;
}

switch (parent.type) {
case 'TSEnumDeclaration':
case 'TSInterfaceDeclaration':
case 'TSTypeAliasDeclaration':
Expand Down Expand Up @@ -198,9 +197,6 @@ function isDeclaration(
// Yes: bar in `function foo(bar = 3) {}` and `const [bar = 3] = []`
// No: bar in `const { bar = 3 }`
return parent.left === id && !isShortHandProperty(parent.parent);

default:
return false;
}
}

Expand Down Expand Up @@ -311,7 +307,7 @@ function isCallExpression(
function getJsDocDeprecation(tags: ts.JSDocTagInfo[]) {
for (const tag of tags) {
if (tag.name === 'deprecated') {
return { reason: stringifyJSDocTagInfoText(tag) };
return { reason: stringifyJSDocTag(tag) };
}
}
return undefined;
Expand Down
43 changes: 43 additions & 0 deletions src/utils/get-source-ancestors.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import { TSESLint, TSESTree } from '@typescript-eslint/utils';

/**
* Get the ancestors of a node from the source code.
*
* NOTE: For ESLint <9.0.0 will return ancestors of the currently-traversed node in the context.
*
* @internal
*/
export function getSourceAncestors<
TMessageIds extends string,
TOptions extends readonly unknown[],
>(
node: TSESTree.Node,
context: TSESLint.RuleContext<TMessageIds, TOptions> | ESLintRuleContextPreV9,
): TSESTree.Node[] {
// Use ESLint 9.0.0 API if available
return isContextV9(context)
? context.sourceCode.getAncestors(node)
: // Fallback to ESLint <9.0.0 API
context.getAncestors();
}

function isContextV9<
TMessageIds extends string,
TOptions extends readonly unknown[],
>(
context: TSESLint.RuleContext<TMessageIds, TOptions> | ESLintRuleContextPreV9,
): context is TSESLint.RuleContext<TMessageIds, TOptions> & {
sourceCode: TSESLint.SourceCode &
Required<Pick<TSESLint.SourceCode, 'getAncestors'>>;
} {
return 'sourceCode' in context && context.sourceCode !== undefined;
}

/**
* Backwards compatibility interface for ESLint <9.0.0
*
* @deprecated API of ESLint <9.0.0
*/
interface ESLintRuleContextPreV9 {
getAncestors(): TSESTree.Node[];
}
2 changes: 2 additions & 0 deletions src/utils/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export * from './get-source-ancestors';
export * from './stringify-jsdoc-tag';
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@ import * as ts from 'typescript';
/**
* Stringifies the text within a JSDocTagInfo AST node with compatibility for
* pre/post TypeScript 4.3 API changes.
*
* @internal
*/
export function stringifyJSDocTagInfoText(
export function stringifyJSDocTag(
tag: ts.JSDocTagInfo | { text: ts.SymbolDisplayPart[] },
): string {
return isJSDocTagInfo4Point2AndBefore(tag)
Expand Down