Bladeren bron

Merge pull request #265 from ckeditor/t/216

Use ckeditor5-dev-task-lint package for linting.
Piotrek Koszuliński 9 jaren geleden
bovenliggende
commit
7b56832655

+ 0 - 1
.jscsrc

@@ -1,5 +1,4 @@
 {
-  "esnext": true,
   "requireCurlyBraces": [
     "if",
     "else",

+ 0 - 3
dev/tasks/dev/tasks/create-package.js

@@ -47,9 +47,6 @@ module.exports = ( ckeditor5Path, workspaceRoot ) => {
 		],
 		'dev/': [
 			'dev/.jshintrc'
-		],
-		'dev/tasks/lint/': [
-			'dev/tasks/lint/tasks.js'
 		]
 	};
 

+ 3 - 1
dev/tasks/dev/templates/gulpfile.js

@@ -14,6 +14,8 @@ const config = {
 	]
 };
 
-require( './dev/tasks/lint/tasks' )( config );
+const ckeditor5Lint = require( 'ckeditor5-dev-lint' )( config );
 
+gulp.task( 'lint', ckeditor5Lint.lint );
+gulp.task( 'lint-staged', ckeditor5Lint.lintStaged );
 gulp.task( 'pre-commit', [ 'lint-staged' ] );

+ 2 - 1
dev/tasks/dev/templates/package.json

@@ -13,7 +13,8 @@
     "gulp-util": "^3.0.7",
     "guppy-pre-commit": "^0.3.0",
     "jshint": "^2.9.1",
-    "jshint-reporter-jscs": "^0.1.0"
+    "jshint-reporter-jscs": "^0.1.0",
+    "ckeditor5-dev-lint": "ckeditor/ckeditor5-dev-lint"
   },
   "engines": {
     "node": ">=5.0.0",

+ 0 - 95
dev/tasks/lint/tasks.js

@@ -1,95 +0,0 @@
-/* jshint node: true, esnext: true */
-
-'use strict';
-
-const gulp = require( 'gulp' );
-const jshint = require( 'gulp-jshint' );
-const jscs = require( 'gulp-jscs' );
-const fs = require( 'fs' );
-const gulpFilter = require( 'gulp-filter' );
-const gutil = require( 'gulp-util' );
-
-module.exports = ( config ) => {
-	const src = [ '**/*.js' ].concat( config.IGNORED_FILES.map( i => '!' + i ), getGitIgnore() );
-
-	const tasks = {
-		/**
-		 * Returns stream containing jshint and jscs reporters.
-		 *
-		 * @returns {Stream}
-		 */
-		lint() {
-			return gulp.src( src )
-				.pipe( lint() );
-		},
-
-		/**
-		 * This method is executed on pre-commit hook, linting only files staged for current commit.
-		 *
-		 * @returns {Stream}
-		 */
-		lintStaged() {
-			const guppy = require( 'git-guppy' )( gulp );
-
-			return guppy.stream( 'pre-commit', { base: './' } )
-				.pipe( gulpFilter( src ) )
-				.pipe( lint() )
-
-				// Error reporting for gulp.
-				.pipe( jscs.reporter( 'fail' ) )
-				.on( 'error', errorHandler )
-				.pipe( jshint.reporter( 'fail' ) )
-				.on( 'error', errorHandler );
-
-			/**
-			 * Handles error from jscs and jshint fail reporters. Stops node process with error code
-			 * and prints error message to the console.
-			 */
-			function errorHandler() {
-				gutil.log( gutil.colors.red( 'Linting failed, commit aborted' ) );
-				process.exit( 1 );
-			}
-		},
-
-		register() {
-			gulp.task( 'lint', tasks.lint );
-			gulp.task( 'lint-staged', tasks.lintStaged );
-		}
-	};
-
-	return tasks;
-
-	/**
-	 * Gets the list of ignores from `.gitignore`.
-	 *
-	 * @returns {String[]} The list of ignores.
-	 */
-	function getGitIgnore( ) {
-		let gitIgnoredFiles = fs.readFileSync( '.gitignore', 'utf8' );
-
-		return gitIgnoredFiles
-			// Remove comment lines.
-			.replace( /^#.*$/gm, '' )
-			// Transform into array.
-			.split( /\n+/ )
-			// Remove empty entries.
-			.filter( ( path ) => !!path )
-			// Add `!` for ignore glob.
-			.map( i => '!' + i );
-	}
-
-	/**
-	 * Returns stream with all linting plugins combined.
-	 *
-	 * @returns {Stream}
-	 */
-	function lint() {
-		const stream = jshint();
-		stream
-			.pipe( jscs() )
-			.pipe( jscs.reporter() )
-			.pipe( jshint.reporter( 'jshint-reporter-jscs' ) );
-
-		return stream;
-	}
-};

+ 0 - 130
dev/tests/lint.js

@@ -1,130 +0,0 @@
-/**
- * @license Copyright (c) 2003-2016, CKSource - Frederico Knabben. All rights reserved.
- * For licensing, see LICENSE.md.
- */
-
-/* global describe, it, beforeEach, afterEach */
-
-const sinon = require( 'sinon' );
-const gulp = require( 'gulp' );
-const gutil = require( 'gulp-util' );
-const stream = require( 'stream' );
-const Vinyl = require( 'vinyl' );
-const jshint = require( 'gulp-jshint' );
-const jscs = require( 'gulp-jscs' );
-const concat = require( 'concat-stream' );
-const chai = require( 'chai' );
-const expect = chai.expect;
-const mockery = require( 'mockery' );
-const through = require( 'through2' );
-
-describe( 'lint', () => {
-	'use strict';
-
-	const config = {
-		ROOT_DIR: '.',
-		BUILD_DIR: 'build',
-		IGNORED_FILES: [ 'lib/**' ]
-	};
-
-	let sandbox;
-
-	beforeEach( () => {
-		mockery.enable( {
-			warnOnReplace: false,
-			warnOnUnregistered: false
-		} );
-
-		mockery.registerMock( 'git-guppy', () => {
-			return {
-				stream() {
-					const files = [
-						new Vinyl( {
-							cwd: './',
-							path: './ckeditor.js',
-							contents: new Buffer( 'function test () {};var a;' )
-						} )
-					];
-					const fakeInputStream = new stream.Readable( { objectMode: true } );
-					fakeInputStream._read = () => {
-						fakeInputStream.push( files.pop() || null );
-					};
-
-					return fakeInputStream;
-				}
-			};
-		} );
-
-		sandbox = sinon.sandbox.create();
-	} );
-
-	afterEach( () => {
-		mockery.disable();
-		sandbox.restore();
-	} );
-
-	describe( 'lint()', () => {
-		it( 'should use jshint and jscs on source files', ( done ) => {
-			const files = [
-				new Vinyl( {
-					cwd: './',
-					path: './ckeditor.js',
-					contents: new Buffer( 'function test () {};var a;' )
-				} )
-			];
-
-			const tasks = require( '../tasks/lint/tasks' )( config );
-			const PassThrough = stream.PassThrough;
-
-			sandbox.stub( gulp, 'src', () => {
-				const fakeInputStream = new stream.Readable( { objectMode: true } );
-				fakeInputStream._read = () => {
-					fakeInputStream.push( files.pop() || null );
-				};
-
-				return fakeInputStream;
-			} );
-
-			sandbox.stub( jscs, 'reporter', () => new PassThrough( { objectMode: true } ) );
-			sandbox.stub( jshint, 'reporter', () => new PassThrough( { objectMode: true } ) );
-
-			tasks.lint().pipe( concat( ( data ) => {
-				expect( data.length ).to.equal( 1 );
-				const file = data[ 0 ];
-				expect( typeof file.jscs ).to.equal( 'object' );
-				expect( typeof file.jshint ).to.equal( 'object' );
-				expect( file.jscs.success ).to.equal( false );
-				expect( file.jshint.success ).to.equal( false );
-				done();
-			} ) );
-		} );
-	} );
-
-	describe( 'lintStaged', () => {
-		it( 'should throw error when linting fails', ( done ) => {
-			const tasks = require( '../tasks/lint/tasks' )( config );
-			const PassThrough = stream.PassThrough;
-
-			const exitStub = sandbox.stub( process, 'exit' );
-			sandbox.stub( gutil, 'log' );
-			sandbox.stub( jscs, 'reporter', ( type ) => {
-				if ( type == 'fail' ) {
-					// Fail reporter should report error to stop linting process.
-					return through( { objectMode: true }, ( file, encoding, cb ) => {
-						cb( new Error() );
-						expect( typeof file.jscs ).to.equal( 'object' );
-						expect( typeof file.jshint ).to.equal( 'object' );
-						expect( file.jscs.success ).to.equal( false );
-						expect( file.jshint.success ).to.equal( false );
-						sinon.assert.calledOnce( exitStub );
-						done();
-					} );
-				} else {
-					return new PassThrough( { objectMode: true } );
-				}
-			} );
-			sandbox.stub( jshint, 'reporter', () => new PassThrough( { objectMode: true } ) );
-			tasks.lintStaged();
-		} );
-	} );
-} );

+ 4 - 1
gulpfile.js

@@ -16,13 +16,16 @@ const config = {
 	]
 };
 
+const ckeditor5Lint = require( 'ckeditor5-dev-lint' )( config );
+
 require( './dev/tasks/build/tasks' )( config ).register();
 require( './dev/tasks/bundle/tasks' )( config ).register();
 require( './dev/tasks/dev/tasks' )( config ).register();
-require( './dev/tasks/lint/tasks' )( config ).register();
 require( './dev/tasks/test/tasks' )( config ).register();
 require( './dev/tasks/docs/tasks' )( config ).register();
 require( './dev/tasks/exec/tasks' )( config ).register();
 
+gulp.task( 'lint', ckeditor5Lint.lint );
+gulp.task( 'lint-staged', ckeditor5Lint.lintStaged );
 gulp.task( 'default', [ 'build' ] );
 gulp.task( 'pre-commit', [ 'lint-staged' ] );

+ 1 - 4
package.json

@@ -34,9 +34,8 @@
     "benderjs-promise": "^0.1.0",
     "benderjs-sinon": "^0.3.0",
     "chai": "^3.4.0",
+    "ckeditor5-dev-lint": "ckeditor/ckeditor5-dev-lint",
     "ckeditor5-dev-utils": "ckeditor/ckeditor5-dev-utils",
-    "concat-stream": "^1.5.1",
-    "git-guppy": "^1.1.0",
     "gulp": "^3.9.0",
     "gulp-babel": "^6.1.0",
     "gulp-cssnano": "^2.1.2",
@@ -60,8 +59,6 @@
     "gzip-size": "^3.0.0",
     "inquirer": "^0.11.0",
     "jsdoc": "^3.4.0",
-    "jshint": "^2.9.1",
-    "jshint-reporter-jscs": "^0.1.0",
     "merge-stream": "^1.0.0",
     "minimist": "^1.2.0",
     "mkdirp": "^0.5.1",