javascript - ESLint's "no-undef" rule is calling my use of Underscore an undefined variable -


i using grunt build tool , eslint linting tool app working on. using underscore node package, , have made use of in app. unfortunately, when run eslint on code, thinks _ undefined variable in following line:

return _.pluck(objects, namecolumn);

this error giving me:

78:21 error "_" not defined no-undef

i prefer not disable no-undef rule eslint, , have tried installing underscore plugin, still receiving error. if else has ideas try this, appreciative!

if there further information can give helping me figured out, let me know!

the official documentation should give idea on how fix this.

the easiest fix add

/* global _ */ 

at top of file.

but since you'll have each new js file, can annoying. if using underscore often, i'd suggest add globals .eslintrc file, example:

{     "globals": {         "_": false     } } 

and save .eslintrc in project root, or optionally in user home directory. although latter not recommended, can convenient, have remember have there :)


explanation of above rule: "_": false means variable named _ tells eslint variable defined globally , not emit no-undef errors variable. @sebastian pointed out, false means variable can't overwritten, code _ = 'something else' yield error no-global-assign. if instead use "_": true (this previous answer), means value can re-assigned , mentioned error not occur.

but keep in mind happen if assign directly global variable have shown in example. can still shadow , eslint won't anything. example, these snippets wouldn't yield no-global-assign:

const _ = 'haha broke _'  

or function argument name, e.g.

function (_) {   console.log(_, 'might not _ looking for')  } 

Comments

Popular posts from this blog

get url and add instance to a model with prefilled foreign key :django admin -

css - Make div keyboard-scrollable in jQuery Mobile? -

ruby on rails - Seeing duplicate requests handled with Unicorn -