Skip to content
This repository has been archived by the owner on Aug 8, 2023. It is now read-only.

Add rules for prohibiting unused vars/expressions #78

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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
6 changes: 5 additions & 1 deletion javascript/.eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,10 @@
"semi": 2,

// Disallow function definitions of the form 'function myFunc() {'
"func-style": [2, "expression"]
"func-style": [2, "expression"],

// Disallow unused variables/expressions
"no-unused-vars": 2,
"no-unused-expressions": 2

Choose a reason for hiding this comment

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

I would suggest to allow the use of short circuits and ternary conditionals, since I see those a lot.

"no-unused-expressions": [2, { allowShortCircuit: true, allowTernary: true }]

}
}
15 changes: 15 additions & 0 deletions javascript/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,21 @@ var length = items.length;
var name = 'foo';
````

## Don't have unused variables/expressions
```javascript
// Bad: Many of these variables/expressions are useless
{} // object - not used
var newArray = []; // newArray - not used
['1', '2', '3', '4'].forEach(function(num, index) { // index - not used
Copy link
Contributor

Choose a reason for hiding this comment

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

What about when something you need is in the second argument? Say we wanted index but not num - a functional programming common practice is to use _ in it's place, but will the linter complain about that? If so I don't think we should add it to the linting rules.

Copy link
Contributor

Choose a reason for hiding this comment

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

@jansepar There are options to eslint we can use to ignore unused arguments to functions. That seems like it'd be a pragmatic compromise here.

"no-unused-vars": [2, {"vars": "all", "args": "none" }]

Reference: https://github.com/eslint/eslint/blob/master/docs/rules/no-unused-vars.md

Interestingly, I have no idea what the 2 is for in the array. All examples used 2 and the docs don't say what it's for. 😕

Choose a reason for hiding this comment

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

@jeremywiebe The 2 indicates that a rule violation will surface an error (exit code of 1) instead of just a warning.

http://eslint.org/docs/user-guide/configuring#configuring-rules

return num++;
});

// Good: Delete things you no longer need
['1', '2', '3', '4'].forEach(function(num) {
return num++;
});
```

##Use semi-colons

There are many more ways that things can break *without* semi-colons than *with* semi-colons.
Expand Down