Browse Source

Fix jumping to a location in debugger

Fix github #175.

Previous implementation was sometimes considering `state.scripts` to
be an array and sometimes an object literal. This resulted in the
variable to contain `undefined` values after each call to
`scriptAdded`.
run-to-here
Damien Cassou 3 years ago
parent
commit
75a708e90f
Signed by untrusted user: DamienCassou GPG Key ID: B68746238E59B548
3 changed files with 38 additions and 18 deletions
  1. +7
    -7
      server/adapters/cdp/helpers.js
  2. +12
    -11
      server/adapters/cdp/index.js
  3. +19
    -0
      server/spec/adapters/cdp/index-spec.js

+ 7
- 7
server/adapters/cdp/helpers.js View File

@ -152,13 +152,13 @@ const convertCallFrame = async (
};
};
const resolveFileLocation = async (location, conf, scripts = []) => {
const resolveFileLocation = async (location, conf, scripts = {}) => {
return await resolveFileLocationWithSourceMaps(location, conf, scripts)
|| resolveFileLocationWithScriptUrls(location, conf, scripts);
};
const resolveFileLocationWithSourceMaps = async ({ file, line }, conf, scripts) => {
for (let script of scripts) {
for (let script of Object.values(scripts)) {
let sourcemap = await getScriptSourceMap(script, conf);
if (sourcemap) {
@ -180,7 +180,7 @@ const resolveFileLocationWithSourceMaps = async ({ file, line }, conf, scripts)
};
const resolveFileLocationWithScriptUrls = ({ file, line }, conf, scripts) => {
for (let script of scripts) {
for (let script of Object.values(scripts)) {
if (script.url) {
let scriptFile = resolveUrl(script.url, conf);
if (file === scriptFile) {
@ -196,8 +196,8 @@ const resolveFileLocationWithScriptUrls = ({ file, line }, conf, scripts) => {
return null;
};
const resolveScriptLocation = async (location, conf, scripts = []) => {
let script = scripts.find(s => s && s.id === location.scriptId);
const resolveScriptLocation = async (location, conf, scripts = {}) => {
let script = Object.values(scripts).find(s => s.id === location.scriptId);
if (!script) {
return { ...location, url: "" };
}
@ -212,13 +212,13 @@ const resolveScriptLocation = async (location, conf, scripts = []) => {
);
};
const resolveUrlLocation = async (location, conf, scripts = []) => {
const resolveUrlLocation = async (location, conf, scripts = {}) => {
return await resolveUrlLocationWithSourceMaps(location, conf, scripts)
|| await resolveUrlLocationWithConfiguration(location, conf, scripts);
};
const resolveUrlLocationWithSourceMaps = async (location, conf, scripts) => {
let script = scripts.find(s => s.url === location.url);
let script = Object.values(scripts).find(s => s.url === location.url);
if (script && await getScriptSourceMap(script, conf)) {
let { source, line, column } = script.sourceMap.originalPositionFor(location);


+ 12
- 11
server/adapters/cdp/index.js View File

@ -17,7 +17,7 @@ const {
let state = {
client: null,
configuration: null,
scripts: [],
scripts: {},
breakpoints: {},
currentCallFrameId: null,
isChrome: false
@ -176,7 +176,7 @@ const continueToLocation = async fileLocation => {
);
if (location) {
let script = state.scripts.find(s => s.url === location.url);
let script = Object.values(state.scripts).find(s => s.url === location.url);
await state.client.Debugger.continueToLocation({
location: {
scriptId: script.id,
@ -196,13 +196,13 @@ const scriptAdded = ({ scriptId, url, sourceMapURL }) => {
let script = { id: scriptId, url, sourceMapURL };
// Remove any previous version of the same script first
for (let id in state.scripts) {
for (let id of Object.keys(state.scripts)) {
if (state.scripts[id].url === url) {
delete state.scripts[id];
}
}
state.scripts.push(script);
state.scripts[scriptId] = script;
// We've got new script, so let's try to resolve unresolved breakpoints
if (script.url || script.sourceMapURL) {
@ -228,7 +228,7 @@ const debuggerPaused = async ({ callFrames, reason, data = {} }) => {
frames: await convertCallFrames(
callFrames,
state.configuration,
Object.values(state.scripts)
state.scripts
),
reason: reason === "exception"
? "Exception occured"
@ -262,7 +262,7 @@ const registerBreakpoint = async breakpoint => {
let urlLocation = await resolveFileLocation(
breakpoint,
state.configuration,
Object.values(state.scripts)
state.scripts
);
// The breakpoint doesn't resolve to any location. The script might not
@ -298,7 +298,7 @@ const registerBreakpoint = async breakpoint => {
column: location.columnNumber
},
state.configuration,
Object.values(state.scripts)
state.scripts
);
breakpointResolved(breakpoint, line);
}
@ -370,8 +370,8 @@ const hasBreakpointAt = ({ id, file, line }) => {
const getSourcemapSources = async () => {
let sources = [];
for (let script of state.scripts) {
let sourcemap = script && await getScriptSourceMap(
for (let script of Object.values(state.scripts)) {
let sourcemap = await getScriptSourceMap(
script,
state.configuration
);
@ -386,7 +386,7 @@ const getSourcemapSources = async () => {
const getScriptSources = async () => {
let sources = [];
return state.scripts
return Object.values(state.scripts)
.filter(s => s.url)
.map(s => resolveUrl(s.url, state.configuration));
};
@ -472,5 +472,6 @@ module.exports = {
continueToLocation,
getSource,
getSourcemapSources,
getScriptSources
getScriptSources,
_test: {state, scriptAdded}
};

+ 19
- 0
server/spec/adapters/cdp/index-spec.js View File

@ -0,0 +1,19 @@
const {
_test: {state, scriptAdded}
} = require("../../../adapters/cdp/index");
// Regression test for GitHub issue #175
describe("Adding a new script with existing url", () => {
it("should not result in an undefined script", () => {
let id1 = "1";
let id2 = "2";
let url = "https://foo.bar";
scriptAdded({scriptId: id1, url});
scriptAdded({scriptId: id2, url});
let scripts = state.scripts;
expect(Object.keys(scripts)).toEqual([id2]);
});
});

Loading…
Cancel
Save