Skip to content

Commit

Permalink
feat: add ability to show rule for JSX usage
Browse files Browse the repository at this point in the history
Adds the ability to show a rule violation when deprecated methods are used in JSX

fix #8
  • Loading branch information
kirkobyte committed Nov 20, 2020
1 parent 7a0dcf6 commit f60558c
Showing 1 changed file with 34 additions and 29 deletions.
63 changes: 34 additions & 29 deletions src/rules/deprecation.ts
Expand Up @@ -49,32 +49,35 @@ export default createRule<Options, MessageIds>({
create(context) {
const services = ESLintUtils.getParserServices(context);

return {
Identifier: (id: TSESTree.Identifier) => {
const insideImportExport = context
.getAncestors()
.some(anc => anc.type.includes('Import'));
if (
insideImportExport ||
(id.type === 'Identifier' && isDeclaration(id, context))
) {
// Don't consider deprecations if inside import/export.
// Also, the spot where something is declared is never deprecated.
return;
}
const rule = (id: TSESTree.Identifier | TSESTree.JSXIdentifier) => {
const insideImportExport = context
.getAncestors()
.some(anc => anc.type.includes('Import'));
if (
insideImportExport ||
((id.type === 'Identifier' || id.type === 'JSXIdentifier') &&
isDeclaration(id, context))
) {
// Don't consider deprecations if inside import/export.
// Also, the spot where something is declared is never deprecated.
return;
}

const deprecation = getDeprecation(id, services, context);
if (deprecation) {
context.report({
node: id,
messageId: 'deprecated',
data: {
name: id.name,
reason: deprecation.reason,
},
});
}
},
const deprecation = getDeprecation(id, services, context);
if (deprecation) {
context.report({
node: id,
messageId: 'deprecated',
data: {
name: id.name,
reason: deprecation.reason,
},
});
}
};
return {
Identifier: rule,
JSXIdentifier: rule,
};
},
});
Expand All @@ -89,7 +92,7 @@ function getParent(context: TSESLint.RuleContext<MessageIds, Options>) {
let lastProcessedDuplicateName: string | undefined;

function isDeclaration(
id: TSESTree.Identifier,
id: TSESTree.Identifier | TSESTree.JSXIdentifier,
context: TSESLint.RuleContext<MessageIds, Options>,
) {
const parent = getParent(context);
Expand Down Expand Up @@ -128,7 +131,7 @@ function isDeclaration(
case 'ArrayPattern': // `const [foo, bar] = baz`
// Array destructuring is truly a declaration on the left side
// (even if there's reassignment)
return parent.elements.includes(id);
return id.type === 'Identifier' && parent.elements.includes(id);

case 'Property':
// no for ObjectExpression: `const foo = { bar: baz }`
Expand Down Expand Up @@ -172,7 +175,7 @@ function isDeclaration(
}

function getDeprecation(
id: TSESTree.Identifier,
id: TSESTree.Identifier | TSESTree.JSXIdentifier,
services: RequiredParserServices,
context: TSESLint.RuleContext<MessageIds, Options>,
) {
Expand Down Expand Up @@ -205,7 +208,7 @@ function getDeprecation(
}

function getSymbol(
id: TSESTree.Identifier,
id: TSESTree.Identifier | TSESTree.JSXIdentifier,
services: RequiredParserServices,
tc: ts.TypeChecker,
) {
Expand Down Expand Up @@ -268,6 +271,8 @@ function isCallExpression(
return node.callee === callee;
} else if (node.type === 'TaggedTemplateExpression') {
return node.tag === callee;
} else if (node.type === 'JSXOpeningElement') {
return node.name === callee;
}
}
return false;
Expand Down

0 comments on commit f60558c

Please sign in to comment.