Skip to content

Commit d136fa4

Browse files
feat: object-shorthand handle quoted properties for ignoreConstructors (#21271)
1 parent 45ad79e commit d136fa4

6 files changed

Lines changed: 222 additions & 12 deletions

File tree

‎docs/src/rules/object-shorthand.md‎

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,40 @@ const foo = {
170170

171171
:::
172172

173+
```json
174+
{
175+
"object-shorthand": ["error", "methods", { "ignoreConstructors": false }]
176+
}
177+
```
178+
179+
Example of **incorrect** code for this rule with the `"methods", { "ignoreConstructors": false }` option:
180+
181+
::: incorrect
182+
183+
```js
184+
/*eslint object-shorthand: ["error", "methods", { "ignoreConstructors": false }]*/
185+
186+
const foo = {
187+
"ConstructorFunction": function() {}
188+
};
189+
```
190+
191+
:::
192+
193+
Example of **correct** code for this rule with the `"methods", { "ignoreConstructors": false }` option:
194+
195+
::: correct
196+
197+
```js
198+
/*eslint object-shorthand: ["error", "methods", { "ignoreConstructors": false }]*/
199+
200+
const foo = {
201+
"ConstructorFunction"() {}
202+
};
203+
```
204+
205+
:::
206+
173207
### methodsIgnorePattern
174208

175209
Example of **correct** code for this rule with the `"always", { "methodsIgnorePattern": "^bar$" }` option:

‎lib/rules/capitalized-comments.js‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,15 @@
99
//------------------------------------------------------------------------------
1010

1111
const astUtils = require("./utils/ast-utils");
12+
const { LETTER_PATTERN, containsLetter } = require("./utils/string-utils");
1213

1314
//------------------------------------------------------------------------------
1415
// Helpers
1516
//------------------------------------------------------------------------------
1617

1718
const DEFAULT_IGNORE_PATTERN = astUtils.COMMENTS_IGNORE_PATTERN,
1819
WHITESPACE = /\s/gu,
19-
MAYBE_URL = /^\s*[^:/?#\s]+:\/\/[^?#]/u, // TODO: Combine w/ max-len pattern?
20-
LETTER_PATTERN = /\p{L}/u;
20+
MAYBE_URL = /^\s*[^:/?#\s]+:\/\/[^?#]/u; // TODO: Combine w/ max-len pattern?
2121

2222
/*
2323
* Base schema body for defining the basic capitalization rule, ignorePattern,
@@ -253,7 +253,7 @@ module.exports = {
253253
// Get the first Unicode character (1 or 2 code units).
254254
const [firstWordChar] = commentWordCharsOnly;
255255

256-
if (!LETTER_PATTERN.test(firstWordChar)) {
256+
if (!containsLetter(firstWordChar)) {
257257
return true;
258258
}
259259

‎lib/rules/object-shorthand.js‎

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,13 @@ const OPTIONS = {
1818
// Requirements
1919
//------------------------------------------------------------------------------
2020
const astUtils = require("./utils/ast-utils");
21-
21+
const { containsLetter } = require("./utils/string-utils");
2222
//--------------------------------------------------------------------------
2323
// Helpers
2424
//--------------------------------------------------------------------------
25-
const CTOR_PREFIX_REGEX = /[^_$0-9]/u;
25+
// Skip non-letter characters used as an identifier prefix: '$', '_' and digits.
26+
// https://tc39.es/ecma262/#prod-IdentifierPart
27+
const IDENTIFIER_PREFIX_END_REGEX = /[^_$0-9]/u;
2628
const JSDOC_COMMENT_REGEX = /^\s*\*/u;
2729

2830
/**
@@ -32,14 +34,18 @@ const JSDOC_COMMENT_REGEX = /^\s*\*/u;
3234
* @private
3335
*/
3436
function isConstructor(name) {
35-
const match = CTOR_PREFIX_REGEX.exec(name);
37+
const match = IDENTIFIER_PREFIX_END_REGEX.exec(name);
3638

3739
// Not a constructor if name has no characters apart from '_', '$' and digits e.g. '_', '$$', '_8'
3840
if (!match) {
3941
return false;
4042
}
4143

42-
const firstChar = name.charAt(match.index);
44+
const firstChar = match[0];
45+
46+
if (!containsLetter(firstChar)) {
47+
return false;
48+
}
4349

4450
return firstChar === firstChar.toUpperCase();
4551
}
@@ -566,18 +572,16 @@ module.exports = {
566572
(node.value.type === "FunctionExpression" ||
567573
node.value.type === "ArrowFunctionExpression")
568574
) {
575+
const propertyName = astUtils.getStaticPropertyName(node);
569576
if (
570577
IGNORE_CONSTRUCTORS &&
571-
node.key.type === "Identifier" &&
572-
isConstructor(node.key.name)
578+
propertyName !== null &&
579+
isConstructor(propertyName)
573580
) {
574581
return;
575582
}
576583

577584
if (METHODS_IGNORE_PATTERN) {
578-
const propertyName =
579-
astUtils.getStaticPropertyName(node);
580-
581585
if (
582586
propertyName !== null &&
583587
METHODS_IGNORE_PATTERN.test(propertyName)

‎lib/rules/utils/string-utils.js‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/**
2+
* @fileoverview Common utils for strings.
3+
*/
4+
"use strict";
5+
//------------------------------------------------------------------------------
6+
// Public Interface
7+
//------------------------------------------------------------------------------
8+
const LETTER_PATTERN = /\p{L}/u;
9+
/**
10+
* Checks if the given string contains a letter.
11+
* @param {string} string The string to test.
12+
* @returns {boolean} `true` if the string contains a letter.
13+
*/
14+
function containsLetter(string) {
15+
return LETTER_PATTERN.test(string);
16+
}
17+
18+
module.exports = {
19+
LETTER_PATTERN,
20+
containsLetter,
21+
};

‎tests/lib/rules/object-shorthand.js‎

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,10 +162,36 @@ ruleTester.run("object-shorthand", rule, {
162162
code: "var x = {_0ConstructorFunction: function(){}, a: b}",
163163
options: ["always", { ignoreConstructors: true }],
164164
},
165+
{
166+
// https://github.com/eslint/eslint/issues/21267
167+
code: 'var x = {"ConstructorFunction": function(){}, a: b}',
168+
options: ["always", { ignoreConstructors: true }],
169+
},
170+
{
171+
// https://github.com/eslint/eslint/issues/21267
172+
code: "var x = {'ConstructorFunction': function(){}, a: b}",
173+
options: ["always", { ignoreConstructors: true }],
174+
},
175+
{
176+
code: "var x = {𐐀Foo: function() {}}",
177+
options: ["always", { ignoreConstructors: true }],
178+
},
165179
{
166180
code: "var x = {notConstructorFunction(){}, b: c}",
167181
options: ["always", { ignoreConstructors: true }],
168182
},
183+
{
184+
code: "var x = {[notConstructorFunction](){}, b: c}",
185+
options: ["always", { ignoreConstructors: true }],
186+
},
187+
{
188+
code: "var x = {[NotConstructorFunction](){}, b: c}",
189+
options: ["always", { ignoreConstructors: true }],
190+
},
191+
{
192+
code: "var x = {'.NotConstructorFunction'(){}, b: c}",
193+
options: ["always", { ignoreConstructors: true }],
194+
},
169195
{
170196
code: "var x = {ConstructorFunction: function(){}, a: b}",
171197
options: ["methods", { ignoreConstructors: true }],
@@ -186,18 +212,58 @@ ruleTester.run("object-shorthand", rule, {
186212
code: "var x = {_0ConstructorFunction: function(){}, a: b}",
187213
options: ["methods", { ignoreConstructors: true }],
188214
},
215+
{
216+
code: 'var x = {"ConstructorFunction": function(){}, a: b}',
217+
options: ["methods", { ignoreConstructors: true }],
218+
},
219+
{
220+
code: "var x = {'ConstructorFunction': function(){}, a: b}",
221+
options: ["methods", { ignoreConstructors: true }],
222+
},
189223
{
190224
code: "var x = {notConstructorFunction(){}, b: c}",
191225
options: ["methods", { ignoreConstructors: true }],
192226
},
227+
{
228+
code: "var x = {[notConstructorFunction](){}, b: c}",
229+
options: ["methods", { ignoreConstructors: true }],
230+
},
231+
{
232+
code: "var x = {[NotConstructorFunction](){}, b: c}",
233+
options: ["methods", { ignoreConstructors: true }],
234+
},
235+
{
236+
code: "var x = {'.NotConstructorFunction'(){}, b: c}",
237+
options: ["methods", { ignoreConstructors: true }],
238+
},
193239
{
194240
code: "var x = {ConstructorFunction: function(){}, a: b}",
195241
options: ["never"],
196242
},
243+
{
244+
code: 'var x = {"ConstructorFunction": function(){}, a: b}',
245+
options: ["never"],
246+
},
247+
{
248+
code: "var x = {'ConstructorFunction': function(){}, a: b}",
249+
options: ["never"],
250+
},
197251
{
198252
code: "var x = {notConstructorFunction: function(){}, b: c}",
199253
options: ["never"],
200254
},
255+
{
256+
code: "var x = {[notConstructorFunction]: function(){}, b: c}",
257+
options: ["never"],
258+
},
259+
{
260+
code: "var x = {[NotConstructorFunction]: function(){}, b: c}",
261+
options: ["never"],
262+
},
263+
{
264+
code: "var x = {'.NotConstructorFunction': function(){}, b: c}",
265+
options: ["never"],
266+
},
201267

202268
// methodsIgnorePattern
203269
{
@@ -910,6 +976,40 @@ ruleTester.run("object-shorthand", rule, {
910976
options: ["methods", { ignoreConstructors: true }],
911977
errors: [METHOD_ERROR],
912978
},
979+
// https://github.com/eslint/eslint/pull/21271#discussion_r3889188639
980+
{
981+
code: "var x = {[y]: function() {}}",
982+
output: "var x = {[y]() {}}",
983+
options: ["methods", { ignoreConstructors: true }],
984+
errors: [METHOD_ERROR],
985+
},
986+
// https://github.com/eslint/eslint/pull/21271#discussion_r3889188639
987+
{
988+
code: "var x = {[Y]: function() {}}",
989+
output: "var x = {[Y]() {}}",
990+
options: ["methods", { ignoreConstructors: true }],
991+
errors: [METHOD_ERROR],
992+
},
993+
{
994+
code: "var x = {'.Y': function() {}}",
995+
output: "var x = {'.Y'() {}}",
996+
options: ["methods", { ignoreConstructors: true }],
997+
errors: [METHOD_ERROR],
998+
},
999+
{
1000+
// https://github.com/eslint/eslint/issues/21267
1001+
code: 'var x = {"ConstructorFunction": function() {}, a: b}',
1002+
output: 'var x = {"ConstructorFunction"() {}, a: b}',
1003+
options: ["methods", { ignoreConstructors: false }],
1004+
errors: [METHOD_ERROR],
1005+
},
1006+
{
1007+
// https://github.com/eslint/eslint/issues/21267
1008+
code: "var x = {'ConstructorFunction': function() {}, a: b}",
1009+
output: "var x = {'ConstructorFunction'() {}, a: b}",
1010+
options: ["methods", { ignoreConstructors: false }],
1011+
errors: [METHOD_ERROR],
1012+
},
9131013

9141014
// methodsIgnorePattern
9151015
{
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/**
2+
* @fileoverview Tests for string utility.
3+
*/
4+
"use strict";
5+
//------------------------------------------------------------------------------
6+
// Requirements
7+
//------------------------------------------------------------------------------
8+
const assert = require("chai").assert;
9+
const { containsLetter } = require("../../../../lib/rules/utils/string-utils");
10+
//------------------------------------------------------------------------------
11+
// Tests
12+
//------------------------------------------------------------------------------
13+
describe("string-utils", () => {
14+
describe("containsLetter", () => {
15+
it("should return true for a letter", () => {
16+
assert.strictEqual(containsLetter("d"), true);
17+
});
18+
19+
it("should return true for a uppercase letter", () => {
20+
assert.strictEqual(containsLetter("D"), true);
21+
});
22+
23+
it("should return true for a letter from a non-Latin alphabet", () => {
24+
assert.strictEqual(containsLetter("я"), true);
25+
});
26+
27+
it("should return true for an uppercase letter from a non-Latin alphabet", () => {
28+
assert.strictEqual(containsLetter("Я"), true);
29+
});
30+
31+
it("should return true for an astral Unicode letter", () => {
32+
assert.strictEqual(containsLetter("𐐷"), true);
33+
});
34+
35+
it("should return false for a digit", () => {
36+
assert.strictEqual(containsLetter("1"), false);
37+
});
38+
39+
it("should return false for punctuation", () => {
40+
assert.strictEqual(containsLetter("!"), false);
41+
});
42+
43+
it("should return false for an empty string", () => {
44+
assert.strictEqual(containsLetter(""), false);
45+
});
46+
47+
it("should return true when a string contains a letter", () => {
48+
assert.strictEqual(containsLetter("1学2"), true);
49+
});
50+
});
51+
});

0 commit comments

Comments
 (0)