microsoft/vscode-react-native

Public

mirrored from https://github.com/microsoft/vscode-react-nativeAvailable

CodeCommitsIssuesPull requestsActionsInsightsSecurity
7b48f26eb8caae7ed9cc1a09249195dfed76fa76

Branches

Tags

  • No tags available.
0Branches0Tags
Go to file
Add file
Code

Clone

HTTPS

Download ZIP

test/debugger/sourceMap.test.ts

234lines · modecode

1// Copyright (c) Microsoft Corporation. All rights reserved.
2// Licensed under the MIT license. See LICENSE file in the project root for details.
3
4import { SourceMapUtil, IStrictUrl } from "../../src/debugger/sourceMap";
5
6import assert = require("assert");
7import * as path from "path";
8import * as url from "url";
9
10suite("sourceMap", function () {
11 test("should convert host filesystem paths to URL-style-paths", function () {
12 const sourceMap = new SourceMapUtil();
13 const filePath = path.join("foo", "bar", "baz");
14 const urlPath = "foo/bar/baz";
15 const result = (<any>sourceMap).makeUnixStylePath(filePath);
16 assert(result === urlPath, `Expected "${urlPath}", found "${result}"`);
17 });
18
19 test("should ignore sourcemap urls in JavaScript objects and strings", function () {
20 const expectedUrlHref = "http://localhost:8081/index.map?platform=android&dev=true";
21 const sourceMap = new SourceMapUtil();
22
23 const testFunction = (scriptBody: string, expectedResult: string) => {
24 const matchResult = (<any>sourceMap).searchSourceMapURL(scriptBody);
25 assert.strictEqual(expectedResult, matchResult && matchResult[2]);
26 };
27
28 testFunction(
29 'var myTest = "\\n//# sourceMappingURL=test.min.js.map\\n\\n//# sourceMappingURL=test.min.js.map\\n\\n";\n' +
30 "//# sourceMappingURL=http://localhost:8081/index.map?platform=android&dev=true",
31 expectedUrlHref,
32 );
33
34 testFunction(
35 " var test_js = function test_js(cdn) {\n" +
36 ` return cdn === true ? '<script src="https://cdn.testjs.com/11.33.77/test.min.js"></script>' : "\\n <script>\\n` +
37 '!function(t,e){\\"object\\"==typeof exports&&\\"object\\"==typeof module?module.exports=e():\\"function\\"==typeof define&&define;' +
38 '\\n //# sourceMappingURL=test.min.js.map\\n </script>\\n";\n' +
39 "};\n" +
40 "//# sourceMappingURL=http://localhost:8081/index.map?platform=android&dev=true",
41 expectedUrlHref,
42 );
43 });
44
45 test("should resolve a valid sourcemap url", function () {
46 const scriptUrl: url.Url = url.parse(
47 "http://localhost:8081/index.ios.bundle?platform=ios&dev=true",
48 );
49 const scriptBody = "//# sourceMappingURL=/index.ios.map?platform=ios&dev=true";
50 const expectedUrlHref = "http://localhost:8081/index.ios.map?platform=ios&dev=true";
51
52 const sourceMap = new SourceMapUtil();
53 const result = sourceMap.getSourceMapURL(scriptUrl, scriptBody);
54 assert.strictEqual(expectedUrlHref, result && result.href);
55 });
56
57 test("should ignore inline sourcemap urls", function () {
58 const scriptUrl: url.Url = url.parse(
59 "http://localhost:8081/index.ios.bundle?platform=ios&dev=true",
60 );
61 const scriptBody =
62 "//# sourceMappingURL=data:application/json;base64,eyJmb28iOiJiYXIifQ==\n" +
63 "//# sourceMappingURL=/index.ios.map?platform=ios&dev=true";
64 const expectedUrlHref = "http://localhost:8081/index.ios.map?platform=ios&dev=true";
65
66 const sourceMap = new SourceMapUtil();
67 const result = sourceMap.getSourceMapURL(scriptUrl, scriptBody);
68 assert.strictEqual(expectedUrlHref, result && result.href);
69 });
70
71 test("should return correct sourcemap url for RN macOS", function () {
72 const scriptUrl: url.Url = url.parse(
73 "http://localhost:8081/index.bundle?platform=macos&dev=true",
74 );
75 const scriptBody =
76 "//# sourceMappingURL=//localhost:8081/index.map?platform=macos&dev=true";
77 const expectedUrlHref = "http://localhost:8081/index.map?platform=macos&dev=true";
78
79 const sourceMap = new SourceMapUtil();
80 const result = sourceMap.getSourceMapURL(scriptUrl, scriptBody);
81 assert.strictEqual(expectedUrlHref, result && result.href);
82 });
83
84 test("should return default IStrictUrl for an invalid sourcemap url", function () {
85 const scriptUrl: url.Url = url.parse(
86 "http://localhost:8081/index.ios.bundle?platform=ios&dev=true",
87 );
88 const scriptBody = "";
89
90 const sourceMap = new SourceMapUtil();
91 const result = sourceMap.getSourceMapURL(scriptUrl, scriptBody);
92 assert.deepStrictEqual(null, result);
93 });
94
95 test("should return default IStrictUrl if there are only inline sourcemap urls", function () {
96 const scriptUrl: url.Url = url.parse(
97 "http://localhost:8081/index.ios.bundle?platform=ios&dev=true",
98 );
99 const scriptBody =
100 "//# sourceMappingURL=data:application/json;base64,eyJmb28iOiJiYXIifQ==\n" +
101 "//# sourceMappingURL=data:application/json;base64,eyJiYXoiOiJxdXV4In0=";
102
103 const sourceMap = new SourceMapUtil();
104 const result = sourceMap.getSourceMapURL(scriptUrl, scriptBody);
105 assert.deepStrictEqual(null, result);
106 });
107
108 test("should update the contents of a source map file", function () {
109 const sourceMapBody: string = JSON.stringify({
110 version: 3,
111 sources: ["test/index.ts"],
112 names: [],
113 mappings: "",
114 file: "test/index.js",
115 sourceRoot: "../../src",
116 });
117 const scriptPath: string = "test/newIndex.ts";
118 const sourcesRootPath: string = "new/src";
119 const expectedSourceMapBody: string = JSON.stringify({
120 version: 3,
121 sources: ["../../test/index.ts"],
122 names: [],
123 mappings: "",
124 file: scriptPath,
125 sourceRoot: "",
126 });
127 const sourceMap = new SourceMapUtil();
128
129 const result: string = sourceMap.updateSourceMapFile(
130 sourceMapBody,
131 scriptPath,
132 sourcesRootPath,
133 );
134 assert.strictEqual(expectedSourceMapBody, result);
135 });
136
137 test("should update source map file path for remote packager", function () {
138 const localRoot = "/home/local";
139 const remoteRoot = "/home/remote";
140 const sourceMapBody: string = JSON.stringify({
141 version: 3,
142 sources: [`${remoteRoot}/test/index.ts`],
143 names: [],
144 mappings: "",
145 file: "test/index.js",
146 sourceRoot: "../../src",
147 });
148 const scriptPath: string = "test/newIndex.ts";
149 const sourcesRootPath: string = `${localRoot}/new/src`;
150 const expectedSourceMapBody: string = JSON.stringify({
151 version: 3,
152 sources: [`../../test/index.ts`],
153 names: [],
154 mappings: "",
155 file: scriptPath,
156 sourceRoot: "",
157 });
158 const sourceMap = new SourceMapUtil();
159
160 const result: string = sourceMap.updateSourceMapFile(
161 sourceMapBody,
162 scriptPath,
163 sourcesRootPath,
164 remoteRoot,
165 localRoot,
166 );
167 assert.strictEqual(expectedSourceMapBody, result);
168 });
169
170 test("should update scripts with source mapping urls", function () {
171 const scriptBody: string = "//# sourceMappingURL=/index.ios.map?platform=ios&dev=true";
172 const sourceMappingUrl: IStrictUrl = <IStrictUrl>url.parse("/index.android.map");
173 const expectedScriptBody = "//# sourceMappingURL=index.android.map";
174 const sourceMap = new SourceMapUtil();
175
176 const result = sourceMap.updateScriptPaths(scriptBody, sourceMappingUrl);
177 assert.strictEqual(expectedScriptBody, result);
178 });
179
180 test("should not update scripts without source mapping urls", function () {
181 const scriptBody: string = "var path = require('path');";
182 const sourceMappingUrl: IStrictUrl = <IStrictUrl>url.parse("/index.android.map");
183 const sourceMap = new SourceMapUtil();
184
185 const result = sourceMap.updateScriptPaths(scriptBody, sourceMappingUrl);
186 assert.strictEqual(scriptBody, result);
187 });
188
189 test("should update absolute source path to relative unix style path", function () {
190 const sourcePath: string = "foo/bar";
191 const sourcesRootPath: string = "baz/fuzz";
192 const expectedPath: string = "../../foo/bar";
193 const sourceMap = new SourceMapUtil();
194
195 const result = (<any>sourceMap).updateSourceMapPath(sourcePath, sourcesRootPath);
196 assert.strictEqual(expectedPath, result);
197 });
198
199 test("should get only the latest sourceMappingURL", function () {
200 const scriptBody: string = `//# sourceMappingURL=abort.controller.js.map
201//# sourceMappingURL=index.map
202//# sourceURL=http://localhost:8081/index.bundle?platform=android&dev=true&minify=false`;
203 const expectedScriptBody = `index.map`;
204 const sourceMap = new SourceMapUtil();
205
206 const result = sourceMap.getSourceMapRelativeUrl(scriptBody);
207 assert.strictEqual(expectedScriptBody, result);
208 });
209
210 test("should remove sourceURL from the bundle script body correctly", function () {
211 const scriptBody: string = `var sourceURL = '//# sourceURL=' + (hasOwnProperty.call(options, 'sourceURL') ? (options.sourceURL + '').replace(/[\\r\\n]/g, ' ') : 'lodash.templateSources[' + ++templateCounter + ']') + '\\n';
212//# sourceMappingURL=index.map
213//# sourceURL=http://localhost:8081/index.bundle?platform=android&dev=true&minify=false`;
214 const expectedScriptBody = `var sourceURL = '//# sourceURL=' + (hasOwnProperty.call(options, 'sourceURL') ? (options.sourceURL + '').replace(/[\\r\\n]/g, ' ') : 'lodash.templateSources[' + ++templateCounter + ']') + '\\n';
215//# sourceMappingURL=index.map\n`;
216 const sourceMap = new SourceMapUtil();
217
218 const result = sourceMap.removeSourceURL(scriptBody);
219 assert.strictEqual(expectedScriptBody, result);
220 });
221
222 test("should remove sourceURL if it is before sourceMappingURL", function () {
223 const scriptBody: string = `var sourceURL = '//# sourceURL=' + (hasOwnProperty.call(options, 'sourceURL') ? (options.sourceURL + '').replace(/[\\r\\n]/g, ' ') : 'lodash.templateSources[' + ++templateCounter + ']') + '\\n';
224//# sourceURL=http://localhost:8081/index.bundle?platform=android&dev=true&minify=false
225//# sourceMappingURL=index.map`;
226 const expectedScriptBody = `var sourceURL = '//# sourceURL=' + (hasOwnProperty.call(options, 'sourceURL') ? (options.sourceURL + '').replace(/[\\r\\n]/g, ' ') : 'lodash.templateSources[' + ++templateCounter + ']') + '\\n';
227
228//# sourceMappingURL=index.map`;
229 const sourceMap = new SourceMapUtil();
230
231 const result = sourceMap.removeSourceURL(scriptBody);
232 assert.strictEqual(expectedScriptBody, result);
233 });
234});
235