dwyl / goodparts

:see_no_evil: An ESLint Style that only allows JavaScript the Good Parts (and "Better Parts") in your code.
GNU General Public License v2.0
78 stars 26 forks source link

How to require env2? #271

Closed jay-meister closed 8 years ago

jay-meister commented 8 years ago
var path = require('path');
require('env2')(path.resolve(__dirname, '.env'));
var HapiPostgresConnection = require('hapi-postgres-connection');

As the kind linter keeps reminding me, I am supposed to declare all variables at the top of the file. I want to require env2 to allow me to require hapi-postgres-connection. But this rubs the linter up the wrong way. I tried declaring it as a var but then it complained that the var was never used.

Advice much appreciated.

SimonLab commented 8 years ago

I had this issue also before, on this occasion I just ignore the line:

require('env2')('.env') // eslint-disable-line 
var ...

I'm not sure if there is a specific eslint rule or options for this case. Does anyone manage to resolve this without eslint-disable-line?

jay-meister commented 8 years ago
require('env2')('.env')
var ... ;  // eslint-disable-line 

Thanks. Although I had to disable the var line, not the require line.

eliasmalik commented 8 years ago

@JMurphyWeb This really strikes me more as a problem with hapi-postgres-connection than with goodparts.

Specifically, why is the plugin reaching out into the outside world and asserting that some environment variable exists? It should instead apply a little dependency injection and either accept DB config as an argument to a wrapping function, or as a field in the plugin options object.

jackcarlisle commented 8 years ago

@JMurphyWeb you could assign the require to a variable and then disable it:

var env = require('env2')('.env'); // eslint-disable-line
jay-meister commented 8 years ago

Yeah I tried that, then saw that env is never used. So either way we need to es-lint-disable-line

@jackcarlisle are you guys using good parts in your project then?

@eliascodes whilst I am sure you are right, I don't like the thought of my linter dictating which modules I choose to use.. It dictates enough already in my life.

jackcarlisle commented 8 years ago

@JMurphyWeb yeah it's a one line solution as opposed to putting es-lint-disable-line beside every var.

Yeah we're using it in our project!

eliasmalik commented 8 years ago

@JMurphyWeb I understand that you want to just get on with writing code, and not having a ball-ache every time some style issue crops up. Fair enough, disabling the line is fine.

I was just making a wider point. And as far as '... my linter dictating which modules I choose to use ...', surely that's the whole point. If the module you want to use forces (or encourages) you to write anti-patterns or code in some other way that might introduce issues, then the linter should try to stop you, and the burden should fall on you (or in this case the module writer), not the linter.