Permalink
Browse files

Build: Verify that dist files are ASCII

  • Loading branch information...
1 parent a7bbfc9 commit e86f00921d0eb0f80a553fa9fd85c059d64a171a @gibson042 gibson042 committed Feb 4, 2016
Showing with 52 additions and 1 deletion.
  1. +4 −1 Gruntfile.js
  2. +48 −0 tasks/ensure_ascii.js
View
@@ -95,6 +95,9 @@ module.exports = function( grunt ) {
}
}
},
+ "ensure_ascii": {
+ files: [ "dist/*.js" ]
+ },
"compare_size": {
files: [ "dist/sizzle.js", "dist/sizzle.min.js" ],
options: {
@@ -265,7 +268,7 @@ module.exports = function( grunt ) {
// "karma:android", "karma:oldAndroid"
] : "karma:phantom" );
- grunt.registerTask( "build", [ "lint", "compile", "uglify", "dist" ] );
+ grunt.registerTask( "build", [ "lint", "compile", "uglify", "dist", "ensure_ascii" ] );
grunt.registerTask( "default", [ "build", "tests", "compare_size" ] );
grunt.registerTask( "bower", "bowercopy" );
View
@@ -0,0 +1,48 @@
+"use strict";
+
+var fs = require( "fs" );
+
+module.exports = function( grunt ) {
+ grunt.registerMultiTask(
+ "ensure_ascii",
+ "Verify that files contain no non-ASCII characters.",
+ function() {
+ var MAX_CONTEXT = 40,
+ reject = /[^\0-\x7F]/g,
+ pass = true;
+
+ this.filesSrc.forEach( function( file ) {
+ var first, firstIndex, lineStart, lineEnd, context,
+ text = fs.readFileSync( file, "utf8" ),
+ offenses = text.match( reject );
+
+ if ( offenses ) {
+ pass = false;
+ firstIndex = reject.exec( text ).index;
+ lineStart = text.lastIndexOf( "\n", firstIndex - 1 );
+ lineEnd = text.indexOf( "\n", firstIndex + 1 );
+ if ( lineEnd === -1 ) {
+ lineEnd = firstIndex + MAX_CONTEXT;
+ }
+ context = text.slice( lineStart + 1, lineEnd );
+ if ( context.length > MAX_CONTEXT ) {
+ if ( (firstIndex - lineStart) < MAX_CONTEXT ) {
+ context = context.slice( 0, MAX_CONTEXT );
+ } else {
+ context = context.substr(
+ firstIndex - lineStart - Math.floor( MAX_CONTEXT / 2 ),
+ MAX_CONTEXT
+ );
+ }
+ }
+ first = "U+" + ("000" + offenses[ 0 ].charCodeAt( 0 ).toString( 16 ))
+ .slice( -4 ).toUpperCase();
+ grunt.log.error( offenses.length + " non-ASCII character(s) in " + file );
+ grunt.log.error( "First (" + first + ") at index " + firstIndex + ": " +
+ context );
+ }
+ } );
+ return pass;
+ }
+ );
+};

0 comments on commit e86f009

Please sign in to comment.