Skip to content

Commit

Permalink
Fix: object-shorthand ignoreConstructors option (fixes #11595) (#11596)
Browse files Browse the repository at this point in the history
  • Loading branch information
overlookmotel authored and platinumazure committed Apr 13, 2019
1 parent eeea893 commit 608a02c
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 1 deletion.
11 changes: 10 additions & 1 deletion lib/rules/object-shorthand.js
Expand Up @@ -113,14 +113,23 @@ module.exports = {
// Helpers
//--------------------------------------------------------------------------

const CTOR_PREFIX_REGEX = /[^_$0-9]/u;

/**
* Determines if the first character of the name is a capital letter.
* @param {string} name The name of the node to evaluate.
* @returns {boolean} True if the first character of the property name is a capital letter, false if not.
* @private
*/
function isConstructor(name) {
const firstChar = name.charAt(0);
const match = CTOR_PREFIX_REGEX.exec(name);

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

const firstChar = name.charAt(match.index);

return firstChar === firstChar.toUpperCase();
}
Expand Down
72 changes: 72 additions & 0 deletions tests/lib/rules/object-shorthand.js
Expand Up @@ -131,6 +131,22 @@ ruleTester.run("object-shorthand", rule, {
code: "var x = {ConstructorFunction: function(){}, a: b}",
options: ["always", { ignoreConstructors: true }]
},
{
code: "var x = {_ConstructorFunction: function(){}, a: b}",
options: ["always", { ignoreConstructors: true }]
},
{
code: "var x = {$ConstructorFunction: function(){}, a: b}",
options: ["always", { ignoreConstructors: true }]
},
{
code: "var x = {__ConstructorFunction: function(){}, a: b}",
options: ["always", { ignoreConstructors: true }]
},
{
code: "var x = {_0ConstructorFunction: function(){}, a: b}",
options: ["always", { ignoreConstructors: true }]
},
{
code: "var x = {notConstructorFunction(){}, b: c}",
options: ["always", { ignoreConstructors: true }]
Expand All @@ -139,6 +155,22 @@ ruleTester.run("object-shorthand", rule, {
code: "var x = {ConstructorFunction: function(){}, a: b}",
options: ["methods", { ignoreConstructors: true }]
},
{
code: "var x = {_ConstructorFunction: function(){}, a: b}",
options: ["methods", { ignoreConstructors: true }]
},
{
code: "var x = {$ConstructorFunction: function(){}, a: b}",
options: ["methods", { ignoreConstructors: true }]
},
{
code: "var x = {__ConstructorFunction: function(){}, a: b}",
options: ["methods", { ignoreConstructors: true }]
},
{
code: "var x = {_0ConstructorFunction: function(){}, a: b}",
options: ["methods", { ignoreConstructors: true }]
},
{
code: "var x = {notConstructorFunction(){}, b: c}",
options: ["methods", { ignoreConstructors: true }]
Expand Down Expand Up @@ -708,6 +740,46 @@ ruleTester.run("object-shorthand", rule, {
errors: [LONGFORM_PROPERTY_ERROR]
},

// ignoreConstructors
{
code: "var x = {y: function() {}}",
output: "var x = {y() {}}",
options: ["methods", { ignoreConstructors: true }],
errors: [METHOD_ERROR]
},
{

// https://github.com/eslint/eslint/issues/11595
code: "var x = {_y: function() {}}",
output: "var x = {_y() {}}",
options: ["methods", { ignoreConstructors: true }],
errors: [METHOD_ERROR]
},
{

// https://github.com/eslint/eslint/issues/11595
code: "var x = {$y: function() {}}",
output: "var x = {$y() {}}",
options: ["methods", { ignoreConstructors: true }],
errors: [METHOD_ERROR]
},
{

// https://github.com/eslint/eslint/issues/11595
code: "var x = {__y: function() {}}",
output: "var x = {__y() {}}",
options: ["methods", { ignoreConstructors: true }],
errors: [METHOD_ERROR]
},
{

// https://github.com/eslint/eslint/issues/11595
code: "var x = {_0y: function() {}}",
output: "var x = {_0y() {}}",
options: ["methods", { ignoreConstructors: true }],
errors: [METHOD_ERROR]
},

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

0 comments on commit 608a02c

Please sign in to comment.