8
0

remove-use-strict.js 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  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 filterGitignore = require( '../utils/filtergitignore' );
  10. const tools = require( '../../../utils/tools' );
  11. /**
  12. * Removes lines with `'use strict';` directive.
  13. *
  14. * Example:
  15. *
  16. * gulp exec --task remove-use-strict
  17. *
  18. * @param {String} workdir
  19. * @returns {Stream}
  20. */
  21. module.exports = function executeRemoveUseStrict( workdir ) {
  22. updateJshintrc( workdir );
  23. reformatDevsJshintrc( workdir );
  24. return removeUseStrict( workdir );
  25. };
  26. // Updates .jshintrc file's `strict` option with `implied` value.
  27. //
  28. // @param {String} workdir Path of directory to be processed.
  29. function updateJshintrc( workdir ) {
  30. [ '/', 'tests/' ].forEach(
  31. dir => {
  32. const jshintrcPath = path.join( workdir, dir, '.jshintrc' );
  33. tools.updateJSONFile( jshintrcPath, json => {
  34. json.strict = 'implied';
  35. return json;
  36. } );
  37. }
  38. );
  39. }
  40. // Only reformats (to match other .jshintrc files and package.json code style) the .jshintrc from dev/.
  41. //
  42. // @param {String} workdir Path of directory to be processed.
  43. function reformatDevsJshintrc( workdir ) {
  44. const jshintrcPath = path.join( workdir, 'dev', '.jshintrc' );
  45. tools.updateJSONFile( jshintrcPath, json => json );
  46. }
  47. // Removes `'use strict';` directive from project's source files. Omits files listed in `.gitignore`.
  48. //
  49. // @param {String} workdir Path of directory to be processed.
  50. // @returns {Stream}
  51. function removeUseStrict( workdir ) {
  52. const glob = path.join( workdir, '@(src|tests)/**/*.js' );
  53. const useStrictRegex = /^\s*'use strict';\s*$/gm;
  54. return gulp.src( glob )
  55. .pipe( filterGitignore() )
  56. .pipe( replace(
  57. useStrictRegex,
  58. '',
  59. { skipBinary: true }
  60. ) )
  61. .pipe( gulp.dest( workdir ) );
  62. }