Skip to content

Commit

Permalink
feat: improve the key width calculation in key-spacing rule (#16154)
Browse files Browse the repository at this point in the history
  • Loading branch information
snitin315 authored Jul 30, 2022
1 parent c461542 commit 92bf49a
Show file tree
Hide file tree
Showing 3 changed files with 180 additions and 4 deletions.
5 changes: 4 additions & 1 deletion lib/rules/key-spacing.js
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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" }
]
}
]
});

0 comments on commit 92bf49a

Please sign in to comment.