prefer-dom-node-text-content.js 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. 'use strict';
  2. const {memberExpressionSelector} = require('./selectors/index.js');
  3. const ERROR = 'error';
  4. const SUGGESTION = 'suggestion';
  5. const messages = {
  6. [ERROR]: 'Prefer `.textContent` over `.innerText`.',
  7. [SUGGESTION]: 'Switch to `.textContent`.',
  8. };
  9. const memberExpressionPropertySelector = `${memberExpressionSelector('innerText')} > .property`;
  10. const destructuringSelector = [
  11. 'ObjectPattern',
  12. ' > ',
  13. 'Property.properties',
  14. '[kind="init"]',
  15. '[computed!=true]',
  16. ' > ',
  17. 'Identifier.key',
  18. '[name="innerText"]',
  19. ].join('');
  20. /** @param {import('eslint').Rule.RuleContext} context */
  21. const create = () => ({
  22. [memberExpressionPropertySelector](node) {
  23. return {
  24. node,
  25. messageId: ERROR,
  26. suggest: [
  27. {
  28. messageId: SUGGESTION,
  29. fix: fixer => fixer.replaceText(node, 'textContent'),
  30. },
  31. ],
  32. };
  33. },
  34. [destructuringSelector](node) {
  35. return {
  36. node,
  37. messageId: ERROR,
  38. suggest: [
  39. {
  40. messageId: SUGGESTION,
  41. fix: fixer => fixer.replaceText(
  42. node,
  43. node.parent.shorthand ? 'textContent: innerText' : 'textContent',
  44. ),
  45. },
  46. ],
  47. };
  48. },
  49. });
  50. /** @type {import('eslint').Rule.RuleModule} */
  51. module.exports = {
  52. create,
  53. meta: {
  54. type: 'suggestion',
  55. docs: {
  56. description: 'Prefer `.textContent` over `.innerText`.',
  57. },
  58. hasSuggestions: true,
  59. messages,
  60. },
  61. };