no-namespace.js 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. /**
  2. * @fileoverview Enforce that namespaces are not used in React elements
  3. * @author Yacine Hmito
  4. */
  5. 'use strict';
  6. const elementType = require('jsx-ast-utils/elementType');
  7. const docsUrl = require('../util/docsUrl');
  8. const isCreateElement = require('../util/isCreateElement');
  9. const report = require('../util/report');
  10. // ------------------------------------------------------------------------------
  11. // Rule Definition
  12. // ------------------------------------------------------------------------------
  13. const messages = {
  14. noNamespace: 'React component {{name}} must not be in a namespace, as React does not support them',
  15. };
  16. /** @type {import('eslint').Rule.RuleModule} */
  17. module.exports = {
  18. meta: {
  19. docs: {
  20. description: 'Enforce that namespaces are not used in React elements',
  21. category: 'Possible Errors',
  22. recommended: false,
  23. url: docsUrl('no-namespace'),
  24. },
  25. messages,
  26. schema: [],
  27. },
  28. create(context) {
  29. return {
  30. CallExpression(node) {
  31. if (isCreateElement(context, node) && node.arguments.length > 0 && node.arguments[0].type === 'Literal') {
  32. const name = node.arguments[0].value;
  33. if (typeof name !== 'string' || name.indexOf(':') === -1) return undefined;
  34. report(context, messages.noNamespace, 'noNamespace', {
  35. node,
  36. data: {
  37. name,
  38. },
  39. });
  40. }
  41. },
  42. JSXOpeningElement(node) {
  43. const name = elementType(node);
  44. if (typeof name !== 'string' || name.indexOf(':') === -1) return undefined;
  45. report(context, messages.noNamespace, 'noNamespace', {
  46. node,
  47. data: {
  48. name,
  49. },
  50. });
  51. },
  52. };
  53. },
  54. };