prefer-dom-node-text-content
Prefer textContent over innerText. This rule is part of eslint-plugin-conventions.
Prefer textContent over innerText. This rule is part of eslint-plugin-conventions.
Quick Summary
| Aspect | Details |
|---|---|
| Severity | Warning (performance) |
| Auto-Fix | 💡 Suggestion (converts property) |
| Category | Quality |
| ESLint MCP | ✅ Optimized for ESLint MCP integration |
| Best For | DOM manipulation, performance-critical code |
Rule Details
innerText triggers reflow and is slower. textContent is more performant and works in all contexts.
The rule reports reading or writing innerText in every spelling:
el.innerText, el['innerText'], el?.innerText, an assignment target, and
destructuring it (const { innerText } = el, renamed too). It does not report
textContent, a runtime key (el[key]), an innerText key in an object
literal (it builds an object; no element is touched) or a variable named
innerText. The suggestion rewrites member accesses; destructuring gets no
suggestion, because renaming the key would rename the binding too.
Why This Matters
| Issue | Impact | Solution |
|---|---|---|
| ⚡ Performance | innerText triggers reflow | textContent |
| 🎨 Style awareness | innerText reads computed style | Avoid when not needed |
| 🖥️ SSR support | innerText requires layout | textContent works always |
Examples
❌ Incorrect
const text = element.innerText; // Triggers reflow
element.innerText = 'Hello'; // Slower✅ Correct
const text = element.textContent; // No reflow
element.textContent = 'Hello'; // Faster
// Use innerText only when you need style-aware text
// (hidden elements, CSS text-transform, etc.)Configuration Examples
Basic Usage
{
rules: {
'conventions/prefer-dom-node-text-content': 'warn'
}
}Related Rules
prefer-code-point- Unicode handling
Further Reading
- textContent - MDN - MDN reference
- Difference between textContent and innerText - Comparison
Known False Negatives
- The rule is not type-aware: it does not check that the receiver is a DOM
element, so a non-DOM object with an
innerTextproperty is reported too, and an element reached throughReflect.get(el, 'innerText')is not.
Use this rule
npm install -D oxlint-plugin-conventions{ "jsPlugins": ["oxlint-plugin-conventions"], "rules": { "conventions/prefer-dom-node-text-content": "error" }}import { defineConfig } from 'vite-plus';export default defineConfig({ lint: { jsPlugins: ['oxlint-plugin-conventions'], rules: { 'conventions/prefer-dom-node-text-content': 'error', }, },});oxlint-plugin-conventions re-exports eslint-plugin-conventions/oxlint: the same rules under the same IDs, and no ESLint install. Why oxlint.
Did this rule catch something? Star the repo to get new CWE coverage as we ship it — or follow the AI-code-security benchmarks behind these rules.
prefer-dependency-version-strategy
Enforce consistent version strategy (caret ^, tilde ~, exact, range, or any) for package.json dependencies. Pairs with a lockfile-alignment check to give complete dependency validation.
require-data-testid
Require stable data-testid attributes on interactive elements for end-to-end test reliability