Skip to content

Commit 0caf18a

Browse files
Copilotjakebailey
andauthored
Fix Chinese character display by matching TypeScript's printer selection logic (#1559)
Co-authored-by: Copilot <[email protected]> Co-authored-by: jakebailey <[email protected]>
1 parent 75572ab commit 0caf18a

File tree

3 files changed

+61
-4
lines changed

3 files changed

+61
-4
lines changed

internal/checker/printer.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,10 @@ func createPrinterWithRemoveCommentsOmitTrailingSemicolon(emitContext *printer.E
2222
}
2323

2424
func createPrinterWithRemoveCommentsNeverAsciiEscape(emitContext *printer.EmitContext) *printer.Printer {
25-
// TODO: NeverAsciiEscape support
26-
return printer.NewPrinter(printer.PrinterOptions{RemoveComments: true}, printer.PrintHandlers{}, emitContext)
25+
return printer.NewPrinter(printer.PrinterOptions{
26+
RemoveComments: true,
27+
NeverAsciiEscape: true,
28+
}, printer.PrintHandlers{}, emitContext)
2729
}
2830

2931
type semicolonRemoverWriter struct {
@@ -256,6 +258,7 @@ func (c *Checker) symbolToStringEx(symbol *ast.Symbol, enclosingDeclaration *ast
256258
sourceFile = ast.GetSourceFileOfNode(enclosingDeclaration)
257259
}
258260
var printer_ *printer.Printer
261+
// add neverAsciiEscape for GH#39027
259262
if enclosingDeclaration != nil && enclosingDeclaration.Kind == ast.KindSourceFile {
260263
printer_ = createPrinterWithRemoveCommentsNeverAsciiEscape(nodeBuilder.EmitContext())
261264
} else {
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package fourslash_test
2+
3+
import (
4+
"testing"
5+
6+
"github.com/microsoft/typescript-go/internal/fourslash"
7+
"github.com/microsoft/typescript-go/internal/testutil"
8+
)
9+
10+
func TestChineseCharacterDisplayInHover(t *testing.T) {
11+
t.Parallel()
12+
defer testutil.RecoverAndFail(t, "Panic on fourslash test")
13+
const content = `
14+
interface 中文界面 {
15+
上居中: string;
16+
下居中: string;
17+
}
18+
19+
class 中文类 {
20+
获取中文属性(): 中文界面 {
21+
return {
22+
上居中: "上居中",
23+
下居中: "下居中"
24+
};
25+
}
26+
}
27+
28+
let /*instanceHover*/实例 = new 中文类();
29+
let 属性对象 = 实例./*methodHover*/获取中文属性();`
30+
f := fourslash.NewFourslash(t, nil /*capabilities*/, content)
31+
f.VerifyQuickInfoAt(t, "instanceHover", "let 实例: 中文类", "")
32+
f.VerifyQuickInfoAt(t, "methodHover", "(method) 中文类.获取中文属性(): 中文界面", "")
33+
}
34+
35+
func TestChineseCharacterDisplayInUnionTypes(t *testing.T) {
36+
t.Parallel()
37+
defer testutil.RecoverAndFail(t, "Panic on fourslash test")
38+
const content = `
39+
// Test the original issue: Chinese characters in method parameters should display correctly
40+
class TSLine {
41+
setLengthTextPositionPreset(/*methodParam*/preset: "上居中" | "下居中" | "右居中" | "左居中"): void {}
42+
}
43+
44+
let lines = new TSLine();
45+
lines./*method*/setLengthTextPositionPreset;`
46+
f := fourslash.NewFourslash(t, nil /*capabilities*/, content)
47+
48+
// Verify that the method displays Chinese characters correctly in hover (this was the original problem)
49+
f.VerifyQuickInfoAt(t, "method", `(method) TSLine.setLengthTextPositionPreset(preset: "上居中" | "下居中" | "右居中" | "左居中"): void`, "")
50+
}

internal/printer/printer.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ type PrinterOptions struct {
4545
OmitBraceSourceMapPositions bool
4646
// ExtendedDiagnostics bool
4747
OnlyPrintJSDocStyle bool
48-
// NeverAsciiEscape bool
48+
NeverAsciiEscape bool
4949
// StripInternal bool
5050
PreserveSourceNewlines bool
5151
// TerminateUnterminatedLiterals bool
@@ -974,7 +974,11 @@ func (p *Printer) emitTokenNodeEx(node *ast.TokenNode, flags tokenEmitFlags) {
974974
// SyntaxKindTemplateMiddle
975975
// SyntaxKindTemplateTail
976976
func (p *Printer) emitLiteral(node *ast.LiteralLikeNode, flags getLiteralTextFlags) {
977-
// !!! Printer option to control whether to escape non-ASCII characters
977+
// Add NeverAsciiEscape flag if the printer option is set
978+
if p.Options.NeverAsciiEscape {
979+
flags |= getLiteralTextFlagsNeverAsciiEscape
980+
}
981+
978982
text := p.getLiteralTextOfNode(node, nil /*sourceFile*/, flags)
979983

980984
// !!! Printer option to control source map emit, which causes us to use a different write method on the

0 commit comments

Comments
 (0)