@@ -44,42 +44,47 @@ namespace ts {
44
44
string ( ) : string ;
45
45
}
46
46
47
- // Pool writers to avoid needing to allocate them for every symbol we write.
48
- const stringWriters : StringSymbolWriter [ ] = [ ] ;
49
- export function getSingleLineStringWriter ( ) : StringSymbolWriter {
50
- if ( stringWriters . length === 0 ) {
51
- let str = "" ;
52
-
53
- const writeText : ( text : string ) => void = text => str += text ;
54
- return {
55
- string : ( ) => str ,
56
- writeKeyword : writeText ,
57
- writeOperator : writeText ,
58
- writePunctuation : writeText ,
59
- writeSpace : writeText ,
60
- writeStringLiteral : writeText ,
61
- writeParameter : writeText ,
62
- writeProperty : writeText ,
63
- writeSymbol : writeText ,
64
-
65
- // Completely ignore indentation for string writers. And map newlines to
66
- // a single space.
67
- writeLine : ( ) => str += " " ,
68
- increaseIndent : noop ,
69
- decreaseIndent : noop ,
70
- clear : ( ) => str = "" ,
71
- trackSymbol : noop ,
72
- reportInaccessibleThisError : noop ,
73
- reportPrivateInBaseOfClassExpression : noop ,
74
- } ;
75
- }
76
-
77
- return stringWriters . pop ( ) ;
78
- }
79
-
80
- export function releaseStringWriter ( writer : StringSymbolWriter ) {
81
- writer . clear ( ) ;
82
- stringWriters . push ( writer ) ;
47
+ const stringWriter = createSingleLineStringWriter ( ) ;
48
+ let stringWriterAcquired = false ;
49
+
50
+ function createSingleLineStringWriter ( ) : StringSymbolWriter {
51
+ let str = "" ;
52
+
53
+ const writeText : ( text : string ) => void = text => str += text ;
54
+ return {
55
+ string : ( ) => str ,
56
+ writeKeyword : writeText ,
57
+ writeOperator : writeText ,
58
+ writePunctuation : writeText ,
59
+ writeSpace : writeText ,
60
+ writeStringLiteral : writeText ,
61
+ writeParameter : writeText ,
62
+ writeProperty : writeText ,
63
+ writeSymbol : writeText ,
64
+
65
+ // Completely ignore indentation for string writers. And map newlines to
66
+ // a single space.
67
+ writeLine : ( ) => str += " " ,
68
+ increaseIndent : noop ,
69
+ decreaseIndent : noop ,
70
+ clear : ( ) => str = "" ,
71
+ trackSymbol : noop ,
72
+ reportInaccessibleThisError : noop ,
73
+ reportPrivateInBaseOfClassExpression : noop ,
74
+ } ;
75
+ }
76
+
77
+ export function usingSingleLineStringWriter ( action : ( writer : StringSymbolWriter ) => void ) : string {
78
+ try {
79
+ Debug . assert ( ! stringWriterAcquired ) ;
80
+ stringWriterAcquired = true ;
81
+ action ( stringWriter ) ;
82
+ return stringWriter . string ( ) ;
83
+ }
84
+ finally {
85
+ stringWriter . clear ( ) ;
86
+ stringWriterAcquired = false ;
87
+ }
83
88
}
84
89
85
90
export function getFullWidth ( node : Node ) {
0 commit comments