Skip to content
This repository has been archived by the owner on Mar 23, 2024. It is now read-only.

requireMatchingFunctionName: requires function names to match member and property names #850

Closed
wants to merge 1 commit into from

Conversation

xaka
Copy link
Contributor

@xaka xaka commented Dec 22, 2014

Example of member name mismatch:

var test = {};
test.foo = function bar() {};

Example of property name mismatch:

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

Fixes #846
Closes #850

@xaka xaka force-pushed the require-function-name-match branch from e491f51 to 083c63a Compare December 22, 2014 00:55
xaka added a commit to xaka/node-jscs that referenced this pull request Dec 22, 2014
… function names

Example of member name mismatch:

```js
function Test() {};
Test.prototype.foo = function bar() {};
```

Example of property name mismatch:

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

Fixes jscs-dev#846
Closes jscs-dev#850
@coveralls
Copy link

Coverage Status

Coverage increased (+0.03%) when pulling e491f51 on xaka:require-function-name-match into 9aae17e on jscs-dev:master.

@coveralls
Copy link

Coverage Status

Coverage increased (+0.03%) when pulling 083c63a on xaka:require-function-name-match into 9aae17e on jscs-dev:master.

@xaka xaka force-pushed the require-function-name-match branch from 083c63a to 6c57462 Compare December 22, 2014 01:02
xaka added a commit to xaka/node-jscs that referenced this pull request Dec 22, 2014
… function names

Example of member name mismatch:

```js
function Test() {};
Test.prototype.foo = function bar() {};
```

Example of property name mismatch:

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

Fixes jscs-dev#846
Closes jscs-dev#850
@xaka xaka force-pushed the require-function-name-match branch from 6c57462 to f54d69a Compare December 22, 2014 01:03
xaka added a commit to xaka/node-jscs that referenced this pull request Dec 22, 2014
… function names

Example of member name mismatch:

```js
function Test() {};
Test.prototype.foo = function bar() {};
```

Example of property name mismatch:

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

Fixes jscs-dev#846
Closes jscs-dev#850
@coveralls
Copy link

Coverage Status

Coverage increased (+0.03%) when pulling f54d69a on xaka:require-function-name-match into 9aae17e on jscs-dev:master.

@coveralls
Copy link

Coverage Status

Coverage increased (+0.03%) when pulling f54d69a on xaka:require-function-name-match into 9aae17e on jscs-dev:master.


function checkForMember(assignment, errors) {
// anonymous function OR names mismatch
if (!assignment.right.id || (assignment.left.property.name !== assignment.right.id.name)) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the first clause mandates too much. This rule now enforces disallowAnonymousFunctions and requireFunctionNameMatch.

What do you think about this rule enforcing that if a function expression is named, its name must match the assignment property or member?

@mrjoelkemp
Copy link
Member

Great work @xaka! This is a very clear implementation. I left one comment on the scope of what this rule handles. Once that's addressed, it looks good to land; I'll wait for another reviewer to give the +1 before merging.

@mikesherov
Copy link
Contributor

requireMatchingFunctionName would be more English readable IMO. Let's rename before we land.

I'm also concerned that this rule is bloat, which isn't to say it's not a good rule, but now that we have a sane plugin infra we should be more discriminating.

@mikesherov
Copy link
Contributor

Also, what if the property name is a reserved word. Named function expressions can't have those as function names while properties can, right?

@xaka
Copy link
Contributor Author

xaka commented Dec 22, 2014

  1. The problem with requireAnonymousFunctions and disallowAnonymousFunctions is that it's either everything or nothing, and there is no middle ground. For me personally there should be no anonymous functions allowed in function expressions, but at the same time I don't care much about anonymous functions for callbacks and events handling. I think the solution would be another rule like disallowAnonymousFunctionExpressions. Would you agree? I'll update the code and remove any checks for anonymous functions for now.
  2. requireMatchingFunctionName sounds even better, will do
  3. Good point about reserved words. What would be the appropriate solution here? Do we have a list of reserved words available at code level so when they are in use, we'll be less restrictive? If so, what about allowing to use $ as a prefix when reserved word is detected?

Thanks for constructive feedback!

@xaka xaka force-pushed the require-function-name-match branch from f54d69a to b636959 Compare December 22, 2014 16:53
xaka added a commit to xaka/node-jscs that referenced this pull request Dec 22, 2014
…tch function names

Example of member name mismatch:

```js
function Test() {};
Test.prototype.foo = function bar() {};
```

Example of property name mismatch:

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

Fixes jscs-dev#846
Closes jscs-dev#850
@xaka xaka changed the title requireFunctionNameMatch: requires member and property names to match function names requireMatchingFunctionName: requires member and property names to match function names Dec 22, 2014
@coveralls
Copy link

Coverage Status

Coverage increased (+0.04%) when pulling b636959 on xaka:require-function-name-match into 9aae17e on jscs-dev:master.

@xaka xaka force-pushed the require-function-name-match branch from b636959 to 6422e23 Compare December 22, 2014 16:56
xaka added a commit to xaka/node-jscs that referenced this pull request Dec 22, 2014
…tch function names

Example of member name mismatch:

```js
function Test() {};
Test.prototype.foo = function bar() {};
```

Example of property name mismatch:

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

Fixes jscs-dev#846
Closes jscs-dev#850
@xaka xaka force-pushed the require-function-name-match branch from 6422e23 to 79a6499 Compare December 22, 2014 16:57
xaka added a commit to xaka/node-jscs that referenced this pull request Dec 22, 2014
…tch function names

Example of member name mismatch:

```js
function Test() {};
Test.prototype.foo = function bar() {};
```

Example of property name mismatch:

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

Fixes jscs-dev#846
Closes jscs-dev#850
@xaka
Copy link
Contributor Author

xaka commented Dec 22, 2014

I've updated the code. I wish I could change the branch name w/o opening another PR so for now it'll be the only mismatching place.

@coveralls
Copy link

Coverage Status

Coverage increased (+0.04%) when pulling 79a6499 on xaka:require-function-name-match into 9aae17e on jscs-dev:master.

@mrjoelkemp
Copy link
Member

We have some helpful lists in lib/utils that should help identify reserved words.

@xaka xaka force-pushed the require-function-name-match branch from 79a6499 to a2e6501 Compare December 23, 2014 17:09
xaka added a commit to xaka/node-jscs that referenced this pull request Dec 23, 2014
…and property names

Example of member name mismatch:

```js
var test = {};
test.foo = function bar() {};
```

Example of property name mismatch:

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

Fixes jscs-dev#846
Closes jscs-dev#850
@xaka xaka changed the title requireMatchingFunctionName: requires member and property names to match function names requireMatchingFunctionName: requires function names to match member and property names Dec 23, 2014
@coveralls
Copy link

Coverage Status

Coverage increased (+0.05%) when pulling a2e6501 on xaka:require-function-name-match into f2496f4 on jscs-dev:master.

@xaka
Copy link
Contributor Author

xaka commented Dec 23, 2014

I've updated the code and documentation. So now when reserved work is detected, we just skip it. I've also added a suggestion to prefix such names with $ instead of enforcing it when the rule is on. Personally I'd enforce such change, but we can leave it for next iteration.

@mikesherov
Copy link
Contributor

@xaka, thanks again for contributing. Please remove the $ prefix suggestion language from the rule, as I think it's best to let the user decide for best to mitigate the problem.

@xaka xaka force-pushed the require-function-name-match branch from a2e6501 to 86f85fb Compare December 23, 2014 17:29
xaka added a commit to xaka/node-jscs that referenced this pull request Dec 23, 2014
…and property names

Example of member name mismatch:

```js
var test = {};
test.foo = function bar() {};
```

Example of property name mismatch:

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

Fixes jscs-dev#846
Closes jscs-dev#850
@xaka
Copy link
Contributor Author

xaka commented Dec 23, 2014

@mikesherov done, sir!

@coveralls
Copy link

Coverage Status

Coverage increased (+0.05%) when pulling 86f85fb on xaka:require-function-name-match into f2496f4 on jscs-dev:master.

// object.foo = function bar() {}
// object['foo'] = function bar() {}
case 'AssignmentExpression':
checker = checkForMember;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

just call checkForMember(node.parentNode, errors); here;

@mikesherov
Copy link
Contributor

@xaka just a couple of more comments and a rebase should bring this one across the finish line. Thanks again for contributing!

@xaka xaka force-pushed the require-function-name-match branch from 86f85fb to 4749849 Compare January 27, 2015 17:11
xaka added a commit to xaka/node-jscs that referenced this pull request Jan 27, 2015
…and property names

Example of member name mismatch:

```js
var test = {};
test.foo = function bar() {};
```

Example of property name mismatch:

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

Fixes jscs-dev#846
Closes jscs-dev#850
…and property names

Example of member name mismatch:

```js
var test = {};
test.foo = function bar() {};
```

Example of property name mismatch:

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

Fixes jscs-dev#846
Closes jscs-dev#850
@xaka xaka force-pushed the require-function-name-match branch from 4749849 to 2b52452 Compare January 27, 2015 17:27
@xaka
Copy link
Contributor Author

xaka commented Jan 27, 2015

@mikesherov Done. Please double-check the documentation part as you changed the way it's generated and I did my best to follow the same path.

@coveralls
Copy link

Coverage Status

Coverage increased (+0.01%) to 98.74% when pulling 2b52452 on xaka:require-function-name-match into 87a382d on jscs-dev:master.

@hzoo hzoo mentioned this pull request Feb 17, 2015
@markelog
Copy link
Member

@mikesherov are we good here?

@markelog markelog added this to the 2.0 milestone Jun 11, 2015
@markelog markelog closed this in bbd7337 Jun 12, 2015
@markelog
Copy link
Member

Thank you for the patience! And sorry it took that long to land.

Slayer95 pushed a commit to Slayer95/node-jscs that referenced this pull request Jun 12, 2015
Requires function names to match member and property names

Example of member name mismatch:

```js
var test = {};
test.foo = function bar() {};
```

Example of property name mismatch:

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

Fixes jscs-dev#846
Closes jscs-dev#850
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Require function names to match member and property names
6 participants