Skip to content

Rule text-encoding-identifier-case: Change utf8 canonical to utf-8 #2600

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion docs/rules/no-array-callback-reference.md
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ array.forEach(function (element) {

```js
function readFile(filename) {
return fs.readFile(filename, 'utf8');
return fs.readFile(filename, 'utf-8');
}

Promise.map(filenames, readFile);
Expand Down
6 changes: 3 additions & 3 deletions docs/rules/prefer-json-parse-buffer.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@ Passing in a buffer may not be performant and is not compatible with TypeScript.
## Fail

```js
const packageJson = JSON.parse(await fs.readFile('./package.json', 'utf8'));
const packageJson = JSON.parse(await fs.readFile('./package.json', 'utf-8'));
```

```js
const promise = fs.readFile('./package.json', {encoding: 'utf8'});
const promise = fs.readFile('./package.json', {encoding: 'utf-8'});
const packageJson = JSON.parse(await promise);
```

Expand All @@ -29,7 +29,7 @@ const packageJson = JSON.parse(await fs.readFile('./package.json'));
```

```js
const promise = fs.readFile('./package.json', {encoding: 'utf8', signal});
const promise = fs.readFile('./package.json', {encoding: 'utf-8', signal});
const packageJson = JSON.parse(await promise);
```

Expand Down
2 changes: 1 addition & 1 deletion docs/rules/prefer-module.md
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ const file = path.join(__dirname, 'foo.js');
```

```js
const content = fs.readFileSync(__filename, 'utf8');
const content = fs.readFileSync(__filename, 'utf-8');
```

```js
Expand Down
12 changes: 8 additions & 4 deletions docs/rules/text-encoding-identifier-case.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<!-- end auto-generated rule header -->
<!-- Do not manually modify this header. Run: `npm run fix:eslint-docs` -->

- Enforce `'utf8'` for [UTF-8](https://en.wikipedia.org/wiki/UTF-8) encoding.
- Enforce `'utf-8'` for [UTF-8](https://en.wikipedia.org/wiki/UTF-8) encoding.
- Enforce `'ascii'` for [ASCII](https://en.wikipedia.org/wiki/ASCII) encoding.

This rule only auto-fix encoding in `fs.readFile()` and `fs.readFileSync()`.
Expand All @@ -23,19 +23,23 @@ await fs.readFile(file, 'ASCII');
```

```js
const string = buffer.toString('utf-8');
const string = buffer.toString('utf8');
```

```js
const string = buffer.toString('UTF8');
```

## Pass

```js
await fs.readFile(file, 'utf8');
await fs.readFile(file, 'utf-8');
```

```js
await fs.readFile(file, 'ascii');
```

```js
const string = buffer.toString('utf8');
const string = buffer.toString('utf-8');
```
21 changes: 3 additions & 18 deletions rules/text-encoding-identifier-case.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ const messages = {
const getReplacement = encoding => {
switch (encoding.toLowerCase()) {
// eslint-disable-next-line unicorn/text-encoding-identifier-case
case 'utf-8':
case 'utf8': {
return 'utf8';
case 'utf8':
case 'utf-8': {
return 'utf-8';
}

case 'ascii': {
Expand Down Expand Up @@ -41,21 +41,6 @@ const create = () => ({
return;
}

if (
// eslint-disable-next-line unicorn/text-encoding-identifier-case
node.value === 'utf-8'
&& node.parent.type === 'JSXAttribute'
&& node.parent.value === node
&& node.parent.name.type === 'JSXIdentifier'
&& node.parent.name.name.toLowerCase() === 'charset'
&& node.parent.parent.type === 'JSXOpeningElement'
&& node.parent.parent.attributes.includes(node.parent)
&& node.parent.parent.name.type === 'JSXIdentifier'
&& node.parent.parent.name.name.toLowerCase() === 'meta'
) {
return;
}

const {raw} = node;
const value = raw.slice(1, -1);

Expand Down
2 changes: 1 addition & 1 deletion scripts/create-rule.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ function checkFiles(ruleId) {
function renderTemplate({source, target, data}) {
const templateFile = path.join(dirname, `template/${source}`);
const targetFile = path.join(ROOT, target);
const templateContent = fs.readFileSync(templateFile, 'utf8');
const templateContent = fs.readFileSync(templateFile, 'utf-8');

const compiled = template(templateContent);
const content = compiled(data);
Expand Down
2 changes: 1 addition & 1 deletion scripts/rename-rule.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ async function renameRule(from, to) {
}

// eslint-disable-next-line no-await-in-loop
let text = await fsAsync.readFile(file, 'utf8');
let text = await fsAsync.readFile(file, 'utf-8');
text = text.replaceAll(from, to);
// eslint-disable-next-line no-await-in-loop
await fsAsync.writeFile(file, text);
Expand Down
2 changes: 1 addition & 1 deletion test/integration/run-eslint.js
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ function getBabelParserConfig(project) {
async function runEslint(project) {
const eslintIgnoreFile = path.join(project.location, '.eslintignore');
const ignore = fs.existsSync(eslintIgnoreFile)
? fs.readFileSync(eslintIgnoreFile, 'utf8').split('\n').filter(line => line && !line.startsWith('#'))
? fs.readFileSync(eslintIgnoreFile, 'utf-8').split('\n').filter(line => line && !line.startsWith('#'))
: [];

const eslint = new ESLint({
Expand Down
2 changes: 1 addition & 1 deletion test/integration/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import runEslint from './run-eslint.js';

if (isCI) {
const CI_CONFIG_FILE = new URL('../../.github/workflows/main.yml', import.meta.url);
const content = fs.readFileSync(CI_CONFIG_FILE, 'utf8');
const content = fs.readFileSync(CI_CONFIG_FILE, 'utf-8');
const config = YAML.parse(content).jobs.integration.strategy.matrix.group;

const expected = [...new Set(allProjects.map(project => String(project.group + 1)))];
Expand Down
6 changes: 3 additions & 3 deletions test/package.js
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ test('Every rule has valid meta.type', t => {
});

test('Every deprecated rules listed in docs/deprecated-rules.md', async t => {
const content = await fsAsync.readFile('docs/deprecated-rules.md', 'utf8');
const content = await fsAsync.readFile('docs/deprecated-rules.md', 'utf-8');
const rulesInMarkdown = new Set(content.match(/(?<=^## ).*?$/gm));

for (const name of deprecatedRules) {
Expand All @@ -125,7 +125,7 @@ test('Every rule file has the appropriate contents', t => {
for (const ruleFile of ruleFiles) {
const ruleName = path.basename(ruleFile, '.js');
const rulePath = path.join('rules', `${ruleName}.js`);
const ruleContents = fs.readFileSync(rulePath, 'utf8');
const ruleContents = fs.readFileSync(rulePath, 'utf-8');

t.true(ruleContents.includes('/** @type {import(\'eslint\').Rule.RuleModule} */'), `${ruleName} includes jsdoc comment for rule type`);
}
Expand All @@ -140,7 +140,7 @@ test('Every rule has a doc with the appropriate content', t => {
}

/// const documentPath = path.join('docs/rules', `${ruleName}.md`);
/// const documentContents = fs.readFileSync(documentPath, 'utf8');
/// const documentContents = fs.readFileSync(documentPath, 'utf-8');

// TODO: Disabled until https://github.com/sindresorhus/eslint-plugin-unicorn/issues/2530 is done.
// Check for examples.
Expand Down
Binary file modified test/snapshots/better-regex.js.snap
Binary file not shown.
Binary file modified test/snapshots/consistent-assert.js.snap
Binary file not shown.
Binary file modified test/snapshots/consistent-date-clone.js.snap
Binary file not shown.
Binary file modified test/snapshots/consistent-empty-array-spread.js.snap
Binary file not shown.
Binary file modified test/snapshots/consistent-existence-index-check.js.snap
Binary file not shown.
Binary file modified test/snapshots/consistent-function-scoping.js.snap
Binary file not shown.
Binary file modified test/snapshots/empty-brace-spaces.js.snap
Binary file not shown.
Binary file modified test/snapshots/error-message.js.snap
Binary file not shown.
Binary file modified test/snapshots/explicit-length-check.js.snap
Binary file not shown.
Binary file modified test/snapshots/filename-case.js.snap
Binary file not shown.
Binary file modified test/snapshots/import-style.js.snap
Binary file not shown.
Binary file modified test/snapshots/new-for-builtins.js.snap
Binary file not shown.
Binary file modified test/snapshots/no-abusive-eslint-disable.js.snap
Binary file not shown.
Binary file modified test/snapshots/no-accessor-recursion.js.snap
Binary file not shown.
Binary file modified test/snapshots/no-anonymous-default-export.js.snap
Binary file not shown.
Binary file modified test/snapshots/no-array-callback-reference.js.snap
Binary file not shown.
Binary file modified test/snapshots/no-array-for-each.js.snap
Binary file not shown.
Binary file modified test/snapshots/no-array-method-this-argument.js.snap
Binary file not shown.
Binary file modified test/snapshots/no-array-push-push.js.snap
Binary file not shown.
Binary file modified test/snapshots/no-await-expression-member.js.snap
Binary file not shown.
Binary file modified test/snapshots/no-await-in-promise-methods.js.snap
Binary file not shown.
Binary file modified test/snapshots/no-console-spaces.js.snap
Binary file not shown.
Binary file modified test/snapshots/no-document-cookie.js.snap
Binary file not shown.
Binary file modified test/snapshots/no-empty-file.js.snap
Binary file not shown.
Binary file modified test/snapshots/no-for-loop.js.snap
Binary file not shown.
Binary file modified test/snapshots/no-hex-escape.js.snap
Binary file not shown.
Binary file modified test/snapshots/no-instanceof-builtins.js.snap
Binary file not shown.
Binary file modified test/snapshots/no-invalid-fetch-options.js.snap
Binary file not shown.
Binary file modified test/snapshots/no-invalid-remove-event-listener.js.snap
Binary file not shown.
Binary file modified test/snapshots/no-length-as-slice-end.js.snap
Binary file not shown.
Binary file modified test/snapshots/no-lonely-if.js.snap
Binary file not shown.
Binary file modified test/snapshots/no-magic-array-flat-depth.js.snap
Binary file not shown.
Binary file modified test/snapshots/no-named-default.js.snap
Binary file not shown.
Binary file modified test/snapshots/no-negated-condition.js.snap
Binary file not shown.
Binary file modified test/snapshots/no-negation-in-equality-check.js.snap
Binary file not shown.
Binary file modified test/snapshots/no-nested-ternary.js.snap
Binary file not shown.
Binary file modified test/snapshots/no-new-array.js.snap
Binary file not shown.
Binary file modified test/snapshots/no-new-buffer.js.snap
Binary file not shown.
Binary file modified test/snapshots/no-null.js.snap
Binary file not shown.
Binary file modified test/snapshots/no-object-as-default-parameter.js.snap
Binary file not shown.
Binary file modified test/snapshots/no-process-exit.js.snap
Binary file not shown.
Binary file modified test/snapshots/no-single-promise-in-promise-methods.js.snap
Binary file not shown.
Binary file modified test/snapshots/no-static-only-class.js.snap
Binary file not shown.
Binary file modified test/snapshots/no-thenable.js.snap
Binary file not shown.
Binary file modified test/snapshots/no-this-assignment.js.snap
Binary file not shown.
Binary file modified test/snapshots/no-typeof-undefined.js.snap
Binary file not shown.
Binary file modified test/snapshots/no-unnecessary-await.js.snap
Binary file not shown.
Binary file modified test/snapshots/no-unreadable-array-destructuring.js.snap
Binary file not shown.
Binary file modified test/snapshots/no-unreadable-iife.js.snap
Binary file not shown.
Binary file modified test/snapshots/no-unused-properties.js.snap
Binary file not shown.
Binary file modified test/snapshots/no-useless-fallback-in-spread.js.snap
Binary file not shown.
Binary file modified test/snapshots/no-useless-length-check.js.snap
Binary file not shown.
Binary file modified test/snapshots/no-useless-spread.js.snap
Binary file not shown.
Binary file modified test/snapshots/no-useless-switch-case.js.snap
Binary file not shown.
Binary file modified test/snapshots/no-useless-undefined.js.snap
Binary file not shown.
Binary file modified test/snapshots/no-zero-fractions.js.snap
Binary file not shown.
Binary file modified test/snapshots/number-literal-case.js.snap
Binary file not shown.
Binary file modified test/snapshots/numeric-separators-style.js.snap
Binary file not shown.
Binary file modified test/snapshots/prefer-add-event-listener.js.snap
Binary file not shown.
Binary file modified test/snapshots/prefer-array-flat-map.js.snap
Binary file not shown.
Binary file modified test/snapshots/prefer-array-flat.js.snap
Binary file not shown.
Binary file modified test/snapshots/prefer-array-index-of.js.snap
Binary file not shown.
Binary file modified test/snapshots/prefer-array-some.js.snap
Binary file not shown.
Binary file modified test/snapshots/prefer-at.js.snap
Binary file not shown.
Binary file modified test/snapshots/prefer-blob-reading-methods.js.snap
Binary file not shown.
Binary file modified test/snapshots/prefer-code-point.js.snap
Binary file not shown.
Binary file modified test/snapshots/prefer-date-now.js.snap
Binary file not shown.
Binary file modified test/snapshots/prefer-dom-node-dataset.js.snap
Binary file not shown.
Binary file modified test/snapshots/prefer-dom-node-remove.js.snap
Binary file not shown.
Binary file modified test/snapshots/prefer-dom-node-text-content.js.snap
Binary file not shown.
Binary file modified test/snapshots/prefer-event-target.js.snap
Binary file not shown.
Binary file modified test/snapshots/prefer-export-from.js.snap
Binary file not shown.
Binary file modified test/snapshots/prefer-global-this.js.snap
Binary file not shown.
Binary file modified test/snapshots/prefer-includes.js.snap
Binary file not shown.
Binary file modified test/snapshots/prefer-json-parse-buffer.js.snap
Binary file not shown.
Binary file modified test/snapshots/prefer-keyboard-event-key.js.snap
Binary file not shown.
Binary file modified test/snapshots/prefer-logical-operator-over-ternary.js.snap
Binary file not shown.
Binary file modified test/snapshots/prefer-math-min-max.js.snap
Binary file not shown.
Binary file modified test/snapshots/prefer-math-trunc.js.snap
Binary file not shown.
Binary file modified test/snapshots/prefer-modern-math-apis.js.snap
Binary file not shown.
Binary file modified test/snapshots/prefer-module.js.snap
Binary file not shown.
Binary file modified test/snapshots/prefer-native-coercion-functions.js.snap
Binary file not shown.
Binary file modified test/snapshots/prefer-negative-index.js.snap
Binary file not shown.
Binary file modified test/snapshots/prefer-node-protocol.js.snap
Binary file not shown.
Binary file modified test/snapshots/prefer-number-properties.js.snap
Binary file not shown.
Binary file modified test/snapshots/prefer-object-from-entries.js.snap
Binary file not shown.
Binary file modified test/snapshots/prefer-optional-catch-binding.js.snap
Binary file not shown.
Binary file modified test/snapshots/prefer-prototype-methods.js.snap
Binary file not shown.
Binary file modified test/snapshots/prefer-query-selector.js.snap
Binary file not shown.
Binary file modified test/snapshots/prefer-regexp-test.js.snap
Binary file not shown.
Binary file modified test/snapshots/prefer-set-has.js.snap
Binary file not shown.
Binary file modified test/snapshots/prefer-set-size.js.snap
Binary file not shown.
Binary file modified test/snapshots/prefer-spread.js.snap
Binary file not shown.
Binary file modified test/snapshots/prefer-string-raw.js.snap
Binary file not shown.
Binary file modified test/snapshots/prefer-string-replace-all.js.snap
Binary file not shown.
Binary file modified test/snapshots/prefer-string-slice.js.snap
Binary file not shown.
Binary file modified test/snapshots/prefer-string-starts-ends-with.js.snap
Binary file not shown.
Binary file modified test/snapshots/prefer-string-trim-start-end.js.snap
Binary file not shown.
Binary file modified test/snapshots/prefer-structured-clone.js.snap
Binary file not shown.
Binary file modified test/snapshots/prefer-switch.js.snap
Binary file not shown.
Binary file modified test/snapshots/prefer-top-level-await.js.snap
Binary file not shown.
Binary file modified test/snapshots/prefer-type-error.js.snap
Binary file not shown.
Binary file modified test/snapshots/relative-url-style.js.snap
Binary file not shown.
Binary file modified test/snapshots/require-array-join-separator.js.snap
Binary file not shown.
Binary file modified test/snapshots/require-number-to-fixed-digits-argument.js.snap
Binary file not shown.
Binary file modified test/snapshots/require-post-message-target-origin.js.snap
Binary file not shown.
Binary file modified test/snapshots/switch-case-braces.js.snap
Binary file not shown.
Binary file modified test/snapshots/template-indent.js.snap
Binary file not shown.
Loading
Loading