remove-use-strict.js 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. /**
  2. * @license Copyright (c) 2003-2016, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. 'use strict';
  6. const gulp = require( 'gulp' );
  7. const path = require( 'path' );
  8. const replace = require( 'gulp-replace' );
  9. const mergeStream = require( 'merge-stream' );
  10. const filterGitignore = require( '../utils/filtergitignore' );
  11. const jshintrcDirs = [
  12. '/',
  13. 'dev/',
  14. 'tests/'
  15. ];
  16. /**
  17. * Removes lines with `'use strict';` directive.
  18. *
  19. * Example:
  20. *
  21. * gulp exec --task remove-use-strict
  22. *
  23. * @param {String} workdir
  24. * @returns {Stream}
  25. */
  26. module.exports = function executeRemoveUseStrict( workdir ) {
  27. return mergeStream(
  28. updateJshintrc( workdir ),
  29. removeUseStrict( workdir )
  30. );
  31. };
  32. // Updates .jshintrc file's `strict` option with `implied` value.
  33. //
  34. // @param {String} workdir Path of directory to be processed.
  35. // @returns {Stream}
  36. function updateJshintrc( workdir ) {
  37. const jshintrcGlob = jshintrcDirs.map(
  38. dir => path.join( workdir, dir, '.jshintrc' )
  39. );
  40. // Match everything after `"strict":` apart from optional comma. This should be matched into separate group.
  41. const strictRegex = /"strict":[^,\n\r]*(,?)$/m;
  42. const replaceWith = '"strict": "implied"';
  43. return gulp.src( jshintrcGlob, { base: workdir } )
  44. .pipe( replace(
  45. strictRegex,
  46. `${ replaceWith }$1`
  47. ) )
  48. .pipe( gulp.dest( workdir ) );
  49. }
  50. // Removes `'use strict';` directive from project's source files. Omits files listed in `.gitignore`.
  51. //
  52. // @param {String} workdir Path of directory to be processed.
  53. // @returns {Stream}
  54. function removeUseStrict( workdir ) {
  55. const glob = path.join( workdir, '**/*' );
  56. const useStrictRegex = /^\s*'use strict';\s*$/gm;
  57. return gulp.src( glob )
  58. .pipe( filterGitignore() )
  59. .pipe( replace(
  60. useStrictRegex,
  61. '',
  62. { skipBinary: true }
  63. ) )
  64. .pipe( gulp.dest( workdir ) );
  65. }