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

Add support for Hack(#) -style Pipeline Operator #43617

Draft
wants to merge 12 commits into
base: main
Choose a base branch
from
50 changes: 49 additions & 1 deletion src/compiler/binder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -825,6 +825,9 @@ namespace ts {
case SyntaxKind.CallExpression:
bindCallExpressionFlow(node as CallExpression);
break;
case SyntaxKind.PipelineHackExpression:
bindPipelineHackExpressionFlow(node as PipelineHackExpression);
break;
case SyntaxKind.NonNullExpression:
bindNonNullExpressionFlow(node as NonNullExpression);
break;
Expand Down Expand Up @@ -1793,6 +1796,42 @@ namespace ts {
}
}

function bindPipelineHackExpressionFlow(node: PipelineHackExpression) {
if (!blockScopeContainer.locals) {
blockScopeContainer.locals = createSymbolTable();
addToContainerChain(blockScopeContainer);
}
// const bindingName = InternalSymbolName.HackPipelineReference;
// const newSymbol = bindAnonymousDeclaration(node.dummyDeclaration, SymbolFlags.BlockScopedVariable, bindingName);
declareSymbol(blockScopeContainer.locals, /*parent*/ undefined, node.dummyDeclaration, SymbolFlags.BlockScopedVariable, SymbolFlags.None);
// const hashDeclaration = setTextRange(factory.createBindingElement(undefined, undefined, '#', node.arguments[0]), { pos: node.pos, end: node.pos });
// setTextRange(hashDeclaration.name!, { pos: node.pos, end: node.pos });
// (hashDeclaration as any).parent = node;
// declareSymbol(blockScopeContainer.locals, /*parent*/ undefined, hashDeclaration, SymbolFlags.BlockScopedVariable, SymbolFlags.BlockScopedVariableExcludes);


// bindBlockScopedDeclaration(hashDeclaration, SymbolFlags.BlockScopedVariable, SymbolFlags.BlockScopedVariableExcludes);
// // const obp = factory.createObjectBindingPattern([hashDeclaration]);
// setParent(hashDeclaration, obp);
// // // const result = createSymbol(SymbolFlags.BlockScopedVariable, '#' as __String);
// // // result.declarations = [hashDeclaration];
// // // result.parent = node;
// setParent(obp, node);
// // const syn = factory.createSyntheticExpression(ObjectBindinPattern, false, hashDeclaration)

// If the target of the call expression is a function expression or arrow function we have
// an immediately invoked function expression (IIFE). Initialize the flowNode property to
// the current control flow (which includes evaluation of the IIFE arguments).
bind(node.argument);
bind(node.expression);
// if (node.expression.kind === SyntaxKind.PropertyAccessExpression) {
// const propertyAccess = <PropertyAccessExpression>node.expression;
// if (isIdentifier(propertyAccess.name) && isNarrowableOperand(propertyAccess.expression) && isPushOrUnshiftIdentifier(propertyAccess.name)) {
// currentFlow = createFlowMutation(FlowFlags.ArrayMutation, currentFlow, node);
// }
// }
}

function getContainerFlags(node: Node): ContainerFlags {
switch (node.kind) {
case SyntaxKind.ClassExpression:
Expand Down Expand Up @@ -1849,7 +1888,8 @@ namespace ts {
case SyntaxKind.ForInStatement:
case SyntaxKind.ForOfStatement:
case SyntaxKind.CaseBlock:
return ContainerFlags.IsBlockScopedContainer;
case SyntaxKind.PipelineHackExpression:
return ContainerFlags.IsBlockScopedContainer | ContainerFlags.HasLocals;

case SyntaxKind.Block:
// do not treat blocks directly inside a function as a block-scoped-container.
Expand Down Expand Up @@ -1933,6 +1973,7 @@ namespace ts {
case SyntaxKind.ClassStaticBlockDeclaration:
case SyntaxKind.TypeAliasDeclaration:
case SyntaxKind.MappedType:
case SyntaxKind.PipelineHackExpression:
// All the children of these container types are never visible through another
// symbol (i.e. through another symbol's 'exports' or 'members'). Instead,
// they're only accessed 'lexically' (i.e. from code that exists underneath
Expand Down Expand Up @@ -2656,6 +2697,8 @@ namespace ts {
case SyntaxKind.FunctionExpression:
case SyntaxKind.ArrowFunction:
return bindFunctionExpression(node as FunctionExpression);
// case SyntaxKind.PipelineExpression:
// return bindPipelineExpression(<PipelineExpression>node);

case SyntaxKind.CallExpression:
const assignmentKind = getAssignmentDeclarationKind(node as CallExpression);
Expand Down Expand Up @@ -3368,6 +3411,11 @@ namespace ts {
return bindAnonymousDeclaration(node, SymbolFlags.Function, bindingName);
}

// function bindPipelineExpression(node: PipelineExpression) {
// const bindingName = InternalSymbolName.HackPipelineReference;
// bindAnonymousDeclaration(node.dummyDeclaration, SymbolFlags.BlockScopedVariable, bindingName);
// }

function bindPropertyOrMethodOrAccessor(node: Declaration, symbolFlags: SymbolFlags, symbolExcludes: SymbolFlags) {
if (!file.isDeclarationFile && !(node.flags & NodeFlags.Ambient) && isAsyncFunction(node)) {
emitFlags |= NodeFlags.HasAsyncFunctions;
Expand Down
Loading