Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Suppress `Expected an identifier and instead saw 'default' (a reserved word)` in JSLint with Mongoose

I'm using jshint to validate my JavaScript files.

On the server-side I'm using node.js with Mongoose. In Mongoose I'm encouraged to write schemata in a fashion like:

var UserSchema = new mongoose.Schema({
    firstname : { type: String, default: '' }
});

When running linting, I get error:

Expected an identifier and instead saw 'default' (a reserved word).

Is there a way to suppress this error? I really would prefer that behaviour instead of writing:

var UserSchema = new mongoose.Schema({
    firstname : { type: String, "default": '' }
});
like image 327
jsalonen Avatar asked Jun 07 '12 16:06

jsalonen


2 Answers

default is indeed a reserved word in JavaScript (https://developer.mozilla.org/en/JavaScript/Reference/Reserved_Words). While technically you can use default in an object property name without any problems, you could end up having problems with that notation if your interpreter is strict (like lint is).

Simplest way to go forward: fix the problem by adding quotes. Lint won't whine you any longer. The code is two characters longer, but so what - linting passes and you are guaranteed to not have problems due to use of a reserved keyword.

like image 96
jsalonen Avatar answered Oct 19 '22 02:10

jsalonen


You can also use the "es5" option to disable this from occurring.

See: http://jslinterrors.com/expected-an-identifier-and-instead-saw-a-a-reserved-word/

like image 21
robert.bo.roth Avatar answered Oct 19 '22 01:10

robert.bo.roth