├── .npmrc ├── .gitattributes ├── .gitignore ├── screenshot.png ├── _fixture.js ├── .editorconfig ├── gulpfile.js ├── .github └── workflows │ └── main.yml ├── license ├── package.json ├── index.js ├── test.js └── readme.md /.npmrc: -------------------------------------------------------------------------------- 1 | package-lock=false 2 | -------------------------------------------------------------------------------- /.gitattributes: -------------------------------------------------------------------------------- 1 | * text=auto eol=lf 2 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | yarn.lock 3 | -------------------------------------------------------------------------------- /screenshot.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xojs/gulp-xo/HEAD/screenshot.png -------------------------------------------------------------------------------- /_fixture.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | // TODO: foo 3 | var unicorn_rainbow = 'foo'; 4 | -------------------------------------------------------------------------------- /.editorconfig: -------------------------------------------------------------------------------- 1 | root = true 2 | 3 | [*] 4 | indent_style = tab 5 | end_of_line = lf 6 | charset = utf-8 7 | trim_trailing_whitespace = true 8 | insert_final_newline = true 9 | 10 | [*.yml] 11 | indent_style = space 12 | indent_size = 2 13 | -------------------------------------------------------------------------------- /gulpfile.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | const gulp = require('gulp'); 3 | const xo = require('.'); 4 | 5 | exports.default = () => ( 6 | gulp.src('_fixture.js') 7 | .pipe(xo()) 8 | .pipe(xo.format()) 9 | .pipe(xo.failAfterError()) 10 | ); 11 | -------------------------------------------------------------------------------- /.github/workflows/main.yml: -------------------------------------------------------------------------------- 1 | name: CI 2 | on: 3 | - push 4 | - pull_request 5 | jobs: 6 | test: 7 | name: Node.js ${{ matrix.node-version }} 8 | runs-on: ubuntu-latest 9 | strategy: 10 | fail-fast: false 11 | matrix: 12 | node-version: 13 | - 14 14 | - 12 15 | - 10 16 | steps: 17 | - uses: actions/checkout@v2 18 | - uses: actions/setup-node@v1 19 | with: 20 | node-version: ${{ matrix.node-version }} 21 | - run: npm install 22 | - run: npm test 23 | -------------------------------------------------------------------------------- /license: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) Sindre Sorhus (https://sindresorhus.com) 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: 6 | 7 | The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. 8 | 9 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 10 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "gulp-xo", 3 | "version": "0.25.0", 4 | "description": "Validate files with XO", 5 | "license": "MIT", 6 | "repository": "xojs/gulp-xo", 7 | "funding": "https://github.com/sponsors/sindresorhus", 8 | "author": { 9 | "name": "Sindre Sorhus", 10 | "email": "sindresorhus@gmail.com", 11 | "url": "https://sindresorhus.com" 12 | }, 13 | "engines": { 14 | "node": ">=10" 15 | }, 16 | "scripts": { 17 | "test": "ava" 18 | }, 19 | "files": [ 20 | "index.js" 21 | ], 22 | "keywords": [ 23 | "gulpplugin", 24 | "xo", 25 | "xoxo", 26 | "hugs", 27 | "kisses", 28 | "happy", 29 | "happiness", 30 | "code", 31 | "quality", 32 | "style", 33 | "lint", 34 | "linter", 35 | "jscs", 36 | "jshint", 37 | "jslint", 38 | "eslint", 39 | "validate", 40 | "code style", 41 | "standard", 42 | "strict", 43 | "check", 44 | "checker", 45 | "verify", 46 | "enforce", 47 | "hint", 48 | "simple" 49 | ], 50 | "dependencies": { 51 | "eslint-formatter-pretty": "^3.0.1", 52 | "gulp-eslint": "^6.0.0", 53 | "plugin-error": "^1.0.1", 54 | "through2": "^3.0.0", 55 | "xo": "^0.32.0" 56 | }, 57 | "devDependencies": { 58 | "ava": "^2.4.0", 59 | "gulp": "^4.0.0", 60 | "p-event": "^4.1.0", 61 | "vinyl": "^2.1.0", 62 | "vinyl-file": "^3.0.0" 63 | }, 64 | "peerDependencies": { 65 | "gulp": ">=4" 66 | }, 67 | "peerDependenciesMeta": { 68 | "gulp": { 69 | "optional": true 70 | } 71 | }, 72 | "xo": { 73 | "ignores": [ 74 | "test/_fixture.js" 75 | ] 76 | } 77 | } 78 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | const eslint = require('gulp-eslint'); 3 | const formatterPretty = require('eslint-formatter-pretty'); 4 | const through = require('through2'); 5 | const xo = require('xo'); 6 | const PluginError = require('plugin-error'); 7 | 8 | module.exports = options => { 9 | options = { 10 | quiet: false, 11 | ...options 12 | }; 13 | 14 | return through.obj(function (file, encoding, callback) { 15 | if (file.isNull()) { 16 | callback(null, file); 17 | return; 18 | } 19 | 20 | if (file.isStream()) { 21 | callback(new PluginError('gulp-xo', 'Streaming not supported')); 22 | return; 23 | } 24 | 25 | let report; 26 | try { 27 | report = xo.lintText(file.contents.toString(), { 28 | ...options, 29 | cwd: file.cwd, 30 | filename: file.path 31 | }); 32 | } catch (error) { 33 | this.emit('error', new PluginError('gulp-xo', error, {fileName: file.path})); 34 | } 35 | 36 | let result = report.results; 37 | 38 | if (result.length === 0) { 39 | callback(null, file); 40 | return; 41 | } 42 | 43 | if (options.quiet) { 44 | result = xo.getErrorResults(result); 45 | } 46 | 47 | file.eslint = result[0]; 48 | 49 | if (file.eslint.output) { 50 | file.contents = Buffer.from(file.eslint.output); 51 | file.eslint.fixed = true; 52 | } 53 | 54 | callback(null, file); 55 | }); 56 | }; 57 | 58 | Object.assign(module.exports, eslint); 59 | 60 | for (const fn of ['formatEach', 'format']) { 61 | module.exports[fn] = (formatter, writable) => ( 62 | eslint[fn](formatter ? xo.getFormatter(formatter) : formatterPretty, writable) 63 | ); 64 | } 65 | -------------------------------------------------------------------------------- /test.js: -------------------------------------------------------------------------------- 1 | import fs from 'fs'; 2 | import test from 'ava'; 3 | import vinylFile from 'vinyl-file'; 4 | import pEvent from 'p-event'; 5 | import Vinyl from 'vinyl'; 6 | import xo from '.'; 7 | 8 | test('main', async t => { 9 | const stream = xo(); 10 | stream.on('data', file => { 11 | t.truthy(file.eslint); 12 | t.truthy(file.eslint.messages.length); 13 | t.pass(); 14 | }); 15 | const finish = pEvent(stream, 'finish'); 16 | stream.write(vinylFile.readSync('_fixture.js')); 17 | stream.write(vinylFile.readSync('_fixture.js')); 18 | stream.end(); 19 | await finish; 20 | }); 21 | 22 | test('default formatter', async t => { 23 | const stream = xo(); 24 | stream.pipe(xo.format(null, report => { 25 | t.truthy(/[×✖]/.test(report)); 26 | t.pass(); 27 | })); 28 | const finish = pEvent(stream, 'finish'); 29 | stream.end(vinylFile.readSync('_fixture.js')); 30 | await finish; 31 | }); 32 | 33 | test('fix option', async t => { 34 | fs.writeFileSync('.eslintignore', '_ignored.js'); 35 | 36 | const stream = xo({ 37 | fix: true 38 | }); 39 | 40 | stream.on('data', file => { 41 | t.is(file.contents.toString(), 'alert();\n'); 42 | t.pass(); 43 | }); 44 | const finish = pEvent(stream, 'finish'); 45 | stream.end(new Vinyl({ 46 | path: __filename, 47 | contents: Buffer.from('alert()') 48 | })); 49 | await finish; 50 | 51 | fs.unlinkSync('.eslintignore'); 52 | }); 53 | 54 | test.serial.failing('ignored files', async t => { 55 | fs.writeFileSync('.eslintignore', '_fixture.js'); 56 | 57 | const stream = xo(); 58 | stream.on('data', file => { 59 | t.falsy(file.eslint); 60 | t.pass(); 61 | }); 62 | const finish = pEvent(stream, 'finish'); 63 | stream.end(vinylFile.readSync('_fixture.js')); 64 | await finish; 65 | 66 | fs.unlinkSync('.eslintignore'); 67 | }); 68 | -------------------------------------------------------------------------------- /readme.md: -------------------------------------------------------------------------------- 1 | # gulp-xo 2 | 3 | > Validate files with [XO](https://github.com/xojs/xo) 4 | 5 | ![](screenshot.png) 6 | 7 | *Issues regarding rules should be reported on the ESLint [issue tracker](https://github.com/eslint/eslint/issues) as it's the actual linter.* 8 | 9 | ## Install 10 | 11 | ``` 12 | $ npm install --save-dev gulp-xo 13 | ``` 14 | 15 | ## Usage 16 | 17 | ```js 18 | const gulp = require('gulp'); 19 | const xo = require('gulp-xo'); 20 | 21 | exports.default = () => ( 22 | gulp.src('src/app.js') 23 | .pipe(xo()) 24 | .pipe(xo.format()) 25 | .pipe(xo.failAfterError()) 26 | ); 27 | ``` 28 | 29 | ## API 30 | 31 | ### xo(options?) 32 | 33 | #### options 34 | 35 | Type: `object` 36 | 37 | Any additional options to the below are [passed directly to XO](https://github.com/xojs/xo/blob/main/readme.md#config). However, you should prefer setting your XO config in `package.json` so editors and other tools can also read it. Only pass them here if you want to use options different from those of your current project. You might want to do this if your Gulp task lints or builds files that are/will be part of a separate project. 38 | 39 | ##### fix 40 | 41 | Type: `boolean` 42 | 43 | This option instructs ESLint to try to fix as many issues as possible. The fixes are applied to the gulp stream. The fixed content can be saved to file using `gulp.dest` (See [example/fix.js](https://github.com/adametry/gulp-eslint/blob/main/example/fix.js)). Rules that are fixable can be found in ESLint's [rules list](https://eslint.org/docs/rules/). 44 | 45 | When fixes are applied, a "fixed" property is set to `true` on the fixed file's ESLint result. 46 | 47 | ##### quiet 48 | 49 | Type: `boolean`\ 50 | Default: `false` 51 | 52 | Report errors only. 53 | 54 | ### [xo.format(formatter, output)](https://github.com/adametry/gulp-eslint/#eslintformatformatter-output) 55 | 56 | ### [xo.failAfterError()](https://github.com/adametry/gulp-eslint/#eslintfailaftererror) 57 | 58 | ### [xo.failOnError()](https://github.com/adametry/gulp-eslint/#eslintfailonerror) 59 | 60 | ### [xo.formatEach(formatter, output)](https://github.com/adametry/gulp-eslint/#eslintformateachformatter-output) 61 | 62 | ### [xo.result(action)](https://github.com/adametry/gulp-eslint/#eslintresultaction) 63 | 64 | ### [xo.results(action)](https://github.com/adametry/gulp-eslint/#eslintresultsaction) 65 | 66 | ## Related 67 | 68 | - [gulp-eslint](https://github.com/adametry/gulp-eslint) - Gulp plugin for ESLint 69 | - [gulp-reporter](https://github.com/gucong3000/gulp-reporter) - Error reporter for CSSLint, EditorConfig, ESLint, HTMLHint, PostCSS, TSLint, XO 70 | --------------------------------------------------------------------------------