-
-
Notifications
You must be signed in to change notification settings - Fork 389
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
179 additions
and
0 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
# Prefer `String#codePointAt(…)` over `String#charCodeAt(…)` and `String.fromCodePoint(…)` over `String.fromCharCode(…)` | ||
|
||
Unicode is better supported in [`String#codePointAt()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/codePointAt) and [`String.fromCodePoint()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/fromCodePoint). | ||
|
||
- [Different between `String.fromCodePoint()` and `String.fromCharCode()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/fromCodePoint#compared_to_fromcharcode) | ||
|
||
## Fail | ||
|
||
```js | ||
const unicorn = '🦄'.charCodeAt(0).toString(16); | ||
``` | ||
|
||
```js | ||
const unicorn = String.fromCharCode(0x1f984); | ||
``` | ||
|
||
## Pass | ||
|
||
```js | ||
const unicorn = '🦄'.codePointAt(0).toString(16); | ||
``` | ||
|
||
```js | ||
const unicorn = String.fromCodePoint(0x1f984); | ||
``` |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
'use strict'; | ||
const {methodCallSelector} = require('./selectors/index.js'); | ||
|
||
const messages = { | ||
'error/charCodeAt': 'Prefer `String#codePointAt()` over `String#charCodeAt()`.', | ||
'error/fromCharCode': 'Prefer `String.fromCodePoint()` over `String.fromCharCode()`.', | ||
'suggestion/charCodeAt': 'Use `String#codePointAt()`.', | ||
'suggestion/fromCharCode': 'Use `String.fromCodePoint()`.', | ||
}; | ||
|
||
const cases = [ | ||
{ | ||
selector: methodCallSelector('charCodeAt'), | ||
replacement: 'codePointAt', | ||
}, | ||
{ | ||
selector: methodCallSelector({object: 'String', method: 'fromCharCode'}), | ||
replacement: 'fromCodePoint', | ||
}, | ||
]; | ||
|
||
const create = () => Object.fromEntries( | ||
cases.map(({selector, replacement}) => [ | ||
selector, | ||
node => { | ||
const method = node.callee.property; | ||
const methodName = method.name; | ||
const fix = fixer => fixer.replaceText(method, replacement); | ||
|
||
return { | ||
node: method, | ||
messageId: `error/${methodName}`, | ||
suggest: [ | ||
{ | ||
messageId: `suggestion/${methodName}`, | ||
fix, | ||
}, | ||
], | ||
}; | ||
}, | ||
]), | ||
); | ||
|
||
module.exports = { | ||
create, | ||
meta: { | ||
type: 'suggestion', | ||
docs: { | ||
description: 'Prefer `String#codePointAt(…)` over `String#charCodeAt(…)` and `String.fromCodePoint(…)` over `String.fromCharCode(…)`.', | ||
}, | ||
hasSuggestions: true, | ||
schema: [], | ||
messages, | ||
}, | ||
}; |
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,35 @@ | ||
import {getTester} from './utils/test.mjs'; | ||
|
||
const {test} = getTester(import.meta); | ||
|
||
test.snapshot({ | ||
valid: [ | ||
'"🦄".codePointAt(0)', | ||
'foo.charCodeAt', | ||
'new foo.charCodeAt', | ||
'charCodeAt(0)', | ||
'foo.charCodeAt?.(0)', | ||
'foo?.charCodeAt(0)', | ||
'foo[charCodeAt](0)', | ||
'foo["charCodeAt"](0)', | ||
'foo.notCharCodeAt(0)', | ||
|
||
'String.fromCodePoint(0x1f984)', | ||
'String.fromCodePoint', | ||
'new String.fromCodePoint', | ||
'fromCodePoint(foo)', | ||
'String.fromCodePoint?.(foo)', | ||
'String?.fromCodePoint(foo)', | ||
'window.String.fromCodePoint(foo)', | ||
'String[fromCodePoint](foo)', | ||
'String["fromCodePoint"](foo)', | ||
'String.notFromCodePoint(foo)', | ||
'NotString.fromCodePoint(foo)', | ||
], | ||
invalid: [ | ||
'string.charCodeAt(index)', | ||
'(( (( string )).charCodeAt( ((index)), )))', | ||
'String.fromCharCode( code )', | ||
'(( (( String )).fromCharCode( ((code)), ) ))', | ||
], | ||
}); |
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,61 @@ | ||
# Snapshot report for `test/prefer-code-point.mjs` | ||
|
||
The actual snapshot is saved in `prefer-code-point.mjs.snap`. | ||
|
||
Generated by [AVA](https://avajs.dev). | ||
|
||
## Invalid #1 | ||
1 | string.charCodeAt(index) | ||
|
||
> Error 1/1 | ||
`␊ | ||
> 1 | string.charCodeAt(index)␊ | ||
| ^^^^^^^^^^ Prefer \`String#codePointAt()\` over \`String#charCodeAt()\`.␊ | ||
␊ | ||
--------------------------------------------------------------------------------␊ | ||
Suggestion 1/1: Use \`String#codePointAt()\`.␊ | ||
1 | string.codePointAt(index)␊ | ||
` | ||
|
||
## Invalid #2 | ||
1 | (( (( string )).charCodeAt( ((index)), ))) | ||
|
||
> Error 1/1 | ||
`␊ | ||
> 1 | (( (( string )).charCodeAt( ((index)), )))␊ | ||
| ^^^^^^^^^^ Prefer \`String#codePointAt()\` over \`String#charCodeAt()\`.␊ | ||
␊ | ||
--------------------------------------------------------------------------------␊ | ||
Suggestion 1/1: Use \`String#codePointAt()\`.␊ | ||
1 | (( (( string )).codePointAt( ((index)), )))␊ | ||
` | ||
|
||
## Invalid #3 | ||
1 | String.fromCharCode( code ) | ||
|
||
> Error 1/1 | ||
`␊ | ||
> 1 | String.fromCharCode( code )␊ | ||
| ^^^^^^^^^^^^ Prefer \`String.fromCodePoint()\` over \`String.fromCharCode()\`.␊ | ||
␊ | ||
--------------------------------------------------------------------------------␊ | ||
Suggestion 1/1: Use \`String.fromCodePoint()\`.␊ | ||
1 | String.fromCodePoint( code )␊ | ||
` | ||
|
||
## Invalid #4 | ||
1 | (( (( String )).fromCharCode( ((code)), ) )) | ||
|
||
> Error 1/1 | ||
`␊ | ||
> 1 | (( (( String )).fromCharCode( ((code)), ) ))␊ | ||
| ^^^^^^^^^^^^ Prefer \`String.fromCodePoint()\` over \`String.fromCharCode()\`.␊ | ||
␊ | ||
--------------------------------------------------------------------------------␊ | ||
Suggestion 1/1: Use \`String.fromCodePoint()\`.␊ | ||
1 | (( (( String )).fromCodePoint( ((code)), ) ))␊ | ||
` |
Binary file not shown.