securityos/node_modules/eslint-plugin-unicorn/rules/prefer-dom-node-text-conten...

66 lines
1.3 KiB
JavaScript
Raw Normal View History

2024-09-06 15:32:35 +00:00
'use strict';
const {memberExpressionSelector} = require('./selectors/index.js');
const ERROR = 'error';
const SUGGESTION = 'suggestion';
const messages = {
[ERROR]: 'Prefer `.textContent` over `.innerText`.',
[SUGGESTION]: 'Switch to `.textContent`.',
};
const memberExpressionPropertySelector = `${memberExpressionSelector('innerText')} > .property`;
const destructuringSelector = [
'ObjectPattern',
' > ',
'Property.properties',
'[kind="init"]',
'[computed!=true]',
' > ',
'Identifier.key',
'[name="innerText"]',
].join('');
/** @param {import('eslint').Rule.RuleContext} context */
const create = () => ({
[memberExpressionPropertySelector](node) {
return {
node,
messageId: ERROR,
suggest: [
{
messageId: SUGGESTION,
fix: fixer => fixer.replaceText(node, 'textContent'),
},
],
};
},
[destructuringSelector](node) {
return {
node,
messageId: ERROR,
suggest: [
{
messageId: SUGGESTION,
fix: fixer => fixer.replaceText(
node,
node.parent.shorthand ? 'textContent: innerText' : 'textContent',
),
},
],
};
},
});
/** @type {import('eslint').Rule.RuleModule} */
module.exports = {
create,
meta: {
type: 'suggestion',
docs: {
description: 'Prefer `.textContent` over `.innerText`.',
},
hasSuggestions: true,
messages,
},
};