-
-
Notifications
You must be signed in to change notification settings - Fork 353
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add text-encoding-identifier-case
rule
#1718
Merged
sindresorhus
merged 8 commits into
sindresorhus:main
from
fisker:text-encoding-identifier-case
Feb 9, 2022
Merged
Changes from 7 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
d6aa8c9
Create rule
fisker 5ad676e
Add `text-encoding-identifier-case` rule
fisker f139331
Fix linting
fisker d0d42e5
Linting
fisker 66591e8
Fix `fixable`
fisker a3ac096
Linting
fisker dfd0ee8
Merge branch 'main' into text-encoding-identifier-case
fisker 025fc96
Update description
fisker File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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,39 @@ | ||
# Enforce consistent case style for text encoding identifier | ||
|
||
<!-- Do not manually modify RULE_NOTICE part. Run: `npm run generate-rule-notices` --> | ||
<!-- RULE_NOTICE --> | ||
✅ *This rule is part of the [recommended](https://github.com/sindresorhus/eslint-plugin-unicorn#recommended-config) config.* | ||
|
||
💡 *This rule provides [suggestions](https://eslint.org/docs/developer-guide/working-with-rules#providing-suggestions).* | ||
<!-- /RULE_NOTICE --> | ||
|
||
- Enforce `'utf8'` for [UTF-8](https://en.wikipedia.org/wiki/UTF-8) encoding. | ||
- Enforce `'ascii'` for [ASCII](https://en.wikipedia.org/wiki/ASCII) encoding. | ||
|
||
## Fail | ||
|
||
```js | ||
await fs.readFile(file, 'UTF-8'); | ||
``` | ||
|
||
```js | ||
await fs.readFile(file, 'ASCII'); | ||
``` | ||
|
||
```js | ||
const string = buffer.toString('utf-8'); | ||
``` | ||
|
||
## Pass | ||
|
||
```js | ||
await fs.readFile(file, 'utf8'); | ||
``` | ||
|
||
```js | ||
await fs.readFile(file, 'ascii'); | ||
``` | ||
|
||
```js | ||
const string = buffer.toString('utf8'); | ||
``` |
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,69 @@ | ||
'use strict'; | ||
const {replaceStringLiteral} = require('./fix/index.js'); | ||
|
||
const MESSAGE_ID_ERROR = 'text-encoding-identifier/error'; | ||
const MESSAGE_ID_SUGGESTION = 'text-encoding-identifier/suggestion'; | ||
const messages = { | ||
[MESSAGE_ID_ERROR]: 'Prefer `{{replacement}}` over `{{value}}`.', | ||
[MESSAGE_ID_SUGGESTION]: 'Replace `{{value}}` with `{{replacement}}`.', | ||
}; | ||
|
||
const getReplacement = encoding => { | ||
switch (encoding.toLowerCase()) { | ||
case 'utf8': | ||
case 'utf-8': | ||
return 'utf8'; | ||
case 'ascii': | ||
return 'ascii'; | ||
// No default | ||
} | ||
}; | ||
|
||
/** @param {import('eslint').Rule.RuleContext} context */ | ||
const create = () => ({ | ||
Literal(node) { | ||
if (typeof node.value !== 'string') { | ||
return; | ||
} | ||
|
||
const {raw} = node; | ||
const value = raw.slice(1, -1); | ||
|
||
const replacement = getReplacement(value); | ||
if (!replacement || replacement === value) { | ||
return; | ||
} | ||
|
||
const messageData = { | ||
value, | ||
replacement, | ||
}; | ||
|
||
return { | ||
node, | ||
messageId: MESSAGE_ID_ERROR, | ||
data: messageData, | ||
suggest: [ | ||
{ | ||
messageId: MESSAGE_ID_SUGGESTION, | ||
data: messageData, | ||
/** @param {import('eslint').Rule.RuleFixer} fixer */ | ||
fix: fixer => replaceStringLiteral(fixer, node, replacement), | ||
}, | ||
], | ||
}; | ||
}, | ||
}); | ||
|
||
/** @type {import('eslint').Rule.RuleModule} */ | ||
module.exports = { | ||
create, | ||
meta: { | ||
type: 'suggestion', | ||
docs: { | ||
description: 'Enforce consistent case style for text encoding identifier.', | ||
}, | ||
hasSuggestions: true, | ||
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
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,89 @@ | ||
# Snapshot report for `test/text-encoding-identifier-case.mjs` | ||
|
||
The actual snapshot is saved in `text-encoding-identifier-case.mjs.snap`. | ||
|
||
Generated by [AVA](https://avajs.dev). | ||
|
||
## Invalid #1 | ||
1 | "UTF-8" | ||
|
||
> Error 1/1 | ||
|
||
`␊ | ||
> 1 | "UTF-8"␊ | ||
| ^^^^^^^ Prefer \`utf8\` over \`UTF-8\`.␊ | ||
␊ | ||
--------------------------------------------------------------------------------␊ | ||
Suggestion 1/1: Replace \`UTF-8\` with \`utf8\`.␊ | ||
1 | "utf8"␊ | ||
` | ||
|
||
## Invalid #2 | ||
1 | "utf-8" | ||
|
||
> Error 1/1 | ||
|
||
`␊ | ||
> 1 | "utf-8"␊ | ||
| ^^^^^^^ Prefer \`utf8\` over \`utf-8\`.␊ | ||
␊ | ||
--------------------------------------------------------------------------------␊ | ||
Suggestion 1/1: Replace \`utf-8\` with \`utf8\`.␊ | ||
1 | "utf8"␊ | ||
` | ||
|
||
## Invalid #3 | ||
1 | 'utf-8' | ||
|
||
> Error 1/1 | ||
|
||
`␊ | ||
> 1 | 'utf-8'␊ | ||
| ^^^^^^^ Prefer \`utf8\` over \`utf-8\`.␊ | ||
␊ | ||
--------------------------------------------------------------------------------␊ | ||
Suggestion 1/1: Replace \`utf-8\` with \`utf8\`.␊ | ||
1 | 'utf8'␊ | ||
` | ||
|
||
## Invalid #4 | ||
1 | "Utf8" | ||
|
||
> Error 1/1 | ||
|
||
`␊ | ||
> 1 | "Utf8"␊ | ||
| ^^^^^^ Prefer \`utf8\` over \`Utf8\`.␊ | ||
␊ | ||
--------------------------------------------------------------------------------␊ | ||
Suggestion 1/1: Replace \`Utf8\` with \`utf8\`.␊ | ||
1 | "utf8"␊ | ||
` | ||
|
||
## Invalid #5 | ||
1 | "ASCII" | ||
|
||
> Error 1/1 | ||
|
||
`␊ | ||
> 1 | "ASCII"␊ | ||
| ^^^^^^^ Prefer \`ascii\` over \`ASCII\`.␊ | ||
␊ | ||
--------------------------------------------------------------------------------␊ | ||
Suggestion 1/1: Replace \`ASCII\` with \`ascii\`.␊ | ||
1 | "ascii"␊ | ||
` | ||
|
||
## Invalid #6 | ||
1 | await fs.readFile(file, "UTF-8",) | ||
|
||
> Error 1/1 | ||
|
||
`␊ | ||
> 1 | await fs.readFile(file, "UTF-8",)␊ | ||
| ^^^^^^^ Prefer \`utf8\` over \`UTF-8\`.␊ | ||
␊ | ||
--------------------------------------------------------------------------------␊ | ||
Suggestion 1/1: Replace \`UTF-8\` with \`utf8\`.␊ | ||
1 | await fs.readFile(file, "utf8",)␊ | ||
` |
Binary file not shown.
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,24 @@ | ||
import {getTester} from './utils/test.mjs'; | ||
|
||
const {test} = getTester(import.meta); | ||
|
||
test.snapshot({ | ||
valid: [ | ||
'`UTF-8`', | ||
'"utf8"', | ||
'"utf+8"', | ||
'" utf8 "', | ||
'\'utf8\'', | ||
'"\\u0055tf8"', | ||
'const ASCII = 1', | ||
'const UTF8 = 1', | ||
], | ||
invalid: [ | ||
'"UTF-8"', | ||
'"utf-8"', | ||
'\'utf-8\'', | ||
'"Utf8"', | ||
'"ASCII"', | ||
'await fs.readFile(file, "UTF-8",)', | ||
], | ||
}); |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.