/*---------------------------------------------------------
* Copyright (C) Microsoft Corporation. All rights reserved.
*--------------------------------------------------------*/
import { diffArrays } from 'diff';
import { Fragment, FunctionComponent, h } from 'preact';
import { useState } from 'preact/hooks';
import {
DiagnosticBreakpointArgs,
IDiagnosticBreakpoint,
IDiagnosticDump,
IDiagnosticUiLocation,
} from '../adapter/diagnosics';
import Cdp from '../cdp/api';
import { flatten } from '../common/objUtils';
import { DecisionButtons } from './decisionButtons';
import { basename, isBrowserType, isNodeType, prettyName } from './diagnosticPaths';
import { useDump } from './useDump';
import { usePersistedState } from './usePersistentState';
export const BreakpointHelper: FunctionComponent = () => {
const dump = useDump();
return (
{dump.breakpoints.map((bp, i) => )}
);
};
const hasAnyMatchedCdpBreakpoint = (bp: IDiagnosticBreakpoint, dump: IDiagnosticDump) =>
bp.cdp.some(bp => {
if ('location' in bp.args) {
return true; // set by script id
}
if (bp.args.url) {
const url = bp.args.url;
return dump.sources.some(s => s.url === url);
}
if (bp.args.urlRegex) {
const re = new RegExp(bp.args.urlRegex);
return dump.sources.some(s => re.test(s.url));
}
return false;
});
const buildTracing = (bp: IDiagnosticBreakpoint, dump: IDiagnosticDump) => {
let key = 0;
const steps = [
✅ This breakpoint was initially set in:
{bp.source.path} line {bp.params.line} column {bp.params.column || 1}
,
];
if (!hasAnyMatchedCdpBreakpoint(bp, dump)) {
steps.push();
return steps;
}
steps.push(
✅ In the runtime, the breakpoint was set in:
{bp.cdp.map((cdp, i) => )}
,
);
const applied = bp.cdp.filter(cdp => cdp.state === 1 /* Applied */);
const uiLocations = flatten(
applied.map(a => (a.state === 1 /* Applied */ ? a.uiLocations : [])),
);
if (!uiLocations.length) {
steps.push(
,
);
return steps;
}
steps.push(
✅ The runtime acknowledged and adjusted the breakpoint, and it mapped back to the following
locations:
{uiLocations.map((l, i) => )}
,
If this is not right, your compiled code might be out of date with your sources. If you
don't think this is the case and something else is wrong, please{' '}
open an issue!
,
);
return steps;
};
const NoUiLocation: FunctionComponent = () => {
const dump = useDump();
return (
❓ We sent the breakpoint, but it didn't bind to any locations. If this is unexpected:
-
Make sure that your program is loading or running this script. You can add a{' '}
debugger; statement to check this: your program will pause when it hits it.
-
If your breakpoint is set in certain places, such as on the last empty line of a file, the
runtime might not be able to find anywhere to place it.
{isNodeType(dump) && (
-
Unless you{' '}
run with --nolazy
, Node.js might not resolve breakpoints for code it hasn't parsed yet.
)}
- If necessary, make sure your compiled files are up-to-date with your source files.
);
};
const Breakpoint: FunctionComponent<{ bp: IDiagnosticBreakpoint }> = ({ bp }) => {
if (!bp.source.path) {
return null;
}
const dump = useDump();
return (
{prettyName(
{ absolutePath: bp.source.path as string, url: bp.source.path as string },
dump,
)}
:{bp.params.line}:{bp.params.column || 1}
);
};
const FailedToSetLocation: FunctionComponent<{ bp: IDiagnosticBreakpoint }> = ({ bp }) => {
const dump = useDump();
const desiredBasename = basename({ url: bp.source.path as string });
const matchingSources = dump.sources.filter(
src => basename(src).toLowerCase() === desiredBasename.toLowerCase(),
);
if (!matchingSources.length) {
return (
);
}
return (
❓ We couldn't find a corresponding source location, but found some other files with the
same name:
{matchingSources.map(s => (
-
))}
{isBrowserType(dump)
? (
You may need to adjust the webRoot in your launch.json{' '}
if you're building from a subfolder, or tweak your sourceMapPathOverrides.
)
: (
If this is the same file, you may need to adjust your build tool{' '}
{isBrowserType(dump) && (
or webRoot in the launch.json
)} to correct the paths.
)}
);
};
const TextDiff: FunctionComponent<{ original: string; updated: string }> = ({
original,
updated,
}) => (
{diffArrays(original.split(/[/\\]/g), updated.split(/[/\\]/g), { ignoreCase: true }).map(
(diff, i) => (
{i > 0 ? '/' : ''}
{diff.value.join('/')}
),
)}
);
const UiLocation: FunctionComponent<{ loc: IDiagnosticUiLocation }> = ({ loc }) => {
const dump = useDump();
const source = dump.sources.find(s => s.sourceReference === loc.sourceReference);
return (
{source?.absolutePath ?? source?.url ?? 'unknown'} line {loc.lineNumber} column
{' '}
{loc.columnNumber}
);
};
const CdpBreakpoint: FunctionComponent<{ cdp: DiagnosticBreakpointArgs; index: number }> = ({
cdp,
index,
}) => {
const dump = useDump();
const [showRegex, setShowRegex] = usePersistedState(`showCdpBp${index}`, false);
const { url, line, col, regex } = 'location' in cdp.args
? {
url: dump.sources.find(
s =>
!s.compiledSourceRefToUrl
&& s.scriptIds.includes(
(cdp.args as Cdp.Debugger.SetBreakpointParams).location.scriptId,
),
)?.url,
regex: undefined,
line: cdp.args.location.lineNumber + 1,
col: (cdp.args.location.columnNumber || 0) + 1,
}
: {
url: cdp.args.urlRegex ? demangleUrlRegex(cdp.args.urlRegex) : cdp.args.url,
regex: cdp.args.urlRegex,
line: cdp.args.lineNumber + 1,
col: (cdp.args.columnNumber || 0) + 1,
};
return (
{url} line {line} column {col}{' '}
{regex && setShowRegex(!showRegex)}>via this regex}
{showRegex && (
{regex}
)}
);
};
const demangleUrlRegex = (re: string) =>
re
.replace(/\[([[a-z])[A-Z]\]/g, (_, letter) => letter) // ugly case-sensivity regex groups
.replace(/\\\\/, '\\') // escaped backslashes
.replace(/\\\//g, '/') // escaped forward slashes
.replace(/\|.+$/g, '') // drive absolute path (only keep file uri)
.replace(/\\\./g, '.'); // escaped .
const enum NoMatchingSourceHint {
Direct = 'Loaded in directly',
SourceMap = 'Be parsed from a sourcemap',
}
const NoMatchingDecisionButtons = DecisionButtons([
NoMatchingSourceHint.Direct,
NoMatchingSourceHint.SourceMap,
]);
const NoMatchingSourceHelper: FunctionComponent<{ basename: string }> = ({ basename }) => {
const dump = useDump();
const [hint, setHint] = useState(
!basename.endsWith('.js') ? NoMatchingSourceHint.SourceMap : undefined,
);
return (
❓ We couldn't find a corresponding source location, and didn't find any source with the
name {basename}.
How did you expect this file to be loaded? (If you have a compilation step, you should pick
'sourcemap')
{hint === NoMatchingSourceHint.Direct
&& (isBrowserType(dump)
? (
It looks like your webpage didn't load this script; breakpoints won't be bound until
the file they're set in is loaded. Make sure your script is imported from the right
location using a {'