Skip to content

Commit

Permalink
Merge pull request #53 from aaronjensen/add-fix-stdout
Browse files Browse the repository at this point in the history
Add --fix-to-stdout
  • Loading branch information
mantoni authored Oct 16, 2016
2 parents 5da9a2d + f2567d9 commit a912bb5
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 3 deletions.
38 changes: 38 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@ along with an access token in `~/.eslint_d`.

## Editor integration

### Linting

- __Sublime__: Check out [SublimeLinter-contrib-eslint\_d][SublimeLinter].
- __Vim__: Install the [syntastic][] plugin, then make sure this is in your
`.vimrc`:
Expand All @@ -82,6 +84,42 @@ along with an access token in `~/.eslint_d`.
If you're using `eslint_d` in any other editor, please tell me!
### Automatic Fixing
`eslint_d` has an additional flag that `eslint` does not have, `--fix-to-stdout`
which prints the fixed file to stdout. This allows editors to add before save
hooks to automatically fix a file prior to saving. It must be used with
`--stdin`.
- __Emacs__: Add this to your `init.el`, be sure to add hooks for the actual
major modes you use:
```elisp
(defun eslint-fix ()
(interactive)
(setq temp-point (point))
(shell-command-on-region
;; Region
(point-min)
(point-max)
;; Command
"eslint_d --stdin --fix-to-stdout"
;; Output to current buffer
t
;; Replace buffer
t
;; Error buffer name
"*eslint-fix error*"
;; Display error buffer
t)
;; Refresh syntax highlighting
(font-lock-fontify-buffer)
(goto-char temp-point))
(add-hook 'js-mode-hook
(lambda () (add-hook 'before-save-hook #'eslint-fix)))
```
## Moar speed
If you're really into performance and want the lowest possible latency, talk to
Expand Down
18 changes: 15 additions & 3 deletions lib/linter.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ function translateOptions(cliOptions, cwd) {
cache: cliOptions.cache,
cacheFile: cliOptions.cacheFile,
cacheLocation: cliOptions.cacheLocation,
fix: cliOptions.fix,
fix: cliOptions.fix || cliOptions.fixToStdout,
allowInlineConfig: cliOptions.inlineConfig,
printConfig: cliOptions.printConfig,
cwd: cwd
Expand Down Expand Up @@ -49,7 +49,12 @@ module.exports = function (cwd, args, text) {
}))
};
}
var currentOptions = options.parse([0, 0].concat(args));
var currentOptions;
try {
currentOptions = options.parse([0, 0].concat(args));
} catch (e) {
return e.message + '\n# exit 1';
}
cwdDeps.chalk.enabled = currentOptions.color;
var files = currentOptions._;
var stdin = currentOptions.stdin;
Expand All @@ -74,12 +79,19 @@ module.exports = function (cwd, args, text) {

return JSON.stringify(fileConfig, null, ' ');
}
if (currentOptions.fixToStdout && !stdin) {
return 'The --fix-to-stdout option must be used with --stdin.'
+ '\n# exit 1';
}
var report;
if (stdin && text) {
if (stdin) {
report = engine.executeOnText(text, currentOptions.stdinFilename);
} else {
report = engine.executeOnFiles(files);
}
if (currentOptions.fixToStdout) {
return report.results[0].output || text;
}
if (currentOptions.fix) {
cwdDeps.eslint.CLIEngine.outputFixes(report);
}
Expand Down
6 changes: 6 additions & 0 deletions lib/options.js
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,12 @@ module.exports = optionator({
default: false,
description: 'Automatically fix problems'
},
{
option: 'fix-to-stdout',
type: 'Boolean',
description: 'Print the fix results to stdout instead of regular output, '
+ 'must be used with --stdin'
},
{
option: 'debug',
type: 'Boolean',
Expand Down

0 comments on commit a912bb5

Please sign in to comment.