no-unreadable-iife.js 1018 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. 'use strict';
  2. const {
  3. isParenthesized,
  4. getParenthesizedRange,
  5. } = require('./utils/parentheses.js');
  6. const toLocation = require('./utils/to-location.js');
  7. const MESSAGE_ID_ERROR = 'no-unreadable-iife';
  8. const messages = {
  9. [MESSAGE_ID_ERROR]: 'IIFE with parenthesized arrow function body is considered unreadable.',
  10. };
  11. const selector = [
  12. 'CallExpression',
  13. ' > ',
  14. 'ArrowFunctionExpression.callee',
  15. ' > ',
  16. ':not(BlockStatement).body',
  17. ].join('');
  18. /** @param {import('eslint').Rule.RuleContext} context */
  19. const create = context => ({
  20. [selector](node) {
  21. const sourceCode = context.getSourceCode();
  22. if (!isParenthesized(node, sourceCode)) {
  23. return;
  24. }
  25. return {
  26. node,
  27. loc: toLocation(getParenthesizedRange(node, sourceCode), sourceCode),
  28. messageId: MESSAGE_ID_ERROR,
  29. };
  30. },
  31. });
  32. /** @type {import('eslint').Rule.RuleModule} */
  33. module.exports = {
  34. create,
  35. meta: {
  36. type: 'suggestion',
  37. docs: {
  38. description: 'Disallow unreadable IIFEs.',
  39. },
  40. hasSuggestions: false,
  41. messages,
  42. },
  43. };