fix(language-service): Increase project/script version in MockHost.reset() (#33200)
PR Close #33200
This commit is contained in:
		
							parent
							
								
									becd62d4a1
								
							
						
					
					
						commit
						43241a560a
					
				| @ -14,17 +14,22 @@ import {TypeScriptServiceHost} from '../src/typescript_host'; | |||||||
| 
 | 
 | ||||||
| import {MockTypescriptHost} from './test_utils'; | import {MockTypescriptHost} from './test_utils'; | ||||||
| 
 | 
 | ||||||
| describe('hover', () => { | fdescribe('hover', () => { | ||||||
|   let mockHost: MockTypescriptHost; |   // const mockHost: MockTypescriptHost;
 | ||||||
|   let tsLS: ts.LanguageService; |   // const tsLS: ts.LanguageService;
 | ||||||
|   let ngLSHost: TypeScriptServiceHost; |   // const ngLSHost: TypeScriptServiceHost;
 | ||||||
|   let ngLS: LanguageService; |   // const ngLS: LanguageService;
 | ||||||
|  |   const mockHost = new MockTypescriptHost(['/app/main.ts']); | ||||||
|  |   const tsLS = ts.createLanguageService(mockHost); | ||||||
|  |   const ngLSHost = new TypeScriptServiceHost(mockHost, tsLS); | ||||||
|  |   const ngLS = createLanguageService(ngLSHost); | ||||||
| 
 | 
 | ||||||
|   beforeEach(() => { |   beforeEach(() => { | ||||||
|     mockHost = new MockTypescriptHost(['/app/main.ts', '/app/parsing-cases.ts']); |     // mockHost = new MockTypescriptHost(['/app/main.ts', '/app/parsing-cases.ts']);
 | ||||||
|     tsLS = ts.createLanguageService(mockHost); |     // tsLS = ts.createLanguageService(mockHost);
 | ||||||
|     ngLSHost = new TypeScriptServiceHost(mockHost, tsLS); |     // ngLSHost = new TypeScriptServiceHost(mockHost, tsLS);
 | ||||||
|     ngLS = createLanguageService(ngLSHost); |     // ngLS = createLanguageService(ngLSHost);
 | ||||||
|  |     mockHost.reset(); | ||||||
|   }); |   }); | ||||||
| 
 | 
 | ||||||
|   it('should be able to find field in an interpolation', () => { |   it('should be able to find field in an interpolation', () => { | ||||||
| @ -183,20 +188,20 @@ describe('hover', () => { | |||||||
|   }); |   }); | ||||||
| 
 | 
 | ||||||
|   it('should be able to find the NgModule of a directive', () => { |   it('should be able to find the NgModule of a directive', () => { | ||||||
|     const fileName = '/app/parsing-cases.ts'; |     const fileName = '/app/app.component.ts'; | ||||||
|     mockHost.override(fileName, ` |     mockHost.override(fileName, ` | ||||||
|       import {Directive} from '@angular/core'; |       import {Directive} from '@angular/core'; | ||||||
| 
 | 
 | ||||||
|       @Directive({ |       @Directive({ | ||||||
|         selector: '[string-model]', |         selector: '[string-model]', | ||||||
|       }) |       }) | ||||||
|       export class «StringModel» {}`);
 |       export class «AppComponent» {}`);
 | ||||||
|     const marker = mockHost.getReferenceMarkerFor(fileName, 'StringModel'); |     const marker = mockHost.getReferenceMarkerFor(fileName, 'AppComponent'); | ||||||
|     const quickInfo = ngLS.getHoverAt(fileName, marker.start); |     const quickInfo = ngLS.getHoverAt(fileName, marker.start); | ||||||
|     expect(quickInfo).toBeTruthy(); |     expect(quickInfo).toBeTruthy(); | ||||||
|     const {textSpan, displayParts} = quickInfo !; |     const {textSpan, displayParts} = quickInfo !; | ||||||
|     expect(textSpan).toEqual(marker); |     expect(textSpan).toEqual(marker); | ||||||
|     expect(toText(displayParts)).toBe('(directive) AppModule.StringModel: class'); |     expect(toText(displayParts)).toBe('(directive) AppModule.AppComponent: class'); | ||||||
|   }); |   }); | ||||||
| }); | }); | ||||||
| 
 | 
 | ||||||
|  | |||||||
| @ -92,7 +92,7 @@ const COMPILER_OPTIONS: Readonly<ts.CompilerOptions> = { | |||||||
| }; | }; | ||||||
| 
 | 
 | ||||||
| export class MockTypescriptHost implements ts.LanguageServiceHost { | export class MockTypescriptHost implements ts.LanguageServiceHost { | ||||||
|   private angularPath?: string; |   private readonly angularPath: string; | ||||||
|   private readonly nodeModulesPath: string; |   private readonly nodeModulesPath: string; | ||||||
|   private readonly scriptVersion = new Map<string, number>(); |   private readonly scriptVersion = new Map<string, number>(); | ||||||
|   private readonly overrides = new Map<string, string>(); |   private readonly overrides = new Map<string, string>(); | ||||||
| @ -127,14 +127,16 @@ export class MockTypescriptHost implements ts.LanguageServiceHost { | |||||||
|   } |   } | ||||||
| 
 | 
 | ||||||
|   addScript(fileName: string, content: string) { |   addScript(fileName: string, content: string) { | ||||||
|  |     if (this.scriptVersion.has(fileName)) { | ||||||
|  |       throw new Error(`${fileName} is already in the root files.`); | ||||||
|  |     } | ||||||
|  |     this.scriptVersion.set(fileName, 0); | ||||||
|     this.projectVersion++; |     this.projectVersion++; | ||||||
|     this.overrides.set(fileName, content); |     this.overrides.set(fileName, content); | ||||||
|     this.overrideDirectory.add(path.dirname(fileName)); |     this.overrideDirectory.add(path.dirname(fileName)); | ||||||
|     this.scriptNames.push(fileName); |     this.scriptNames.push(fileName); | ||||||
|   } |   } | ||||||
| 
 | 
 | ||||||
|   forgetAngular() { this.angularPath = undefined; } |  | ||||||
| 
 |  | ||||||
|   overrideOptions(options: Partial<ts.CompilerOptions>) { |   overrideOptions(options: Partial<ts.CompilerOptions>) { | ||||||
|     this.options = {...this.options, ...options}; |     this.options = {...this.options, ...options}; | ||||||
|     this.projectVersion++; |     this.projectVersion++; | ||||||
| @ -185,6 +187,16 @@ export class MockTypescriptHost implements ts.LanguageServiceHost { | |||||||
|    * Reset the project to its original state, effectively removing all overrides. |    * Reset the project to its original state, effectively removing all overrides. | ||||||
|    */ |    */ | ||||||
|   reset() { |   reset() { | ||||||
|  |     // project version and script version must be monotonically increasing,
 | ||||||
|  |     // they must not be reset to zero.
 | ||||||
|  |     this.projectVersion++; | ||||||
|  |     for (const fileName of this.overrides.keys()) { | ||||||
|  |       const version = this.scriptVersion.get(fileName); | ||||||
|  |       if (version === undefined) { | ||||||
|  |         throw new Error(`No prior version found for ${fileName}`); | ||||||
|  |       } | ||||||
|  |       this.scriptVersion.set(fileName, version + 1); | ||||||
|  |     } | ||||||
|     // Remove overrides from scriptNames
 |     // Remove overrides from scriptNames
 | ||||||
|     let length = 0; |     let length = 0; | ||||||
|     for (let i = 0; i < this.scriptNames.length; ++i) { |     for (let i = 0; i < this.scriptNames.length; ++i) { | ||||||
| @ -251,7 +263,7 @@ export class MockTypescriptHost implements ts.LanguageServiceHost { | |||||||
|           return result; |           return result; | ||||||
|         } |         } | ||||||
|       } |       } | ||||||
|       if (this.angularPath && name.startsWith('/' + node_modules + at_angular)) { |       if (name.startsWith('/' + node_modules + at_angular)) { | ||||||
|         return this.myPath.posix.join( |         return this.myPath.posix.join( | ||||||
|             this.angularPath, name.substr(node_modules.length + at_angular.length + 1)); |             this.angularPath, name.substr(node_modules.length + at_angular.length + 1)); | ||||||
|       } |       } | ||||||
|  | |||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user