Skip to content

Commit

Permalink
Update: Option to ignore constructor Fns object-shorthand (fixes esli…
Browse files Browse the repository at this point in the history
  • Loading branch information
kaicataldo committed Nov 30, 2015
1 parent 931e0a2 commit 5af12e5
Show file tree
Hide file tree
Showing 3 changed files with 110 additions and 10 deletions.
23 changes: 22 additions & 1 deletion docs/rules/object-shorthand.md
Expand Up @@ -93,7 +93,28 @@ The rule takes an option which specifies when it should be applied. It can be se
You can set the option in configuration like this:

```json
"object-shorthand": [2, "always"]
{
"object-shorthand": [2, "always"]
}
```

While set to "always" or "methods", constructor functions can be ignored with the optional parameter "ignoreConstructors" enabled.

```json
{
"object-shorthand": [2, "always", { "ignoreConstructors": true }]
}
```

The following will *not* warn when "ignoreConstructors" is enabled:

```js
/*eslint object-shorthand: [2, "always", { "ignoreConstructors": true ]}*/
/*eslint-env es6*/

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

## When Not To Use It
Expand Down
88 changes: 81 additions & 7 deletions lib/rules/object-shorthand.js
Expand Up @@ -16,14 +16,31 @@ var OPTIONS = {
//------------------------------------------------------------------------------
// Rule Definition
//------------------------------------------------------------------------------

module.exports = function(context) {

var APPLY = context.options[0] || OPTIONS.always;
var APPLY_TO_METHODS = APPLY === OPTIONS.methods || APPLY === OPTIONS.always;
var APPLY_TO_PROPS = APPLY === OPTIONS.properties || APPLY === OPTIONS.always;
var APPLY_NEVER = APPLY === OPTIONS.never;

var PARAMS = context.options[1] || {};
var IGNORE_CONSTRUCTORS = PARAMS.ignoreConstructors;

//--------------------------------------------------------------------------
// Helpers
//--------------------------------------------------------------------------

/**
* 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) {
var firstChar = name.charAt(0);

return firstChar === firstChar.toUpperCase();
}

//--------------------------------------------------------------------------
// Public
//--------------------------------------------------------------------------
Expand All @@ -50,6 +67,9 @@ module.exports = function(context) {
}

if (node.value.type === "FunctionExpression" && !node.value.id && APPLY_TO_METHODS) {
if (IGNORE_CONSTRUCTORS && isConstructor(node.key.name)) {
return;
}

// {x: function(){}} should be written as {x() {}}
context.report(node, "Expected method shorthand.");
Expand All @@ -67,8 +87,62 @@ module.exports = function(context) {

};

module.exports.schema = [
{
"enum": ["always", "methods", "properties", "never"]
}
];
module.exports.schema = {
"anyOf": [
{
"type": "array",
"items": [
{
"enum": [0, 1, 2]
},
{
"enum": ["always", "methods", "properties", "never"]
}
],
"minItems": 1,
"maxItems": 2
},
{
"type": "array",
"items": [
{
"enum": [0, 1, 2]
},
{
"type": "object",
"properties": {
"ignoreConstructors": {
"type": "boolean"
}
},
"additionalProperties": false
}
],
"minItems": 1,
"maxItems": 2
},
{
"type": "array",
"items": [
{
"enum": [0, 1, 2]
},
{

"enum": ["always", "methods", "properties", "never"]
},
{
"type": "object",
"properties": {
"ignoreConstructors": {
"type": "boolean"
}
},
"additionalProperties": false
}
],
"minItems": 1,
"maxItems": 3
}
]
};
9 changes: 7 additions & 2 deletions tests/lib/rules/object-shorthand.js
Expand Up @@ -80,8 +80,13 @@ ruleTester.run("object-shorthand", rule, {
{ code: "var x = {y}", ecmaFeatures: features, options: ["properties"] },
{ code: "var x = {y: {b}}", ecmaFeatures: features, options: ["properties"] },
{ code: "var x = {a: n, c: d, f: g}", ecmaFeatures: features, options: ["never"] },
{ code: "var x = {a: function(){}, b: {c: d}}", ecmaFeatures: features, options: ["never"] }
{ code: "var x = {a: function(){}, b: {c: d}}", ecmaFeatures: features, options: ["never"] },

// ignoreConstructors
{ code: "var x = {ConstructorFunction: function(){}, a: b}", ecmaFeatures: features, options: [{ "ignoreConstructors": true }] },
{ code: "var x = {a(){}, b: c}", ecmaFeatures: features, options: [{ "ignoreConstructors": true }] },
{ code: "var x = {ConstructorFunction: function(){}, a: b}", ecmaFeatures: features, options: ["methods", { "ignoreConstructors": true }] },
{ code: "var x = {a(){}, b: c}", ecmaFeatures: features, options: ["methods", { "ignoreConstructors": true }] }
],
invalid: [
{ code: "var x = {x: x}", ecmaFeatures: features, errors: [{ message: "Expected property shorthand.", type: "Property" }] },
Expand All @@ -91,6 +96,7 @@ ruleTester.run("object-shorthand", rule, {
{ code: "var x = {y: function() {}}", ecmaFeatures: features, errors: [{ message: "Expected method shorthand.", type: "Property" }] },
{ code: "var x = {y: function*() {}}", ecmaFeatures: features, errors: [{ message: "Expected method shorthand.", type: "Property" }] },
{ code: "var x = {x: y, y: z, a: a}", ecmaFeatures: features, errors: [{ message: "Expected property shorthand.", type: "Property" }] },
{ code: "var x = {ConstructorFunction: function(){}, a: b}", ecmaFeatures: features, errors: [{ message: "Expected method shorthand.", type: "Property" }] },
{ code: "var x = {x: y, y: z, a: function(){}, b() {}}", ecmaFeatures: features, errors: [{ message: "Expected method shorthand.", type: "Property" }] },
{ code: "var x = {x: x, y: function() {}}", ecmaFeatures: features, errors: [{ message: "Expected property shorthand.", type: "Property" }, { message: "Expected method shorthand.", type: "Property" }]},
{ code: "doSomething({x: x})", ecmaFeatures: features, errors: [{ message: "Expected property shorthand.", type: "Property" }] },
Expand All @@ -108,6 +114,5 @@ ruleTester.run("object-shorthand", rule, {
{ code: "var x = {y}", ecmaFeatures: features, errors: [{ message: "Expected longform property syntax.", type: "Property" }], options: ["never"]},
{ code: "var x = {y, a: b, *x(){}}", ecmaFeatures: features, errors: [{ message: "Expected longform property syntax.", type: "Property" }, { message: "Expected longform method syntax.", type: "Property" }], options: ["never"]},
{ code: "var x = {y: {x}}", ecmaFeatures: features, errors: [{ message: "Expected longform property syntax.", type: "Property" }], options: ["never"]}

]
});

0 comments on commit 5af12e5

Please sign in to comment.