typescript
Version:
TypeScript is a language for application scale JavaScript development
835 lines • 28.6 kB
JavaScript
// Code generated by _scripts/generate-encoder.ts. DO NOT EDIT.
import { getTokenPosOfNode, ModifierFlags, SyntaxKind, } from "../../ast/index.js";
import { modifierToFlag, NODE_CHILD_MASK, NODE_DATA_TYPE_MASK, NODE_EXTENDED_DATA_MASK, NODE_STRING_INDEX_MASK, popcount8, RemoteNodeBase, } from "./node.infrastructure.js";
import { childProperties, KIND_NODE_LIST, NODE_DATA_TYPE_CHILDREN, NODE_DATA_TYPE_EXTENDED, NODE_DATA_TYPE_STRING, NODE_LEN, NODE_OFFSET_DATA, NODE_OFFSET_END, NODE_OFFSET_FLAGS, NODE_OFFSET_KIND, NODE_OFFSET_NEXT, NODE_OFFSET_PARENT, NODE_OFFSET_POS, } from "./protocol.js";
export class RemoteNodeList extends Array {
// Inherited Array methods like filter/map/slice use ArraySpeciesCreate, which would
// otherwise call `new RemoteNodeList(length)` and fail. Produce a plain Array instead.
static get [Symbol.species]() {
return Array;
}
parent;
hasTrailingComma;
transformFlags = 0;
view;
index;
_byteIndex;
// Cursor memoizing the last resolved (logical index -> node index) so that
// sequential forward access (index loops and list[i], plus forEach/map/
// reduce/filter) resumes instead of re-walking from the head, turning an
// O(n) pass over the whole list from O(n^2) into O(n).
_cursorIndex = 0;
_cursorNodeIndex = 0;
get pos() {
return this.view.getUint32(this._byteIndex + NODE_OFFSET_POS, true);
}
get end() {
return this.view.getUint32(this._byteIndex + NODE_OFFSET_END, true);
}
get next() {
return this.view.getUint32(this._byteIndex + NODE_OFFSET_NEXT, true);
}
get data() {
return this.view.getUint32(this._byteIndex + NODE_OFFSET_DATA, true);
}
sourceFile;
constructor(view, index, parent, sourceFile, offsetNodes) {
super();
this.view = view;
this.index = index;
this.parent = parent;
this.sourceFile = sourceFile;
this._byteIndex = offsetNodes + index * NODE_LEN;
this.length = this.data;
this._cursorNodeIndex = index + 1;
const length = this.length;
for (let i = 16; i < length; i++) {
Object.defineProperty(this, i, {
get() {
return this.at(i);
},
});
}
}
get 0() {
return this.at(0);
}
get 1() {
return this.at(1);
}
get 2() {
return this.at(2);
}
get 3() {
return this.at(3);
}
get 4() {
return this.at(4);
}
get 5() {
return this.at(5);
}
get 6() {
return this.at(6);
}
get 7() {
return this.at(7);
}
get 8() {
return this.at(8);
}
get 9() {
return this.at(9);
}
get 10() {
return this.at(10);
}
get 11() {
return this.at(11);
}
get 12() {
return this.at(12);
}
get 13() {
return this.at(13);
}
get 14() {
return this.at(14);
}
get 15() {
return this.at(15);
}
*[Symbol.iterator]() {
if (!this.length)
return;
let next = this.index + 1;
while (next) {
const child = this.getOrCreateChildAtNodeIndex(next);
next = child.next;
yield child;
}
}
forEachNode(visitNode) {
if (!this.length)
return;
let next = this.index + 1;
while (next) {
const child = this.getOrCreateChildAtNodeIndex(next);
next = child.next;
const result = visitNode(child);
if (result)
return result;
}
}
at(index) {
if (!Number.isInteger(index)) {
return undefined;
}
if (index >= this.data || (index < 0 && -index > this.data)) {
return undefined;
}
if (index < 0) {
index = this.length + index;
}
// Walk the raw buffer following each node's `next` pointer instead of
// materializing every intermediate RemoteNode just to read it. Resume from
// the memoized cursor when possible so sequential forward access is O(1)
// amortized (a full in-order pass is O(n) rather than O(n^2)).
const offsetNodes = this.sourceFile._offsetNodes;
let i;
let next;
if (index >= this._cursorIndex) {
i = this._cursorIndex;
next = this._cursorNodeIndex;
}
else {
i = 0;
next = this.index + 1;
}
for (; i < index; i++) {
next = this.view.getUint32(offsetNodes + next * NODE_LEN + NODE_OFFSET_NEXT, true);
}
this._cursorIndex = index;
this._cursorNodeIndex = next;
return this.getOrCreateChildAtNodeIndex(next);
}
getOrCreateChildAtNodeIndex(index) {
let child = this.sourceFile.nodes[index];
if (!child) {
const kind = this.view.getUint32(this.sourceFile._offsetNodes + index * NODE_LEN + NODE_OFFSET_KIND, true);
if (kind === KIND_NODE_LIST) {
throw new Error("NodeList cannot directly contain another NodeList");
}
const sf = this.sourceFile;
child = new RemoteNode(this.view, index, this.parent, sf, sf._offsetNodes);
sf.nodes[index] = child;
sf._timing?.recordMaterialization();
}
return child;
}
__print() {
const result = [];
result.push(`kind: NodeList`);
result.push(`index: ${this.index}`);
result.push(`byteIndex: ${this._byteIndex}`);
result.push(`length: ${this.length}`);
return result.join("\n");
}
}
export class RemoteNode extends RemoteNodeBase {
static NODE_LEN = NODE_LEN;
get sourceFile() {
return this._sourceFile;
}
_sourceFile;
get id() {
return `${this.index}.${this.kind}.${this.sourceFile.path}`;
}
constructor(view, index, parent, sourceFile, offsetNodes) {
super(view, index, parent, offsetNodes + index * NODE_LEN);
this._sourceFile = sourceFile;
}
forEachChild(visitNode, visitList) {
if (this.hasChildren()) {
let next = this.index + 1;
do {
const child = this.getOrCreateChildAtNodeIndex(next);
if (child instanceof RemoteNodeList) {
if (visitList) {
const result = visitList(child);
if (result) {
return result;
}
}
else {
const result = child.forEachNode(visitNode);
if (result) {
return result;
}
}
}
else if (child.kind !== SyntaxKind.JSDoc) {
const result = visitNode(child);
if (result) {
return result;
}
}
next = child.next;
} while (next);
}
}
get jsDoc() {
if (!this.hasChildren()) {
return undefined;
}
let result;
let next = this.index + 1;
do {
const child = this.getOrCreateChildAtNodeIndex(next);
if (!(child instanceof RemoteNodeList) && child.kind === SyntaxKind.JSDoc) {
(result ??= []).push(child);
}
next = child.next;
} while (next);
return result;
}
getSourceFile() {
return this.sourceFile;
}
getStart(sourceFile, includeJsDocComment) {
return getTokenPosOfNode(this, sourceFile ?? this.getSourceFile(), includeJsDocComment);
}
getFullStart() {
return this.pos;
}
getEnd() {
return this.end;
}
getWidth(sourceFile) {
return this.getEnd() - this.getStart(sourceFile);
}
getFullWidth() {
return this.end - this.pos;
}
getLeadingTriviaWidth(sourceFile) {
return this.getStart(sourceFile) - this.pos;
}
getFullText(sourceFile) {
return (sourceFile ?? this.getSourceFile()).text.substring(this.pos, this.end);
}
getText(sourceFile) {
sourceFile ??= this.getSourceFile();
return sourceFile.text.substring(this.getStart(sourceFile), this.end);
}
getString(index) {
const offsetStringTableOffsets = this.sourceFile._offsetStringTableOffsets;
const start = this.view.getUint32(offsetStringTableOffsets + index * 4, true);
const end = this.view.getUint32(offsetStringTableOffsets + (index + 1) * 4, true);
const offsetStringTable = this.sourceFile._offsetStringTable;
const text = new Uint8Array(this.view.buffer, this.view.byteOffset + offsetStringTable + start, end - start);
return this.sourceFile._decoder.decode(text);
}
getOrCreateChildAtNodeIndex(index) {
let child = this.sourceFile.nodes[index];
if (!child) {
const sf = this.sourceFile;
const offsetNodes = sf._offsetNodes;
const kind = this.view.getUint32(offsetNodes + index * NODE_LEN + NODE_OFFSET_KIND, true);
child = kind === KIND_NODE_LIST
? new RemoteNodeList(this.view, index, this, sf, offsetNodes)
: new RemoteNode(this.view, index, this, sf, offsetNodes);
sf.nodes[index] = child;
sf._timing?.recordMaterialization();
}
return child;
}
hasChildren() {
if (this._byteIndex >= this.view.byteLength - NODE_LEN) {
return false;
}
const nextNodeParent = this.view.getUint32(this.sourceFile._offsetNodes + (this.index + 1) * NODE_LEN + NODE_OFFSET_PARENT, true);
return nextNodeParent === this.index;
}
getNamedChild(propertyName) {
const kind = this.kind;
const propertyNames = childProperties[kind];
if (!propertyNames) {
return undefined;
}
const order = propertyNames.indexOf(propertyName);
if (order === -1) {
return undefined;
}
return this.getChildAtOrder(order);
}
getChildAtOrder(order) {
const mask = this.childMask;
if (!(mask & (1 << order))) {
// Property is not present
return undefined;
}
// The property index is `order`, minus the number of zeros in the mask that are in bit positions less
// than the `order`th bit. Example:
//
// This is a MethodDeclaration with mask 0b01110101. The possible properties are
// ["modifiers", "asteriskToken", "name", "postfixToken", "typeParameters", "parameters", "type", "body"]
// (it has modifiers, name, typeParameters, parameters, and type).
//
// | Bit | 7 | 6 | 5 | 4 | 3 | 2 | 1 | 0 |
// | ----- | ---- | ---- | ---------- | -------------- | ------------ | ---- | ------------- | --------- |
// | Value | 0 | 1 | 1 | 1 | 0 | 1 | 0 | 1 |
// | Name | body | type | parameters | typeParameters | postfixToken | name | asteriskToken | modifiers |
//
// We are trying to get the index of "parameters" (bit = 5).
// First, set all the more significant bits to 1:
//
// | Bit | 7 | 6 | 5 | 4 | 3 | 2 | 1 | 0 |
// | ----- | ---- | ---- | ---------- | -------------- | ------------ | ---- | ------------- | --------- |
// | Value | 1 | 1 | 1 | 1 | 0 | 1 | 0 | 1 |
//
// Then, flip the bits:
//
// | Bit | 7 | 6 | 5 | 4 | 3 | 2 | 1 | 0 |
// | ----- | ---- | ---- | ---------- | -------------- | ------------ | ---- | ------------- | --------- |
// | Value | 0 | 0 | 0 | 0 | 1 | 0 | 1 | 0 |
//
// Counting the 1s gives us the number of *missing properties* before the `order`th property. If every property
// were present, we would have `parameters = children[5]`, but since `postfixToken` and `astersiskToken` are
// missing, we have `parameters = children[5 - 2]`.
const propertyIndex = order - popcount8[~(mask | ((0xff << order) & 0xff)) & 0xff];
let childIndex = this.index + 1;
for (let i = 0; i < propertyIndex; i++) {
// Walk through children via their `next` pointer until we get to the right property index
childIndex = this.view.getUint32(this.sourceFile._offsetNodes + childIndex * NODE_LEN + NODE_OFFSET_NEXT, true);
}
return this.getOrCreateChildAtNodeIndex(childIndex);
}
__print() {
const result = [];
result.push(`index: ${this.index}`);
result.push(`byteIndex: ${this._byteIndex}`);
result.push(`kind: ${SyntaxKind[this.kind]}`);
result.push(`pos: ${this.pos}`);
result.push(`end: ${this.end}`);
result.push(`next: ${this.next}`);
result.push(`parent: ${this.parentIndex}`);
result.push(`data: ${this.data.toString(2).padStart(32, "0")}`);
const dataType = this.dataType === NODE_DATA_TYPE_CHILDREN ? "children" :
this.dataType === NODE_DATA_TYPE_STRING ? "string" :
"extended";
result.push(`dataType: ${dataType}`);
if (this.dataType === NODE_DATA_TYPE_CHILDREN) {
result.push(`childMask: ${this.childMask.toString(2).padStart(8, "0")}`);
result.push(`childProperties: ${childProperties[this.kind]?.join(", ")}`);
}
return result.join("\n");
}
__printChildren() {
const result = [];
let next = this.index + 1;
while (next) {
const child = this.getOrCreateChildAtNodeIndex(next);
next = child.next;
result.push(child.__print());
}
return result.join("\n\n");
}
__printSubtree() {
const result = [this.__print()];
this.forEachChild(function visitNode(node) {
result.push(node.__print());
node.forEachChild(visitNode);
}, visitList => {
result.push(visitList.__print());
});
return result.join("\n\n");
}
// ═══ Generated boolean property getters ═══
get containsOnlyTriviaWhiteSpaces() {
return (this.data & (1 << 24)) !== 0;
}
get isArrayType() {
return (this.data & (1 << 24)) !== 0;
}
get isBracketed() {
return (this.data & (1 << 24)) !== 0;
}
get isExportEquals() {
return (this.data & (1 << 24)) !== 0;
}
get isNameFirst() {
return (this.data & (1 << 25)) !== 0;
}
get isTypeOf() {
return (this.data & (1 << 24)) !== 0;
}
get isTypeOnly() {
return (this.data & (1 << 24)) !== 0;
}
get multiLine() {
return (this.data & (1 << 24)) !== 0;
}
// ═══ Generated SyntaxKind union property getters ═══
get keyword() {
switch (this.kind) {
case SyntaxKind.ModuleDeclaration:
return (this.data >> 24) & 0x1 ? SyntaxKind.NamespaceKeyword : SyntaxKind.ModuleKeyword;
}
}
get keywordToken() {
switch (this.kind) {
case SyntaxKind.MetaProperty:
return (this.data >> 24) & 0x1 ? SyntaxKind.NewKeyword : SyntaxKind.ImportKeyword;
}
}
get operator() {
switch (this.kind) {
case SyntaxKind.PrefixUnaryExpression: {
const idx = (this.data >> 24) & 0x7;
if (idx === 1)
return SyntaxKind.MinusToken;
if (idx === 2)
return SyntaxKind.TildeToken;
if (idx === 3)
return SyntaxKind.ExclamationToken;
if (idx === 4)
return SyntaxKind.PlusPlusToken;
if (idx === 5)
return SyntaxKind.MinusMinusToken;
return SyntaxKind.PlusToken;
}
case SyntaxKind.PostfixUnaryExpression:
return (this.data >> 24) & 0x1 ? SyntaxKind.MinusMinusToken : SyntaxKind.PlusPlusToken;
case SyntaxKind.TypeOperator: {
const idx = (this.data >> 24) & 0x3;
if (idx === 1)
return SyntaxKind.ReadonlyKeyword;
if (idx === 2)
return SyntaxKind.UniqueKeyword;
return SyntaxKind.KeyOfKeyword;
}
}
}
get phaseModifier() {
switch (this.kind) {
case SyntaxKind.ImportClause: {
const idx = (this.data >> 24) & 0x3;
if (idx === 0)
return undefined;
return idx === 1 ? SyntaxKind.TypeKeyword : idx === 2 ? SyntaxKind.DeferKeyword : undefined;
}
}
}
get token() {
switch (this.kind) {
case SyntaxKind.HeritageClause:
return (this.data >> 24) & 0x1 ? SyntaxKind.ImplementsKeyword : SyntaxKind.ExtendsKeyword;
case SyntaxKind.ImportAttributes:
return (this.data >> 25) & 0x1 ? SyntaxKind.AssertKeyword : SyntaxKind.WithKeyword;
}
}
get templateFlags() {
switch (this.kind) {
case SyntaxKind.TemplateHead:
case SyntaxKind.TemplateMiddle:
case SyntaxKind.TemplateTail:
const extendedDataOffset = this.sourceFile._offsetExtendedData + (this.data & NODE_EXTENDED_DATA_MASK);
return this.view.getUint32(extendedDataOffset + 8, true);
}
}
get tokenFlags() {
switch (this.kind) {
case SyntaxKind.StringLiteral:
case SyntaxKind.NumericLiteral:
case SyntaxKind.BigIntLiteral:
case SyntaxKind.RegularExpressionLiteral:
const extendedDataOffset = this.sourceFile._offsetExtendedData + (this.data & NODE_EXTENDED_DATA_MASK);
return this.view.getUint32(extendedDataOffset + 4, true);
default:
return 0;
}
}
// ═══ Generated child property getters ═══
get argument() {
return this.getNamedChild("argument");
}
get argumentExpression() {
return this.getNamedChild("argumentExpression");
}
get arguments() {
return this.getNamedChild("arguments");
}
get assertsModifier() {
return this.getNamedChild("assertsModifier");
}
get asteriskToken() {
return this.getNamedChild("asteriskToken");
}
get attributes() {
return this.getNamedChild("attributes");
}
get awaitModifier() {
return this.getNamedChild("awaitModifier");
}
get block() {
return this.getNamedChild("block");
}
get body() {
return this.getNamedChild("body");
}
get caseBlock() {
return this.getNamedChild("caseBlock");
}
get catchClause() {
return this.getNamedChild("catchClause");
}
get checkType() {
return this.getNamedChild("checkType");
}
get children() {
return this.getNamedChild("children");
}
get className() {
return this.getNamedChild("className");
}
get clauses() {
return this.getNamedChild("clauses");
}
get closingElement() {
return this.getNamedChild("closingElement");
}
get closingFragment() {
return this.getNamedChild("closingFragment");
}
get colonToken() {
return this.getNamedChild("colonToken");
}
get comment() {
return this.getNamedChild("comment");
}
get condition() {
return this.getNamedChild("condition");
}
get constraint() {
return this.getNamedChild("constraint");
}
get declarationList() {
return this.getNamedChild("declarationList");
}
get declarations() {
return this.getNamedChild("declarations");
}
get defaultType() {
return this.getNamedChild("defaultType");
}
get dotDotDotToken() {
return this.getNamedChild("dotDotDotToken");
}
get elements() {
return this.getNamedChild("elements");
}
get elementType() {
return this.getNamedChild("elementType");
}
get elseStatement() {
return this.getNamedChild("elseStatement");
}
get endOfFileToken() {
return this.getNamedChild("endOfFileToken");
}
get equalsGreaterThanToken() {
return this.getNamedChild("equalsGreaterThanToken");
}
get equalsToken() {
return this.getNamedChild("equalsToken");
}
get exclamationToken() {
return this.getNamedChild("exclamationToken");
}
get exportClause() {
return this.getNamedChild("exportClause");
}
get expression() {
return this.getNamedChild("expression");
}
get exprName() {
return this.getNamedChild("exprName");
}
get extendsType() {
return this.getNamedChild("extendsType");
}
get falseType() {
return this.getNamedChild("falseType");
}
get finallyBlock() {
return this.getNamedChild("finallyBlock");
}
get head() {
return this.getNamedChild("head");
}
get heritageClauses() {
return this.getNamedChild("heritageClauses");
}
get importClause() {
return this.getNamedChild("importClause");
}
get incrementor() {
return this.getNamedChild("incrementor");
}
get indexType() {
return this.getNamedChild("indexType");
}
get initializer() {
return this.getNamedChild("initializer");
}
get jsdocPropertyTags() {
return this.getNamedChild("jsdocPropertyTags");
}
get label() {
return this.getNamedChild("label");
}
get left() {
return this.getNamedChild("left");
}
get literal() {
return this.getNamedChild("literal");
}
get members() {
return this.getNamedChild("members");
}
get modifiers() {
return this.getNamedChild("modifiers");
}
get moduleReference() {
return this.getNamedChild("moduleReference");
}
get moduleSpecifier() {
return this.getNamedChild("moduleSpecifier");
}
get name() {
return this.getNamedChild("name");
}
get namedBindings() {
return this.getNamedChild("namedBindings");
}
get nameExpression() {
return this.getNamedChild("nameExpression");
}
get namespace() {
return this.getNamedChild("namespace");
}
get nameType() {
return this.getNamedChild("nameType");
}
get objectAssignmentInitializer() {
return this.getNamedChild("objectAssignmentInitializer");
}
get objectType() {
return this.getNamedChild("objectType");
}
get openingElement() {
return this.getNamedChild("openingElement");
}
get openingFragment() {
return this.getNamedChild("openingFragment");
}
get operand() {
return this.getNamedChild("operand");
}
get operatorToken() {
return this.getNamedChild("operatorToken");
}
get parameterName() {
return this.getNamedChild("parameterName");
}
get parameters() {
return this.getNamedChild("parameters");
}
get postfixToken() {
return this.getNamedChild("postfixToken");
}
get properties() {
return this.getNamedChild("properties");
}
get propertyName() {
return this.getNamedChild("propertyName");
}
get qualifier() {
return this.getNamedChild("qualifier");
}
get questionDotToken() {
return this.getNamedChild("questionDotToken");
}
get questionToken() {
return this.getNamedChild("questionToken");
}
get readonlyToken() {
return this.getNamedChild("readonlyToken");
}
get right() {
return this.getNamedChild("right");
}
get statement() {
return this.getNamedChild("statement");
}
get statements() {
return this.getNamedChild("statements");
}
get tag() {
return this.getNamedChild("tag");
}
get tagName() {
return this.getNamedChild("tagName");
}
get tags() {
return this.getNamedChild("tags");
}
get template() {
return this.getNamedChild("template");
}
get templateSpans() {
return this.getNamedChild("templateSpans");
}
get thenStatement() {
return this.getNamedChild("thenStatement");
}
get thisArg() {
return this.getNamedChild("thisArg");
}
get trueType() {
return this.getNamedChild("trueType");
}
get tryBlock() {
return this.getNamedChild("tryBlock");
}
get tupleNameSource() {
return this.getNamedChild("tupleNameSource");
}
get type() {
return this.getNamedChild("type");
}
get typeArguments() {
return this.getNamedChild("typeArguments");
}
get typeExpression() {
return this.getNamedChild("typeExpression");
}
get typeName() {
return this.getNamedChild("typeName");
}
get typeParameter() {
return this.getNamedChild("typeParameter");
}
get typeParameters() {
return this.getNamedChild("typeParameters");
}
get types() {
return this.getNamedChild("types");
}
get value() {
return this.getNamedChild("value");
}
get variableDeclaration() {
return this.getNamedChild("variableDeclaration");
}
get whenFalse() {
return this.getNamedChild("whenFalse");
}
get whenTrue() {
return this.getNamedChild("whenTrue");
}
// ═══ Generated string property getters ═══
get text() {
switch (this.kind) {
case SyntaxKind.Identifier:
case SyntaxKind.PrivateIdentifier:
case SyntaxKind.JsxText:
case SyntaxKind.JSDocText:
case SyntaxKind.JSDocLink:
case SyntaxKind.JSDocLinkPlain:
case SyntaxKind.JSDocLinkCode: {
const stringIndex = this.data & NODE_STRING_INDEX_MASK;
return this.getString(stringIndex);
}
case SyntaxKind.StringLiteral:
case SyntaxKind.NumericLiteral:
case SyntaxKind.BigIntLiteral:
case SyntaxKind.RegularExpressionLiteral:
case SyntaxKind.NoSubstitutionTemplateLiteral:
case SyntaxKind.TemplateHead:
case SyntaxKind.TemplateMiddle:
case SyntaxKind.TemplateTail:
case SyntaxKind.SourceFile: {
const extendedDataOffset = this.sourceFile._offsetExtendedData + (this.data & NODE_EXTENDED_DATA_MASK);
const stringIndex = this.view.getUint32(extendedDataOffset, true);
return this.getString(stringIndex);
}
}
}
get rawText() {
switch (this.kind) {
case SyntaxKind.TemplateHead:
case SyntaxKind.TemplateMiddle:
case SyntaxKind.TemplateTail:
const extendedDataOffset = this.sourceFile._offsetExtendedData + (this.data & NODE_EXTENDED_DATA_MASK);
const stringIndex = this.view.getUint32(extendedDataOffset + 4, true);
return this.getString(stringIndex);
}
}
// ═══ Generated extended data property getters ═══
// ═══ Other property getters ═══
get flags() {
return this.view.getUint32(this._byteIndex + NODE_OFFSET_FLAGS, true);
}
get modifierFlags() {
const mods = this.modifiers;
if (!mods)
return ModifierFlags.None;
let flags = ModifierFlags.None;
for (const mod of mods) {
flags |= modifierToFlag(mod.kind);
}
return flags;
}
}
//# sourceMappingURL=node.generated.js.map