prefer-dom-node-append.js 997 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. 'use strict';
  2. const isValueNotUsable = require('./utils/is-value-not-usable.js');
  3. const {methodCallSelector, notDomNodeSelector} = require('./selectors/index.js');
  4. const MESSAGE_ID = 'prefer-dom-node-append';
  5. const messages = {
  6. [MESSAGE_ID]: 'Prefer `Node#append()` over `Node#appendChild()`.',
  7. };
  8. const selector = [
  9. methodCallSelector({
  10. method: 'appendChild',
  11. argumentsLength: 1,
  12. }),
  13. notDomNodeSelector('callee.object'),
  14. notDomNodeSelector('arguments.0'),
  15. ].join('');
  16. /** @param {import('eslint').Rule.RuleContext} context */
  17. const create = () => ({
  18. [selector](node) {
  19. const fix = isValueNotUsable(node)
  20. ? fixer => fixer.replaceText(node.callee.property, 'append')
  21. : undefined;
  22. return {
  23. node,
  24. messageId: MESSAGE_ID,
  25. fix,
  26. };
  27. },
  28. });
  29. /** @type {import('eslint').Rule.RuleModule} */
  30. module.exports = {
  31. create,
  32. meta: {
  33. type: 'suggestion',
  34. docs: {
  35. description: 'Prefer `Node#append()` over `Node#appendChild()`.',
  36. },
  37. fixable: 'code',
  38. messages,
  39. },
  40. };