jsx-uses-vars.js 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. /**
  2. * @fileoverview Prevent variables used in JSX to be marked as unused
  3. * @author Yannick Croissant
  4. */
  5. 'use strict';
  6. const docsUrl = require('../util/docsUrl');
  7. // ------------------------------------------------------------------------------
  8. // Rule Definition
  9. // ------------------------------------------------------------------------------
  10. module.exports = {
  11. meta: {
  12. docs: {
  13. description: 'Prevent variables used in JSX to be marked as unused',
  14. category: 'Best Practices',
  15. recommended: true,
  16. url: docsUrl('jsx-uses-vars')
  17. },
  18. schema: []
  19. },
  20. create(context) {
  21. return {
  22. JSXOpeningElement(node) {
  23. let name;
  24. if (node.name.namespace) {
  25. // <Foo:Bar>
  26. return;
  27. }
  28. if (node.name.name) {
  29. // <Foo>
  30. name = node.name.name;
  31. } else if (node.name.object) {
  32. // <Foo...Bar>
  33. let parent = node.name.object;
  34. while (parent.object) {
  35. parent = parent.object;
  36. }
  37. name = parent.name;
  38. } else {
  39. return;
  40. }
  41. context.markVariableAsUsed(name);
  42. }
  43. };
  44. }
  45. };