Remove preprinter, add parenthesizer callback to emit (#43652)

This commit is contained in:
Ron Buckton 2021-04-19 09:34:46 -07:00 committed by GitHub
parent b1ab2b98be
commit e0d551606f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 827 additions and 1857 deletions

File diff suppressed because it is too large Load Diff

View File

@ -2139,7 +2139,7 @@ namespace ts {
/*decorators*/ undefined,
/*modifiers*/ undefined,
name,
initializer
initializer && parenthesizerRules().parenthesizeExpressionForDisallowedComma(initializer)
);
node.propertyName = asName(propertyName);
node.dotDotDotToken = dotDotDotToken;

View File

@ -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,
@ -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.
*
@ -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);
}
@ -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.
@ -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,

View File

@ -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,
};
}

View File

@ -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;
@ -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;

View File

@ -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);

View File

@ -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
</>;