Skip to content

Commit

Permalink
Merge pull request #101 from devTristan/local-linter-versions
Browse files Browse the repository at this point in the history
use the local linter version when possible
  • Loading branch information
ricardofbarros committed Apr 11, 2016
2 parents ff28676 + 7af4928 commit f640055
Showing 1 changed file with 26 additions and 13 deletions.
39 changes: 26 additions & 13 deletions lib/utils/select-style.js
Original file line number Diff line number Diff line change
@@ -1,22 +1,32 @@
// Dependencies
var allowUnsafeNewFunction = require('loophole').allowUnsafeNewFunction
var pickStandard = function (style) {
function requireWithLocalOverride (moduleName, dir) {
try {
return module.constructor._load(moduleName, {
paths: module.constructor._nodeModulePaths(dir)
})
} catch (err) {
return require(moduleName)
}
}
var pickStandard = function (style, dir) {
return allowUnsafeNewFunction(function () {
switch (style) {
case 'standard':
return require('standard')
return requireWithLocalOverride('standard', dir)
case 'happiness':
return require('happiness')
return requireWithLocalOverride('happiness', dir)
case 'uber-standard':
return require('uber-standard')
return requireWithLocalOverride('uber-standard', dir)
default:
return require('semistandard')
return requireWithLocalOverride('semistandard', dir)
}
})
}

var pkgConfig = require('pkg-config')
var intersection = require('lodash.intersection')
var dirname = require('path').dirname

function getStyleThroughDevDeps (filePath) {
// This will get the devDependencies
Expand All @@ -30,23 +40,25 @@ function getStyleThroughDevDeps (filePath) {
var foundLinters = intersection(Object.keys(devDeps), knownLinters)
var hasKnownLinter = Boolean(foundLinters.length)
if (devDeps && hasKnownLinter) {
var dir = dirname(filePath)

// standard style
if (devDeps.standard) {
return pickStandard('standard')
return pickStandard('standard', dir)
}

// happiness style
if (devDeps.happiness) {
return pickStandard('happiness')
return pickStandard('happiness', dir)
}

// uber-standard
if (devDeps['uber-standard']) {
return pickStandard('uber-standard')
return pickStandard('uber-standard', dir)
}

// semistandard style
return pickStandard('semistandard')
return pickStandard('semistandard', dir)
}

// no style
Expand All @@ -63,16 +75,17 @@ module.exports = function selectStyle (config, filePath) {
}

function getLinterFromStyle (style) {
var dir = dirname(filePath)
if (style === 'standard') {
return pickStandard('standard')
return pickStandard('standard', dir)
}
if (style === 'happiness') {
return pickStandard('happiness')
return pickStandard('happiness', dir)
}
if (style === 'uber-standard') {
return pickStandard('uber-standard')
return pickStandard('uber-standard', dir)
}
return pickStandard('semistandard')
return pickStandard('semistandard', dir)
}

// fallback to style select value to decide which style
Expand Down

0 comments on commit f640055

Please sign in to comment.