Skip to content

Commit

Permalink
Merge pull request #53 from D1SoveR/master
Browse files Browse the repository at this point in the history
Add support for per-file directive overrides.
  • Loading branch information
cfjedimaster committed Jun 5, 2014
2 parents c968e1f + c27fa46 commit 1ef58cf
Showing 1 changed file with 42 additions and 0 deletions.
42 changes: 42 additions & 0 deletions main.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ define(function (require, exports, module) {
function handleHinterAsync(text, fullPath) {
var deferred = new $.Deferred();
_loadConfig(fullPath)
.then(_applyOverrides(fullPath))
.done(function (cfg) {
deferred.resolve(handleHinter(text, fullPath, cfg));
});
Expand Down Expand Up @@ -151,6 +152,47 @@ define(function (require, exports, module) {
});
return result.promise();
}

/**
* Applies per-file overrides, if any were provided in the configuration.
* Follows format supported in commit #df60b9c on JSHint repository:
* https://github.com/jshint/jshint/commit/df60b9c75daa4321a4d064fcab04e14692c94039
*
* @param {string} fullPath absolute path to the processed file
* @returns {Function} function that returns a promise for configuration object with overrides applied
*/
function _applyOverrides(fullPath) {

var basePath = ProjectManager.getProjectRoot().fullPath,
filePath = FileUtils.getRelativeFilename(basePath, fullPath);

return function(cfg) {

var bundle,
has = Object.prototype.hasOwnProperty.call.bind(Object.prototype.hasOwnProperty),
overrides = cfg.options.overrides,
pattern;

if (overrides) {
delete cfg.options.overrides;

for (pattern in overrides) {
if (has(overrides, pattern) && (new RegExp(pattern)).test(filePath)) {
bundle = overrides[pattern];

if (bundle.globals) {
$.extend(true, cfg.globals, bundle.globals);
delete bundle.globals;
}
$.extend(true, cfg.options, bundle);

}
}
}

return cfg;
};
}

/**
* Looks up the configuration file in the filesystem hierarchy and loads it.
Expand Down

0 comments on commit 1ef58cf

Please sign in to comment.