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

Remove preprinter, add parenthesizer callback to emit #43652

Merged
merged 1 commit into from
Apr 19, 2021
Merged
Show file tree
Hide file tree
Changes from all 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
2,674 changes: 805 additions & 1,869 deletions src/compiler/emitter.ts

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion src/compiler/factory/nodeFactory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2139,7 +2139,7 @@ namespace ts {
/*decorators*/ undefined,
/*modifiers*/ undefined,
name,
initializer
initializer && parenthesizerRules().parenthesizeExpressionForDisallowedComma(initializer)
);
node.propertyName = asName(propertyName);
node.dotDotDotToken = dotDotDotToken;
Expand Down
31 changes: 30 additions & 1 deletion src/compiler/factory/parenthesizerRules.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,12 @@ namespace ts {
cachedLiteralKind: SyntaxKind;
}

let binaryLeftOperandParenthesizerCache: ESMap<BinaryOperator, (node: Expression) => Expression> | undefined;
let binaryRightOperandParenthesizerCache: ESMap<BinaryOperator, (node: Expression) => Expression> | undefined;

return {
getParenthesizeLeftSideOfBinaryForOperator,
getParenthesizeRightSideOfBinaryForOperator,
parenthesizeLeftSideOfBinary,
parenthesizeRightSideOfBinary,
parenthesizeExpressionOfComputedPropertyName,
Expand All @@ -27,6 +32,26 @@ namespace ts {
parenthesizeTypeArguments,
};

function getParenthesizeLeftSideOfBinaryForOperator(operatorKind: BinaryOperator) {
binaryLeftOperandParenthesizerCache ||= new Map();
let parenthesizerRule = binaryLeftOperandParenthesizerCache.get(operatorKind);
if (!parenthesizerRule) {
parenthesizerRule = node => parenthesizeLeftSideOfBinary(operatorKind, node);
binaryLeftOperandParenthesizerCache.set(operatorKind, parenthesizerRule);
}
return parenthesizerRule;
}

function getParenthesizeRightSideOfBinaryForOperator(operatorKind: BinaryOperator) {
binaryRightOperandParenthesizerCache ||= new Map();
let parenthesizerRule = binaryRightOperandParenthesizerCache.get(operatorKind);
if (!parenthesizerRule) {
parenthesizerRule = node => parenthesizeRightSideOfBinary(operatorKind, /*leftSide*/ undefined, node);
binaryRightOperandParenthesizerCache.set(operatorKind, parenthesizerRule);
}
return parenthesizerRule;
}

/**
* Determines whether the operand to a BinaryExpression needs to be parenthesized.
*
Expand Down Expand Up @@ -210,7 +235,7 @@ namespace ts {
return parenthesizeBinaryOperand(binaryOperator, leftSide, /*isLeftSideOfBinary*/ true);
}

function parenthesizeRightSideOfBinary(binaryOperator: SyntaxKind, leftSide: Expression, rightSide: Expression): Expression {
function parenthesizeRightSideOfBinary(binaryOperator: SyntaxKind, leftSide: Expression | undefined, rightSide: Expression): Expression {
return parenthesizeBinaryOperand(binaryOperator, rightSide, /*isLeftSideOfBinary*/ false, leftSide);
}

Expand Down Expand Up @@ -352,6 +377,8 @@ namespace ts {
return expression;
}

function parenthesizeConciseBodyOfArrowFunction(body: Expression): Expression;
function parenthesizeConciseBodyOfArrowFunction(body: ConciseBody): ConciseBody;
function parenthesizeConciseBodyOfArrowFunction(body: ConciseBody): ConciseBody {
if (!isBlock(body) && (isCommaSequence(body) || getLeftmostExpression(body, /*stopAtCallExpressions*/ false).kind === SyntaxKind.ObjectLiteralExpression)) {
// TODO(rbuckton): Verifiy whether `setTextRange` is needed.
Expand Down Expand Up @@ -403,6 +430,8 @@ namespace ts {
}

export const nullParenthesizerRules: ParenthesizerRules = {
getParenthesizeLeftSideOfBinaryForOperator: _ => identity,
getParenthesizeRightSideOfBinaryForOperator: _ => identity,
parenthesizeLeftSideOfBinary: (_binaryOperator, leftSide) => leftSide,
parenthesizeRightSideOfBinary: (_binaryOperator, _leftSide, rightSide) => rightSide,
parenthesizeExpressionOfComputedPropertyName: identity,
Expand Down
34 changes: 17 additions & 17 deletions src/compiler/transformer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -562,31 +562,31 @@ namespace ts {
}

export const nullTransformationContext: TransformationContext = {
get factory() { return factory; },
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is simply a reordering of existing properties to match the order in transformNodes so they (hopefully) share the same "map" in V8.

enableEmitNotification: noop,
enableSubstitution: noop,
endLexicalEnvironment: returnUndefined,
factory,
getCompilerOptions: () => ({}),
getEmitHost: notImplemented,
getEmitResolver: notImplemented,
getEmitHost: notImplemented,
getEmitHelperFactory: notImplemented,
startLexicalEnvironment: noop,
resumeLexicalEnvironment: noop,
suspendLexicalEnvironment: noop,
endLexicalEnvironment: returnUndefined,
setLexicalEnvironmentFlags: noop,
getLexicalEnvironmentFlags: () => 0,
hoistFunctionDeclaration: noop,
hoistVariableDeclaration: noop,
hoistFunctionDeclaration: noop,
addInitializationStatement: noop,
isEmitNotificationEnabled: notImplemented,
isSubstitutionEnabled: notImplemented,
onEmitNode: noop,
onSubstituteNode: notImplemented,
readEmitHelpers: notImplemented,
requestEmitHelper: noop,
resumeLexicalEnvironment: noop,
startLexicalEnvironment: noop,
suspendLexicalEnvironment: noop,
addDiagnostic: noop,
startBlockScope: noop,
endBlockScope: returnUndefined,
addBlockScopedVariable: noop
addBlockScopedVariable: noop,
requestEmitHelper: noop,
readEmitHelpers: notImplemented,
enableSubstitution: noop,
enableEmitNotification: noop,
isSubstitutionEnabled: notImplemented,
isEmitNotificationEnabled: notImplemented,
onSubstituteNode: noEmitSubstitution,
onEmitNode: noEmitNotification,
addDiagnostic: noop,
};
}
3 changes: 3 additions & 0 deletions src/compiler/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6785,6 +6785,8 @@ namespace ts {

/* @internal */
export interface ParenthesizerRules {
getParenthesizeLeftSideOfBinaryForOperator(binaryOperator: SyntaxKind): (leftSide: Expression) => Expression;
getParenthesizeRightSideOfBinaryForOperator(binaryOperator: SyntaxKind): (rightSide: Expression) => Expression;
Comment on lines +6788 to +6789
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These are new, and exist to support passing in a (node: Node) => Node parenthesizer rule for binary expressions. The implementation simply wraps parenthesizeLeftSideOfBinary or parenthesizeRightSideOfBinary and caches the result for reuse.

parenthesizeLeftSideOfBinary(binaryOperator: SyntaxKind, leftSide: Expression): Expression;
parenthesizeRightSideOfBinary(binaryOperator: SyntaxKind, leftSide: Expression | undefined, rightSide: Expression): Expression;
parenthesizeExpressionOfComputedPropertyName(expression: Expression): Expression;
Expand All @@ -6798,6 +6800,7 @@ namespace ts {
parenthesizeExpressionsOfCommaDelimitedList(elements: readonly Expression[]): NodeArray<Expression>;
parenthesizeExpressionForDisallowedComma(expression: Expression): Expression;
parenthesizeExpressionOfExpressionStatement(expression: Expression): Expression;
parenthesizeConciseBodyOfArrowFunction(body: Expression): Expression;
parenthesizeConciseBodyOfArrowFunction(body: ConciseBody): ConciseBody;
parenthesizeMemberOfConditionalType(member: TypeNode): TypeNode;
parenthesizeMemberOfElementType(member: TypeNode): TypeNode;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -161,4 +161,6 @@ var _brokenTree = (0, renderer_1.dom)(component_1.MySFC, { x: 1, y: 2 },
(0, renderer_1.dom)(component_1.MyClass, { x: 3, y: 4 }),
(0, renderer_1.dom)(component_1.MyClass, { x: 5, y: 6 }));
// Should fail, nondom isn't allowed as children of dom
var _brokenTree2 = (0, renderer_1.dom)(DOMSFC, { x: 1, y: 2 }, component_1.tree, component_1.tree);
var _brokenTree2 = (0, renderer_1.dom)(DOMSFC, { x: 1, y: 2 },
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This indentation matches the emit in release-4.2.

component_1.tree,
component_1.tree);
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,5 @@ let x = <MyComp<Prop> a={10} b="hi" />; // error, no type arguments in js
exports.__esModule = true;
var component_1 = require("./component");
var React = require("react");
var x = (<component_1.MyComp />, <Prop> a={10} b="hi" />; // error, no type arguments in js
</>);
var x = <component_1.MyComp />, <Prop> a={10} b="hi" />; // error, no type arguments in js
</>;