Skip to content

Commit

Permalink
Remove baseNodeFactory
Browse files Browse the repository at this point in the history
  • Loading branch information
rbuckton committed Dec 17, 2022
1 parent cadf6e8 commit c2c2643
Show file tree
Hide file tree
Showing 9 changed files with 52 additions and 147 deletions.
1 change: 0 additions & 1 deletion src/compiler/_namespaces/ts.NodeConstructors.ts

This file was deleted.

1 change: 0 additions & 1 deletion src/compiler/_namespaces/ts.ObjectConstructors.ts

This file was deleted.

11 changes: 2 additions & 9 deletions src/compiler/_namespaces/ts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ export * from "../diagnosticInformationMap.generated";
export * from "../scanner";
export * from "../utilitiesPublic";
export * from "../utilities";
export * from "../factory/baseNodeFactory";
export * from "../factory/parenthesizerRules";
export * from "../factory/nodeConverters";
export * from "../factory/nodeFactory";
Expand Down Expand Up @@ -72,11 +71,5 @@ import * as moduleSpecifiers from "./ts.moduleSpecifiers";
export { moduleSpecifiers };
import * as performance from "./ts.performance";
export { performance };
/** @internal */
import * as NodeConstructors from "./ts.NodeConstructors";
/** @internal */
export { NodeConstructors };
/** @internal */
import * as ObjectConstructors from "./ts.ObjectConstructors";
/** @internal */
export { ObjectConstructors };
/** @internal */ export * as NodeConstructors from "../nodeConstructors";
/** @internal */ export * as ObjectConstructors from "../objectConstructors";
54 changes: 0 additions & 54 deletions src/compiler/factory/baseNodeFactory.ts

This file was deleted.

70 changes: 32 additions & 38 deletions src/compiler/factory/nodeFactory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ import {
AsteriskToken,
AwaitExpression,
AwaitKeyword,
BaseNodeFactory,
BigIntLiteral,
BinaryExpression,
BinaryOperator,
Expand Down Expand Up @@ -59,7 +58,6 @@ import {
ConstructorTypeNode,
ConstructSignatureDeclaration,
ContinueStatement,
createBaseNodeFactory,
createNodeConverters,
createParenthesizerRules,
createScanner,
Expand Down Expand Up @@ -467,15 +465,15 @@ import {
WithStatement,
YieldExpression,
} from "../_namespaces/ts";
// import {
// Node as NodeObject,
// Identifier as IdentifierObject,
// PrivateIdentifier as PrivateIdentifierObject,
// Token as TokenObject,
// SourceFile as SourceFileObject,
// } from "../nodeConstructors";
import {
SourceMapSourceObject as SourceMapSourceObject,
IdentifierObject,
NodeObject,
PrivateIdentifierObject,
SourceFileObject,
TokenObject,
} from "../nodeConstructors";
import {
SourceMapSourceObject,
} from "../objectConstructors";

let nextAutoGenerateId = 0;
Expand All @@ -491,6 +489,8 @@ export const enum NodeFactoryFlags {
NoIndentationOnFreshPropertyAccess = 1 << 2,
// Do not set an `original` pointer when updating a node.
NoOriginalNode = 1 << 3,
// Mark nodes as synthetic
MarkSynthetic = 1 << 4,
}

const nodeFactoryPatchers: ((factory: NodeFactory) => void)[] = [];
Expand All @@ -507,7 +507,8 @@ export function addNodeFactoryPatcher(fn: (factory: NodeFactory) => void) {
*
* @internal
*/
export function createNodeFactory(flags: NodeFactoryFlags, baseFactory: BaseNodeFactory): NodeFactory {
export function createNodeFactory(flags: NodeFactoryFlags): NodeFactory {
const markSynthetic = (flags & NodeFactoryFlags.MarkSynthetic) === NodeFactoryFlags.MarkSynthetic;
const update = flags & NodeFactoryFlags.NoOriginalNode ? updateWithoutOriginal : updateWithOriginal;

// Lazily load the parenthesizer, node converters, and some factory methods until they are used.
Expand All @@ -531,7 +532,6 @@ export function createNodeFactory(flags: NodeFactoryFlags, baseFactory: BaseNode
const factory: NodeFactory = {
get parenthesizer() { return parenthesizerRules(); },
get converters() { return converters(); },
baseFactory,
flags,
createNodeArray,
createNumericLiteral,
Expand Down Expand Up @@ -1074,7 +1074,9 @@ export function createNodeFactory(flags: NodeFactoryFlags, baseFactory: BaseNode
}

function createBaseNode<T extends Node>(kind: T["kind"]) {
return baseFactory.createBaseNode(kind) as Mutable<T>;
const node = new NodeObject(kind) as Node as Mutable<T>;
if (markSynthetic) node.flags |= NodeFlags.Synthesized;
return node;
}

function createBaseDeclaration<T extends Declaration>(kind: T["kind"]) {
Expand Down Expand Up @@ -1160,7 +1162,8 @@ export function createNodeFactory(flags: NodeFactoryFlags, baseFactory: BaseNode
//

function createBaseIdentifier(escapedText: __String, originalKeywordKind: SyntaxKind | undefined) {
const node = baseFactory.createBaseIdentifierNode() as Mutable<Identifier>;
const node = new IdentifierObject() as Mutable<Identifier>;
if (markSynthetic) node.flags |= NodeFlags.Synthesized;
node.originalKeywordKind = originalKeywordKind;
node.escapedText = escapedText;
node.autoGenerate = undefined;
Expand Down Expand Up @@ -1257,7 +1260,8 @@ export function createNodeFactory(flags: NodeFactoryFlags, baseFactory: BaseNode
}

function createBasePrivateIdentifier(escapedText: __String) {
const node = baseFactory.createBasePrivateIdentifierNode() as Mutable<PrivateIdentifier>;
const node = new PrivateIdentifierObject() as Mutable<PrivateIdentifier>;
if (markSynthetic) node.flags |= NodeFlags.Synthesized;
node.escapedText = escapedText;
node.autoGenerate = undefined;
node.transformFlags |= TransformFlags.ContainsClassFields;
Expand Down Expand Up @@ -1306,7 +1310,9 @@ export function createNodeFactory(flags: NodeFactoryFlags, baseFactory: BaseNode
//

function createBaseToken<T extends Node>(kind: T["kind"]) {
return baseFactory.createBaseTokenNode(kind) as Mutable<T>;
const node = new TokenObject(kind) as Node as Mutable<T>;
if (markSynthetic) node.flags |= NodeFlags.Synthesized;
return node;
}

// @api
Expand Down Expand Up @@ -6033,7 +6039,8 @@ export function createNodeFactory(flags: NodeFactoryFlags, baseFactory: BaseNode
endOfFileToken: EndOfFileToken,
flags: NodeFlags
) {
const node = baseFactory.createBaseSourceFileNode() as Mutable<SourceFile>;
const node = new SourceFileObject() as Mutable<SourceFile>;
if (markSynthetic) node.flags |= NodeFlags.Synthesized;
node.statements = createNodeArray(statements);
node.endOfFileToken = endOfFileToken;
node.flags |= flags;
Expand Down Expand Up @@ -6116,7 +6123,8 @@ export function createNodeFactory(flags: NodeFactoryFlags, baseFactory: BaseNode
function cloneSourceFileWorker(source: SourceFile) {
// TODO: This mechanism for cloning results in megamorphic property reads and writes. In future perf-related
// work, we should consider switching explicit property assignments instead of using `for..in`.
const node = baseFactory.createBaseSourceFileNode() as Mutable<SourceFile>;
const node = new SourceFileObject() as Mutable<SourceFile>;
if (markSynthetic) node.flags |= NodeFlags.Synthesized;
node.flags |= source.flags & ~NodeFlags.Synthesized;
for (const p in source) {
if (hasProperty(node, p) || !hasProperty(source, p)) {
Expand Down Expand Up @@ -6452,11 +6460,12 @@ export function createNodeFactory(flags: NodeFactoryFlags, baseFactory: BaseNode
}

const clone =
!isNodeKind(node.kind) ? baseFactory.createBaseTokenNode(node.kind) as T :
baseFactory.createBaseNode(node.kind) as T;
!isNodeKind(node.kind) ? new TokenObject(node.kind) as Node as Mutable<T> :
new NodeObject(node.kind) as Node as Mutable<T>;

(clone as Mutable<T>).flags |= (node.flags & ~NodeFlags.Synthesized);
(clone as Mutable<T>).transformFlags = node.transformFlags;
if (markSynthetic) clone.flags |= NodeFlags.Synthesized;
clone.flags |= (node.flags & ~NodeFlags.Synthesized);
clone.transformFlags = node.transformFlags;
setOriginalNode(clone, node);

for (const key in node) {
Expand Down Expand Up @@ -7388,22 +7397,7 @@ export function getTransformFlagsSubtreeExclusions(kind: SyntaxKind) {
}
}

const baseFactory = createBaseNodeFactory();

function makeSynthetic(node: Node) {
(node as Mutable<Node>).flags |= NodeFlags.Synthesized;
return node;
}

const syntheticFactory: BaseNodeFactory = {
createBaseSourceFileNode: () => makeSynthetic(baseFactory.createBaseSourceFileNode()),
createBaseIdentifierNode: () => makeSynthetic(baseFactory.createBaseIdentifierNode()),
createBasePrivateIdentifierNode: () => makeSynthetic(baseFactory.createBasePrivateIdentifierNode()),
createBaseTokenNode: kind => makeSynthetic(baseFactory.createBaseTokenNode(kind)),
createBaseNode: kind => makeSynthetic(baseFactory.createBaseNode(kind)),
};

export const factory = createNodeFactory(NodeFactoryFlags.NoIndentationOnFreshPropertyAccess, syntheticFactory);
export const factory = createNodeFactory(NodeFactoryFlags.NoIndentationOnFreshPropertyAccess | NodeFactoryFlags.MarkSynthetic);

export function createUnparsedSourceFile(text: string): UnparsedSource;
export function createUnparsedSourceFile(inputFile: InputFiles, type: "js" | "dts", stripInternal?: boolean): UnparsedSource;
Expand Down
44 changes: 11 additions & 33 deletions src/compiler/parser.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { IdentifierObject, NodeObject, PrivateIdentifierObject, SourceFileObject, TokenObject } from "./nodeConstructors";
import * as ts from "./_namespaces/ts";
import {
AccessorDeclaration,
Expand All @@ -17,7 +16,6 @@ import {
AsteriskToken,
attachFileToDiagnostics,
AwaitExpression,
BaseNodeFactory,
BinaryExpression,
BinaryOperatorToken,
BindingElement,
Expand Down Expand Up @@ -401,21 +399,8 @@ const enum SpeculationKind {
Reparse
}

/**
* NOTE: You should not use this, it is only exported to support `createNode` in `~/src/deprecatedCompat/deprecations.ts`.
*
* @internal
*/
export const parseBaseNodeFactory: BaseNodeFactory = {
createBaseSourceFileNode: () => new SourceFileObject(),
createBaseIdentifierNode: () => new IdentifierObject(),
createBasePrivateIdentifierNode: () => new PrivateIdentifierObject(),
createBaseTokenNode: kind => new TokenObject(kind),
createBaseNode: kind => new NodeObject(kind),
};

/** @internal */
export const parseNodeFactory = createNodeFactory(NodeFactoryFlags.NoParenthesizerRules, parseBaseNodeFactory);
export const parseNodeFactory = createNodeFactory(NodeFactoryFlags.NoParenthesizerRules);

function visitNode<T>(cbNode: (node: Node) => T, node: Node | undefined): T | undefined {
return node && cbNode(node);
Expand Down Expand Up @@ -1410,22 +1395,7 @@ namespace Parser {

const disallowInAndDecoratorContext = NodeFlags.DisallowInContext | NodeFlags.DecoratorContext;

function countNode(node: Node) {
nodeCount++;
return node;
}

// Rather than using `createBaseNodeFactory` here, we establish a `BaseNodeFactory` that closes over the
// constructors above, which are reset each time `initializeState` is called.
const baseNodeFactory: BaseNodeFactory = {
createBaseSourceFileNode: () => countNode(new SourceFileObject()),
createBaseIdentifierNode: () => countNode(new IdentifierObject()),
createBasePrivateIdentifierNode: () => countNode(new PrivateIdentifierObject()),
createBaseTokenNode: kind => countNode(new TokenObject(kind)),
createBaseNode: kind => countNode(new NodeObject(kind))
};

const factory = createNodeFactory(NodeFactoryFlags.NoParenthesizerRules | NodeFactoryFlags.NoNodeConverters | NodeFactoryFlags.NoOriginalNode, baseNodeFactory);
const factory = createNodeFactory(NodeFactoryFlags.NoParenthesizerRules | NodeFactoryFlags.NoNodeConverters | NodeFactoryFlags.NoOriginalNode);

let fileName: string;
let sourceFlags: NodeFlags;
Expand Down Expand Up @@ -1898,9 +1868,16 @@ namespace Parser {
setTextRangePosWidth(sourceFile, 0, sourceText.length);
setFields(sourceFile);

// include the file in the count
nodeCount++;

// If we parsed this as an external module, it may contain top-level await
if (!isDeclarationFile && isExternalModule(sourceFile) && sourceFile.transformFlags & TransformFlags.ContainsPossibleTopLevelAwait) {
sourceFile = reparseTopLevelAwait(sourceFile);
const updated = reparseTopLevelAwait(sourceFile);
if (sourceFile !== updated) {
nodeCount++;
sourceFile = updated;
}
setFields(sourceFile);
}

Expand Down Expand Up @@ -2514,6 +2491,7 @@ namespace Parser {
(node as Mutable<T>).flags |= NodeFlags.ThisNodeHasError;
}

nodeCount++;
return node;
}

Expand Down
2 changes: 0 additions & 2 deletions src/compiler/types.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import {
BaseNodeFactory,
CreateSourceFileOptions,
EmitHelperFactory,
GetCanonicalFileName,
Expand Down Expand Up @@ -8135,7 +8134,6 @@ export interface GeneratedNamePart {
export interface NodeFactory {
/** @internal */ readonly parenthesizer: ParenthesizerRules;
/** @internal */ readonly converters: NodeConverters;
/** @internal */ readonly baseFactory: BaseNodeFactory;
/** @internal */ readonly flags: NodeFactoryFlags;
createNodeArray<T extends Node>(elements?: readonly T[], hasTrailingComma?: boolean): NodeArray<T>;

Expand Down
12 changes: 6 additions & 6 deletions src/deprecatedCompat/4.0/nodeFactoryTopLevelExports.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,10 @@ import {
NamedImportBindings,
Node,
NodeArray,
NodeConstructors,
NoSubstitutionTemplateLiteral,
NumericLiteral,
ParameterDeclaration,
parseBaseNodeFactory,
PostfixUnaryExpression,
PrefixUnaryExpression,
PrimaryExpression,
Expand Down Expand Up @@ -1345,11 +1345,11 @@ export const createLogicalNot = deprecate(function createLogicalNot(operand: Exp
/** @deprecated Use an appropriate `factory` method instead. */
export const createNode = deprecate(function createNode(kind: SyntaxKind, pos = 0, end = 0): Node {
return setTextRangePosEnd(
kind === SyntaxKind.SourceFile ? parseBaseNodeFactory.createBaseSourceFileNode() :
kind === SyntaxKind.Identifier ? parseBaseNodeFactory.createBaseIdentifierNode() :
kind === SyntaxKind.PrivateIdentifier ? parseBaseNodeFactory.createBasePrivateIdentifierNode() :
!isNodeKind(kind) ? parseBaseNodeFactory.createBaseTokenNode(kind) :
parseBaseNodeFactory.createBaseNode(kind),
kind === SyntaxKind.SourceFile ? new NodeConstructors.SourceFileObject() :
kind === SyntaxKind.Identifier ? new NodeConstructors.IdentifierObject() :
kind === SyntaxKind.PrivateIdentifier ? new NodeConstructors.PrivateIdentifierObject() :
!isNodeKind(kind) ? new NodeConstructors.TokenObject(kind) :
new NodeConstructors.NodeObject(kind),
pos,
end
);
Expand Down
4 changes: 1 addition & 3 deletions src/services/textChanges.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1329,9 +1329,7 @@ function isTrivia(s: string) {
// are more aggressive than is strictly necessary.
const textChangesTransformationContext: TransformationContext = {
...nullTransformationContext,
factory: createNodeFactory(
nullTransformationContext.factory.flags | NodeFactoryFlags.NoParenthesizerRules,
nullTransformationContext.factory.baseFactory),
factory: createNodeFactory(nullTransformationContext.factory.flags | NodeFactoryFlags.NoParenthesizerRules),
};

/** @internal */
Expand Down

0 comments on commit c2c2643

Please sign in to comment.