UNPKG

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