Skip to content
Permalink
Browse files

Fix: object-shorthand ignoreConstructors option (fixes #11595) (#11596)

  • Loading branch information...
overlookmotel authored and platinumazure committed Apr 13, 2019
1 parent eeea893 commit 608a02c60656b96c3219d342eee7e98b55bdd580
Showing with 82 additions and 1 deletion.
  1. +10 −1 lib/rules/object-shorthand.js
  2. +72 −0 tests/lib/rules/object-shorthand.js
@@ -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();
}
@@ -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 }]
@@ -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 }]
@@ -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}",

0 comments on commit 608a02c

Please sign in to comment.
You can’t perform that action at this time.