-
Notifications
You must be signed in to change notification settings - Fork 4.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Avoid usage of unguarded getRangeAt and add eslint rule (#16212)
* Avoid usage of unguarded getRangeAt and add eslint rule * Address PR comments * ESLint Plugin: Add CHANGELOG entry for avoid-unguarded-get-range-at * ESLint Plugin: Rename avoid-unguarded-get-range-at to no-unguarded-get-range-at
- Loading branch information
Showing
7 changed files
with
68 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
18 changes: 18 additions & 0 deletions
18
packages/eslint-plugin/docs/rules/no-unguarded-get-range-at.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
# Avoid unguarded getRangeAt (no-unguarded-get-range-at) | ||
|
||
Some browsers (e.g. Safari) will throw an error when `getRangeAt` is called and there are no ranges in the selection. | ||
|
||
## Rule details | ||
|
||
Example of **incorrect** code for this rule: | ||
|
||
```js | ||
window.getSelection().getRangeAt( 0 ); | ||
``` | ||
|
||
Example of **correct** code for this rule: | ||
|
||
```js | ||
const selection = window.getSelection(); | ||
const range = selection.rangeCount ? selection.getRangeAt( 0 ) : null; | ||
``` |
29 changes: 29 additions & 0 deletions
29
packages/eslint-plugin/rules/__tests__/no-unguarded-get-range-at.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
/** | ||
* External dependencies | ||
*/ | ||
import { RuleTester } from 'eslint'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import rule from '../no-unguarded-get-range-at'; | ||
|
||
const ruleTester = new RuleTester( { | ||
parserOptions: { | ||
ecmaVersion: 6, | ||
}, | ||
} ); | ||
|
||
ruleTester.run( 'no-unguarded-get-range-at', rule, { | ||
valid: [ | ||
{ | ||
code: `const selection = window.getSelection(); const range = selection.rangeCount ? selection.getRangeAt( 0 ) : null;`, | ||
}, | ||
], | ||
invalid: [ | ||
{ | ||
code: `window.getSelection().getRangeAt( 0 );`, | ||
errors: [ { message: 'Avoid unguarded getRangeAt' } ], | ||
}, | ||
], | ||
} ); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
module.exports = { | ||
meta: { | ||
type: 'problem', | ||
schema: [], | ||
}, | ||
create( context ) { | ||
return { | ||
'CallExpression[callee.object.callee.object.name="window"][callee.object.callee.property.name="getSelection"][callee.property.name="getRangeAt"]'( node ) { | ||
context.report( { | ||
node, | ||
message: 'Avoid unguarded getRangeAt', | ||
} ); | ||
}, | ||
}; | ||
}, | ||
}; |