Skip to content
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

feat: improve the key width calculation in key-spacing rule #16154

Merged
merged 5 commits into from Jul 30, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 4 additions & 1 deletion lib/rules/key-spacing.js
Expand Up @@ -9,6 +9,9 @@
//------------------------------------------------------------------------------

const astUtils = require("./utils/ast-utils");
const GraphemeSplitter = require("grapheme-splitter");

const splitter = new GraphemeSplitter();

//------------------------------------------------------------------------------
// Helpers
Expand Down Expand Up @@ -508,7 +511,7 @@ module.exports = {
const startToken = sourceCode.getFirstToken(property);
const endToken = getLastTokenBeforeColon(property.key);

return endToken.range[1] - startToken.range[0];
return splitter.countGraphemes(sourceCode.getText().slice(startToken.range[0], endToken.range[1]));
}

/**
Expand Down
1 change: 1 addition & 0 deletions package.json
Expand Up @@ -73,6 +73,7 @@
"functional-red-black-tree": "^1.0.1",
"glob-parent": "^6.0.1",
"globals": "^13.15.0",
"grapheme-splitter": "^1.0.4",
"ignore": "^5.2.0",
"import-fresh": "^3.0.0",
"imurmurhash": "^0.1.4",
Expand Down
178 changes: 175 additions & 3 deletions tests/lib/rules/key-spacing.js
Expand Up @@ -896,8 +896,82 @@ ruleTester.run("key-spacing", rule, {
on: "value"
}
}]
}
],
},

// https://github.com/eslint/eslint/issues/15914
{
code: `
var foo = {
"a": "bar",
"𐌘": "baz"
};
`,
options: [{
align: {
on: "value"
}
}]
},
{
code: `
var foo = {
"a": "bar",
"Á": "baz",
"o͂": "qux",
"m̅": "xyz",
"ř": "abc"

};
`,
options: [{
align: {
on: "value"
}
}]
},
{
code: `
var foo = {
"🌷": "bar", // 2 code points
"🎁": "baz", // 2 code points
"🇮🇳": "qux", // 4 code points
"🏳️‍🌈": "xyz", // 6 code points
};
`,
options: [{
align: {
on: "value"
}
}]
},
{
code: `
const foo = {
"a": "bar",
[𐌘]: "baz"
};
`,
options: [{
align: {
on: "value"
}
}],
parserOptions: { ecmaVersion: 6 }
},
{
code: `
const foo = {
"abc": "bar",
[ 𐌘 ]: "baz"
};
`,
options: [{
align: {
on: "value"
}
}],
parserOptions: { ecmaVersion: 6 }
}],
invalid: [{
code: "var a ={'key' : value };",
output: "var a ={'key':value };",
Expand Down Expand Up @@ -2203,5 +2277,103 @@ ruleTester.run("key-spacing", rule, {
{ messageId: "missingValue", data: { computed: "", key: "bar" }, line: 3, column: 12, type: "Literal" },
{ messageId: "missingValue", data: { computed: "", key: "baz" }, line: 3, column: 20, type: "Literal" }
]
}]
},
{
code: `
const foo = {
"a": "bar",
[ 𐌘 ]: "baz"
};
`,
output: `
const foo = {
"a": "bar",
[ 𐌘 ]: "baz"
};
`,
options: [{
align: {
on: "value"
}
}],
parserOptions: { ecmaVersion: 6 },
errors: [
{ messageId: "missingValue", data: { computed: "", key: "a" }, line: 3, column: 22, type: "Literal" }
]
},
{
code: `
const foo = {
"a": "bar",
[ 𐌘 ]: "baz"
};
`,
output: `
const foo = {
"a" : "bar",
[ 𐌘 ]: "baz"
};
`,
options: [{
align: {
on: "colon"
}
}],
parserOptions: { ecmaVersion: 6 },
errors: [
{ messageId: "missingKey", data: { computed: "", key: "a" }, line: 3, column: 17, type: "Literal" }
]
},
{
code: `
const foo = {
"a": "bar",
"𐌘": "baz"
};
`,
output: `
const foo = {
"a": "bar",
"𐌘": "baz"
};
`,
options: [{
align: {
on: "value"
}
}],
parserOptions: { ecmaVersion: 6 },
errors: [
{ messageId: "extraValue", data: { computed: "", key: "a" }, line: 3, column: 20, type: "Literal" }
]
},
{
code: `
var foo = {
"🌷": "bar", // 2 code points
"🎁": "baz", // 2 code points
"🇮🇳": "qux", // 4 code points
"🏳️‍🌈": "xyz", // 6 code points
};
`,
output: `
var foo = {
"🌷": "bar", // 2 code points
"🎁": "baz", // 2 code points
"🇮🇳": "qux", // 4 code points
"🏳️‍🌈": "xyz", // 6 code points
};
`,
options: [{
align: {
on: "value"
}
}],
errors: [
{ messageId: "extraValue", data: { computed: "", key: "🌷" }, line: 3, column: 21, type: "Literal" },
{ messageId: "extraValue", data: { computed: "", key: "🎁" }, line: 4, column: 21, type: "Literal" },
{ messageId: "extraValue", data: { computed: "", key: "🇮🇳" }, line: 5, column: 23, type: "Literal" }
]
}
]
});