Skip to content
This repository was archived by the owner on Sep 21, 2021. It is now read-only.

Commit 42f01cf

Browse files
authored
Support symmetric locations (#666)
1 parent 850b73a commit 42f01cf

File tree

9 files changed

+300
-183
lines changed

9 files changed

+300
-183
lines changed

packages/devtools-source-map/package.json

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@
66
"author": "J. Ryan Stinnett <[email protected]>",
77
"repository": "github:devtools-html/devtools-core",
88
"bugs": "https://github.com/devtools-html/devtools-core/issues",
9-
"homepage": "https://github.com/devtools-html/devtools-core/tree/master/packages/devtools-source-map#readme",
9+
"homepage":
10+
"https://github.com/devtools-html/devtools-core/tree/master/packages/devtools-source-map#readme",
1011
"main": "src/index.js",
1112
"scripts": {
1213
"copy-assets": "node bin/copy-assets",
@@ -29,7 +30,11 @@
2930
"jest": {
3031
"rootDir": "src",
3132
"testMatch": ["**/tests/**/*.js"],
32-
"testPathIgnorePatterns": ["/node_modules/", "/tests/fixtures/"],
33+
"testPathIgnorePatterns": [
34+
"/node_modules/",
35+
"/tests/fixtures/",
36+
"<rootDir>/tests/helpers.js"
37+
],
3338
"transformIgnorePatterns": [],
3439
"setupFiles": [],
3540
"moduleNameMapper": {}

packages/devtools-source-map/src/source-map.js

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -68,9 +68,8 @@ async function getGeneratedLocation(
6868

6969
return {
7070
sourceId: generatedSourceId,
71-
line: line,
72-
// Treat 0 as no column so that line breakpoints work correctly.
73-
column: column === 0 ? undefined : column
71+
line,
72+
column
7473
};
7574
}
7675

@@ -84,19 +83,19 @@ async function getOriginalLocation(location: Location): Promise<Location> {
8483
return location;
8584
}
8685

87-
const { source: url, line, column } = map.originalPositionFor({
86+
const { source: sourceUrl, line, column } = map.originalPositionFor({
8887
line: location.line,
89-
column: location.column == null ? Infinity : location.column
88+
column: location.column == null ? 0 : location.column
9089
});
9190

92-
if (url == null) {
91+
if (sourceUrl == null) {
9392
// No url means the location didn't map.
9493
return location;
9594
}
9695

9796
return {
98-
sourceId: generatedToOriginalId(location.sourceId, url),
99-
sourceUrl: url,
97+
sourceId: generatedToOriginalId(location.sourceId, sourceUrl),
98+
sourceUrl,
10099
line,
101100
column
102101
};
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
function componentWillReceiveProps(nextProps) {
2+
console.log('start');
3+
const { selectedSource } = nextProps;
4+
5+
if (
6+
nextProps.startPanelSize !== this.props.startPanelSize ||
7+
nextProps.endPanelSize !== this.props.endPanelSize
8+
) {
9+
this.state.editor.codeMirror.setSize();
10+
}
11+
console.log('done');
12+
}

packages/devtools-source-map/src/tests/fixtures/if.out.js

Lines changed: 14 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/devtools-source-map/src/tests/fixtures/if.out.js.map

Lines changed: 7 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
const { isOriginalId } = require("../utils");
2+
const { getOriginalURLs } = require("../source-map");
3+
4+
const fs = require("fs");
5+
const path = require("path");
6+
7+
function formatLocations(locs) {
8+
return locs.map(formatLocation).join(" -> ");
9+
}
10+
11+
function formatLocation(loc) {
12+
const label = isOriginalId(loc.sourceId) ? "O" : "G";
13+
const col = loc.column === undefined ? "u" : loc.column;
14+
return `${label}[${loc.line}, ${col}]`;
15+
}
16+
17+
function getMap(_path) {
18+
const mapPath = path.join(__dirname, _path);
19+
return fs.readFileSync(mapPath, "utf8");
20+
}
21+
22+
async function setupBundleFixture(name) {
23+
const source = {
24+
id: `${name}.js`,
25+
sourceMapURL: `${name}.js.map`,
26+
url: `http://example.com/${name}.js`
27+
};
28+
29+
require("devtools-utils/src/network-request").mockImplementationOnce(() => {
30+
const content = getMap(`fixtures/${name}.js.map`);
31+
return { content };
32+
});
33+
34+
return await getOriginalURLs(source);
35+
}
36+
37+
module.exports = {
38+
formatLocations,
39+
formatLocation,
40+
setupBundleFixture,
41+
getMap
42+
};
Lines changed: 153 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,153 @@
1+
/* This Source Code Form is subject to the terms of the Mozilla Public
2+
* License, v. 2.0. If a copy of the MPL was not distributed with this
3+
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
4+
5+
jest.mock("devtools-utils/src/network-request");
6+
const {
7+
getOriginalURLs,
8+
hasMappedSource,
9+
getOriginalLocation,
10+
getGeneratedLocation,
11+
clearSourceMaps
12+
} = require("../source-map");
13+
14+
const {
15+
formatLocations,
16+
formatLocation,
17+
setupBundleFixture,
18+
getMap
19+
} = require("./helpers");
20+
21+
describe("getOriginalLocation", async () => {
22+
beforeEach(() => {
23+
clearSourceMaps();
24+
});
25+
26+
test("maps a generated location", async () => {
27+
await setupBundleFixture("bundle");
28+
const location = {
29+
sourceId: "bundle.js",
30+
line: 49
31+
};
32+
33+
const originalLocation = await getOriginalLocation(location);
34+
expect(originalLocation).toEqual({
35+
column: 0,
36+
line: 3,
37+
sourceId: "bundle.js/originalSource-fe2c41d3535b76c158e39ba4f3ff826a",
38+
sourceUrl: "webpack:///entry.js"
39+
});
40+
});
41+
42+
test("does not map an original location", async () => {
43+
const location = {
44+
column: 0,
45+
line: 3,
46+
sourceId: "bundle.js/originalSource-fe2c41d3535b76c158e39ba4f3ff826a",
47+
sourceUrl: "webpack:///entry.js"
48+
};
49+
const originalLocation = await getOriginalLocation(location);
50+
expect(originalLocation).toEqual(originalLocation);
51+
});
52+
});
53+
54+
describe("getGeneratedLocation", async () => {
55+
beforeEach(() => {
56+
clearSourceMaps();
57+
});
58+
59+
test("maps an original location", async () => {
60+
await setupBundleFixture("bundle");
61+
const location = {
62+
column: 0,
63+
line: 3,
64+
sourceId: "bundle.js/originalSource-fe2c41d3535b76c158e39ba4f3ff826a"
65+
};
66+
67+
const source = {
68+
url: "webpack:///entry.js",
69+
id: "bundle.js/originalSource-fe2c41d3535b76c158e39ba4f3ff826a"
70+
};
71+
72+
const generatedLocation = await getGeneratedLocation(location, source);
73+
expect(generatedLocation).toEqual({
74+
sourceId: "bundle.js",
75+
line: 49,
76+
column: 0
77+
});
78+
});
79+
80+
test("location mapping is symmetric", async () => {
81+
// we expect symmetric mappings, which means that if
82+
// we map a generated location to an original location,
83+
// and then map it back, we should get the original generated value.
84+
// e.g. G[8, 0] -> O[5, 4] -> G[8, 0]
85+
86+
await setupBundleFixture("if.out");
87+
88+
const genLoc1 = {
89+
sourceId: "if.out.js",
90+
column: 0,
91+
line: 8
92+
};
93+
94+
const ifSource = {
95+
url: "if.js",
96+
id: "if.out.js/originalSource-5ad3141023dae912c5f8833c7e03beeb"
97+
};
98+
99+
const oLoc = await getOriginalLocation(genLoc1);
100+
const genLoc2 = await getGeneratedLocation(oLoc, ifSource);
101+
102+
expect(genLoc2).toEqual({
103+
sourceId: "if.out.js",
104+
column: 0,
105+
line: 8
106+
});
107+
});
108+
109+
test("undefined column is handled like 0 column", async () => {
110+
// we expect that an undefined column will be handled like a
111+
// location w/ column 0. e.g. G[8, u] -> O[5, 4] -> G[8, 0]
112+
113+
await setupBundleFixture("if.out");
114+
115+
const genLoc1 = {
116+
sourceId: "if.out.js",
117+
column: undefined,
118+
line: 8
119+
};
120+
121+
const ifSource = {
122+
url: "if.js",
123+
id: "if.out.js/originalSource-5ad3141023dae912c5f8833c7e03beeb"
124+
};
125+
126+
const oLoc = await getOriginalLocation(genLoc1);
127+
const genLoc2 = await getGeneratedLocation(oLoc, ifSource);
128+
// console.log(formatLocations([genLoc1, oLoc, genLoc2]));
129+
130+
expect(genLoc2).toEqual({
131+
sourceId: "if.out.js",
132+
column: 0,
133+
line: 8
134+
});
135+
});
136+
137+
test("does not map an original location", async () => {
138+
const location = {
139+
column: 0,
140+
line: 3,
141+
sourceId: "bundle.js/originalSource-fe2c41d3535b76c158e39ba4f3ff826a",
142+
sourceUrl: "webpack:///entry.js"
143+
};
144+
145+
const source = {
146+
url: "webpack:///entry.js",
147+
id: "bundle.js/originalSource-fe2c41d3535b76c158e39ba4f3ff826a"
148+
};
149+
150+
const generatedLocation = await getGeneratedLocation(location, source);
151+
expect(generatedLocation).toEqual(generatedLocation);
152+
});
153+
});

0 commit comments

Comments
 (0)