jenkins-bot has submitted this change and it was merged.

Change subject: build: Create a 'typos' task to detect common typos
......................................................................


build: Create a 'typos' task to detect common typos

Change-Id: Iba63e2766319b13fb0dd374eda240828d2a55f96
---
M Gruntfile.js
A build/tasks/typos.js
A build/typos.json
3 files changed, 113 insertions(+), 2 deletions(-)

Approvals:
  Trevor Parscal: Looks good to me, approved
  Esanders: Looks good to me, but someone else must approve
  jenkins-bot: Verified



diff --git a/Gruntfile.js b/Gruntfile.js
index 4240c86..c4a18cc 100644
--- a/Gruntfile.js
+++ b/Gruntfile.js
@@ -98,6 +98,12 @@
                                return files;
                        }() )
                },
+               typos: {
+                       options: {
+                               typos: 'build/typos.json'
+                       },
+                       src: '{src,php}/**/*.{js,json,less,css}'
+               },
                concat: {
                        options: {
                                banner: grunt.file.read( 'build/banner.txt' )
@@ -330,13 +336,13 @@
                'concat:css', 'cssjanus', 'csscomb', 'cssmin'
        ] );
        grunt.registerTask( 'build-i18n', [ 'copy:i18n' ] );
-       grunt.registerTask( 'build', [ 'clean:build', 'fileExists', 
'build-code', 'build-styling', 'build-i18n', 'clean:tmp' ] );
+       grunt.registerTask( 'build', [ 'clean:build', 'fileExists', 'typos', 
'build-code', 'build-styling', 'build-i18n', 'clean:tmp' ] );
 
        grunt.registerTask( 'git-build', [ 'pre-git-build', 'build' ] );
 
        // Quickly build a no-frills vector-only ltr-only version for 
development
        grunt.registerTask( 'quick-build', [
-               'pre-git-build', 'clean:build', 'fileExists',
+               'pre-git-build', 'clean:build', 'fileExists', 'typos',
                'concat:js',
                'copy:lessTemp', 'colorizeSvg', 'less:distSvg', 'copy:svg',
                'copy:imagesApex', 'copy:imagesMediaWiki',
diff --git a/build/tasks/typos.js b/build/tasks/typos.js
new file mode 100644
index 0000000..600cc80
--- /dev/null
+++ b/build/tasks/typos.js
@@ -0,0 +1,89 @@
+/*!
+ * Check files from 'src' for typos; fail if any are found.
+ */
+
+/*jshint node:true */
+module.exports = function ( grunt ) {
+
+       grunt.registerMultiTask( 'typos', function () {
+               var typosData, typosCSRegExp, typosCIRegExp, file,
+                       options = this.options( {
+                               typos: 'typos.json'
+                       } ),
+                       files = this.filesSrc,
+                       fileCount = files.length,
+                       typosJSON = grunt.file.read( options.typos ),
+                       typoCount = 0,
+                       ok = true;
+
+               try {
+                       typosData = JSON.parse( typosJSON );
+               } catch ( e ) {
+                       grunt.log.error( 'Could not parse ' + options.typos + 
': ' + e );
+               }
+
+               typosData.caseSensitive = typosData.caseSensitive || [];
+               typosData.caseInsensitive = typosData.caseInsensitive || [];
+
+               typoCount += typosData.caseSensitive.length + 
typosData.caseInsensitive.length;
+
+               function patternMap( typo ) {
+                       return typo[0];
+               }
+
+               if ( typosData.caseSensitive.length ) {
+                       typosCSRegExp = new RegExp(
+                               '(' + typosData.caseSensitive.map( patternMap 
).join( '|' )  + ')', 'g'
+                       );
+               }
+
+               if ( typosData.caseInsensitive.length ) {
+                       typosCIRegExp = new RegExp(
+                               '(' + typosData.caseInsensitive.map( patternMap 
).join( '|' )  + ')', 'gi'
+                       );
+               }
+
+               function findTypo( line, lineNumber, filepath, typos, flags ) {
+                       // Check each pattern to find the replacement
+                       typos.forEach( function ( typo ) {
+                               var replace,
+                                       pattern = new RegExp( typo[0], flags ),
+                                       matches = line.match( pattern );
+
+                               if ( matches ) {
+                                       ok = false;
+                                       replace = matches[0].replace( pattern, 
typo[1], flags );
+                                       grunt.log.error(
+                                               'File "' + filepath + '" 
contains typo "' + matches[0] + '" on line ' + lineNumber +
+                                               ', did you mean "' + replace + 
'"?'
+                                       );
+                               }
+                       } );
+               }
+
+               files.forEach( function ( filepath ) {
+                       if ( grunt.file.isDir( filepath ) ) {
+                               fileCount--;
+                               return;
+                       }
+                       file = grunt.file.read( filepath );
+                       file.split( '\n' ).forEach( function ( line, lineNumber 
) {
+                               // Check for any typos on the line with group 
expressions
+                               if ( typosCSRegExp && typosCSRegExp.test( line 
) ) {
+                                       findTypo( line, lineNumber, filepath, 
typosData.caseSensitive, 'g' );
+                               }
+                               if ( typosCIRegExp && typosCIRegExp.test( line 
) ) {
+                                       findTypo( line, lineNumber, filepath, 
typosData.caseInsensitive, 'gi' );
+                               }
+                       } );
+               } );
+
+               if ( !ok ) {
+                       return false;
+               }
+
+               grunt.log.ok( 'No typos found; ' +
+                       fileCount + ' file' + ( fileCount !== 1 ? 's' : '') + ' 
checked for ' +
+                       typoCount + ' typo' + ( typoCount !== 1 ? 's' : '') + 
'.' );
+       } );
+};
diff --git a/build/typos.json b/build/typos.json
new file mode 100644
index 0000000..faa0960
--- /dev/null
+++ b/build/typos.json
@@ -0,0 +1,16 @@
+{
+       "caseSensitive": [
+               [ "@returns", "@return" ]
+       ],
+       "caseInsensitive": [
+               [ "paralell", "parallel" ],
+               [ "properites", "properties" ],
+               [ "visiblit(ies|y)", "visibilit$1" ],
+               [ "movablilties", "movabilities" ],
+               [ "inpsect(ors?|ion)", "inspect$1" ],
+               [ "teh", "the" ],
+               [ "intialization", "initialization" ],
+               [ "durring", "during" ],
+               [ "occured", "occurred" ]
+       ]
+}
\ No newline at end of file

-- 
To view, visit https://gerrit.wikimedia.org/r/174147
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: merged
Gerrit-Change-Id: Iba63e2766319b13fb0dd374eda240828d2a55f96
Gerrit-PatchSet: 6
Gerrit-Project: oojs/ui
Gerrit-Branch: master
Gerrit-Owner: Jforrester <[email protected]>
Gerrit-Reviewer: Bartosz DziewoƄski <[email protected]>
Gerrit-Reviewer: Esanders <[email protected]>
Gerrit-Reviewer: Jforrester <[email protected]>
Gerrit-Reviewer: Krinkle <[email protected]>
Gerrit-Reviewer: Trevor Parscal <[email protected]>
Gerrit-Reviewer: jenkins-bot <>

_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to