Skip to content

Commit

Permalink
Remove preprinter, add parenthesizer callback to emit (#43652)
Browse files Browse the repository at this point in the history
  • Loading branch information
rbuckton authored Apr 19, 2021
1 parent b1ab2b9 commit e0d5516
Show file tree
Hide file tree
Showing 7 changed files with 861 additions and 1,891 deletions.
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; },
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 @@ -6801,6 +6801,8 @@ namespace ts {

/* @internal */
export interface ParenthesizerRules {
getParenthesizeLeftSideOfBinaryForOperator(binaryOperator: SyntaxKind): (leftSide: Expression) => Expression;
getParenthesizeRightSideOfBinaryForOperator(binaryOperator: SyntaxKind): (rightSide: Expression) => Expression;
parenthesizeLeftSideOfBinary(binaryOperator: SyntaxKind, leftSide: Expression): Expression;
parenthesizeRightSideOfBinary(binaryOperator: SyntaxKind, leftSide: Expression | undefined, rightSide: Expression): Expression;
parenthesizeExpressionOfComputedPropertyName(expression: Expression): Expression;
Expand All @@ -6814,6 +6816,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 },
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
</>;

0 comments on commit e0d5516

Please sign in to comment.