Thanks to visit codestin.com
Credit goes to github.com

Skip to content

chore: add eslint-utils wrappers around deprecated ESLint methods #7914

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

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
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import prettier from '@prettier/sync';
import { getContextualType } from '@typescript-eslint/type-utils';
import type { TSESTree } from '@typescript-eslint/utils';
import { AST_NODE_TYPES, ESLintUtils } from '@typescript-eslint/utils';
import { getSourceCode } from '@typescript-eslint/utils/eslint-utils';

import { createRule } from '../util';

Expand Down Expand Up @@ -147,7 +148,7 @@ export default createRule<Options, MessageIds>({
},
],
create(context, [{ formatWithPrettier }]) {
const sourceCode = context.getSourceCode();
const sourceCode = getSourceCode(context);
const services = ESLintUtils.getParserServices(context);
const checker = services.program.getTypeChecker();

Expand Down
9 changes: 7 additions & 2 deletions packages/eslint-plugin-tslint/src/rules/config.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
import { ESLintUtils } from '@typescript-eslint/utils';
import {
getCwd,
getFilename,
getSourceCode,
} from '@typescript-eslint/utils/eslint-utils';
import path from 'path';
import type { RuleSeverity } from 'tslint';
import { Configuration } from 'tslint';
Expand Down Expand Up @@ -119,8 +124,8 @@ export default createRule<Options, MessageIds>({
context,
[{ rules: tslintRules, rulesDirectory: tslintRulesDirectory, lintFile }],
) {
const fileName = path.resolve(context.getCwd(), context.getFilename());
const sourceCode = context.getSourceCode().text;
const fileName = path.resolve(getCwd(context), getFilename(context));
const sourceCode = getSourceCode(context).text;
const services = ESLintUtils.getParserServices(context);
const program = services.program;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import type { TSESTree } from '@typescript-eslint/utils';
import { AST_NODE_TYPES } from '@typescript-eslint/utils';
import { getSourceCode } from '@typescript-eslint/utils/eslint-utils';

import { createRule, getNameFromMember, MemberNameType } from '../util';

Expand Down Expand Up @@ -30,7 +31,7 @@ export default createRule({
},
defaultOptions: [],
create(context) {
const sourceCode = context.getSourceCode();
const sourceCode = getSourceCode(context);

interface Method {
name: string;
Expand Down
3 changes: 2 additions & 1 deletion packages/eslint-plugin/src/rules/array-type.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import type { TSESTree } from '@typescript-eslint/utils';
import { AST_NODE_TYPES } from '@typescript-eslint/utils';
import { getSourceCode } from '@typescript-eslint/utils/eslint-utils';

import { createRule, isParenthesized } from '../util';

Expand Down Expand Up @@ -134,7 +135,7 @@ export default createRule<Options, MessageIds>({
},
],
create(context, [options]) {
const sourceCode = context.getSourceCode();
const sourceCode = getSourceCode(context);

const defaultOption = options.default;
const readonlyOption = options.readonly ?? defaultOption;
Expand Down
3 changes: 2 additions & 1 deletion packages/eslint-plugin/src/rules/await-thenable.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import type { TSESLint } from '@typescript-eslint/utils';
import { getSourceCode } from '@typescript-eslint/utils/eslint-utils';
import * as tsutils from 'ts-api-utils';

import {
Expand Down Expand Up @@ -50,7 +51,7 @@ export default createRule({
{
messageId: 'removeAwait',
fix(fixer): TSESLint.RuleFix {
const sourceCode = context.getSourceCode();
const sourceCode = getSourceCode(context);
const awaitKeyword = nullThrows(
sourceCode.getFirstToken(node, isAwaitKeyword),
NullThrowsReasons.MissingToken('await', 'await expression'),
Expand Down
3 changes: 2 additions & 1 deletion packages/eslint-plugin/src/rules/ban-ts-comment.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { AST_TOKEN_TYPES, type TSESLint } from '@typescript-eslint/utils';
import { getSourceCode } from '@typescript-eslint/utils/eslint-utils';

import { createRule, getStringLength } from '../util';

Expand Down Expand Up @@ -102,7 +103,7 @@ export default createRule<[Options], MessageIds>({
/^\/*\s*@ts-(?<directive>expect-error|ignore|check|nocheck)(?<description>.*)/;
const commentDirectiveRegExMultiLine =
/^\s*(?:\/|\*)*\s*@ts-(?<directive>expect-error|ignore|check|nocheck)(?<description>.*)/;
const sourceCode = context.getSourceCode();
const sourceCode = getSourceCode(context);

const descriptionFormats = new Map<string, RegExp>();
for (const directive of [
Expand Down
3 changes: 2 additions & 1 deletion packages/eslint-plugin/src/rules/ban-tslint-comment.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { AST_TOKEN_TYPES } from '@typescript-eslint/utils';
import { getSourceCode } from '@typescript-eslint/utils/eslint-utils';

import { createRule } from '../util';

Expand Down Expand Up @@ -31,7 +32,7 @@ export default createRule({
},
defaultOptions: [],
create: context => {
const sourceCode = context.getSourceCode();
const sourceCode = getSourceCode(context);
return {
Program(): void {
const comments = sourceCode.getAllComments();
Expand Down
3 changes: 2 additions & 1 deletion packages/eslint-plugin/src/rules/ban-types.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import type { TSESLint, TSESTree } from '@typescript-eslint/utils';
import { AST_NODE_TYPES } from '@typescript-eslint/utils';
import { getSourceCode } from '@typescript-eslint/utils/eslint-utils';

import { createRule, objectReduceKey } from '../util';

Expand Down Expand Up @@ -218,7 +219,7 @@ export default createRule<Options, MessageIds>({

function checkBannedTypes(
typeNode: TSESTree.Node,
name = stringifyNode(typeNode, context.getSourceCode()),
name = stringifyNode(typeNode, getSourceCode(context)),
): void {
const bannedType = bannedTypes.get(name);

Expand Down
3 changes: 2 additions & 1 deletion packages/eslint-plugin/src/rules/block-spacing.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import type { TSESTree } from '@typescript-eslint/utils';
import { AST_TOKEN_TYPES } from '@typescript-eslint/utils';
import { getSourceCode } from '@typescript-eslint/utils/eslint-utils';

import type {
InferMessageIdsTypeFromRule,
Expand Down Expand Up @@ -30,7 +31,7 @@ export default createRule<Options, MessageIds>({
defaultOptions: ['always'],

create(context, [whenToApplyOption]) {
const sourceCode = context.getSourceCode();
const sourceCode = getSourceCode(context);
const baseRules = baseRule.create(context);
const always = whenToApplyOption !== 'never';
const messageId = always ? 'missing' : 'extra';
Expand Down
3 changes: 2 additions & 1 deletion packages/eslint-plugin/src/rules/brace-style.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import type { TSESTree } from '@typescript-eslint/utils';
import { getSourceCode } from '@typescript-eslint/utils/eslint-utils';

import type {
InferMessageIdsTypeFromRule,
Expand Down Expand Up @@ -32,7 +33,7 @@ export default createRule<Options, MessageIds>({
context.options;

const isAllmanStyle = style === 'allman';
const sourceCode = context.getSourceCode();
const sourceCode = getSourceCode(context);
const rules = baseRule.create(context);

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import type { TSESLint, TSESTree } from '@typescript-eslint/utils';
import { AST_NODE_TYPES } from '@typescript-eslint/utils';
import { getSourceCode } from '@typescript-eslint/utils/eslint-utils';

import { createRule } from '../util';

Expand Down Expand Up @@ -95,7 +96,7 @@ export default createRule<Options, MessageIds>({
{
messageId: 'preferFieldStyleSuggestion',
fix(fixer): TSESLint.RuleFix {
const sourceCode = context.getSourceCode();
const sourceCode = getSourceCode(context);
const name = sourceCode.getText(node.key);

let text = '';
Expand Down Expand Up @@ -130,7 +131,7 @@ export default createRule<Options, MessageIds>({
{
messageId: 'preferGetterStyleSuggestion',
fix(fixer): TSESLint.RuleFix {
const sourceCode = context.getSourceCode();
const sourceCode = getSourceCode(context);
const name = sourceCode.getText(node.key);

let text = '';
Expand Down
3 changes: 2 additions & 1 deletion packages/eslint-plugin/src/rules/class-methods-use-this.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import type { TSESTree } from '@typescript-eslint/utils';
import { AST_NODE_TYPES } from '@typescript-eslint/utils';
import { getSourceCode } from '@typescript-eslint/utils/eslint-utils';

import {
createRule,
Expand Down Expand Up @@ -99,7 +100,7 @@ export default createRule<Options, MessageIds>({
};
let stack: Stack | undefined;

const sourceCode = context.getSourceCode();
const sourceCode = getSourceCode(context);

function pushContext(
member?: TSESTree.MethodDefinition | TSESTree.PropertyDefinition,
Expand Down
3 changes: 2 additions & 1 deletion packages/eslint-plugin/src/rules/comma-dangle.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import type { TSESTree } from '@typescript-eslint/utils';
import { AST_NODE_TYPES } from '@typescript-eslint/utils';
import { getSourceCode } from '@typescript-eslint/utils/eslint-utils';

import type {
InferMessageIdsTypeFromRule,
Expand Down Expand Up @@ -94,7 +95,7 @@ export default createRule<Options, MessageIds>({
defaultOptions: ['never'],
create(context, [options]) {
const rules = baseRule.create(context);
const sourceCode = context.getSourceCode();
const sourceCode = getSourceCode(context);
const normalizedOptions = normalizeOptions(options);

const predicate = {
Expand Down
3 changes: 2 additions & 1 deletion packages/eslint-plugin/src/rules/comma-spacing.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import type { TSESTree } from '@typescript-eslint/utils';
import { AST_TOKEN_TYPES } from '@typescript-eslint/utils';
import { getSourceCode } from '@typescript-eslint/utils/eslint-utils';

import {
createRule,
Expand Down Expand Up @@ -55,7 +56,7 @@ export default createRule<Options, MessageIds>({
},
],
create(context, [{ before: spaceBefore, after: spaceAfter }]) {
const sourceCode = context.getSourceCode();
const sourceCode = getSourceCode(context);
const tokensAndComments = sourceCode.tokensAndComments;
const ignoredTokens = new Set<TSESTree.PunctuatorToken>();

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import type { TSESTree } from '@typescript-eslint/utils';
import { AST_NODE_TYPES } from '@typescript-eslint/utils';
import { getSourceCode } from '@typescript-eslint/utils/eslint-utils';

import { createRule } from '../util';

Expand Down Expand Up @@ -31,7 +32,7 @@ export default createRule<Options, MessageIds>({
},
defaultOptions: ['constructor'],
create(context, [mode]) {
const sourceCode = context.getSourceCode();
const sourceCode = getSourceCode(context);
return {
'VariableDeclarator,PropertyDefinition,:matches(FunctionDeclaration,FunctionExpression) > AssignmentPattern'(
node:
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import type { TSESLint, TSESTree } from '@typescript-eslint/utils';
import { AST_NODE_TYPES, ASTUtils } from '@typescript-eslint/utils';
import { getScope, getSourceCode } from '@typescript-eslint/utils/eslint-utils';

import { createRule } from '../util';

Expand Down Expand Up @@ -28,7 +29,7 @@ export default createRule<Options, MessageIds>({
},
defaultOptions: ['record'],
create(context, [mode]) {
const sourceCode = context.getSourceCode();
const sourceCode = getSourceCode(context);

function checkMembers(
members: TSESTree.TypeElement[],
Expand Down Expand Up @@ -63,7 +64,7 @@ export default createRule<Options, MessageIds>({
}

if (parentId) {
const scope = context.getScope();
const scope = getScope(context);
const superVar = ASTUtils.findVariable(scope, parentId.name);
if (superVar) {
const isCircular = superVar.references.some(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import type { TSESLint, TSESTree } from '@typescript-eslint/utils';
import { AST_NODE_TYPES } from '@typescript-eslint/utils';
import { getSourceCode } from '@typescript-eslint/utils/eslint-utils';
import * as ts from 'typescript';

import {
Expand Down Expand Up @@ -90,7 +91,7 @@ export default createRule<Options, MessageIds>({
},
],
create(context, [options]) {
const sourceCode = context.getSourceCode();
const sourceCode = getSourceCode(context);
const parserServices = getParserServices(context, true);

function isConst(node: TSESTree.TypeNode): boolean {
Expand Down Expand Up @@ -251,9 +252,9 @@ export default createRule<Options, MessageIds>({
fixer.replaceText(node, getTextWithParentheses(node.expression)),
fixer.insertTextAfter(
node,
` satisfies ${context
.getSourceCode()
.getText(node.typeAnnotation)}`,
` satisfies ${getSourceCode(context).getText(
node.typeAnnotation,
)}`,
),
],
});
Expand Down
20 changes: 11 additions & 9 deletions packages/eslint-plugin/src/rules/consistent-type-definitions.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
import type { TSESLint, TSESTree } from '@typescript-eslint/utils';
import { AST_NODE_TYPES, AST_TOKEN_TYPES } from '@typescript-eslint/utils';
import {
getAncestors,
getSourceCode,
} from '@typescript-eslint/utils/eslint-utils';

import { createRule } from '../util';

Expand All @@ -26,21 +30,19 @@ export default createRule({
},
defaultOptions: ['interface'],
create(context, [option]) {
const sourceCode = context.getSourceCode();
const sourceCode = getSourceCode(context);

/**
* Iterates from the highest parent to the currently traversed node
* to determine whether any node in tree is globally declared module declaration
*/
function isCurrentlyTraversedNodeWithinModuleDeclaration(): boolean {
return context
.getAncestors()
.some(
node =>
node.type === AST_NODE_TYPES.TSModuleDeclaration &&
node.declare &&
node.global,
);
return getAncestors(context).some(
node =>
node.type === AST_NODE_TYPES.TSModuleDeclaration &&
node.declare &&
node.global,
);
}

return {
Expand Down
3 changes: 2 additions & 1 deletion packages/eslint-plugin/src/rules/consistent-type-exports.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import type { TSESLint, TSESTree } from '@typescript-eslint/utils';
import { AST_NODE_TYPES } from '@typescript-eslint/utils';
import { getSourceCode } from '@typescript-eslint/utils/eslint-utils';
import { SymbolFlags } from 'typescript';

import {
Expand Down Expand Up @@ -74,7 +75,7 @@ export default createRule<Options, MessageIds>({
],

create(context, [{ fixMixedExportsWithInlineTypeSpecifier }]) {
const sourceCode = context.getSourceCode();
const sourceCode = getSourceCode(context);
const sourceExportsMap: Record<string, SourceExports> = {};
const services = getParserServices(context);

Expand Down
8 changes: 6 additions & 2 deletions packages/eslint-plugin/src/rules/consistent-type-imports.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
import type { TSESLint, TSESTree } from '@typescript-eslint/utils';
import { AST_NODE_TYPES } from '@typescript-eslint/utils';
import {
getDeclaredVariables,
getSourceCode,
} from '@typescript-eslint/utils/eslint-utils';

import {
createRule,
Expand Down Expand Up @@ -104,7 +108,7 @@ export default createRule<Options, MessageIds>({
const prefer = option.prefer ?? 'type-imports';
const disallowTypeAnnotations = option.disallowTypeAnnotations !== false;
const fixStyle = option.fixStyle ?? 'separate-type-imports';
const sourceCode = context.getSourceCode();
const sourceCode = getSourceCode(context);

const sourceImportsMap: Record<string, SourceImports> = {};

Expand Down Expand Up @@ -168,7 +172,7 @@ export default createRule<Options, MessageIds>({
continue;
}

const [variable] = context.getDeclaredVariables(specifier);
const [variable] = getDeclaredVariables(context, specifier);
if (variable.references.length === 0) {
unusedSpecifiers.push(specifier);
} else {
Expand Down
Loading