Skip to content

Commit fd5d3d3

Browse files
authoredAug 14, 2022
feat: add methodsIgnorePattern option to object-shorthand rule (#16185)
Fixes #15796
1 parent 9f5a752 commit fd5d3d3

File tree

3 files changed

+122
-0
lines changed

3 files changed

+122
-0
lines changed
 

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

+17
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,7 @@ Additionally, the rule takes an optional object configuration:
115115

116116
* `"avoidQuotes": true` indicates that long-form syntax is preferred whenever the object key is a string literal (default: `false`). Note that this option can only be enabled when the string option is set to `"always"`, `"methods"`, or `"properties"`.
117117
* `"ignoreConstructors": true` can be used to prevent the rule from reporting errors for constructor functions. (By default, the rule treats constructors the same way as other functions.) Note that this option can only be enabled when the string option is set to `"always"` or `"methods"`.
118+
* `"methodsIgnorePattern"` (`string`) for methods whose names match this regex pattern, the method shorthand will not be enforced. Note that this option can only be used when the string option is set to `"always"` or `"methods"`.
118119
* `"avoidExplicitReturnArrows": true` indicates that methods are preferred over explicit-return arrow functions for function properties. (By default, the rule allows either of these.) Note that this option can only be enabled when the string option is set to `"always"` or `"methods"`.
119120

120121
### `avoidQuotes`
@@ -179,6 +180,22 @@ var foo = {
179180

180181
:::
181182

183+
### `methodsIgnorePattern`
184+
185+
Example of **correct** code for this rule with the `"always", { "methodsIgnorePattern": "^bar$" }` option:
186+
187+
::: correct
188+
189+
```js
190+
/*eslint object-shorthand: ["error", "always", { "methodsIgnorePattern": "^bar$" }]*/
191+
192+
var foo = {
193+
bar: function() {}
194+
};
195+
```
196+
197+
:::
198+
182199
### `avoidExplicitReturnArrows`
183200

184201
```json

‎lib/rules/object-shorthand.js

+15
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,9 @@ module.exports = {
7878
ignoreConstructors: {
7979
type: "boolean"
8080
},
81+
methodsIgnorePattern: {
82+
type: "string"
83+
},
8184
avoidQuotes: {
8285
type: "boolean"
8386
},
@@ -115,6 +118,9 @@ module.exports = {
115118

116119
const PARAMS = context.options[1] || {};
117120
const IGNORE_CONSTRUCTORS = PARAMS.ignoreConstructors;
121+
const METHODS_IGNORE_PATTERN = PARAMS.methodsIgnorePattern
122+
? new RegExp(PARAMS.methodsIgnorePattern, "u")
123+
: null;
118124
const AVOID_QUOTES = PARAMS.avoidQuotes;
119125
const AVOID_EXPLICIT_RETURN_ARROWS = !!PARAMS.avoidExplicitReturnArrows;
120126
const sourceCode = context.getSourceCode();
@@ -457,6 +463,15 @@ module.exports = {
457463
if (IGNORE_CONSTRUCTORS && node.key.type === "Identifier" && isConstructor(node.key.name)) {
458464
return;
459465
}
466+
467+
if (METHODS_IGNORE_PATTERN) {
468+
const propertyName = astUtils.getStaticPropertyName(node);
469+
470+
if (propertyName !== null && METHODS_IGNORE_PATTERN.test(propertyName)) {
471+
return;
472+
}
473+
}
474+
460475
if (AVOID_QUOTES && isStringLiteral(node.key)) {
461476
return;
462477
}

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

+90
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,52 @@ ruleTester.run("object-shorthand", rule, {
185185
options: ["never"]
186186
},
187187

188+
// methodsIgnorePattern
189+
{
190+
code: "var x = { foo: function() {} }",
191+
options: ["always", { methodsIgnorePattern: "^foo$" }]
192+
},
193+
{
194+
code: "var x = { foo: function() {} }",
195+
options: ["methods", { methodsIgnorePattern: "^foo$" }]
196+
},
197+
{
198+
code: "var x = { foo: function*() {} }",
199+
options: ["always", { methodsIgnorePattern: "^foo$" }]
200+
},
201+
{
202+
code: "var x = { foo: async function() {} }",
203+
options: ["always", { methodsIgnorePattern: "^foo$" }]
204+
},
205+
{
206+
code: "var x = { foo: () => { return 5; } }",
207+
options: ["always", { methodsIgnorePattern: "^foo$", avoidExplicitReturnArrows: true }]
208+
},
209+
{
210+
code: "var x = { 'foo': function() {} }",
211+
options: ["always", { methodsIgnorePattern: "^foo$" }]
212+
},
213+
{
214+
code: "var x = { ['foo']: function() {} }",
215+
options: ["always", { methodsIgnorePattern: "^foo$" }]
216+
},
217+
{
218+
code: "var x = { 123: function() {} }",
219+
options: ["always", { methodsIgnorePattern: "^123$" }]
220+
},
221+
{
222+
code: "var x = { afoob: function() {} }",
223+
options: ["always", { methodsIgnorePattern: "foo" }]
224+
},
225+
{
226+
code: "var x = { afoob: function() {} }",
227+
options: ["always", { methodsIgnorePattern: "^.foo.$" }]
228+
},
229+
{
230+
code: "var x = { '👍foo👍': function() {} }", // this wouldn't pass without the "u" flag
231+
options: ["always", { methodsIgnorePattern: "^.foo.$" }]
232+
},
233+
188234
// avoidQuotes
189235
{
190236
code: "var x = {'a': function(){}}",
@@ -781,6 +827,50 @@ ruleTester.run("object-shorthand", rule, {
781827
errors: [METHOD_ERROR]
782828
},
783829

830+
// methodsIgnorePattern
831+
{
832+
code: "var x = { afoob: function() {} }",
833+
output: "var x = { afoob() {} }",
834+
options: ["always", { methodsIgnorePattern: "^foo$" }],
835+
errors: [METHOD_ERROR]
836+
},
837+
{
838+
code: "var x = { afoob: function() {} }",
839+
output: "var x = { afoob() {} }",
840+
options: ["methods", { methodsIgnorePattern: "^foo$" }],
841+
errors: [METHOD_ERROR]
842+
},
843+
{
844+
code: "var x = { 'afoob': function() {} }",
845+
output: "var x = { 'afoob'() {} }",
846+
options: ["always", { methodsIgnorePattern: "^foo$" }],
847+
errors: [METHOD_ERROR]
848+
},
849+
{
850+
code: "var x = { 1234: function() {} }",
851+
output: "var x = { 1234() {} }",
852+
options: ["always", { methodsIgnorePattern: "^123$" }],
853+
errors: [METHOD_ERROR]
854+
},
855+
{
856+
code: "var x = { bar: function() {} }",
857+
output: "var x = { bar() {} }",
858+
options: ["always", { methodsIgnorePattern: "foo" }],
859+
errors: [METHOD_ERROR]
860+
},
861+
{
862+
code: "var x = { [foo]: function() {} }",
863+
output: "var x = { [foo]() {} }",
864+
options: ["always", { methodsIgnorePattern: "foo" }],
865+
errors: [METHOD_ERROR]
866+
},
867+
{
868+
code: "var x = { foo: foo }", // does not apply to properties
869+
output: "var x = { foo }",
870+
options: ["always", { methodsIgnorePattern: "^foo$" }],
871+
errors: [PROPERTY_ERROR]
872+
},
873+
784874
// avoidQuotes
785875
{
786876
code: "var x = {a: a}",

0 commit comments

Comments
 (0)
Please sign in to comment.