The linker is implemented using a Babel transform such that Babel needs to parse and walk a source file to find the declarations that need to be compiled. If it can be determined that a source file is known not to contain any declarations the parsing and walking can be skipped as a performance improvement. This commit adds an exposed function for tools that integrate the linker to use to allow short-circuiting of the linker transform. PR Close #40137
		
			
				
	
	
		
			68 lines
		
	
	
		
			1.9 KiB
		
	
	
	
		
			TypeScript
		
	
	
	
	
	
			
		
		
	
	
			68 lines
		
	
	
		
			1.9 KiB
		
	
	
	
		
			TypeScript
		
	
	
	
	
	
| /**
 | |
|  * @license
 | |
|  * Copyright Google LLC All Rights Reserved.
 | |
|  *
 | |
|  * Use of this source code is governed by an MIT-style license that can be
 | |
|  * found in the LICENSE file at https://angular.io/license
 | |
|  */
 | |
| 
 | |
| import {needsLinking} from '../../src/file_linker/needs_linking';
 | |
| 
 | |
| describe('needsLinking', () => {
 | |
|   it('should return true for directive declarations', () => {
 | |
|     expect(needsLinking('file.js', `
 | |
|       export class Dir {
 | |
|         ɵdir = ɵɵngDeclareDirective({type: Dir});
 | |
|       }
 | |
|     `)).toBeTrue();
 | |
|   });
 | |
| 
 | |
|   it('should return true for namespaced directive declarations', () => {
 | |
|     expect(needsLinking('file.js', `
 | |
|       export class Dir {
 | |
|         ɵdir = ng.ɵɵngDeclareDirective({type: Dir});
 | |
|       }
 | |
|     `)).toBeTrue();
 | |
|   });
 | |
| 
 | |
|   it('should return true for unrelated usages of ɵɵngDeclareDirective', () => {
 | |
|     expect(needsLinking('file.js', `
 | |
|       const fnName = 'ɵɵngDeclareDirective';
 | |
|     `)).toBeTrue();
 | |
|   });
 | |
| 
 | |
|   it('should return false when the file does not contain ɵɵngDeclareDirective', () => {
 | |
|     expect(needsLinking('file.js', `
 | |
|       const foo = ngDeclareDirective;
 | |
|     `)).toBeFalse();
 | |
|   });
 | |
| 
 | |
|   it('should return true for component declarations', () => {
 | |
|     expect(needsLinking('file.js', `
 | |
|       export class Cmp {
 | |
|         ɵdir = ɵɵngDeclareComponent({type: Cmp});
 | |
|       }
 | |
|     `)).toBeTrue();
 | |
|   });
 | |
| 
 | |
|   it('should return true for namespaced component declarations', () => {
 | |
|     expect(needsLinking('file.js', `
 | |
|       export class Cmp {
 | |
|         ɵdir = ng.ɵɵngDeclareComponent({type: Cmp});
 | |
|       }
 | |
|     `)).toBeTrue();
 | |
|   });
 | |
| 
 | |
|   it('should return true for unrelated usages of ɵɵngDeclareComponent', () => {
 | |
|     expect(needsLinking('file.js', `
 | |
|       const fnName = 'ɵɵngDeclareComponent';
 | |
|     `)).toBeTrue();
 | |
|   });
 | |
| 
 | |
|   it('should return false when the file does not contain ɵɵngDeclareComponent', () => {
 | |
|     expect(needsLinking('file.js', `
 | |
|       const foo = ngDeclareComponent;
 | |
|     `)).toBeFalse();
 | |
|   });
 | |
| });
 |