no-object-as-default-parameter.js 1.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. 'use strict';
  2. const MESSAGE_ID_IDENTIFIER = 'identifier';
  3. const MESSAGE_ID_NON_IDENTIFIER = 'non-identifier';
  4. const messages = {
  5. [MESSAGE_ID_IDENTIFIER]: 'Do not use an object literal as default for parameter `{{parameter}}`.',
  6. [MESSAGE_ID_NON_IDENTIFIER]: 'Do not use an object literal as default.',
  7. };
  8. const objectParameterSelector = [
  9. ':function > AssignmentPattern.params',
  10. '[right.type="ObjectExpression"]',
  11. '[right.properties.length>0]',
  12. ].join('');
  13. /** @param {import('eslint').Rule.RuleContext} context */
  14. const create = () => ({
  15. [objectParameterSelector]: node => {
  16. const {left, right} = node;
  17. if (left.type === 'Identifier') {
  18. return {
  19. node: left,
  20. messageId: MESSAGE_ID_IDENTIFIER,
  21. data: {parameter: left.name},
  22. };
  23. }
  24. return {
  25. node: right,
  26. messageId: MESSAGE_ID_NON_IDENTIFIER,
  27. };
  28. },
  29. });
  30. /** @type {import('eslint').Rule.RuleModule} */
  31. module.exports = {
  32. create,
  33. meta: {
  34. type: 'problem',
  35. docs: {
  36. description: 'Disallow the use of objects as default parameters.',
  37. },
  38. messages,
  39. },
  40. };