Files
react-native-devtools-frontend/node_modules/webidl2/lib/productions/operation.js
T
Johan Bay 986acd0681 Add dependencies for js signature generation to top-level node_modules
This change makes it easier to run the script from directly
`roll_py.deps` without having to fetch the dependencies first. This also
makes it easier to track which dependencies our tooling have.

Change-Id: I1ce55074ee6a1fffa6f2a2f05a56d0d74b4aca90
Bug: 1255619
Reviewed-on: https://chromium-review.googlesource.com/c/devtools/devtools-frontend/+/3431346
Reviewed-by: Mathias Bynens <mathias@chromium.org>
Commit-Queue: Johan Bay <jobay@chromium.org>
2022-02-02 17:33:42 +00:00

105 lines
2.9 KiB
JavaScript

import { Base } from "./base.js";
import {
return_type,
argument_list,
unescape,
autoParenter,
} from "./helpers.js";
import { validationError } from "../error.js";
export class Operation extends Base {
/**
* @typedef {import("../tokeniser.js").Token} Token
*
* @param {import("../tokeniser.js").Tokeniser} tokeniser
* @param {object} [options]
* @param {Token} [options.special]
* @param {Token} [options.regular]
*/
static parse(tokeniser, { special, regular } = {}) {
const tokens = { special };
const ret = autoParenter(
new Operation({ source: tokeniser.source, tokens })
);
if (special && special.value === "stringifier") {
tokens.termination = tokeniser.consume(";");
if (tokens.termination) {
ret.arguments = [];
return ret;
}
}
if (!special && !regular) {
tokens.special = tokeniser.consume("getter", "setter", "deleter");
}
ret.idlType =
return_type(tokeniser) || tokeniser.error("Missing return type");
tokens.name =
tokeniser.consumeKind("identifier") || tokeniser.consume("includes");
tokens.open =
tokeniser.consume("(") || tokeniser.error("Invalid operation");
ret.arguments = argument_list(tokeniser);
tokens.close =
tokeniser.consume(")") || tokeniser.error("Unterminated operation");
tokens.termination =
tokeniser.consume(";") ||
tokeniser.error("Unterminated operation, expected `;`");
return ret.this;
}
get type() {
return "operation";
}
get name() {
const { name } = this.tokens;
if (!name) {
return "";
}
return unescape(name.value);
}
get special() {
if (!this.tokens.special) {
return "";
}
return this.tokens.special.value;
}
*validate(defs) {
yield* this.extAttrs.validate(defs);
if (!this.name && ["", "static"].includes(this.special)) {
const message = `Regular or static operations must have both a return type and an identifier.`;
yield validationError(this.tokens.open, this, "incomplete-op", message);
}
if (this.idlType) {
yield* this.idlType.validate(defs);
}
for (const argument of this.arguments) {
yield* argument.validate(defs);
}
}
/** @param {import("../writer.js").Writer} w */
write(w) {
const { parent } = this;
const body = this.idlType
? [
w.ts.type(this.idlType.write(w)),
w.name_token(this.tokens.name, { data: this, parent }),
w.token(this.tokens.open),
w.ts.wrap(this.arguments.map((arg) => arg.write(w))),
w.token(this.tokens.close),
]
: [];
return w.ts.definition(
w.ts.wrap([
this.extAttrs.write(w),
this.tokens.name
? w.token(this.tokens.special)
: w.token(this.tokens.special, w.ts.nameless, { data: this, parent }),
...body,
w.token(this.tokens.termination),
]),
{ data: this, parent }
);
}
}