Skip to content

Commit b48a51a

Browse files
committed
Remove preprinter, add parenthesizer callback to emit
1 parent 7c10135 commit b48a51a

File tree

7 files changed

+861
-1891
lines changed

7 files changed

+861
-1891
lines changed

src/compiler/emitter.ts

Lines changed: 805 additions & 1869 deletions
Large diffs are not rendered by default.

src/compiler/factory/nodeFactory.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2139,7 +2139,7 @@ namespace ts {
21392139
/*decorators*/ undefined,
21402140
/*modifiers*/ undefined,
21412141
name,
2142-
initializer
2142+
initializer && parenthesizerRules().parenthesizeExpressionForDisallowedComma(initializer)
21432143
);
21442144
node.propertyName = asName(propertyName);
21452145
node.dotDotDotToken = dotDotDotToken;

src/compiler/factory/parenthesizerRules.ts

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,12 @@ namespace ts {
55
cachedLiteralKind: SyntaxKind;
66
}
77

8+
let binaryLeftOperandParenthesizerCache: ESMap<BinaryOperator, (node: Expression) => Expression> | undefined;
9+
let binaryRightOperandParenthesizerCache: ESMap<BinaryOperator, (node: Expression) => Expression> | undefined;
10+
811
return {
12+
getParenthesizeLeftSideOfBinaryForOperator,
13+
getParenthesizeRightSideOfBinaryForOperator,
914
parenthesizeLeftSideOfBinary,
1015
parenthesizeRightSideOfBinary,
1116
parenthesizeExpressionOfComputedPropertyName,
@@ -27,6 +32,26 @@ namespace ts {
2732
parenthesizeTypeArguments,
2833
};
2934

35+
function getParenthesizeLeftSideOfBinaryForOperator(operatorKind: BinaryOperator) {
36+
binaryLeftOperandParenthesizerCache ||= new Map();
37+
let parenthesizerRule = binaryLeftOperandParenthesizerCache.get(operatorKind);
38+
if (!parenthesizerRule) {
39+
parenthesizerRule = node => parenthesizeLeftSideOfBinary(operatorKind, node);
40+
binaryLeftOperandParenthesizerCache.set(operatorKind, parenthesizerRule);
41+
}
42+
return parenthesizerRule;
43+
}
44+
45+
function getParenthesizeRightSideOfBinaryForOperator(operatorKind: BinaryOperator) {
46+
binaryRightOperandParenthesizerCache ||= new Map();
47+
let parenthesizerRule = binaryRightOperandParenthesizerCache.get(operatorKind);
48+
if (!parenthesizerRule) {
49+
parenthesizerRule = node => parenthesizeRightSideOfBinary(operatorKind, /*leftSide*/ undefined, node);
50+
binaryRightOperandParenthesizerCache.set(operatorKind, parenthesizerRule);
51+
}
52+
return parenthesizerRule;
53+
}
54+
3055
/**
3156
* Determines whether the operand to a BinaryExpression needs to be parenthesized.
3257
*
@@ -210,7 +235,7 @@ namespace ts {
210235
return parenthesizeBinaryOperand(binaryOperator, leftSide, /*isLeftSideOfBinary*/ true);
211236
}
212237

213-
function parenthesizeRightSideOfBinary(binaryOperator: SyntaxKind, leftSide: Expression, rightSide: Expression): Expression {
238+
function parenthesizeRightSideOfBinary(binaryOperator: SyntaxKind, leftSide: Expression | undefined, rightSide: Expression): Expression {
214239
return parenthesizeBinaryOperand(binaryOperator, rightSide, /*isLeftSideOfBinary*/ false, leftSide);
215240
}
216241

@@ -352,6 +377,8 @@ namespace ts {
352377
return expression;
353378
}
354379

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

405432
export const nullParenthesizerRules: ParenthesizerRules = {
433+
getParenthesizeLeftSideOfBinaryForOperator: _ => identity,
434+
getParenthesizeRightSideOfBinaryForOperator: _ => identity,
406435
parenthesizeLeftSideOfBinary: (_binaryOperator, leftSide) => leftSide,
407436
parenthesizeRightSideOfBinary: (_binaryOperator, _leftSide, rightSide) => rightSide,
408437
parenthesizeExpressionOfComputedPropertyName: identity,

src/compiler/transformer.ts

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -562,31 +562,31 @@ namespace ts {
562562
}
563563

564564
export const nullTransformationContext: TransformationContext = {
565-
get factory() { return factory; },
566-
enableEmitNotification: noop,
567-
enableSubstitution: noop,
568-
endLexicalEnvironment: returnUndefined,
565+
factory,
569566
getCompilerOptions: () => ({}),
570-
getEmitHost: notImplemented,
571567
getEmitResolver: notImplemented,
568+
getEmitHost: notImplemented,
572569
getEmitHelperFactory: notImplemented,
570+
startLexicalEnvironment: noop,
571+
resumeLexicalEnvironment: noop,
572+
suspendLexicalEnvironment: noop,
573+
endLexicalEnvironment: returnUndefined,
573574
setLexicalEnvironmentFlags: noop,
574575
getLexicalEnvironmentFlags: () => 0,
575-
hoistFunctionDeclaration: noop,
576576
hoistVariableDeclaration: noop,
577+
hoistFunctionDeclaration: noop,
577578
addInitializationStatement: noop,
578-
isEmitNotificationEnabled: notImplemented,
579-
isSubstitutionEnabled: notImplemented,
580-
onEmitNode: noop,
581-
onSubstituteNode: notImplemented,
582-
readEmitHelpers: notImplemented,
583-
requestEmitHelper: noop,
584-
resumeLexicalEnvironment: noop,
585-
startLexicalEnvironment: noop,
586-
suspendLexicalEnvironment: noop,
587-
addDiagnostic: noop,
588579
startBlockScope: noop,
589580
endBlockScope: returnUndefined,
590-
addBlockScopedVariable: noop
581+
addBlockScopedVariable: noop,
582+
requestEmitHelper: noop,
583+
readEmitHelpers: notImplemented,
584+
enableSubstitution: noop,
585+
enableEmitNotification: noop,
586+
isSubstitutionEnabled: notImplemented,
587+
isEmitNotificationEnabled: notImplemented,
588+
onSubstituteNode: noEmitSubstitution,
589+
onEmitNode: noEmitNotification,
590+
addDiagnostic: noop,
591591
};
592592
}

src/compiler/types.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6785,6 +6785,8 @@ namespace ts {
67856785

67866786
/* @internal */
67876787
export interface ParenthesizerRules {
6788+
getParenthesizeLeftSideOfBinaryForOperator(binaryOperator: SyntaxKind): (leftSide: Expression) => Expression;
6789+
getParenthesizeRightSideOfBinaryForOperator(binaryOperator: SyntaxKind): (rightSide: Expression) => Expression;
67886790
parenthesizeLeftSideOfBinary(binaryOperator: SyntaxKind, leftSide: Expression): Expression;
67896791
parenthesizeRightSideOfBinary(binaryOperator: SyntaxKind, leftSide: Expression | undefined, rightSide: Expression): Expression;
67906792
parenthesizeExpressionOfComputedPropertyName(expression: Expression): Expression;
@@ -6798,6 +6800,7 @@ namespace ts {
67986800
parenthesizeExpressionsOfCommaDelimitedList(elements: readonly Expression[]): NodeArray<Expression>;
67996801
parenthesizeExpressionForDisallowedComma(expression: Expression): Expression;
68006802
parenthesizeExpressionOfExpressionStatement(expression: Expression): Expression;
6803+
parenthesizeConciseBodyOfArrowFunction(body: Expression): Expression;
68016804
parenthesizeConciseBodyOfArrowFunction(body: ConciseBody): ConciseBody;
68026805
parenthesizeMemberOfConditionalType(member: TypeNode): TypeNode;
68036806
parenthesizeMemberOfElementType(member: TypeNode): TypeNode;

tests/baselines/reference/inlineJsxFactoryDeclarationsLocalTypes.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -161,4 +161,6 @@ var _brokenTree = (0, renderer_1.dom)(component_1.MySFC, { x: 1, y: 2 },
161161
(0, renderer_1.dom)(component_1.MyClass, { x: 3, y: 4 }),
162162
(0, renderer_1.dom)(component_1.MyClass, { x: 5, y: 6 }));
163163
// Should fail, nondom isn't allowed as children of dom
164-
var _brokenTree2 = (0, renderer_1.dom)(DOMSFC, { x: 1, y: 2 }, component_1.tree, component_1.tree);
164+
var _brokenTree2 = (0, renderer_1.dom)(DOMSFC, { x: 1, y: 2 },
165+
component_1.tree,
166+
component_1.tree);

tests/baselines/reference/jsxCheckJsxNoTypeArgumentsAllowed.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,5 +23,5 @@ let x = <MyComp<Prop> a={10} b="hi" />; // error, no type arguments in js
2323
exports.__esModule = true;
2424
var component_1 = require("./component");
2525
var React = require("react");
26-
var x = (<component_1.MyComp />, <Prop> a={10} b="hi" />; // error, no type arguments in js
27-
</>);
26+
var x = <component_1.MyComp />, <Prop> a={10} b="hi" />; // error, no type arguments in js
27+
</>;

0 commit comments

Comments
 (0)