Skip to content

Bug: key-spacing with character encoded as two code units #15914

@regseb

Description

@regseb

Environment

Node version: v18.2.0
npm version: 8.10.0
Local ESLint version: 8.16.0
Global ESLint version: none
Operating System: Ubuntu 22.04 LTS

What parser are you using?

Default (Espree)

What did you do?

Configuration
{
    "parserOptions": {
        "ecmaVersion": "latest",
        "sourceType": "module"
    },
    "rules": {
        "key-spacing": [2, { "align": "value" }]
    }
}
const foo = {
    "a": "bar",
    "𐌘": "baz" // U+10318 https://en.wikipedia.org/wiki/Old_Italic_(Unicode_block)
};

Object.keys(foo).forEach((key) => {
    console.log(key, key.length, [...key].length);
});
// a 1 1
// 𐌘 2 1

What did you expect to happen?

No error reported.

What actually happened?

$ npx eslint index.js

/home/regseb/testcase/index.js
  2:10  error  Missing space before value for key 'a'  key-spacing

✖ 1 problem (1 error, 0 warnings)
  1 error and 0 warnings potentially fixable with the `--fix` option.

Participation

  • I am willing to submit a pull request for this issue.

Additional comments

JavaScript uses UTF-16 for String:

  • A character before U+FFFF is encoded as one code unit.
  • A character after U+FFFF is encoded as two code units.

String.length counts code units instead of characters.

I think we need to change the function getKeyWidth().

Metadata

Metadata

Assignees

Labels

acceptedThere is consensus among the team that this change meets the criteria for inclusionarchived due to ageThis issue has been archived; please open a new issue for any further discussionbugESLint is working incorrectlyrepro:yesIssues with a reproducible exampleruleRelates to ESLint's core rules

Type

No type

Projects

Status

Complete

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions