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: add methodsIgnorePattern option to object-shorthand rule #16185

Merged
merged 1 commit into from Aug 14, 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
17 changes: 17 additions & 0 deletions docs/src/rules/object-shorthand.md
Expand Up @@ -115,6 +115,7 @@ Additionally, the rule takes an optional object configuration:

* `"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"`.
* `"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"`.
* `"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"`.
* `"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"`.

### `avoidQuotes`
Expand Down Expand Up @@ -179,6 +180,22 @@ var foo = {

:::

### `methodsIgnorePattern`

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

::: correct

```js
/*eslint object-shorthand: ["error", "always", { "methodsIgnorePattern": "^bar$" }]*/

var foo = {
bar: function() {}
};
```

:::

### `avoidExplicitReturnArrows`

```json
Expand Down
15 changes: 15 additions & 0 deletions lib/rules/object-shorthand.js
Expand Up @@ -78,6 +78,9 @@ module.exports = {
ignoreConstructors: {
type: "boolean"
},
methodsIgnorePattern: {
type: "string"
},
avoidQuotes: {
type: "boolean"
},
Expand Down Expand Up @@ -115,6 +118,9 @@ module.exports = {

const PARAMS = context.options[1] || {};
const IGNORE_CONSTRUCTORS = PARAMS.ignoreConstructors;
const METHODS_IGNORE_PATTERN = PARAMS.methodsIgnorePattern
? new RegExp(PARAMS.methodsIgnorePattern, "u")
: null;
const AVOID_QUOTES = PARAMS.avoidQuotes;
const AVOID_EXPLICIT_RETURN_ARROWS = !!PARAMS.avoidExplicitReturnArrows;
const sourceCode = context.getSourceCode();
Expand Down Expand Up @@ -457,6 +463,15 @@ module.exports = {
if (IGNORE_CONSTRUCTORS && node.key.type === "Identifier" && isConstructor(node.key.name)) {
return;
}

if (METHODS_IGNORE_PATTERN) {
const propertyName = astUtils.getStaticPropertyName(node);

if (propertyName !== null && METHODS_IGNORE_PATTERN.test(propertyName)) {
return;
}
}

if (AVOID_QUOTES && isStringLiteral(node.key)) {
return;
}
Expand Down
90 changes: 90 additions & 0 deletions tests/lib/rules/object-shorthand.js
Expand Up @@ -185,6 +185,52 @@ ruleTester.run("object-shorthand", rule, {
options: ["never"]
},

// methodsIgnorePattern
{
code: "var x = { foo: function() {} }",
options: ["always", { methodsIgnorePattern: "^foo$" }]
},
{
code: "var x = { foo: function() {} }",
options: ["methods", { methodsIgnorePattern: "^foo$" }]
},
{
code: "var x = { foo: function*() {} }",
options: ["always", { methodsIgnorePattern: "^foo$" }]
},
{
code: "var x = { foo: async function() {} }",
options: ["always", { methodsIgnorePattern: "^foo$" }]
},
{
code: "var x = { foo: () => { return 5; } }",
options: ["always", { methodsIgnorePattern: "^foo$", avoidExplicitReturnArrows: true }]
},
{
code: "var x = { 'foo': function() {} }",
options: ["always", { methodsIgnorePattern: "^foo$" }]
},
{
code: "var x = { ['foo']: function() {} }",
options: ["always", { methodsIgnorePattern: "^foo$" }]
},
{
code: "var x = { 123: function() {} }",
options: ["always", { methodsIgnorePattern: "^123$" }]
},
{
code: "var x = { afoob: function() {} }",
options: ["always", { methodsIgnorePattern: "foo" }]
},
{
code: "var x = { afoob: function() {} }",
options: ["always", { methodsIgnorePattern: "^.foo.$" }]
},
{
code: "var x = { '👍foo👍': function() {} }", // this wouldn't pass without the "u" flag
options: ["always", { methodsIgnorePattern: "^.foo.$" }]
},

// avoidQuotes
{
code: "var x = {'a': function(){}}",
Expand Down Expand Up @@ -781,6 +827,50 @@ ruleTester.run("object-shorthand", rule, {
errors: [METHOD_ERROR]
},

// methodsIgnorePattern
{
code: "var x = { afoob: function() {} }",
output: "var x = { afoob() {} }",
options: ["always", { methodsIgnorePattern: "^foo$" }],
errors: [METHOD_ERROR]
},
{
code: "var x = { afoob: function() {} }",
output: "var x = { afoob() {} }",
options: ["methods", { methodsIgnorePattern: "^foo$" }],
errors: [METHOD_ERROR]
},
{
code: "var x = { 'afoob': function() {} }",
output: "var x = { 'afoob'() {} }",
options: ["always", { methodsIgnorePattern: "^foo$" }],
errors: [METHOD_ERROR]
},
{
code: "var x = { 1234: function() {} }",
output: "var x = { 1234() {} }",
options: ["always", { methodsIgnorePattern: "^123$" }],
errors: [METHOD_ERROR]
},
{
code: "var x = { bar: function() {} }",
output: "var x = { bar() {} }",
options: ["always", { methodsIgnorePattern: "foo" }],
errors: [METHOD_ERROR]
},
{
code: "var x = { [foo]: function() {} }",
output: "var x = { [foo]() {} }",
options: ["always", { methodsIgnorePattern: "foo" }],
errors: [METHOD_ERROR]
},
{
code: "var x = { foo: foo }", // does not apply to properties
output: "var x = { foo }",
options: ["always", { methodsIgnorePattern: "^foo$" }],
errors: [PROPERTY_ERROR]
},

// avoidQuotes
{
code: "var x = {a: a}",
Expand Down