Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 24 additions & 9 deletions src/adapter/completions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,21 @@ const validIdentifierRe = /^[$_\p{ID_Start}][$_\u200C\u200D\p{ID_Continue}]*$/u;
/**
* Provides REPL completions for the debug session.
*/

/**
* Serialize a CDP primitive RemoteObject into a JS expression suitable for
* interpolating into {@link enumeratePropertiesTemplate}.
*/
function expressionForRemotePrimitive(object: Cdp.Runtime.RemoteObject): string {
if (object.unserializableValue) {
return object.unserializableValue;
}
if (object.type === 'undefined') {
return 'undefined';
}
return JSON.stringify(object.value);
}

@injectable()
export class Completions {
constructor(
Expand Down Expand Up @@ -340,26 +355,26 @@ export class Completions {
return { result: [], isArray: false };
}

// No object ID indicates a primitive. Call enumeration on the value
// directly. We don't do this all the time, since our enumeration logic
// triggers Chrome's side-effect detect and fails.
// No object ID indicates a primitive. Enumerate from the already-evaluated
// value rather than re-reading the expression on the call frame: the
// template binds its own locals (minified names like `n`/`target`), which
// would otherwise shadow a same-named user binding (#2399).
if (!objRefResult.result.objectId) {
const primitiveParams = {
...params,
returnByValue: true,
throwOnSideEffect: false,
expression: enumeratePropertiesTemplate.expr(
`(${expression})`,
expressionForRemotePrimitive(objRefResult.result),
JSON.stringify(prefix),
JSON.stringify(isInGlobalScope),
),
};

const propsResult = await this.evaluator.evaluate(
callFrameId
? { ...primitiveParams, callFrameId }
: { ...primitiveParams, contextId: executionContextId },
);
const propsResult = await this.evaluator.evaluate({
...primitiveParams,
contextId: executionContextId,
});

return !propsResult || propsResult.exceptionDetails
? { result: [], isArray: false }
Expand Down
44 changes: 44 additions & 0 deletions src/test/completion/completion.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -310,6 +310,50 @@ describe('completion', () => {
]);
});


itIntegrates('completes primitive when name clashes with template locals (#2399)', async ({ r }) => {
const p = await r.launch(`
<script>
function foo() {
const n = 1;
const target = 'abc';
debugger;
}

foo();
</script>
`);

const untilStopped = p.dap.once('stopped');
p.load();

const frame = (
await p.dap.stackTrace({
threadId: (await untilStopped).threadId!,
})
).stackFrames[0];

const forN = await p.dap.completions({
text: 'n.',
column: 3,
frameId: frame.id,
});
expect(forN.targets.some(t => t.label === 'toFixed')).to.equal(
true,
`expected number completions for n., got ${JSON.stringify(forN.targets.slice(0, 5))}`,
);

const forTarget = await p.dap.completions({
text: 'target.',
column: 8,
frameId: frame.id,
});
expect(forTarget.targets.some(t => t.label === 'toLowerCase' || t.label === 'length')).to.equal(
true,
`expected string completions for target., got ${JSON.stringify(forTarget.targets.slice(0, 5))}`,
);
});

itIntegrates('$returnValue', async ({ r }) => {
const getFrameId = async () =>
(
Expand Down