Skip to content

Commit

Permalink
Adding more linting rules. Fixed the watch bug. #47
Browse files Browse the repository at this point in the history
  • Loading branch information
rizowski committed Oct 23, 2015
1 parent da7686f commit ad36d1b
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 112 deletions.
8 changes: 7 additions & 1 deletion .eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,12 @@
"quotes": [2, "single", "avoid-escape"],
"strict": [2, "global"],
"no-unused-vars": 2,
"semi": [2, "always"]
"no-underscore-dangle": 1,
"semi": [2, "always"],
"no-undef": 2,
"no-use-before-define": 2,
"block-scoped-var": 2,
"space-in-parens": [2, "never"],
"object-curly-spacing": [2, "always"]
}
}
2 changes: 1 addition & 1 deletion CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,5 @@ To make a pull request for an issue, please fork the repo, and create a branch o
In order to get the debug logs, you will need to change your command a little. Please prefix the command you are trying to run with `DEBUG=*`. Copy everything printed and past it in the issue.

```bash
DEBUG=* esw ...
$ DEBUG=* esw
```
5 changes: 3 additions & 2 deletions src/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,8 @@ var parsedOptions;
var eslArgs;
var exitCode;

var args = process.argv;

function runLint(args, options){
logger.debug(args);
var child = eslintCli(args, options);

child.on('exit', function(code){
Expand All @@ -38,6 +37,7 @@ function keyListener(args, options){
stdin.on('keypress', function(ch, key){
logger.debug('%s was pressed', key.name);
if(key.name === 'return'){
logger.debug('Rerunning lint...')
runLint(args, options);
}
if(key.ctrl && key.name === 'c') {
Expand All @@ -49,6 +49,7 @@ function keyListener(args, options){
}

getOptions(function(options){
var args = process.argv;
logger.debug('Arguments passed: %o', args);
parsedOptions = options.parse(args);
logger.debug('Parsing args');
Expand Down
2 changes: 1 addition & 1 deletion src/eslint/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ var spawn = child.spawn;

module.exports = function(args, options, childOptions){
if(!options){
options = { _: './'};
options = { _: './' };
}
if(options._ && options._.length === 0){
options._ = './';
Expand Down
38 changes: 19 additions & 19 deletions src/eslint/help.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,6 @@ var _ = require('lodash');
var logger = require('../log')('eslint-help');
logger.debug('Loaded');

function createOption(arr){
var noAlias = arr[0].match(/--\w/);
var option = noAlias ? parseRegular(arr) : parseAlias(arr);
var isEmpty = _.isEmpty(option);
return isEmpty ? undefined : option;
}

function parseAlias(arr){
var alias = arr[0];
logger.debug('Alias found: %s', alias);
var option = parseRegular(_.without(arr, alias));

if(alias){
option.alias = alias.replace('-','');
}
return option;
}

function parseRegular(arr){
logger.debug('Parsing %s', arr[0]);
if(!arr[0]){
Expand All @@ -43,6 +25,24 @@ function parseRegular(arr){
return option;
}

function parseAlias(arr){
var alias = arr[0];
logger.debug('Alias found: %s', alias);
var option = parseRegular(_.without(arr, alias));

if(alias){
option.alias = alias.replace('-','');
}
return option;
}

function createOption(arr){
var noAlias = arr[0].match(/--\w/);
var option = noAlias ? parseRegular(arr) : parseAlias(arr);
var isEmpty = _.isEmpty(option);
return isEmpty ? undefined : option;
}

function parseHelp(helpText){
var helpArr = helpText.split('\n');
var newArr = [];
Expand All @@ -68,7 +68,7 @@ function parseHelp(helpText){
// rewrite in es6 this callback yucky stuff goes away.
module.exports = function(cllbk){
logger.debug('Executing help');
var spawn = eslint(['--help'], {help: true}, {});
var spawn = eslint(['--help'], { help: true }, { });
spawn.stdout.on('data', function(msg){
logger.debug('Help text received');
var eslintHelp = msg.toString();
Expand Down
100 changes: 12 additions & 88 deletions src/watcher.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,66 +2,17 @@
var chokidar = require('chokidar');
var eslint = require('eslint');
var chalk = require('chalk');
var _ = require('lodash');

var success = require('./formatters/helpers/success');
var formatter = require('./formatters/simple-detail');
var logger = require('./log')('watcher');
logger.debug('Loaded');

var defaultExtensions = ['.js'];
var events = {
change: 'change'
};

var cli = new eslint.CLIEngine();
var watch = chokidar.watch()
.on(events.change, function (path) {
logger.debug('CHANGED');
if(!cli.isPathIgnored(path)){
var config = cli.getConfigForFile(path);
lintFile(path, config);
}
}).on('error', function(err){
logger.log(err);
});

/**
* Given an array of paths and extensions, figure out which paths refer to files.
* A file is identified by a path ending in ".ext", where ".ext" is one of the extensions that is provided.
*/
function findFilePaths(paths, extensions) {
var extensionRegex = new RegExp('^.*(' + extensions.join('|') + ')$');

return _.filter(paths, function (path) {
return path.search(extensionRegex) > -1;
});
}

function addGlobToWatcher(path){
logger.debug('Watching: %s', path);
watch.add(path);
}

// Add files and paths to the watcher
function watchPaths(filePaths, directoryPaths, extensions) {
if (filePaths.length > 0) {
logger.debug('watchPaths (filePaths): %o', filePaths);
watch.add(filePaths);
}

if (directoryPaths.length > 0) {
logger.debug('WatchPaths (directories): %o', directoryPaths);
// Remove any trailing slashes from the directory paths for Windows compatibility
directoryPaths = _.map(directoryPaths, function (directoryPath) {
return directoryPath.replace(/\/+$/, '');
});

// For example:
// +(directory1|directory2)/**/*+(.ext1|.ext2)
addGlobToWatcher('+(' + directoryPaths.join('|') + ')/**/*+(' + extensions.join('|') + ')$');
}
};

function successMessage(result) {
logger.debug('result: %o', result);
Expand All @@ -78,44 +29,17 @@ function lintFile(path, config) {
logger.log(formatter(results));
}

function watcher(options) {
var specifiedPaths;
var directoryPaths;
var filePaths;
var extensions;

/**
* If the user has specified extensions, those will be passed in as an array,
* otherwise, options.ext won't be defined.
*/
if (options.ext && options.ext.length > 0) {
extensions = options.ext;
} else {
extensions = defaultExtensions;
}

/**
* If the user has specified particular paths, the _ property will contain an array.
* Otherwise, the _ property will contain "./", the default as a string.
*/
if (options._ instanceof Array) {
specifiedPaths = options._;

/**
* Files and directories require different glob patterns.
* Split the up the paths into 2 separate arrays.
* What is not a file is considered a directory.
*/
filePaths = findFilePaths(specifiedPaths, extensions);
directoryPaths = _.difference(specifiedPaths, filePaths);
watchPaths(filePaths, directoryPaths, extensions);
} else {
logger.debug('Watching default path %s', extensions);
// The default case requires a different glob pattern.
addGlobToWatcher('**/*+(' + extensions.join('|') + ')$');
}
module.exports = function watcher(options) {
chokidar.watch(options._)
.on(events.change, function (path) {
logger.debug('CHANGED');
if(!cli.isPathIgnored(path)){
var config = cli.getConfigForFile(path);
lintFile(path, config);
}
}).on('error', function(err){
logger.log(err);
});

logger.debug('Watching: %o', options._);
}

module.exports = watcher;
};

0 comments on commit ad36d1b

Please sign in to comment.