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

Commit

Permalink
fix(rule): optimize rule checks
Browse files Browse the repository at this point in the history
* Optimise identifierRule: rearrange early returns

* Fix formatting
  • Loading branch information
udivankin authored Nov 14, 2022
1 parent 8dc0a9b commit bcffb64
Showing 1 changed file with 22 additions and 10 deletions.
32 changes: 22 additions & 10 deletions src/rules/deprecation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,25 +58,37 @@ function createRuleForIdentifier(
context: TSESLint.RuleContext<'deprecated', Options>,
): TSESLint.RuleFunction<TSESTree.JSXIdentifier | TSESTree.Identifier> {
return function identifierRule(id) {
const services = ESLintUtils.getParserServices(context);

// Don't consider deprecations in certain cases:
// - Inside an import
const isInsideImport = context
.getAncestors()
.some((anc) => anc.type.includes('Import'));

// - On JSX closing elements (only flag the opening element)
const isClosingElement =
id.type === 'JSXIdentifier' && id.parent?.type === 'JSXClosingElement';

if (isClosingElement) {
return;
}

// - At the spot where something is declared
const isIdDeclaration =
(id.type === 'Identifier' || id.type === 'JSXIdentifier') &&
isDeclaration(id, context);
// - On JSX closing elements (only flag the opening element)
const isClosingElement =
id.type === 'JSXIdentifier' && id.parent?.type === 'JSXClosingElement';
if (isInsideImport || isIdDeclaration || isClosingElement) {

if (isIdDeclaration) {
return;
}

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

if (isInsideImport) {
return;
}

const services = ESLintUtils.getParserServices(context);
const deprecation = getDeprecation(id, services, context);

if (deprecation) {
context.report({
node: id,
Expand Down

0 comments on commit bcffb64

Please sign in to comment.