Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

use the local linter version when possible #101

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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