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 2 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
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

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
42 changes: 29 additions & 13 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,12 +219,17 @@ 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 this function uses "returns_asyc", expand it out and call ourselves again to generate
chromatim marked this conversation as resolved.
Show resolved Hide resolved
// the valid signatures (either just callback or Promise and callback).
const expanded = this._maybeExpandFunctionReturnsAsync(spec);
if (expanded) {
if (expanded.withPromise && expanded.withCallback) {
oliverdunk marked this conversation as resolved.
Show resolved Hide resolved
return [
...this.expandFunctionParams(expanded.withPromise, id),
...this.expandFunctionParams(expanded.withCallback, id),
];
}
return [
...this.expandFunctionParams(expanded.withPromise, id),
...this.expandFunctionParams(expanded.withCallback, id),
];
}
Expand Down
Loading