1 | 'use strict';
|
2 |
|
3 |
|
4 | var test = require('tap').test
|
5 | , generator = require('inline-source-map')
|
6 | , rx = require('..').commentRegex
|
7 | , mapFileRx = require('..').mapFileCommentRegex
|
8 |
|
9 | function comment(prefix, suffix) {
|
10 | rx.lastIndex = 0;
|
11 | return rx.test(prefix + 'sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiIiwic291cmNlcyI6WyJmdW5jdGlvbiBmb28oKSB7XG4gY29uc29sZS5sb2coXCJoZWxsbyBJIGFtIGZvb1wiKTtcbiBjb25zb2xlLmxvZyhcIndobyBhcmUgeW91XCIpO1xufVxuXG5mb28oKTtcbiJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiQUFBQSJ9' + suffix)
|
12 | }
|
13 |
|
14 | function 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 |
|
21 | test('comment regex old spec - @', function (t) {
|
22 | [
|
23 | '//@ ',
|
24 | ' //@ ',
|
25 | '\t//@ ',
|
26 | '//@ ',
|
27 | '/*@ ',
|
28 | ' /*@ ',
|
29 | '\t/*@ ',
|
30 | '/*@ ',
|
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 |
|
45 | test('comment regex new spec - #', function (t) {
|
46 | [
|
47 | ' //# ',
|
48 | '\t//# ',
|
49 | '//# ',
|
50 | '/*# ',
|
51 | ' /*# ',
|
52 | '\t/*# ',
|
53 | '/*# ',
|
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 |
|
68 | function mapFileCommentWrap(s1, s2) {
|
69 | mapFileRx.lastIndex = 0;
|
70 | return mapFileRx.test(s1 + 'sourceMappingURL=foo.js.map' + s2)
|
71 | }
|
72 |
|
73 | test('mapFileComment regex old spec - @', function (t) {
|
74 |
|
75 | [
|
76 | ['//@ ', ''],
|
77 | [' //@ ', ''],
|
78 | ['\t//@ ', ''],
|
79 | ['///@ ', ''],
|
80 | [';//@ ', ''],
|
81 | ['return//@ ', ''],
|
82 | ].forEach(function (x) { t.ok(mapFileCommentWrap(x[0], x[1]), 'matches ' + x.join(' :: ')) });
|
83 |
|
84 | [
|
85 | [' @// @', ''],
|
86 | ['var sm = "//@ ', '"'],
|
87 | ['var sm = \'//@ ', '\''],
|
88 | ['var sm = \' //@ ', '\''],
|
89 | ].forEach(function (x) { t.ok(!mapFileCommentWrap(x[0], x[1]), 'does not match ' + x.join(' :: ')) })
|
90 | t.end()
|
91 | })
|
92 |
|
93 | test('mapFileComment regex new spec - #', function (t) {
|
94 | [
|
95 | ['//# ', ''],
|
96 | [' //# ', ''],
|
97 | ['\t//# ', ''],
|
98 | ['///# ', ''],
|
99 | [';//# ', ''],
|
100 | ['return//# ', ''],
|
101 | ].forEach(function (x) { t.ok(mapFileCommentWrap(x[0], x[1]), 'matches ' + x.join(' :: ')) });
|
102 |
|
103 | [
|
104 | [' #// #', ''],
|
105 | ['var sm = "//# ', '"'],
|
106 | ['var sm = \'//# ', '\''],
|
107 | ['var sm = \' //# ', '\''],
|
108 | ].forEach(function (x) { t.ok(!mapFileCommentWrap(x[0], x[1]), 'does not match ' + x.join(' :: ')) })
|
109 | t.end()
|
110 | })
|
111 |
|
112 | test('mapFileComment regex /* */ old spec - @', function (t) {
|
113 | [ [ '/*@ ', '*/' ]
|
114 | , [' /*@ ', ' */ ' ]
|
115 | , [ '\t/*@ ', ' \t*/\t ']
|
116 | , [ 'leading string/*@ ', '*/' ]
|
117 | , [ '/*@ ', ' \t*/\t ']
|
118 | ].forEach(function (x) { t.ok(mapFileCommentWrap(x[0], x[1]), 'matches ' + x.join(' :: ')) });
|
119 |
|
120 | [ ['/*@ ', ' */ */ ' ],
|
121 | ['/*@ ', ' */ more text ' ]
|
122 | ].forEach(function (x) { t.ok(!mapFileCommentWrap(x[0], x[1]), 'does not match ' + x.join(' :: ')) });
|
123 | t.end()
|
124 | })
|
125 |
|
126 | test('mapFileComment regex /* */ new spec - #', function (t) {
|
127 | [ [ '/*# ', '*/' ]
|
128 | , [' /*# ', ' */ ' ]
|
129 | , [ '\t/*# ', ' \t*/\t ']
|
130 | , [ 'leading string/*# ', '*/' ]
|
131 | , [ '/*# ', ' \t*/\t ']
|
132 | ].forEach(function (x) { t.ok(mapFileCommentWrap(x[0], x[1]), 'matches ' + x.join(' :: ')) });
|
133 |
|
134 | [ ['/*# ', ' */ */ ' ],
|
135 | ['/*# ', ' */ more text ' ]
|
136 | ].forEach(function (x) { t.ok(!mapFileCommentWrap(x[0], x[1]), 'does not match ' + x.join(' :: ')) });
|
137 | t.end()
|
138 | })
|