jsx-uses-vars.js 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  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: function(context) {
  21. return {
  22. JSXOpeningElement: function(node) {
  23. let name;
  24. if (node.name.namespace && node.name.namespace.name) {
  25. // <Foo:Bar>
  26. name = node.name.namespace.name;
  27. } else if (node.name.name) {
  28. // <Foo>
  29. name = node.name.name;
  30. } else if (node.name.object) {
  31. // <Foo...Bar>
  32. let parent = node.name.object;
  33. while (parent.object) {
  34. parent = parent.object;
  35. }
  36. name = parent.name;
  37. } else {
  38. return;
  39. }
  40. context.markVariableAsUsed(name);
  41. }
  42. };
  43. }
  44. };