1
- ///<reference path='..\..\..\..\src\compiler\typescript.ts' />
2
1
///<reference path='..\..\..\..\src\harness\harness.ts' />
3
- ///<reference path='..\..\..\..\src\services\typescriptServices.ts' />
4
-
5
- class TestSourceFile {
6
- constructor (
7
- public fileName : string ,
8
- public version : number ,
9
- public scriptSnapshot : TypeScript . IScriptSnapshot ,
10
- public isOpen : boolean ,
11
- public byteOrderMark : TypeScript . ByteOrderMark = TypeScript . ByteOrderMark . Utf8 ) {
12
- }
13
- }
14
-
15
- class TestHostSettings {
16
- constructor (
17
- public files : TypeScript . StringHashTable < TestSourceFile > ,
18
- public compilationSettings : TypeScript . CompilationSettings = TypeScript . ImmutableCompilationSettings . defaultSettings ( ) . toCompilationSettings ( ) ) {
19
- }
20
- }
21
-
22
- describe ( "testDocumentRetrievalAndUpdate" , ( ) => {
23
- function getHost ( settings : TestHostSettings ) : TypeScript . Services . ILanguageServiceHost {
24
- return {
25
- getCompilationSettings ( ) : TypeScript . CompilationSettings {
26
- return settings . compilationSettings ;
27
- } ,
28
-
29
- getScriptFileNames ( ) : string [ ] {
30
- return settings . files . getAllKeys ( ) ;
31
- } ,
32
-
33
- getScriptVersion ( fileName : string ) : number {
34
- return settings . files . lookup ( fileName ) . version ;
35
- } ,
36
-
37
- getScriptIsOpen ( fileName : string ) : boolean {
38
- return settings . files . lookup ( fileName ) . isOpen ;
39
- } ,
40
-
41
- getScriptByteOrderMark ( fileName : string ) : TypeScript . ByteOrderMark {
42
- return settings . files . lookup ( fileName ) . byteOrderMark ;
43
- } ,
44
-
45
- getScriptSnapshot ( fileName : string ) : TypeScript . IScriptSnapshot {
46
- return settings . files . lookup ( fileName ) . scriptSnapshot ;
47
- } ,
48
-
49
- getDiagnosticsObject ( ) : TypeScript . Services . ILanguageServicesDiagnostics {
50
- throw TypeScript . Errors . notYetImplemented ( ) ;
51
- } ,
52
-
53
- getLocalizedDiagnosticMessages ( ) : any {
54
- return null ;
55
- } ,
56
-
57
- information ( ) : boolean {
58
- return false ;
59
- } ,
60
-
61
- debug ( ) : boolean {
62
- return false ;
63
- } ,
64
-
65
- warning ( ) : boolean {
66
- return false ;
67
- } ,
68
-
69
- error ( ) : boolean {
70
- return false ;
71
- } ,
72
-
73
- fatal ( ) : boolean {
74
- return false ;
75
- } ,
76
-
77
- log ( s : string ) : void {
78
- } ,
79
-
80
- resolveRelativePath ( path : string , directory : string ) : string {
81
- throw TypeScript . Errors . notYetImplemented ( ) ;
82
- } ,
83
- fileExists ( path : string ) : boolean {
84
- throw TypeScript . Errors . notYetImplemented ( ) ;
85
- } ,
86
- directoryExists ( path : string ) : boolean {
87
- throw TypeScript . Errors . notYetImplemented ( ) ;
88
- } ,
89
- getParentDirectory ( path : string ) : string {
90
- throw TypeScript . Errors . notYetImplemented ( ) ;
91
- } ,
92
- getCancellationToken ( ) : TypeScript . ICancellationToken {
93
- return TypeScript . CancellationToken . None ;
94
- }
95
- }
96
- }
97
-
98
- function getLanguageServiceCompiler ( ls : TypeScript . Services . ILanguageService ) : TypeScript . Services . LanguageServiceCompiler {
99
- return < TypeScript . Services . LanguageServiceCompiler > ( < any > ls ) . compiler
100
- }
101
2
3
+ describe ( "DocumentRegistry" , ( ) => {
102
4
it ( "documents are shared between projects" , ( ) => {
103
- function ensureDocumentIsShared ( prefix : string , ls1 : TypeScript . Services . ILanguageService , ls2 : TypeScript . Services . ILanguageService , fileName : string ) : void {
104
- var c1 = getLanguageServiceCompiler ( ls1 ) ;
105
- var c2 = getLanguageServiceCompiler ( ls2 ) ;
106
- // getDocument synchronized its internal state with host
107
- var doc1 = c1 . getDocument ( fileName ) ;
108
- var doc2 = c2 . getDocument ( fileName ) ;
109
- if ( doc1 !== doc2 ) {
110
- throw new Error ( prefix + ":document should be shared between language services" ) ;
111
- }
112
- }
113
- var files = new TypeScript . StringHashTable < TestSourceFile > ( ) ;
114
- var f1 = new TestSourceFile ( "file1.ts" , 1 , TypeScript . ScriptSnapshot . fromString ( "var x = 1;" ) , false ) ;
115
- files . add ( f1 . fileName , f1 ) ;
116
- var factory = new TypeScript . Services . TypeScriptServicesFactory ( ) ;
5
+ var documentRegistry = ts . createDocumentRegistry ( ) ;
6
+ var defaultCompilerOptions = ts . getDefaultCompilerOptions ( ) ;
117
7
118
- var hostSettings = new TestHostSettings ( files ) ;
119
- var ls1 = factory . createPullLanguageService ( getHost ( hostSettings ) ) ;
120
- var ls2 = factory . createPullLanguageService ( getHost ( hostSettings ) ) ;
8
+ var f1 = documentRegistry . acquireDocument ( "file1.ts" , defaultCompilerOptions , TypeScript . ScriptSnapshot . fromString ( "var x = 1;" ) , 1 , false ) ;
9
+ var f2 = documentRegistry . acquireDocument ( "file1.ts" , defaultCompilerOptions , TypeScript . ScriptSnapshot . fromString ( "var x = 1;" ) , 1 , false ) ;
121
10
122
- ensureDocumentIsShared ( "==1==" , ls1 , ls2 , f1 . fileName ) ;
123
-
124
- f1 . version = 2 ;
125
- f1 . scriptSnapshot = TypeScript . ScriptSnapshot . fromString ( "var x = 2;" ) ;
126
-
127
- ensureDocumentIsShared ( "==2==" , ls1 , ls2 , f1 . fileName ) ;
11
+ assert ( f1 === f2 , "DocumentRegistry should return the same document for the same name" ) ;
128
12
} ) ;
129
13
130
14
it ( "documents are refreshed when settings in compilation settings affect syntax" , ( ) => {
131
- var files = new TypeScript . StringHashTable < TestSourceFile > ( ) ;
132
- var f1 = new TestSourceFile ( "file1.ts" , 1 , TypeScript . ScriptSnapshot . fromString ( "var x = 1;" ) , false ) ;
133
- files . add ( f1 . fileName , f1 ) ;
134
- var factory = new TypeScript . Services . TypeScriptServicesFactory ( ) ;
135
-
136
- var hostSettings = new TestHostSettings ( files ) ;
137
-
138
- var factory = new TypeScript . Services . TypeScriptServicesFactory ( ) ;
139
- var ls = factory . createPullLanguageService ( getHost ( hostSettings ) ) ;
140
- var compiler = getLanguageServiceCompiler ( ls ) ;
141
-
142
- var d1 = compiler . getDocument ( f1 . fileName ) ;
15
+ var documentRegistry = ts . createDocumentRegistry ( ) ;
16
+ var compilerOptions : ts . CompilerOptions = { target : ts . ScriptTarget . ES5 , module : ts . ModuleKind . AMD } ;
143
17
144
18
// change compilation setting that doesn't affect parsing - should have the same document
145
- hostSettings . compilationSettings . generateDeclarationFiles = ! hostSettings . compilationSettings . generateDeclarationFiles ;
146
- var d2 = compiler . getDocument ( f1 . fileName ) ;
19
+ compilerOptions . declaration = true ;
20
+ var f1 = documentRegistry . acquireDocument ( "file1.ts" , compilerOptions , TypeScript . ScriptSnapshot . fromString ( "var x = 1;" ) , 1 , false ) ;
21
+ compilerOptions . declaration = false ;
22
+ var f2 = documentRegistry . acquireDocument ( "file1.ts" , compilerOptions , TypeScript . ScriptSnapshot . fromString ( "var x = 1;" ) , 1 , false ) ;
23
+
24
+ assert ( f1 === f2 , "Expected to have the same document instance" ) ;
147
25
148
- if ( d1 !== d2 ) {
149
- throw new Error ( "Expected to have the same document instance" ) ;
150
- }
151
26
152
27
// change value of compilation setting that is used during production of AST - new document is required
153
- hostSettings . compilationSettings . codeGenTarget = TypeScript . LanguageVersion . EcmaScript5 ;
154
- var d3 = compiler . getDocument ( f1 . fileName ) ;
155
- if ( d2 === d3 ) {
156
- throw new Error ( "Changed codeGenTarget: Expected to have different instances of document" ) ;
157
- }
28
+ compilerOptions . target = ts . ScriptTarget . ES3 ;
29
+ var f3 = documentRegistry . acquireDocument ( "file1.ts" , compilerOptions , TypeScript . ScriptSnapshot . fromString ( "var x = 1;" ) , 1 , false ) ;
158
30
159
- hostSettings . compilationSettings . propagateEnumConstants = ! hostSettings . compilationSettings . propagateEnumConstants ;
160
- var d4 = compiler . getDocument ( f1 . fileName ) ;
161
- if ( d3 === d4 ) {
162
- throw new Error ( "Changed propagateEnumConstants: Expected to have different instances of document" ) ;
163
- }
31
+ assert ( f1 !== f3 , "Changed target: Expected to have different instances of document" ) ;
164
32
165
- hostSettings . compilationSettings . allowAutomaticSemicolonInsertion = ! hostSettings . compilationSettings . allowAutomaticSemicolonInsertion ;
166
- var d5 = compiler . getDocument ( f1 . fileName ) ;
167
- if ( d4 === d5 ) {
168
- throw new Error ( "Changed allowAutomaticSemicolonInsertion: Expected to have different instances of document" ) ;
169
- }
33
+ compilerOptions . module = ts . ModuleKind . CommonJS ;
34
+ var f4 = documentRegistry . acquireDocument ( "file1.ts" , compilerOptions , TypeScript . ScriptSnapshot . fromString ( "var x = 1;" ) , 1 , false ) ;
170
35
36
+ assert ( f1 !== f4 , "Changed module: Expected to have different instances of document" ) ;
171
37
} ) ;
172
38
} ) ;
0 commit comments