Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Set up chrome-types for upcoming schema changes. #67

Merged
merged 5 commits into from
Feb 5, 2024
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
32 changes: 31 additions & 1 deletion tests/lib/traverse.js
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,6 @@ test('expandFunctionParams returns', t => {

});


test('expandFunctionParams returns_async', t => {
const filter = () => true;
const tc = new traverse.TraverseContext(filter);
Expand Down Expand Up @@ -197,6 +196,37 @@ test('expandFunctionParams returns_async', t => {
]);
});

test('expandFunctionParams returns_async does_not_support_promises', t => {
const filter = () => true;
const tc = new traverse.TraverseContext(filter);

/** @type {chromeTypes.TypeSpec} */
const returnsAsyncFunction = {
type: 'function',
parameters: [
{ type: 'string', name: 's' },
],
returns_async: {
name: 'callback',
type: 'function',
does_not_support_promises: 'For testing',
oliverdunk marked this conversation as resolved.
Show resolved Hide resolved
parameters: [{ type: 'number', name: 'whatever' }],
},
};

const returnsAsyncFunctionExpansions = tc.expandFunctionParams(
cloneObject(returnsAsyncFunction),
'api:test',
);
// Since this does not support promises, we expect to only get the callback signature.
t.deepEqual(returnsAsyncFunctionExpansions, [
[
{ type: 'void', name: 'return' },
{ type: 'string', name: 's' },
{ ...returnsAsyncFunction.returns_async },
]
]);
});

test('filter', t => {
/** @type {(spec: chromeTypes.TypeSpec, id: string) => boolean} */
Expand Down
41 changes: 29 additions & 12 deletions tools/lib/traverse.js
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,24 @@ export class TraverseContext {
if (!returns_async) {
return undefined;
}

/** @type {chromeTypes.TypeSpec} */
let callbackParameter = {
...returns_async,
type: 'function',
};
// If this signature doesn't support promises we just convert the "returns_async" field to a
// callback.
if (returns_async.does_not_support_promises) {
return {
withCallback: {
oliverdunk marked this conversation as resolved.
Show resolved Hide resolved
...clone,
parameters: [...spec.parameters ?? [], callbackParameter],
},
};
}

// Otherwise this is a promise signature, so we can do a few checks to make sure it is valid.
if (spec.returns) {
throw new Error(`got returns_async and returns on function spec: ${JSON.stringify(spec)}`);
}
Expand All @@ -168,15 +186,8 @@ export class TraverseContext {
};
}

// We convert the "returns_async" field to a callback (it has all the same values).
// Force it to be optional: by definition if it's omitted then we'll have a Promise-returning
// version. This isn't done correctly in the source.
/** @type {chromeTypes.TypeSpec} */
const callbackParameter = {
...returns_async,
type: 'function',
optional: true,
};
// For promise supporting functions, the callback in inheriently optional, so we force that here.
callbackParameter.optional = true;

return {
withPromise: {
Expand All @@ -198,7 +209,7 @@ export class TraverseContext {

/**
* Chrome supports "early" optional parameters, as well as a special "returns_async" parameter
* which actually means that this returns a `Promise` OR supports a callback.
* which actually means that this API may return a `Promise` OR supports a callback.
*
* This expands all possible combinations for rendering as multiple signatures. The return type
* includes the signature's return in the 0th position, followed by all parameters.
Expand All @@ -208,8 +219,14 @@ export class TraverseContext {
* @return {[chromeTypes.NamedTypeSpec, ...chromeTypes.NamedTypeSpec[]][]}
*/
expandFunctionParams(spec, id) {
// If this is actually a Promise-supporting API, then we call ourselves again to support both
// callback and Promise-based versions.
if (!spec) {
return [];
}

// If this function uses "returns_async", expand it out and call ourselves again to generate
// the valid signatures for both Promise and callback versions. Note: a few functions with
// asynchronous returns don't support a promise version, in which case withPromise is
// undefined here and will return an empty array (handled just above this).
const expanded = this._maybeExpandFunctionReturnsAsync(spec);
if (expanded) {
return [
Expand Down
3 changes: 3 additions & 0 deletions types/chrome.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,9 @@ export interface TypeSpec extends SharedSpec {
// only for top-level namespace types
noinline_doc?: boolean | 'True';

// only for returns_async types
does_not_support_promises?: string;

// special to mark converted-from-event-to-property, not part of definition
_event?: boolean;
}
Expand Down
Loading