namespace ts . formatting { export interface RuleSpec { readonly leftTokenRange : TokenRange ; readonly rightTokenRange : TokenRange ; readonly rule : Rule ; } export function getAllRules ( ) { const allTokens = [ ] ; for ( let token = SyntaxKind . FirstToken ; token <= SyntaxKind . LastToken ; token ++ ) { if ( token !== SyntaxKind . EndOfFileToken ) { allTokens . push ( token ) ; } } function anyTokenExcept ( ... tokens ) { return { tokens : allTokens . filter ( t => ! tokens . some ( t2 => t2 === t ) ) , isSpecific : false } ; } const anyToken = { tokens : allTokens , isSpecific : false } ; const anyTokenIncludingMultilineComments = tokenRangeFrom ( [ ... allTokens , SyntaxKind . MultiLineCommentTrivia ] ) ; const anyTokenIncludingEOF = tokenRangeFrom ( [ ... allTokens , SyntaxKind . EndOfFileToken ] ) ; const keywords = tokenRangeFromRange ( SyntaxKind . FirstKeyword , SyntaxKind . LastKeyword ) ; const binaryOperators = tokenRangeFromRange ( SyntaxKind . FirstBinaryOperator , SyntaxKind . LastBinaryOperator ) ; const binaryKeywordOperators = [ SyntaxKind . InKeyword , SyntaxKind . InstanceOfKeyword , SyntaxKind . OfKeyword , SyntaxKind . AsKeyword , SyntaxKind . IsKeyword ] ; const unaryPrefixOperators = [ SyntaxKind . PlusPlusToken , SyntaxKind . MinusMinusToken , SyntaxKind . TildeToken , SyntaxKind . ExclamationToken ] ; const unaryPrefixExpressions = [ SyntaxKind . NumericLiteral , SyntaxKind . BigIntLiteral , SyntaxKind . Identifier , SyntaxKind . OpenParenToken , SyntaxKind . OpenBracketToken , SyntaxKind . OpenBraceToken , SyntaxKind . ThisKeyword , SyntaxKind . NewKeyword ] ; const unaryPreincrementExpressions = [ SyntaxKind . Identifier , SyntaxKind . OpenParenToken , SyntaxKind . ThisKeyword , SyntaxKind . NewKeyword ] ; const unaryPostincrementExpressions = [ SyntaxKind . Identifier , SyntaxKind . CloseParenToken , SyntaxKind . CloseBracketToken , SyntaxKind . NewKeyword ] ; const unaryPredecrementExpressions = [ SyntaxKind . Identifier , SyntaxKind . OpenParenToken , SyntaxKind . ThisKeyword , SyntaxKind . NewKeyword ] ; const unaryPostdecrementExpressions = [ SyntaxKind . Identifier , SyntaxKind . CloseParenToken , SyntaxKind . CloseBracketToken , SyntaxKind . NewKeyword ] ; const comments = [ SyntaxKind . SingleLineCommentTrivia , SyntaxKind . MultiLineCommentTrivia ] ; const typeNames = [ SyntaxKind . Identifier , ... typeKeywords ] ; const functionOpenBraceLeftTokenRange = anyTokenIncludingMultilineComments ; const typeScriptOpenBraceLeftTokenRange = tokenRangeFrom ( [ SyntaxKind . Identifier , SyntaxKind . MultiLineCommentTrivia , SyntaxKind . ClassKeyword , SyntaxKind . ExportKeyword , SyntaxKind . ImportKeyword ] ) ; const controlOpenBraceLeftTokenRange = tokenRangeFrom ( [ SyntaxKind . CloseParenToken , SyntaxKind . MultiLineCommentTrivia , SyntaxKind . DoKeyword , SyntaxKind . TryKeyword , SyntaxKind . FinallyKeyword , SyntaxKind . ElseKeyword ] ) ; const highPriorityCommonRules = [ rule ( "IgnoreBeforeComment" , anyToken , comments , anyContext , RuleAction . StopProcessingSpaceActions ) , rule ( "IgnoreAfterLineComment" , SyntaxKind . SingleLineCommentTrivia , anyToken , anyContext , RuleAction . StopProcessingSpaceActions ) , rule ( "NotSpaceBeforeColon" , anyToken , SyntaxKind . ColonToken , [ isNonJsxSameLineTokenContext , isNotBinaryOpContext , isNotTypeAnnotationContext ] , RuleAction . DeleteSpace ) , rule ( "SpaceAfterColon" , SyntaxKind . ColonToken , anyToken , [ isNonJsxSameLineTokenContext , isNotBinaryOpContext ] , RuleAction . InsertSpace ) , rule ( "NoSpaceBeforeQuestionMark" , anyToken , SyntaxKind . QuestionToken , [ isNonJsxSameLineTokenContext , isNotBinaryOpContext , isNotTypeAnnotationContext ] , RuleAction . DeleteSpace ) , rule ( "SpaceAfterQuestionMarkInConditionalOperator" , SyntaxKind . QuestionToken , anyToken , [ isNonJsxSameLineTokenContext , isConditionalOperatorContext ] , RuleAction . InsertSpace ) , rule ( "NoSpaceAfterQuestionMark" , SyntaxKind . QuestionToken , anyToken , [ isNonJsxSameLineTokenContext ] , RuleAction . DeleteSpace ) , rule ( "NoSpaceBeforeDot" , anyToken , [ SyntaxKind . DotToken , SyntaxKind . QuestionDotToken ] , [ isNonJsxSameLineTokenContext ] , RuleAction . DeleteSpace ) , rule ( "NoSpaceAfterDot" , [ SyntaxKind . DotToken , SyntaxKind . QuestionDotToken ] , anyToken , [ isNonJsxSameLineTokenContext ] , RuleAction . DeleteSpace ) , rule ( "NoSpaceBetweenImportParenInImportType" , SyntaxKind . ImportKeyword , SyntaxKind . OpenParenToken , [ isNonJsxSameLineTokenContext , isImportTypeContext ] , RuleAction . DeleteSpace ) , rule ( "NoSpaceAfterUnaryPrefixOperator" , unaryPrefixOperators , unaryPrefixExpressions , [ isNonJsxSameLineTokenContext , isNotBinaryOpContext ] , RuleAction . DeleteSpace ) , rule ( "NoSpaceAfterUnaryPreincrementOperator" , SyntaxKind . PlusPlusToken , unaryPreincrementExpressions , [ isNonJsxSameLineTokenContext ] , RuleAction . DeleteSpace ) , rule ( "NoSpaceAfterUnaryPredecrementOperator" , SyntaxKind . MinusMinusToken , unaryPredecrementExpressions , [ isNonJsxSameLineTokenContext ] , RuleAction . DeleteSpace ) , rule ( "NoSpaceBeforeUnaryPostincrementOperator" , unaryPostincrementExpressions , SyntaxKind . PlusPlusToken , [ isNonJsxSameLineTokenContext , isNotStatementConditionContext ] , RuleAction . DeleteSpace ) , rule ( "NoSpaceBeforeUnaryPostdecrementOperator" , unaryPostdecrementExpressions , SyntaxKind . MinusMinusToken , [ isNonJsxSameLineTokenContext , isNotStatementConditionContext ] , RuleAction . DeleteSpace ) , rule ( "SpaceAfterPostincrementWhenFollowedByAdd" , SyntaxKind . PlusPlusToken , SyntaxKind . PlusToken , [ isNonJsxSameLineTokenContext , isBinaryOpContext ] , RuleAction . InsertSpace ) , rule ( "SpaceAfterAddWhenFollowedByUnaryPlus" , SyntaxKind . PlusToken , SyntaxKind . PlusToken , [ isNonJsxSameLineTokenContext , isBinaryOpContext ] , RuleAction . InsertSpace ) , rule ( "SpaceAfterAddWhenFollowedByPreincrement" , SyntaxKind . PlusToken , SyntaxKind . PlusPlusToken , [ isNonJsxSameLineTokenContext , isBinaryOpContext ] , RuleAction . InsertSpace ) , rule ( "SpaceAfterPostdecrementWhenFollowedBySubtract" , SyntaxKind . MinusMinusToken , SyntaxKind . MinusToken , [ isNonJsxSameLineTokenContext , isBinaryOpContext ] , RuleAction . InsertSpace ) , rule ( "SpaceAfterSubtractWhenFollowedByUnaryMinus" , SyntaxKind . MinusToken , SyntaxKind . MinusToken , [ isNonJsxSameLineTokenContext , isBinaryOpContext ] , RuleAction . InsertSpace ) , rule ( "SpaceAfterSubtractWhenFollowedByPredecrement" , SyntaxKind . MinusToken , SyntaxKind . MinusMinusToken , [ isNonJsxSameLineTokenContext , isBinaryOpContext ] , RuleAction . InsertSpace ) , rule ( "NoSpaceAfterCloseBrace" , SyntaxKind . CloseBraceToken , [ SyntaxKind . CommaToken , SyntaxKind . SemicolonToken ] , [ isNonJsxSameLineTokenContext ] , RuleAction . DeleteSpace ) , rule ( "NewLineBeforeCloseBraceInBlockContext" , anyTokenIncludingMultilineComments , SyntaxKind . CloseBraceToken , [ isMultilineBlockContext ] , RuleAction . InsertNewLine ) , rule ( "SpaceAfterCloseBrace" , SyntaxKind . CloseBraceToken , anyTokenExcept ( SyntaxKind . CloseParenToken ) , [ isNonJsxSameLineTokenContext , isAfterCodeBlockContext ] , RuleAction . InsertSpace ) , rule ( "SpaceBetweenCloseBraceAndElse" , SyntaxKind . CloseBraceToken , SyntaxKind . ElseKeyword , [ isNonJsxSameLineTokenContext ] , RuleAction . InsertSpace ) , rule ( "SpaceBetweenCloseBraceAndWhile" , SyntaxKind . CloseBraceToken , SyntaxKind . WhileKeyword , [ isNonJsxSameLineTokenContext ] , RuleAction . InsertSpace ) , rule ( "NoSpaceBetweenEmptyBraceBrackets" , SyntaxKind . OpenBraceToken , SyntaxKind . CloseBraceToken , [ isNonJsxSameLineTokenContext , isObjectContext ] , RuleAction . DeleteSpace ) , rule ( "SpaceAfterConditionalClosingParen" , SyntaxKind . CloseParenToken , SyntaxKind . OpenBracketToken , [ isControlDeclContext ] , RuleAction . InsertSpace ) , rule ( "NoSpaceBetweenFunctionKeywordAndStar" , SyntaxKind . FunctionKeyword , SyntaxKind . AsteriskToken , [ isFunctionDeclarationOrFunctionExpressionContext ] , RuleAction . DeleteSpace ) , rule ( "SpaceAfterStarInGeneratorDeclaration" , SyntaxKind . AsteriskToken , SyntaxKind . Identifier , [ isFunctionDeclarationOrFunctionExpressionContext ] , RuleAction . InsertSpace ) , rule ( "SpaceAfterFunctionInFuncDecl" , SyntaxKind . FunctionKeyword , anyToken , [ isFunctionDeclContext ] , RuleAction . InsertSpace ) , rule ( "NewLineAfterOpenBraceInBlockContext" , SyntaxKind . OpenBraceToken , anyToken , [ isMultilineBlockContext ] , RuleAction . InsertNewLine ) , rule ( "SpaceAfterGetSetInMember" , [ SyntaxKind . GetKeyword , SyntaxKind . SetKeyword ] , SyntaxKind . Identifier , [ isFunctionDeclContext ] , RuleAction . InsertSpace ) , rule ( "NoSpaceBetweenYieldKeywordAndStar" , SyntaxKind . YieldKeyword , SyntaxKind . AsteriskToken , [ isNonJsxSameLineTokenContext , isYieldOrYieldStarWithOperand ] , RuleAction . DeleteSpace ) , rule ( "SpaceBetweenYieldOrYieldStarAndOperand" , [ SyntaxKind . YieldKeyword , SyntaxKind . AsteriskToken ] , anyToken , [ isNonJsxSameLineTokenContext , isYieldOrYieldStarWithOperand ] , RuleAction . InsertSpace ) , rule ( "NoSpaceBetweenReturnAndSemicolon" , SyntaxKind . ReturnKeyword , SyntaxKind . SemicolonToken , [ isNonJsxSameLineTokenContext ] , RuleAction . DeleteSpace ) , rule ( "SpaceAfterCertainKeywords" , [ SyntaxKind . VarKeyword , SyntaxKind . ThrowKeyword , SyntaxKind . NewKeyword , SyntaxKind . DeleteKeyword , SyntaxKind . ReturnKeyword , SyntaxKind . TypeOfKeyword , SyntaxKind . AwaitKeyword ] , anyToken , [ isNonJsxSameLineTokenContext ] , RuleAction . InsertSpace ) , rule ( "SpaceAfterLetConstInVariableDeclaration" , [ SyntaxKind . LetKeyword , SyntaxKind . ConstKeyword ] , anyToken , [ isNonJsxSameLineTokenContext , isStartOfVariableDeclarationList ] , RuleAction . InsertSpace ) , rule ( "NoSpaceBeforeOpenParenInFuncCall" , anyToken , SyntaxKind . OpenParenToken , [ isNonJsxSameLineTokenContext , isFunctionCallOrNewContext , isPreviousTokenNotComma ] , RuleAction . DeleteSpace ) , rule ( "SpaceBeforeBinaryKeywordOperator" , anyToken , binaryKeywordOperators , [ isNonJsxSameLineTokenContext , isBinaryOpContext ] , RuleAction . InsertSpace ) , rule ( "SpaceAfterBinaryKeywordOperator" , binaryKeywordOperators , anyToken , [ isNonJsxSameLineTokenContext , isBinaryOpContext ] , RuleAction . InsertSpace ) , rule ( "SpaceAfterVoidOperator" , SyntaxKind . VoidKeyword , anyToken , [ isNonJsxSameLineTokenContext , isVoidOpContext ] , RuleAction . InsertSpace ) , rule ( "SpaceBetweenAsyncAndOpenParen" , SyntaxKind . AsyncKeyword , SyntaxKind . OpenParenToken , [ isArrowFunctionContext , isNonJsxSameLineTokenContext ] , RuleAction . InsertSpace ) , rule ( "SpaceBetweenAsyncAndFunctionKeyword" , SyntaxKind . AsyncKeyword , [ SyntaxKind . FunctionKeyword , SyntaxKind . Identifier ] , [ isNonJsxSameLineTokenContext ] , RuleAction . InsertSpace ) , rule ( "NoSpaceBetweenTagAndTemplateString" , [ SyntaxKind . Identifier , SyntaxKind . CloseParenToken ] , [ SyntaxKind . NoSubstitutionTemplateLiteral , SyntaxKind . TemplateHead ] , [ isNonJsxSameLineTokenContext ] , RuleAction . DeleteSpace ) , rule ( "SpaceBeforeJsxAttribute" , anyToken , SyntaxKind . Identifier , [ isNextTokenParentJsxAttribute , isNonJsxSameLineTokenContext ] , RuleAction . InsertSpace ) , rule ( "SpaceBeforeSlashInJsxOpeningElement" , anyToken , SyntaxKind . SlashToken , [ isJsxSelfClosingElementContext , isNonJsxSameLineTokenContext ] , RuleAction . InsertSpace ) , rule ( "NoSpaceBeforeGreaterThanTokenInJsxOpeningElement" , SyntaxKind . SlashToken , SyntaxKind . GreaterThanToken , [ isJsxSelfClosingElementContext , isNonJsxSameLineTokenContext ] , RuleAction . DeleteSpace ) , rule ( "NoSpaceBeforeEqualInJsxAttribute" , anyToken , SyntaxKind . EqualsToken , [ isJsxAttributeContext , isNonJsxSameLineTokenContext ] , RuleAction . DeleteSpace ) , rule ( "NoSpaceAfterEqualInJsxAttribute" , SyntaxKind . EqualsToken , anyToken , [ isJsxAttributeContext , isNonJsxSameLineTokenContext ] , RuleAction . DeleteSpace ) , rule ( "NoSpaceAfterModuleImport" , [ SyntaxKind . ModuleKeyword , SyntaxKind . RequireKeyword ] , SyntaxKind . OpenParenToken , [ isNonJsxSameLineTokenContext ] , RuleAction . DeleteSpace ) , rule ( "SpaceAfterCertainTypeScriptKeywords" , [ SyntaxKind . AbstractKeyword , SyntaxKind . ClassKeyword , SyntaxKind . DeclareKeyword , SyntaxKind . DefaultKeyword , SyntaxKind . EnumKeyword , SyntaxKind . ExportKeyword , SyntaxKind . ExtendsKeyword , SyntaxKind . GetKeyword , SyntaxKind . ImplementsKeyword , SyntaxKind . ImportKeyword , SyntaxKind . InterfaceKeyword , SyntaxKind . ModuleKeyword , SyntaxKind . NamespaceKeyword , SyntaxKind . PrivateKeyword , SyntaxKind . PublicKeyword , SyntaxKind . ProtectedKeyword , SyntaxKind . ReadonlyKeyword , SyntaxKind . SetKeyword , SyntaxKind . StaticKeyword , SyntaxKind . TypeKeyword , SyntaxKind . FromKeyword , SyntaxKind . KeyOfKeyword , SyntaxKind . InferKeyword , ] , anyToken , [ isNonJsxSameLineTokenContext ] , RuleAction . InsertSpace ) , rule ( "SpaceBeforeCertainTypeScriptKeywords" , anyToken , [ SyntaxKind . ExtendsKeyword , SyntaxKind . ImplementsKeyword , SyntaxKind . FromKeyword ] , [ isNonJsxSameLineTokenContext ] , RuleAction . InsertSpace ) , rule ( "SpaceAfterModuleName" , SyntaxKind . StringLiteral , SyntaxKind . OpenBraceToken , [ isModuleDeclContext ] , RuleAction . InsertSpace ) , rule ( "SpaceBeforeArrow" , anyToken , SyntaxKind . EqualsGreaterThanToken , [ isNonJsxSameLineTokenContext ] , RuleAction . InsertSpace ) , rule ( "SpaceAfterArrow" , SyntaxKind . EqualsGreaterThanToken , anyToken , [ isNonJsxSameLineTokenContext ] , RuleAction . InsertSpace ) , rule ( "NoSpaceAfterEllipsis" , SyntaxKind . DotDotDotToken , SyntaxKind . Identifier , [ isNonJsxSameLineTokenContext ] , RuleAction . DeleteSpace ) , rule ( "NoSpaceAfterOptionalParameters" , SyntaxKind . QuestionToken , [ SyntaxKind . CloseParenToken , SyntaxKind . CommaToken ] , [ isNonJsxSameLineTokenContext , isNotBinaryOpContext ] , RuleAction . DeleteSpace ) , rule ( "NoSpaceBetweenEmptyInterfaceBraceBrackets" , SyntaxKind . OpenBraceToken , SyntaxKind . CloseBraceToken , [ isNonJsxSameLineTokenContext , isObjectTypeContext ] , RuleAction . DeleteSpace ) , rule ( "NoSpaceBeforeOpenAngularBracket" , typeNames , SyntaxKind . LessThanToken , [ isNonJsxSameLineTokenContext , isTypeArgumentOrParameterOrAssertionContext ] , RuleAction . DeleteSpace ) , rule ( "NoSpaceBetweenCloseParenAndAngularBracket" , SyntaxKind . CloseParenToken , SyntaxKind . LessThanToken , [ isNonJsxSameLineTokenContext , isTypeArgumentOrParameterOrAssertionContext ] , RuleAction . DeleteSpace ) , rule ( "NoSpaceAfterOpenAngularBracket" , SyntaxKind . LessThanToken , anyToken , [ isNonJsxSameLineTokenContext , isTypeArgumentOrParameterOrAssertionContext ] , RuleAction . DeleteSpace ) , rule ( "NoSpaceBeforeCloseAngularBracket" , anyToken , SyntaxKind . GreaterThanToken , [ isNonJsxSameLineTokenContext , isTypeArgumentOrParameterOrAssertionContext ] , RuleAction . DeleteSpace ) , rule ( "NoSpaceAfterCloseAngularBracket" , SyntaxKind . GreaterThanToken , [ SyntaxKind . OpenParenToken , SyntaxKind . OpenBracketToken , SyntaxKind . GreaterThanToken , SyntaxKind . CommaToken ] , [ isNonJsxSameLineTokenContext , isTypeArgumentOrParameterOrAssertionContext , isNotFunctionDeclContext ] , RuleAction . DeleteSpace ) , rule ( "SpaceBeforeAt" , [ SyntaxKind . CloseParenToken , SyntaxKind . Identifier ] , SyntaxKind . AtToken , [ isNonJsxSameLineTokenContext ] , RuleAction . InsertSpace ) , rule ( "NoSpaceAfterAt" , SyntaxKind . AtToken , anyToken , [ isNonJsxSameLineTokenContext ] , RuleAction . DeleteSpace ) , rule ( "SpaceAfterDecorator" , anyToken , [ SyntaxKind . AbstractKeyword , SyntaxKind . Identifier , SyntaxKind . ExportKeyword , SyntaxKind . DefaultKeyword , SyntaxKind . ClassKeyword , SyntaxKind . StaticKeyword , SyntaxKind . PublicKeyword , SyntaxKind . PrivateKeyword , SyntaxKind . ProtectedKeyword , SyntaxKind . GetKeyword , SyntaxKind . SetKeyword , SyntaxKind . OpenBracketToken , SyntaxKind . AsteriskToken , ] , [ isEndOfDecoratorContextOnSameLine ] , RuleAction . InsertSpace ) , rule ( "NoSpaceBeforeNonNullAssertionOperator" , anyToken , SyntaxKind . ExclamationToken , [ isNonJsxSameLineTokenContext , isNonNullAssertionContext ] , RuleAction . DeleteSpace ) , rule ( "NoSpaceAfterNewKeywordOnConstructorSignature" , SyntaxKind . NewKeyword , SyntaxKind . OpenParenToken , [ isNonJsxSameLineTokenContext , isConstructorSignatureContext ] , RuleAction . DeleteSpace ) , rule ( "SpaceLessThanAndNonJSXTypeAnnotation" , SyntaxKind . LessThanToken , SyntaxKind . LessThanToken , [ isNonJsxSameLineTokenContext ] , RuleAction . InsertSpace ) , ] ; const userConfigurableRules = [ rule ( "SpaceAfterConstructor" , SyntaxKind . ConstructorKeyword , SyntaxKind . OpenParenToken , [ isOptionEnabled ( "insertSpaceAfterConstructor" ) , isNonJsxSameLineTokenContext ] , RuleAction . InsertSpace ) , rule ( "NoSpaceAfterConstructor" , SyntaxKind . ConstructorKeyword , SyntaxKind . OpenParenToken , [ isOptionDisabledOrUndefined ( "insertSpaceAfterConstructor" ) , isNonJsxSameLineTokenContext ] , RuleAction . DeleteSpace ) , rule ( "SpaceAfterComma" , SyntaxKind . CommaToken , anyToken , [ isOptionEnabled ( "insertSpaceAfterCommaDelimiter" ) , isNonJsxSameLineTokenContext , isNonJsxElementOrFragmentContext , isNextTokenNotCloseBracket , isNextTokenNotCloseParen ] , RuleAction . InsertSpace ) , rule ( "NoSpaceAfterComma" , SyntaxKind . CommaToken , anyToken , [ isOptionDisabledOrUndefined ( "insertSpaceAfterCommaDelimiter" ) , isNonJsxSameLineTokenContext , isNonJsxElementOrFragmentContext ] , RuleAction . DeleteSpace ) , rule ( "SpaceAfterAnonymousFunctionKeyword" , [ SyntaxKind . FunctionKeyword , SyntaxKind . AsteriskToken ] , SyntaxKind . OpenParenToken , [ isOptionEnabled ( "insertSpaceAfterFunctionKeywordForAnonymousFunctions" ) , isFunctionDeclContext ] , RuleAction . InsertSpace ) , rule ( "NoSpaceAfterAnonymousFunctionKeyword" , [ SyntaxKind . FunctionKeyword , SyntaxKind . AsteriskToken ] , SyntaxKind . OpenParenToken , [ isOptionDisabledOrUndefined ( "insertSpaceAfterFunctionKeywordForAnonymousFunctions" ) , isFunctionDeclContext ] , RuleAction . DeleteSpace ) , rule ( "SpaceAfterKeywordInControl" , keywords , SyntaxKind . OpenParenToken , [ isOptionEnabled ( "insertSpaceAfterKeywordsInControlFlowStatements" ) , isControlDeclContext ] , RuleAction . InsertSpace ) , rule ( "NoSpaceAfterKeywordInControl" , keywords , SyntaxKind . OpenParenToken , [ isOptionDisabledOrUndefined ( "insertSpaceAfterKeywordsInControlFlowStatements" ) , isControlDeclContext ] , RuleAction . DeleteSpace ) , rule ( "SpaceAfterOpenParen" , SyntaxKind . OpenParenToken , anyToken , [ isOptionEnabled ( "insertSpaceAfterOpeningAndBeforeClosingNonemptyParenthesis" ) , isNonJsxSameLineTokenContext ] , RuleAction . InsertSpace ) , rule ( "SpaceBeforeCloseParen" , anyToken , SyntaxKind . CloseParenToken , [ isOptionEnabled ( "insertSpaceAfterOpeningAndBeforeClosingNonemptyParenthesis" ) , isNonJsxSameLineTokenContext ] , RuleAction . InsertSpace ) , rule ( "SpaceBetweenOpenParens" , SyntaxKind . OpenParenToken , SyntaxKind . OpenParenToken , [ isOptionEnabled ( "insertSpaceAfterOpeningAndBeforeClosingNonemptyParenthesis" ) , isNonJsxSameLineTokenContext ] , RuleAction . InsertSpace ) , rule ( "NoSpaceBetweenParens" , SyntaxKind . OpenParenToken , SyntaxKind . CloseParenToken , [ isNonJsxSameLineTokenContext ] , RuleAction . DeleteSpace ) , rule ( "NoSpaceAfterOpenParen" , SyntaxKind . OpenParenToken , anyToken , [ isOptionDisabledOrUndefined ( "insertSpaceAfterOpeningAndBeforeClosingNonemptyParenthesis" ) , isNonJsxSameLineTokenContext ] , RuleAction . DeleteSpace ) , rule ( "NoSpaceBeforeCloseParen" , anyToken , SyntaxKind . CloseParenToken , [ isOptionDisabledOrUndefined ( "insertSpaceAfterOpeningAndBeforeClosingNonemptyParenthesis" ) , isNonJsxSameLineTokenContext ] , RuleAction . DeleteSpace ) , rule ( "SpaceAfterOpenBracket" , SyntaxKind . OpenBracketToken , anyToken , [ isOptionEnabled ( "insertSpaceAfterOpeningAndBeforeClosingNonemptyBrackets" ) , isNonJsxSameLineTokenContext ] , RuleAction . InsertSpace ) , rule ( "SpaceBeforeCloseBracket" , anyToken , SyntaxKind . CloseBracketToken , [ isOptionEnabled ( "insertSpaceAfterOpeningAndBeforeClosingNonemptyBrackets" ) , isNonJsxSameLineTokenContext ] , RuleAction . InsertSpace ) , rule ( "NoSpaceBetweenBrackets" , SyntaxKind . OpenBracketToken , SyntaxKind . CloseBracketToken , [ isNonJsxSameLineTokenContext ] , RuleAction . DeleteSpace ) , rule ( "NoSpaceAfterOpenBracket" , SyntaxKind . OpenBracketToken , anyToken , [ isOptionDisabledOrUndefined ( "insertSpaceAfterOpeningAndBeforeClosingNonemptyBrackets" ) , isNonJsxSameLineTokenContext ] , RuleAction . DeleteSpace ) , rule ( "NoSpaceBeforeCloseBracket" , anyToken , SyntaxKind . CloseBracketToken , [ isOptionDisabledOrUndefined ( "insertSpaceAfterOpeningAndBeforeClosingNonemptyBrackets" ) , isNonJsxSameLineTokenContext ] , RuleAction . DeleteSpace ) , rule ( "SpaceAfterOpenBrace" , SyntaxKind . OpenBraceToken , anyToken , [ isOptionEnabledOrUndefined ( "insertSpaceAfterOpeningAndBeforeClosingNonemptyBraces" ) , isBraceWrappedContext ] , RuleAction . InsertSpace ) , rule ( "SpaceBeforeCloseBrace" , anyToken , SyntaxKind . CloseBraceToken , [ isOptionEnabledOrUndefined ( "insertSpaceAfterOpeningAndBeforeClosingNonemptyBraces" ) , isBraceWrappedContext ] , RuleAction . InsertSpace ) , rule ( "NoSpaceBetweenEmptyBraceBrackets" , SyntaxKind . OpenBraceToken , SyntaxKind . CloseBraceToken , [ isNonJsxSameLineTokenContext , isObjectContext ] , RuleAction . DeleteSpace ) , rule ( "NoSpaceAfterOpenBrace" , SyntaxKind . OpenBraceToken , anyToken , [ isOptionDisabled ( "insertSpaceAfterOpeningAndBeforeClosingNonemptyBraces" ) , isNonJsxSameLineTokenContext ] , RuleAction . DeleteSpace ) , rule ( "NoSpaceBeforeCloseBrace" , anyToken , SyntaxKind . CloseBraceToken , [ isOptionDisabled ( "insertSpaceAfterOpeningAndBeforeClosingNonemptyBraces" ) , isNonJsxSameLineTokenContext ] , RuleAction . DeleteSpace ) , rule ( "SpaceBetweenEmptyBraceBrackets" , SyntaxKind . OpenBraceToken , SyntaxKind . CloseBraceToken , [ isOptionEnabled ( "insertSpaceAfterOpeningAndBeforeClosingEmptyBraces" ) ] , RuleAction . InsertSpace ) , rule ( "NoSpaceBetweenEmptyBraceBrackets" , SyntaxKind . OpenBraceToken , SyntaxKind . CloseBraceToken , [ isOptionDisabled ( "insertSpaceAfterOpeningAndBeforeClosingEmptyBraces" ) , isNonJsxSameLineTokenContext ] , RuleAction . DeleteSpace ) , rule ( "SpaceAfterTemplateHeadAndMiddle" , [ SyntaxKind . TemplateHead , SyntaxKind . TemplateMiddle ] , anyToken , [ isOptionEnabled ( "insertSpaceAfterOpeningAndBeforeClosingTemplateStringBraces" ) , isNonJsxTextContext ] , RuleAction . InsertSpace , RuleFlags . CanDeleteNewLines ) , rule ( "SpaceBeforeTemplateMiddleAndTail" , anyToken , [ SyntaxKind . TemplateMiddle , SyntaxKind . TemplateTail ] , [ isOptionEnabled ( "insertSpaceAfterOpeningAndBeforeClosingTemplateStringBraces" ) , isNonJsxSameLineTokenContext ] , RuleAction . InsertSpace ) , rule ( "NoSpaceAfterTemplateHeadAndMiddle" , [ SyntaxKind . TemplateHead , SyntaxKind . TemplateMiddle ] , anyToken , [ isOptionDisabledOrUndefined ( "insertSpaceAfterOpeningAndBeforeClosingTemplateStringBraces" ) , isNonJsxTextContext ] , RuleAction . DeleteSpace , RuleFlags . CanDeleteNewLines ) , rule ( "NoSpaceBeforeTemplateMiddleAndTail" , anyToken , [ SyntaxKind . TemplateMiddle , SyntaxKind . TemplateTail ] , [ isOptionDisabledOrUndefined ( "insertSpaceAfterOpeningAndBeforeClosingTemplateStringBraces" ) , isNonJsxSameLineTokenContext ] , RuleAction . DeleteSpace ) , rule ( "SpaceAfterOpenBraceInJsxExpression" , SyntaxKind . OpenBraceToken , anyToken , [ isOptionEnabled ( "insertSpaceAfterOpeningAndBeforeClosingJsxExpressionBraces" ) , isNonJsxSameLineTokenContext , isJsxExpressionContext ] , RuleAction . InsertSpace ) , rule ( "SpaceBeforeCloseBraceInJsxExpression" , anyToken , SyntaxKind . CloseBraceToken , [ isOptionEnabled ( "insertSpaceAfterOpeningAndBeforeClosingJsxExpressionBraces" ) , isNonJsxSameLineTokenContext , isJsxExpressionContext ] , RuleAction . InsertSpace ) , rule ( "NoSpaceAfterOpenBraceInJsxExpression" , SyntaxKind . OpenBraceToken , anyToken , [ isOptionDisabledOrUndefined ( "insertSpaceAfterOpeningAndBeforeClosingJsxExpressionBraces" ) , isNonJsxSameLineTokenContext , isJsxExpressionContext ] , RuleAction . DeleteSpace ) , rule ( "NoSpaceBeforeCloseBraceInJsxExpression" , anyToken , SyntaxKind . CloseBraceToken , [ isOptionDisabledOrUndefined ( "insertSpaceAfterOpeningAndBeforeClosingJsxExpressionBraces" ) , isNonJsxSameLineTokenContext , isJsxExpressionContext ] , RuleAction . DeleteSpace ) , rule ( "SpaceAfterSemicolonInFor" , SyntaxKind . SemicolonToken , anyToken , [ isOptionEnabled ( "insertSpaceAfterSemicolonInForStatements" ) , isNonJsxSameLineTokenContext , isForContext ] , RuleAction . InsertSpace ) , rule ( "NoSpaceAfterSemicolonInFor" , SyntaxKind . SemicolonToken , anyToken , [ isOptionDisabledOrUndefined ( "insertSpaceAfterSemicolonInForStatements" ) , isNonJsxSameLineTokenContext , isForContext ] , RuleAction . DeleteSpace ) , rule ( "SpaceBeforeBinaryOperator" , anyToken , binaryOperators , [ isOptionEnabled ( "insertSpaceBeforeAndAfterBinaryOperators" ) , isNonJsxSameLineTokenContext , isBinaryOpContext ] , RuleAction . InsertSpace ) , rule ( "SpaceAfterBinaryOperator" , binaryOperators , anyToken , [ isOptionEnabled ( "insertSpaceBeforeAndAfterBinaryOperators" ) , isNonJsxSameLineTokenContext , isBinaryOpContext ] , RuleAction . InsertSpace ) , rule ( "NoSpaceBeforeBinaryOperator" , anyToken , binaryOperators , [ isOptionDisabledOrUndefined ( "insertSpaceBeforeAndAfterBinaryOperators" ) , isNonJsxSameLineTokenContext , isBinaryOpContext ] , RuleAction . DeleteSpace ) , rule ( "NoSpaceAfterBinaryOperator" , binaryOperators , anyToken , [ isOptionDisabledOrUndefined ( "insertSpaceBeforeAndAfterBinaryOperators" ) , isNonJsxSameLineTokenContext , isBinaryOpContext ] , RuleAction . DeleteSpace ) , rule ( "SpaceBeforeOpenParenInFuncDecl" , anyToken , SyntaxKind . OpenParenToken , [ isOptionEnabled ( "insertSpaceBeforeFunctionParenthesis" ) , isNonJsxSameLineTokenContext , isFunctionDeclContext ] , RuleAction . InsertSpace ) , rule ( "NoSpaceBeforeOpenParenInFuncDecl" , anyToken , SyntaxKind . OpenParenToken , [ isOptionDisabledOrUndefined ( "insertSpaceBeforeFunctionParenthesis" ) , isNonJsxSameLineTokenContext , isFunctionDeclContext ] , RuleAction . DeleteSpace ) , rule ( "NewLineBeforeOpenBraceInControl" , controlOpenBraceLeftTokenRange , SyntaxKind . OpenBraceToken , [ isOptionEnabled ( "placeOpenBraceOnNewLineForControlBlocks" ) , isControlDeclContext , isBeforeMultilineBlockContext ] , RuleAction . InsertNewLine , RuleFlags . CanDeleteNewLines ) , rule ( "NewLineBeforeOpenBraceInFunction" , functionOpenBraceLeftTokenRange , SyntaxKind . OpenBraceToken , [ isOptionEnabled ( "placeOpenBraceOnNewLineForFunctions" ) , isFunctionDeclContext , isBeforeMultilineBlockContext ] , RuleAction . InsertNewLine , RuleFlags . CanDeleteNewLines ) , rule ( "NewLineBeforeOpenBraceInTypeScriptDeclWithBlock" , typeScriptOpenBraceLeftTokenRange , SyntaxKind . OpenBraceToken , [ isOptionEnabled ( "placeOpenBraceOnNewLineForFunctions" ) , isTypeScriptDeclWithBlockContext , isBeforeMultilineBlockContext ] , RuleAction . InsertNewLine , RuleFlags . CanDeleteNewLines ) , rule ( "SpaceAfterTypeAssertion" , SyntaxKind . GreaterThanToken , anyToken , [ isOptionEnabled ( "insertSpaceAfterTypeAssertion" ) , isNonJsxSameLineTokenContext , isTypeAssertionContext ] , RuleAction . InsertSpace ) , rule ( "NoSpaceAfterTypeAssertion" , SyntaxKind . GreaterThanToken , anyToken , [ isOptionDisabledOrUndefined ( "insertSpaceAfterTypeAssertion" ) , isNonJsxSameLineTokenContext , isTypeAssertionContext ] , RuleAction . DeleteSpace ) , rule ( "SpaceBeforeTypeAnnotation" , anyToken , [ SyntaxKind . QuestionToken , SyntaxKind . ColonToken ] , [ isOptionEnabled ( "insertSpaceBeforeTypeAnnotation" ) , isNonJsxSameLineTokenContext , isTypeAnnotationContext ] , RuleAction . InsertSpace ) , rule ( "NoSpaceBeforeTypeAnnotation" , anyToken , [ SyntaxKind . QuestionToken , SyntaxKind . ColonToken ] , [ isOptionDisabledOrUndefined ( "insertSpaceBeforeTypeAnnotation" ) , isNonJsxSameLineTokenContext , isTypeAnnotationContext ] , RuleAction . DeleteSpace ) , rule ( "NoOptionalSemicolon" , SyntaxKind . SemicolonToken , anyTokenIncludingEOF , [ optionEquals ( "semicolons" , SemicolonPreference . Remove ) , isSemicolonDeletionContext ] , RuleAction . DeleteToken ) , rule ( "OptionalSemicolon" , anyToken , anyTokenIncludingEOF , [ optionEquals ( "semicolons" , SemicolonPreference . Insert ) , isSemicolonInsertionContext ] , RuleAction . InsertTrailingSemicolon ) , ] ; const lowPriorityCommonRules = [ rule ( "NoSpaceBeforeSemicolon" , anyToken , SyntaxKind . SemicolonToken , [ isNonJsxSameLineTokenContext ] , RuleAction . DeleteSpace ) , rule ( "SpaceBeforeOpenBraceInControl" , controlOpenBraceLeftTokenRange , SyntaxKind . OpenBraceToken , [ isOptionDisabledOrUndefinedOrTokensOnSameLine ( "placeOpenBraceOnNewLineForControlBlocks" ) , isControlDeclContext , isNotFormatOnEnter , isSameLineTokenOrBeforeBlockContext ] , RuleAction . InsertSpace , RuleFlags . CanDeleteNewLines ) , rule ( "SpaceBeforeOpenBraceInFunction" , functionOpenBraceLeftTokenRange , SyntaxKind . OpenBraceToken , [ isOptionDisabledOrUndefinedOrTokensOnSameLine ( "placeOpenBraceOnNewLineForFunctions" ) , isFunctionDeclContext , isBeforeBlockContext , isNotFormatOnEnter , isSameLineTokenOrBeforeBlockContext ] , RuleAction . InsertSpace , RuleFlags . CanDeleteNewLines ) , rule ( "SpaceBeforeOpenBraceInTypeScriptDeclWithBlock" , typeScriptOpenBraceLeftTokenRange , SyntaxKind . OpenBraceToken , [ isOptionDisabledOrUndefinedOrTokensOnSameLine ( "placeOpenBraceOnNewLineForFunctions" ) , isTypeScriptDeclWithBlockContext , isNotFormatOnEnter , isSameLineTokenOrBeforeBlockContext ] , RuleAction . InsertSpace , RuleFlags . CanDeleteNewLines ) , rule ( "NoSpaceBeforeComma" , anyToken , SyntaxKind . CommaToken , [ isNonJsxSameLineTokenContext ] , RuleAction . DeleteSpace ) , rule ( "NoSpaceBeforeOpenBracket" , anyTokenExcept ( SyntaxKind . AsyncKeyword , SyntaxKind . CaseKeyword ) , SyntaxKind . OpenBracketToken , [ isNonJsxSameLineTokenContext ] , RuleAction . DeleteSpace ) , rule ( "NoSpaceAfterCloseBracket" , SyntaxKind . CloseBracketToken , anyToken , [ isNonJsxSameLineTokenContext , isNotBeforeBlockInFunctionDeclarationContext ] , RuleAction . DeleteSpace ) , rule ( "SpaceAfterSemicolon" , SyntaxKind . SemicolonToken , anyToken , [ isNonJsxSameLineTokenContext ] , RuleAction . InsertSpace ) , rule ( "SpaceBetweenForAndAwaitKeyword" , SyntaxKind . ForKeyword , SyntaxKind . AwaitKeyword , [ isNonJsxSameLineTokenContext ] , RuleAction . InsertSpace ) , rule ( "SpaceBetweenStatements" , [ SyntaxKind . CloseParenToken , SyntaxKind . DoKeyword , SyntaxKind . ElseKeyword , SyntaxKind . CaseKeyword ] , anyToken , [ isNonJsxSameLineTokenContext , isNonJsxElementOrFragmentContext , isNotForContext ] , RuleAction . InsertSpace ) , rule ( "SpaceAfterTryCatchFinally" , [ SyntaxKind . TryKeyword , SyntaxKind . CatchKeyword , SyntaxKind . FinallyKeyword ] , SyntaxKind . OpenBraceToken , [ isNonJsxSameLineTokenContext ] , RuleAction . InsertSpace ) , ] ; return [ ... highPriorityCommonRules , ... userConfigurableRules , ... lowPriorityCommonRules , ] ; } param debugName param left param right param context param action param flags function rule ( debugName , left , right , context , action , flags = RuleFlags . None ) { return { leftTokenRange : toTokenRange ( left ) , rightTokenRange : toTokenRange ( right ) , rule : { debugName , context , action , flags } } ; } function tokenRangeFrom ( tokens ) { return { tokens , isSpecific : true } ; } function toTokenRange ( arg ) { return typeof arg === "number" ? tokenRangeFrom ( [ arg ] ) : isArray ( arg ) ? tokenRangeFrom ( arg ) : arg ; } function tokenRangeFromRange ( from , to , except = [ ] ) { const tokens = [ ] ; for ( let token = from ; token <= to ; token ++ ) { if ( ! contains ( except , token ) ) { tokens . push ( token ) ; } } return tokenRangeFrom ( tokens ) ; } function optionEquals < K extends keyof FormatCodeSettings > ( optionName , optionValue ) { return ( context ) => context . options && context . options [ optionName ] === optionValue ; } function isOptionEnabled ( optionName ) { return ( context ) => context . options && context . options . hasOwnProperty ( optionName ) && ! ! context . options [ optionName ] ; } function isOptionDisabled ( optionName ) { return ( context ) => context . options && context . options . hasOwnProperty ( optionName ) && ! context . options [ optionName ] ; } function isOptionDisabledOrUndefined ( optionName ) { return ( context ) => ! context . options || ! context . options . hasOwnProperty ( optionName ) || ! context . options [ optionName ] ; } function isOptionDisabledOrUndefinedOrTokensOnSameLine ( optionName ) { return ( context ) => ! context . options || ! context . options . hasOwnProperty ( optionName ) || ! context . options [ optionName ] || context . TokensAreOnSameLine ( ) ; } function isOptionEnabledOrUndefined ( optionName ) { return ( context ) => ! context . options || ! context . options . hasOwnProperty ( optionName ) || ! ! context . options [ optionName ] ; } function isForContext ( context ) { return context . contextNode . kind === SyntaxKind . ForStatement ; } function isNotForContext ( context ) { return ! isForContext ( context ) ; } function isBinaryOpContext ( context ) { switch ( context . contextNode . kind ) { case SyntaxKind . BinaryExpression : return ( context . contextNode as BinaryExpression ) . operatorToken . kind !== SyntaxKind . CommaToken ; case SyntaxKind . ConditionalExpression : case SyntaxKind . ConditionalType : case SyntaxKind . AsExpression : case SyntaxKind . ExportSpecifier : case SyntaxKind . ImportSpecifier : case SyntaxKind . TypePredicate : case SyntaxKind . UnionType : case SyntaxKind . IntersectionType : return true ; case SyntaxKind . BindingElement : case SyntaxKind . TypeAliasDeclaration : case SyntaxKind . ImportEqualsDeclaration : case SyntaxKind . ExportAssignment : case SyntaxKind . VariableDeclaration : case SyntaxKind . Parameter : case SyntaxKind . EnumMember : case SyntaxKind . PropertyDeclaration : case SyntaxKind . PropertySignature : return context . currentTokenSpan . kind === SyntaxKind . EqualsToken || context . nextTokenSpan . kind === SyntaxKind . EqualsToken ; case SyntaxKind . ForInStatement : case SyntaxKind . TypeParameter : return context . currentTokenSpan . kind === SyntaxKind . InKeyword || context . nextTokenSpan . kind === SyntaxKind . InKeyword || context . currentTokenSpan . kind === SyntaxKind . EqualsToken || context . nextTokenSpan . kind === SyntaxKind . EqualsToken ; case SyntaxKind . ForOfStatement : return context . currentTokenSpan . kind === SyntaxKind . OfKeyword || context . nextTokenSpan . kind === SyntaxKind . OfKeyword ; } return false ; } function isNotBinaryOpContext ( context ) { return ! isBinaryOpContext ( context ) ; } function isNotTypeAnnotationContext ( context ) { return ! isTypeAnnotationContext ( context ) ; } function isTypeAnnotationContext ( context ) { const contextKind = context . contextNode . kind ; return contextKind === SyntaxKind . PropertyDeclaration || contextKind === SyntaxKind . PropertySignature || contextKind === SyntaxKind . Parameter || contextKind === SyntaxKind . VariableDeclaration || isFunctionLikeKind ( contextKind ) ; } function isConditionalOperatorContext ( context ) { return context . contextNode . kind === SyntaxKind . ConditionalExpression || context . contextNode . kind === SyntaxKind . ConditionalType ; } function isSameLineTokenOrBeforeBlockContext ( context ) { return context . TokensAreOnSameLine ( ) || isBeforeBlockContext ( context ) ; } function isBraceWrappedContext ( context ) { return context . contextNode . kind === SyntaxKind . ObjectBindingPattern || context . contextNode . kind === SyntaxKind . MappedType || isSingleLineBlockContext ( context ) ; } function isBeforeMultilineBlockContext ( context ) { return isBeforeBlockContext ( context ) && ! ( context . NextNodeAllOnSameLine ( ) || context . NextNodeBlockIsOnOneLine ( ) ) ; } function isMultilineBlockContext ( context ) { return isBlockContext ( context ) && ! ( context . ContextNodeAllOnSameLine ( ) || context . ContextNodeBlockIsOnOneLine ( ) ) ; } function isSingleLineBlockContext ( context ) { return isBlockContext ( context ) && ( context . ContextNodeAllOnSameLine ( ) || context . ContextNodeBlockIsOnOneLine ( ) ) ; } function isBlockContext ( context ) { return nodeIsBlockContext ( context . contextNode ) ; } function isBeforeBlockContext ( context ) { return nodeIsBlockContext ( context . nextTokenParent ) ; } function nodeIsBlockContext ( node ) { if ( nodeIsTypeScriptDeclWithBlockContext ( node ) ) { return true ; } switch ( node . kind ) { case SyntaxKind . Block : case SyntaxKind . CaseBlock : case SyntaxKind . ObjectLiteralExpression : case SyntaxKind . ModuleBlock : return true ; } return false ; } function isFunctionDeclContext ( context ) { switch ( context . contextNode . kind ) { case SyntaxKind . FunctionDeclaration : case SyntaxKind . MethodDeclaration : case SyntaxKind . MethodSignature : case SyntaxKind . GetAccessor : case SyntaxKind . SetAccessor : case SyntaxKind . CallSignature : case SyntaxKind . FunctionExpression : case SyntaxKind . Constructor : case SyntaxKind . ArrowFunction : case SyntaxKind . InterfaceDeclaration : return true ; } return false ; } function isNotFunctionDeclContext ( context ) { return ! isFunctionDeclContext ( context ) ; } function isFunctionDeclarationOrFunctionExpressionContext ( context ) { return context . contextNode . kind === SyntaxKind . FunctionDeclaration || context . contextNode . kind === SyntaxKind . FunctionExpression ; } function isTypeScriptDeclWithBlockContext ( context ) { return nodeIsTypeScriptDeclWithBlockContext ( context . contextNode ) ; } function nodeIsTypeScriptDeclWithBlockContext ( node ) { switch ( node . kind ) { case SyntaxKind . ClassDeclaration : case SyntaxKind . ClassExpression : case SyntaxKind . InterfaceDeclaration : case SyntaxKind . EnumDeclaration : case SyntaxKind . TypeLiteral : case SyntaxKind . ModuleDeclaration : case SyntaxKind . ExportDeclaration : case SyntaxKind . NamedExports : case SyntaxKind . ImportDeclaration : case SyntaxKind . NamedImports : return true ; } return false ; } function isAfterCodeBlockContext ( context ) { switch ( context . currentTokenParent . kind ) { case SyntaxKind . ClassDeclaration : case SyntaxKind . ModuleDeclaration : case SyntaxKind . EnumDeclaration : case SyntaxKind . CatchClause : case SyntaxKind . ModuleBlock : case SyntaxKind . SwitchStatement : return true ; case SyntaxKind . Block : { const blockParent = context . currentTokenParent . parent ; if ( ! blockParent || blockParent . kind !== SyntaxKind . ArrowFunction && blockParent . kind !== SyntaxKind . FunctionExpression ) { return true ; } } } return false ; } function isControlDeclContext ( context ) { switch ( context . contextNode . kind ) { case SyntaxKind . IfStatement : case SyntaxKind . SwitchStatement : case SyntaxKind . ForStatement : case SyntaxKind . ForInStatement : case SyntaxKind . ForOfStatement : case SyntaxKind . WhileStatement : case SyntaxKind . TryStatement : case SyntaxKind . DoStatement : case SyntaxKind . WithStatement : case SyntaxKind . CatchClause : return true ; default : return false ; } } function isObjectContext ( context ) { return context . contextNode . kind === SyntaxKind . ObjectLiteralExpression ; } function isFunctionCallContext ( context ) { return context . contextNode . kind === SyntaxKind . CallExpression ; } function isNewContext ( context ) { return context . contextNode . kind === SyntaxKind . NewExpression ; } function isFunctionCallOrNewContext ( context ) { return isFunctionCallContext ( context ) || isNewContext ( context ) ; } function isPreviousTokenNotComma ( context ) { return context . currentTokenSpan . kind !== SyntaxKind . CommaToken ; } function isNextTokenNotCloseBracket ( context ) { return context . nextTokenSpan . kind !== SyntaxKind . CloseBracketToken ; } function isNextTokenNotCloseParen ( context ) { return context . nextTokenSpan . kind !== SyntaxKind . CloseParenToken ; } function isArrowFunctionContext ( context ) { return context . contextNode . kind === SyntaxKind . ArrowFunction ; } function isImportTypeContext ( context ) { return context . contextNode . kind === SyntaxKind . ImportType ; } function isNonJsxSameLineTokenContext ( context ) { return context . TokensAreOnSameLine ( ) && context . contextNode . kind !== SyntaxKind . JsxText ; } function isNonJsxTextContext ( context ) { return context . contextNode . kind !== SyntaxKind . JsxText ; } function isNonJsxElementOrFragmentContext ( context ) { return context . contextNode . kind !== SyntaxKind . JsxElement && context . contextNode . kind !== SyntaxKind . JsxFragment ; } function isJsxExpressionContext ( context ) { return context . contextNode . kind === SyntaxKind . JsxExpression || context . contextNode . kind === SyntaxKind . JsxSpreadAttribute ; } function isNextTokenParentJsxAttribute ( context ) { return context . nextTokenParent . kind === SyntaxKind . JsxAttribute ; } function isJsxAttributeContext ( context ) { return context . contextNode . kind === SyntaxKind . JsxAttribute ; } function isJsxSelfClosingElementContext ( context ) { return context . contextNode . kind === SyntaxKind . JsxSelfClosingElement ; } function isNotBeforeBlockInFunctionDeclarationContext ( context ) { return ! isFunctionDeclContext ( context ) && ! isBeforeBlockContext ( context ) ; } function isEndOfDecoratorContextOnSameLine ( context ) { return context . TokensAreOnSameLine ( ) && ! ! context . contextNode . decorators && nodeIsInDecoratorContext ( context . currentTokenParent ) && ! nodeIsInDecoratorContext ( context . nextTokenParent ) ; } function nodeIsInDecoratorContext ( node ) { while ( isExpressionNode ( node ) ) { node = node . parent ; } return node . kind === SyntaxKind . Decorator ; } function isStartOfVariableDeclarationList ( context ) { return context . currentTokenParent . kind === SyntaxKind . VariableDeclarationList && context . currentTokenParent . getStart ( context . sourceFile ) === context . currentTokenSpan . pos ; } function isNotFormatOnEnter ( context ) { return context . formattingRequestKind !== FormattingRequestKind . FormatOnEnter ; } function isModuleDeclContext ( context ) { return context . contextNode . kind === SyntaxKind . ModuleDeclaration ; } function isObjectTypeContext ( context ) { return context . contextNode . kind === SyntaxKind . TypeLiteral ; } function isConstructorSignatureContext ( context ) { return context . contextNode . kind === SyntaxKind . ConstructSignature ; } function isTypeArgumentOrParameterOrAssertion ( token , parent ) { if ( token . kind !== SyntaxKind . LessThanToken && token . kind !== SyntaxKind . GreaterThanToken ) { return false ; } switch ( parent . kind ) { case SyntaxKind . TypeReference : case SyntaxKind . TypeAssertionExpression : case SyntaxKind . TypeAliasDeclaration : case SyntaxKind . ClassDeclaration : case SyntaxKind . ClassExpression : case SyntaxKind . InterfaceDeclaration : case SyntaxKind . FunctionDeclaration : case SyntaxKind . FunctionExpression : case SyntaxKind . ArrowFunction : case SyntaxKind . MethodDeclaration : case SyntaxKind . MethodSignature : case SyntaxKind . CallSignature : case SyntaxKind . ConstructSignature : case SyntaxKind . CallExpression : case SyntaxKind . NewExpression : case SyntaxKind . ExpressionWithTypeArguments : return true ; default : return false ; } } function isTypeArgumentOrParameterOrAssertionContext ( context ) { return isTypeArgumentOrParameterOrAssertion ( context . currentTokenSpan , context . currentTokenParent ) || isTypeArgumentOrParameterOrAssertion ( context . nextTokenSpan , context . nextTokenParent ) ; } function isTypeAssertionContext ( context ) { return context . contextNode . kind === SyntaxKind . TypeAssertionExpression ; } function isVoidOpContext ( context ) { return context . currentTokenSpan . kind === SyntaxKind . VoidKeyword && context . currentTokenParent . kind === SyntaxKind . VoidExpression ; } function isYieldOrYieldStarWithOperand ( context ) { return context . contextNode . kind === SyntaxKind . YieldExpression && ( context . contextNode as YieldExpression ) . expression !== undefined ; } function isNonNullAssertionContext ( context ) { return context . contextNode . kind === SyntaxKind . NonNullExpression ; } function isNotStatementConditionContext ( context ) { return ! isStatementConditionContext ( context ) ; } function isStatementConditionContext ( context ) { switch ( context . contextNode . kind ) { case SyntaxKind . IfStatement : case SyntaxKind . ForStatement : case SyntaxKind . ForInStatement : case SyntaxKind . ForOfStatement : case SyntaxKind . DoStatement : case SyntaxKind . WhileStatement : return true ; default : return false ; } } function isSemicolonDeletionContext ( context ) { let nextTokenKind = context . nextTokenSpan . kind ; let nextTokenStart = context . nextTokenSpan . pos ; if ( isTrivia ( nextTokenKind ) ) { const nextRealToken = context . nextTokenParent === context . currentTokenParent ? findNextToken ( context . currentTokenParent , findAncestor ( context . currentTokenParent , a => ! a . parent ) ! , context . sourceFile ) : context . nextTokenParent . getFirstToken ( context . sourceFile ) ; if ( ! nextRealToken ) { return true ; } nextTokenKind = nextRealToken . kind ; nextTokenStart = nextRealToken . getStart ( context . sourceFile ) ; } const startLine = context . sourceFile . getLineAndCharacterOfPosition ( context . currentTokenSpan . pos ) . line ; const endLine = context . sourceFile . getLineAndCharacterOfPosition ( nextTokenStart ) . line ; if ( startLine === endLine ) { return nextTokenKind === SyntaxKind . CloseBraceToken || nextTokenKind === SyntaxKind . EndOfFileToken ; } if ( nextTokenKind === SyntaxKind . SemicolonClassElement || nextTokenKind === SyntaxKind . SemicolonToken ) { return false ; } if ( context . contextNode . kind === SyntaxKind . InterfaceDeclaration || context . contextNode . kind === SyntaxKind . TypeAliasDeclaration ) { return ! isPropertySignature ( context . currentTokenParent ) || ! ! context . currentTokenParent . type || nextTokenKind !== SyntaxKind . OpenParenToken ; } if ( isPropertyDeclaration ( context . currentTokenParent ) ) { return ! context . currentTokenParent . initializer ; } return context . currentTokenParent . kind !== SyntaxKind . ForStatement && context . currentTokenParent . kind !== SyntaxKind . EmptyStatement && context . currentTokenParent . kind !== SyntaxKind . SemicolonClassElement && nextTokenKind !== SyntaxKind . OpenBracketToken && nextTokenKind !== SyntaxKind . OpenParenToken && nextTokenKind !== SyntaxKind . PlusToken && nextTokenKind !== SyntaxKind . MinusToken && nextTokenKind !== SyntaxKind . SlashToken && nextTokenKind !== SyntaxKind . RegularExpressionLiteral && nextTokenKind !== SyntaxKind . CommaToken && nextTokenKind !== SyntaxKind . TemplateExpression && nextTokenKind !== SyntaxKind . TemplateHead && nextTokenKind !== SyntaxKind . NoSubstitutionTemplateLiteral && nextTokenKind !== SyntaxKind . DotToken ; } function isSemicolonInsertionContext ( context ) { return positionIsASICandidate ( context . currentTokenSpan . end , context . currentTokenParent , context . sourceFile ) ; } }