Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improvement: add yoda and no-cond-assign eslint rules #5253

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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
2 changes: 2 additions & 0 deletions .eslintrc.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,7 @@ rules:
eol-last: error
eqeqeq: [error, allow-null]
indent: [error, 2, { MemberExpression: "off", SwitchCase: 1 }]
no-cond-assign: error
no-trailing-spaces: error
no-unused-vars: [error, { vars: all, args: none, ignoreRestSiblings: true }]
yoda: error
3 changes: 2 additions & 1 deletion examples/params/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@ app.param(['to', 'from'], function(req, res, next, num, name){
// Load user by id

app.param('user', function(req, res, next, id){
if (req.user = users[id]) {
req.user = users[id]
if (req.user) {
next();
} else {
next(createError(404, 'failed to find user'));
Expand Down
10 changes: 5 additions & 5 deletions lib/request.js
Original file line number Diff line number Diff line change
Expand Up @@ -242,9 +242,9 @@ req.param = function param(name, defaultValue) {
: 'name, default';
deprecate('req.param(' + args + '): Use req.params, req.body, or req.query instead');

if (null != params[name] && params.hasOwnProperty(name)) return params[name];
if (null != body[name]) return body[name];
if (null != query[name]) return query[name];
if (params[name] != null && params.hasOwnProperty(name)) return params[name];
if (body[name] != null) return body[name];
if (query[name] != null) return query[name];

return defaultValue;
};
Expand Down Expand Up @@ -470,10 +470,10 @@ defineGetter(req, 'fresh', function(){
var status = res.statusCode

// GET or HEAD for weak freshness validation only
if ('GET' !== method && 'HEAD' !== method) return false;
if (method !== 'GET' && method !== 'HEAD') return false;

// 2xx or 304 as per rfc2616 14.26
if ((status >= 200 && status < 300) || 304 === status) {
if ((status >= 200 && status < 300) || status === 304) {
return fresh(this.headers, {
'etag': res.get('ETag'),
'last-modified': res.get('Last-Modified')
Expand Down
2 changes: 1 addition & 1 deletion lib/response.js
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ res.send = function send(body) {
if (req.fresh) this.statusCode = 304;

// strip irrelevant headers
if (204 === this.statusCode || 304 === this.statusCode) {
if (this.statusCode === 204 || this.statusCode === 304) {
this.removeHeader('Content-Type');
this.removeHeader('Content-Length');
this.removeHeader('Transfer-Encoding');
Expand Down
5 changes: 3 additions & 2 deletions lib/router/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -113,14 +113,15 @@ proto.param = function param(name, fn) {
}

for (var i = 0; i < len; ++i) {
if (ret = params[i](name, fn)) {
ret = params[i](name, fn)
if (ret) {
fn = ret;
}
}

// ensure we end up with a
// middleware function
if ('function' !== typeof fn) {
if (typeof fn !== 'function') {
throw new Error('invalid param() call for ' + name + ', got ' + fn);
}

Expand Down
8 changes: 4 additions & 4 deletions lib/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,9 @@ exports.wetag = createETagGenerator({ weak: true })
*/

exports.isAbsolute = function(path){
if ('/' === path[0]) return true;
if (':' === path[1] && ('\\' === path[2] || '/' === path[2])) return true; // Windows device path
if ('\\\\' === path.substring(0, 2)) return true; // Microsoft Azure absolute path
if (path[0] === '/') return true;
if (path[1] === ':' && (path[2] === '\\' || path[2] === '/')) return true; // Windows device path
if (path.substring(0, 2) === '\\\\') return true; // Microsoft Azure absolute path
};

/**
Expand Down Expand Up @@ -129,7 +129,7 @@ function acceptParams (str) {

for (var i = 1; i < parts.length; ++i) {
var pms = parts[i].split(/ *= */);
if ('q' === pms[0]) {
if (pms[0] === 'q') {
ret.quality = parseFloat(pms[1]);
} else {
ret.params[pms[0]] = pms[1];
Expand Down
4 changes: 2 additions & 2 deletions test/app.param.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ describe('app', function(){
app.param(function(name, regexp){
if (Object.prototype.toString.call(regexp) === '[object RegExp]') { // See #1557
return function(req, res, next, val){
var captures;
if (captures = regexp.exec(String(val))) {
var captures = regexp.exec(String(val));
if (captures) {
req.params[name] = captures[1];
next();
} else {
Expand Down