Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using JSHint with Express.js / 'delete' (a reserved word)

I'm using Express.js ontop of Node.js to create RESTful API, and using grunt to watch my files and automatically lint my JavaScript.

Every time I use the delete function, it gets flagged by JSHint:

[L218:C9] Expected an identifier and instead saw 'delete' (a reserved word).
app.delete('/api/users/:userid', function deleteUser(req, res, next) {

I understand that 'delete' is a reserved word, but it's chosen by Express.js! Is there a better way to go about linting my Express.js app? Any way to turn off this check??

like image 437
Eric the Red Avatar asked Jul 10 '12 19:07

Eric the Red


3 Answers

In Express.js, use del instead of delete.

app.del('/api/users/:userid', function deleteUser(req, res, next)
like image 134
Bill Avatar answered Oct 15 '22 16:10

Bill


Another way to solve this would have been to use bracket notation instead of dot notation.

app['delete']('/api/users/:userid', function deleteUser(req, res, next) {
  /* function body */
});

This sort of work around was necessary in the past when working with IndexedDB which defines both .delete and .continue methods.

These days this sort of workaround should not be necessary. Ever since ES5 JavaScript has allowed property names to use reserved words. For a long time jsHint defaulted to assuming your code was ES3, but starting with version 2.0.0 it defaults to assuming it is ES5 and will not complain about reserved words being used as property names.

like image 7
Useless Code Avatar answered Oct 15 '22 16:10

Useless Code


In JSHint 1.1.x you can set the es5 option for jshint, and it will allow you to use reserved words as properties per the ES5 specification.

As of JSHint 2.0 es5 option is the default and you should be allowed to use reserved words as properties.

For more info you can head over to http://www.jshint.com/docs/#options

like image 3
Juan Pablo Buritica Avatar answered Oct 15 '22 16:10

Juan Pablo Buritica