remove-use-strict.js 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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 filter = require( 'gulp-filter' );
  11. const { tools } = require( 'ckeditor5-dev-utils' );
  12. /**
  13. * Removes lines with `'use strict';` directive.
  14. *
  15. * Example:
  16. *
  17. * gulp exec --task remove-use-strict --include-root
  18. *
  19. * @param {String} workdir
  20. * @returns {Stream}
  21. */
  22. module.exports = function executeRemoveUseStrict( workdir ) {
  23. updateJshintrc( workdir );
  24. reformatOtherConfigs( workdir );
  25. return removeUseStrict( workdir );
  26. };
  27. // Updates .jshintrc file's `strict` option with `implied` value.
  28. //
  29. // @param {String} workdir Path of directory to be processed.
  30. function updateJshintrc( workdir ) {
  31. [ '/', 'tests/' ].forEach(
  32. dir => {
  33. const jshintrcPath = path.join( workdir, dir, '.jshintrc' );
  34. tools.updateJSONFile( jshintrcPath, json => {
  35. json.strict = 'implied';
  36. return json;
  37. } );
  38. }
  39. );
  40. }
  41. // Reformats (to match other .jshintrc files and package.json code style) the .jshintrc from dev/ and main .jscsrc.
  42. //
  43. // @param {String} workdir Path of directory to be processed.
  44. function reformatOtherConfigs( workdir ) {
  45. tools.updateJSONFile( path.join( workdir, 'dev', '.jshintrc' ), json => json );
  46. tools.updateJSONFile( path.join( workdir, '.jscsrc' ), json => json );
  47. }
  48. // Removes `'use strict';` directive from project's source files. Omits files listed in `.gitignore`.
  49. //
  50. // @param {String} workdir Path of directory to be processed.
  51. // @returns {Stream}
  52. function removeUseStrict( workdir ) {
  53. const glob = path.join( workdir, '**/*.js' );
  54. const filterDev = filter( '@(src|tests)/**/*.js', { restore: true } );
  55. const filterGulpfileAndBender = filter(
  56. [ 'gulpfile.js', 'dev/tasks/dev/templates/gulpfile.js', 'bender.js' ],
  57. { restore: true }
  58. );
  59. const useStrictRegex = /^\s*'use strict';\s*$/gm;
  60. const jshintInlineConfigRegex = /\/\* jshint( browser: false,)? node: true \*\//;
  61. return gulp.src( glob )
  62. .pipe( filterGitignore() )
  63. // Remove use strict from src/ and tests/.
  64. .pipe( filterDev )
  65. .pipe( replace(
  66. useStrictRegex,
  67. ''
  68. ) )
  69. .pipe( filterDev.restore )
  70. // Fix gulpfile.js and bender.js.
  71. .pipe( filterGulpfileAndBender )
  72. .pipe( replace(
  73. jshintInlineConfigRegex,
  74. '/* jshint browser: false, node: true, strict: true */'
  75. ) )
  76. .pipe( filterGulpfileAndBender.restore )
  77. .pipe( gulp.dest( workdir ) );
  78. }