Skip to content

Commit

Permalink
prefer-dom-node-text-content: Check optional chaining (#2062)
Browse files Browse the repository at this point in the history
  • Loading branch information
fisker committed Apr 7, 2023
1 parent 443999b commit 8d6d007
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 13 deletions.
2 changes: 1 addition & 1 deletion rules/prefer-dom-node-text-content.js
Expand Up @@ -8,7 +8,7 @@ const messages = {
[SUGGESTION]: 'Switch to `.textContent`.',
};

const memberExpressionPropertySelector = `${memberExpressionSelector('innerText')} > .property`;
const memberExpressionPropertySelector = `${memberExpressionSelector({property: 'innerText', includeOptional: true})} > .property`;
const destructuringSelector = [
'ObjectPattern',
' > ',
Expand Down
1 change: 1 addition & 0 deletions test/prefer-dom-node-text-content.mjs
Expand Up @@ -19,6 +19,7 @@ test.snapshot({
],
invalid: [
'node.innerText;',
'node?.innerText;',
'node.innerText = \'foo\';',
'innerText.innerText;',
'const {innerText} = node;',
Expand Down
38 changes: 26 additions & 12 deletions test/snapshots/prefer-dom-node-text-content.mjs.md
Expand Up @@ -19,6 +19,20 @@ Generated by [AVA](https://avajs.dev).
`

## Invalid #2
1 | node?.innerText;

> Error 1/1
`␊
> 1 | node?.innerText;␊
| ^^^^^^^^^ Prefer \`.textContent\` over \`.innerText\`.␊
--------------------------------------------------------------------------------␊
Suggestion 1/1: Switch to \`.textContent\`.␊
1 | node?.textContent;␊
`

## Invalid #3
1 | node.innerText = 'foo';

> Error 1/1
Expand All @@ -32,7 +46,7 @@ Generated by [AVA](https://avajs.dev).
1 | node.textContent = 'foo';␊
`

## Invalid #3
## Invalid #4
1 | innerText.innerText;

> Error 1/1
Expand All @@ -46,7 +60,7 @@ Generated by [AVA](https://avajs.dev).
1 | innerText.textContent;␊
`

## Invalid #4
## Invalid #5
1 | const {innerText} = node;

> Error 1/1
Expand All @@ -60,7 +74,7 @@ Generated by [AVA](https://avajs.dev).
1 | const {textContent: innerText} = node;␊
`

## Invalid #5
## Invalid #6
1 | const {innerText,} = node;

> Error 1/1
Expand All @@ -74,7 +88,7 @@ Generated by [AVA](https://avajs.dev).
1 | const {textContent: innerText,} = node;␊
`

## Invalid #6
## Invalid #7
1 | const {innerText: text} = node;

> Error 1/1
Expand All @@ -88,7 +102,7 @@ Generated by [AVA](https://avajs.dev).
1 | const {textContent: text} = node;␊
`

## Invalid #7
## Invalid #8
1 | const {innerText = "default text"} = node;

> Error 1/1
Expand All @@ -102,7 +116,7 @@ Generated by [AVA](https://avajs.dev).
1 | const {textContent: innerText = "default text"} = node;␊
`

## Invalid #8
## Invalid #9
1 | const {innerText: text = "default text"} = node;

> Error 1/1
Expand All @@ -116,7 +130,7 @@ Generated by [AVA](https://avajs.dev).
1 | const {textContent: text = "default text"} = node;␊
`

## Invalid #9
## Invalid #10
1 | ({innerText} = node);

> Error 1/1
Expand All @@ -130,7 +144,7 @@ Generated by [AVA](https://avajs.dev).
1 | ({textContent: innerText} = node);␊
`

## Invalid #10
## Invalid #11
1 | ({innerText: text} = node);

> Error 1/1
Expand All @@ -144,7 +158,7 @@ Generated by [AVA](https://avajs.dev).
1 | ({textContent: text} = node);␊
`

## Invalid #11
## Invalid #12
1 | ({innerText = "default text"} = node);

> Error 1/1
Expand All @@ -158,7 +172,7 @@ Generated by [AVA](https://avajs.dev).
1 | ({textContent: innerText = "default text"} = node);␊
`

## Invalid #12
## Invalid #13
1 | ({innerText: text = "default text"} = node);

> Error 1/1
Expand All @@ -172,7 +186,7 @@ Generated by [AVA](https://avajs.dev).
1 | ({textContent: text = "default text"} = node);␊
`

## Invalid #13
## Invalid #14
1 | function foo({innerText}) {return innerText}

> Error 1/1
Expand All @@ -186,7 +200,7 @@ Generated by [AVA](https://avajs.dev).
1 | function foo({textContent: innerText}) {return innerText}␊
`

## Invalid #14
## Invalid #15
1 | for (const [{innerText}] of elements);

> Error 1/1
Expand Down
Binary file modified test/snapshots/prefer-dom-node-text-content.mjs.snap
Binary file not shown.

0 comments on commit 8d6d007

Please sign in to comment.