comment-regex.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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(prefix, suffix) {
  8. rx.lastIndex = 0;
  9. return rx.test(prefix + 'sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiIiwic291cmNlcyI6WyJmdW5jdGlvbiBmb28oKSB7XG4gY29uc29sZS5sb2coXCJoZWxsbyBJIGFtIGZvb1wiKTtcbiBjb25zb2xlLmxvZyhcIndobyBhcmUgeW91XCIpO1xufVxuXG5mb28oKTtcbiJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiQUFBQSJ9' + suffix)
  10. }
  11. // Source Map v2 Tests
  12. test('comment regex old spec - @', function (t) {
  13. [
  14. '//@ ',
  15. ' //@ ', // with leading space
  16. '\t//@ ', // with leading tab
  17. '//@ ', // with leading text
  18. '/*@ ', // multi line style
  19. ' /*@ ', // multi line style with leading spaces
  20. '\t/*@ ', // multi line style with leading tab
  21. '/*@ ', // multi line style with leading text
  22. ].forEach(function (x) { t.ok(comment(x, ""), 'matches ' + x) });
  23. [
  24. ' @// @',
  25. ' @/* @',
  26. ].forEach(function (x) { t.ok(!comment(x, ""), 'should not match ' + x) })
  27. t.end()
  28. })
  29. test('comment regex new spec - #', function (t) {
  30. [
  31. ' //# ', // with leading spaces
  32. '\t//# ', // with leading tab
  33. '//# ', // with leading text
  34. '/*# ', // multi line style
  35. ' /*# ', // multi line style with leading spaces
  36. '\t/*# ', // multi line style with leading tab
  37. '/*# ', // multi line style with leading text
  38. ].forEach(function (x) { t.ok(comment(x, ""), 'matches ' + x) });
  39. [
  40. ' #// #',
  41. ' #/* #',
  42. ].forEach(function (x) { t.ok(!comment(x, ""), 'should not match ' + x) })
  43. t.end()
  44. })
  45. function mapFileComment(s) {
  46. mapFileRx.lastIndex = 0;
  47. return mapFileRx.test(s + 'sourceMappingURL=foo.js.map')
  48. }
  49. test('mapFileComment regex old spec - @', function (t) {
  50. [
  51. '//@ ',
  52. ' //@ ',
  53. '\t//@ ',
  54. '///@ ',
  55. ].forEach(function (x) { t.ok(mapFileComment(x), 'matches ' + x) });
  56. [
  57. ' @// @',
  58. ].forEach(function (x) { t.ok(!mapFileComment(x), 'does not match ' + x) })
  59. t.end()
  60. })
  61. test('mapFileComment regex new spec - #', function (t) {
  62. [
  63. '//@ ',
  64. ' //@ ', // with leading space
  65. '\t//@ ', // with leading tab
  66. '//@ ', // with leading text
  67. ].forEach(function (x) { t.ok(mapFileComment(x), 'matches ' + x) });
  68. [
  69. ' #// #',
  70. ].forEach(function (x) { t.ok(!mapFileComment(x), 'does not match ' + x) })
  71. t.end()
  72. })
  73. function mapFileCommentWrap(s1, s2) {
  74. mapFileRx.lastIndex = 0;
  75. return mapFileRx.test(s1 + 'sourceMappingURL=foo.js.map' + s2)
  76. }
  77. test('mapFileComment regex /* */ old spec - @', function (t) {
  78. [ [ '/*@ ', '*/' ]
  79. , [' /*@ ', ' */ ' ] // with leading spaces
  80. , [ '\t/*@ ', ' \t*/\t '] // with a leading tab
  81. , [ 'leading string/*@ ', '*/' ] // with a leading string
  82. , [ '/*@ ', ' \t*/\t '] // with trailing whitespace
  83. ].forEach(function (x) { t.ok(mapFileCommentWrap(x[0], x[1]), 'matches ' + x.join(' :: ')) });
  84. [ ['/*@ ', ' */ */ ' ], // not the last thing on its line
  85. ['/*@ ', ' */ more text ' ] // not the last thing on its line
  86. ].forEach(function (x) { t.ok(!mapFileCommentWrap(x[0], x[1]), 'does not match ' + x.join(' :: ')) });
  87. t.end()
  88. })
  89. test('mapFileComment regex /* */ new spec - #', function (t) {
  90. [ [ '/*# ', '*/' ]
  91. , [' /*# ', ' */ ' ] // with leading spaces
  92. , [ '\t/*# ', ' \t*/\t '] // with a leading tab
  93. , [ 'leading string/*# ', '*/' ] // with a leading string
  94. , [ '/*# ', ' \t*/\t '] // with trailing whitespace
  95. ].forEach(function (x) { t.ok(mapFileCommentWrap(x[0], x[1]), 'matches ' + x.join(' :: ')) });
  96. [ ['/*# ', ' */ */ ' ], // not the last thing on its line
  97. ['/*# ', ' */ more text ' ] // not the last thing on its line
  98. ].forEach(function (x) { t.ok(!mapFileCommentWrap(x[0], x[1]), 'does not match ' + x.join(' :: ')) });
  99. t.end()
  100. })