Skip to content

Commit b35fa04

Browse files
authored
Fix broken formatting rules around namespaced JSX attributes (#55294)
1 parent 28cd1fb commit b35fa04

File tree

2 files changed

+33
-2
lines changed

2 files changed

+33
-2
lines changed

src/services/formatting/rules.ts

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ export function getAllRules(): RuleSpec[] {
9090
rule("IgnoreAfterLineComment", SyntaxKind.SingleLineCommentTrivia, anyToken, anyContext, RuleAction.StopProcessingSpaceActions),
9191

9292
rule("NotSpaceBeforeColon", anyToken, SyntaxKind.ColonToken, [isNonJsxSameLineTokenContext, isNotBinaryOpContext, isNotTypeAnnotationContext], RuleAction.DeleteSpace),
93-
rule("SpaceAfterColon", SyntaxKind.ColonToken, anyToken, [isNonJsxSameLineTokenContext, isNotBinaryOpContext], RuleAction.InsertSpace),
93+
rule("SpaceAfterColon", SyntaxKind.ColonToken, anyToken, [isNonJsxSameLineTokenContext, isNotBinaryOpContext, isNextTokenParentNotJsxNamespacedName], RuleAction.InsertSpace),
9494
rule("NoSpaceBeforeQuestionMark", anyToken, SyntaxKind.QuestionToken, [isNonJsxSameLineTokenContext, isNotBinaryOpContext, isNotTypeAnnotationContext], RuleAction.DeleteSpace),
9595
// insert space after '?' only when it is used in conditional operator
9696
rule("SpaceAfterQuestionMarkInConditionalOperator", SyntaxKind.QuestionToken, anyToken, [isNonJsxSameLineTokenContext, isConditionalOperatorContext], RuleAction.InsertSpace),
@@ -179,6 +179,8 @@ export function getAllRules(): RuleSpec[] {
179179
rule("NoSpaceBeforeGreaterThanTokenInJsxOpeningElement", SyntaxKind.SlashToken, SyntaxKind.GreaterThanToken, [isJsxSelfClosingElementContext, isNonJsxSameLineTokenContext], RuleAction.DeleteSpace),
180180
rule("NoSpaceBeforeEqualInJsxAttribute", anyToken, SyntaxKind.EqualsToken, [isJsxAttributeContext, isNonJsxSameLineTokenContext], RuleAction.DeleteSpace),
181181
rule("NoSpaceAfterEqualInJsxAttribute", SyntaxKind.EqualsToken, anyToken, [isJsxAttributeContext, isNonJsxSameLineTokenContext], RuleAction.DeleteSpace),
182+
rule("NoSpaceBeforeJsxNamespaceColon", SyntaxKind.Identifier, SyntaxKind.ColonToken, [isNextTokenParentJsxNamespacedName], RuleAction.DeleteSpace),
183+
rule("NoSpaceAfterJsxNamespaceColon", SyntaxKind.ColonToken, SyntaxKind.Identifier, [isNextTokenParentJsxNamespacedName], RuleAction.DeleteSpace),
182184

183185
// TypeScript-specific rules
184186
// Use of module as a function call. e.g.: import m2 = module("m2");
@@ -749,13 +751,23 @@ function isJsxExpressionContext(context: FormattingContext): boolean {
749751
}
750752

751753
function isNextTokenParentJsxAttribute(context: FormattingContext): boolean {
752-
return context.nextTokenParent.kind === SyntaxKind.JsxAttribute;
754+
return context.nextTokenParent.kind === SyntaxKind.JsxAttribute || (
755+
context.nextTokenParent.kind === SyntaxKind.JsxNamespacedName && context.nextTokenParent.parent.kind === SyntaxKind.JsxAttribute
756+
);
753757
}
754758

755759
function isJsxAttributeContext(context: FormattingContext): boolean {
756760
return context.contextNode.kind === SyntaxKind.JsxAttribute;
757761
}
758762

763+
function isNextTokenParentNotJsxNamespacedName(context: FormattingContext): boolean {
764+
return context.nextTokenParent.kind !== SyntaxKind.JsxNamespacedName;
765+
}
766+
767+
function isNextTokenParentJsxNamespacedName(context: FormattingContext): boolean {
768+
return context.nextTokenParent.kind === SyntaxKind.JsxNamespacedName;
769+
}
770+
759771
function isJsxSelfClosingElementContext(context: FormattingContext): boolean {
760772
return context.contextNode.kind === SyntaxKind.JsxSelfClosingElement;
761773
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/// <reference path='fourslash.ts' />
2+
3+
// Github issue #55293
4+
5+
//@Filename: file.tsx
6+
//// function foo() {
7+
//// const a = <ns: foobar x : test1 x :test2="string" x:test3={true?1:0} />;
8+
////
9+
//// return a;
10+
//// }
11+
12+
format.document();
13+
14+
verify.currentFileContentIs(
15+
`function foo() {
16+
const a = <ns:foobar x:test1 x:test2="string" x:test3={true ? 1 : 0} />;
17+
18+
return a;
19+
}`);

0 commit comments

Comments
 (0)