comment-regex.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. 'use strict';
  2. /*jshint asi: true */
  3. var test = require('tap').test
  4. , generator = require('inline-source-map')
  5. , rx = require('..').commentRegex
  6. , mapFileRx = require('..').mapFileCommentRegex
  7. function comment(s) {
  8. rx.lastIndex = 0;
  9. return rx.test(s + 'sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiIiwic291cmNlcyI6WyJmdW5jdGlvbiBmb28oKSB7XG4gY29uc29sZS5sb2coXCJoZWxsbyBJIGFtIGZvb1wiKTtcbiBjb25zb2xlLmxvZyhcIndobyBhcmUgeW91XCIpO1xufVxuXG5mb28oKTtcbiJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiQUFBQSJ9')
  10. }
  11. test('comment regex old spec - @', function (t) {
  12. [ '//@ '
  13. , ' //@ '
  14. , '\t//@ '
  15. ].forEach(function (x) { t.ok(comment(x), 'matches ' + x) });
  16. [ '///@ '
  17. , '}}//@ '
  18. , ' @// @'
  19. ].forEach(function (x) { t.ok(!comment(x), 'does not match ' + x) })
  20. t.end()
  21. })
  22. test('comment regex new spec - #', function (t) {
  23. [ '//# '
  24. , ' //# '
  25. , '\t//# '
  26. ].forEach(function (x) { t.ok(comment(x), 'matches ' + x) });
  27. [ '///# '
  28. , '}}//# '
  29. , ' #// #'
  30. ].forEach(function (x) { t.ok(!comment(x), 'does not match ' + x) })
  31. t.end()
  32. })
  33. function mapFileComment(s) {
  34. mapFileRx.lastIndex = 0;
  35. return mapFileRx.test(s + 'sourceMappingURL=foo.js.map')
  36. }
  37. test('mapFileComment regex old spec - @', function (t) {
  38. [ '//@ '
  39. , ' //@ '
  40. , '\t//@ '
  41. ].forEach(function (x) { t.ok(mapFileComment(x), 'matches ' + x) });
  42. [ '///@ '
  43. , '}}//@ '
  44. , ' @// @'
  45. ].forEach(function (x) { t.ok(!mapFileComment(x), 'does not match ' + x) })
  46. t.end()
  47. })
  48. test('mapFileComment regex new spec - #', function (t) {
  49. [ '//# '
  50. , ' //# '
  51. , '\t//# '
  52. ].forEach(function (x) { t.ok(mapFileComment(x), 'matches ' + x) });
  53. [ '///# '
  54. , '}}//# '
  55. , ' #// #'
  56. ].forEach(function (x) { t.ok(!mapFileComment(x), 'does not match ' + x) })
  57. t.end()
  58. })
  59. function mapFileCommentWrap(s1, s2) {
  60. mapFileRx.lastIndex = 0;
  61. return mapFileRx.test(s1 + 'sourceMappingURL=foo.js.map' + s2)
  62. }
  63. test('mapFileComment regex /* */ old spec - @', function (t) {
  64. [ [ '/*@ ', '*/' ]
  65. , [' /*@ ', ' */ ' ]
  66. , [ '\t/*@ ', ' \t*/\t ']
  67. ].forEach(function (x) { t.ok(mapFileCommentWrap(x[0], x[1]), 'matches ' + x.join(' :: ')) });
  68. [ [ '/*/*@ ', '*/' ]
  69. , ['}}/*@ ', ' */ ' ]
  70. , [ ' @/*@ ', ' \t*/\t ']
  71. ].forEach(function (x) { t.ok(!mapFileCommentWrap(x[0], x[1]), 'does not match ' + x.join(' :: ')) });
  72. t.end()
  73. })
  74. test('mapFileComment regex /* */ new spec - #', function (t) {
  75. [ [ '/*# ', '*/' ]
  76. , [' /*# ', ' */ ' ]
  77. , [ '\t/*# ', ' \t*/\t ']
  78. ].forEach(function (x) { t.ok(mapFileCommentWrap(x[0], x[1]), 'matches ' + x.join(' :: ')) });
  79. [ [ '/*/*# ', '*/' ]
  80. , ['}}/*# ', ' */ ' ]
  81. , [ ' #/*# ', ' \t*/\t ']
  82. ].forEach(function (x) { t.ok(!mapFileCommentWrap(x[0], x[1]), 'does not match ' + x.join(' :: ')) });
  83. t.end()
  84. })