comment-regex.js 5.4 KB

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