|
8 | 8 |
|
9 | 9 | import ts from 'typescript'; |
10 | 10 |
|
| 11 | +import {absoluteFrom, getSourceFileOrError} from '../../file_system'; |
11 | 12 | import {initMockFileSystem} from '../../file_system/testing'; |
12 | 13 | import {Reference} from '../../imports'; |
13 | | -import {TypeCheckingConfig} from '../api'; |
14 | | -import {ALL_ENABLED_CONFIG, tcb, TestDeclaration, TestDirective} from '../testing'; |
| 14 | +import {OptimizeFor, TypeCheckingConfig} from '../api'; |
| 15 | +import {ALL_ENABLED_CONFIG, setup, tcb, TestDeclaration, TestDirective} from '../testing'; |
15 | 16 |
|
16 | 17 |
|
17 | 18 | describe('type check blocks', () => { |
@@ -1744,4 +1745,55 @@ describe('type check blocks', () => { |
1744 | 1745 | expect(result).toContain('(this).trackingFn(_t2, _t1, ((this).prop));'); |
1745 | 1746 | }); |
1746 | 1747 | }); |
| 1748 | + |
| 1749 | + describe('import generation', () => { |
| 1750 | + const TEMPLATE = `<div dir [test]="null"></div>`; |
| 1751 | + const DIRECTIVE: TestDeclaration = { |
| 1752 | + type: 'directive', |
| 1753 | + name: 'Dir', |
| 1754 | + selector: '[dir]', |
| 1755 | + inputs: { |
| 1756 | + test: { |
| 1757 | + isSignal: true, |
| 1758 | + bindingPropertyName: 'test', |
| 1759 | + classPropertyName: 'test', |
| 1760 | + required: true, |
| 1761 | + transform: null, |
| 1762 | + } |
| 1763 | + } |
| 1764 | + }; |
| 1765 | + |
| 1766 | + it('should prefer namespace imports in type check files for new imports', () => { |
| 1767 | + const result = tcb(TEMPLATE, [DIRECTIVE]); |
| 1768 | + |
| 1769 | + expect(result).toContain(`import * as i1 from '@angular/core';`); |
| 1770 | + expect(result).toContain(`[i1.ɵINPUT_SIGNAL_BRAND_WRITE_TYPE]`); |
| 1771 | + }); |
| 1772 | + |
| 1773 | + it('should re-use existing imports from original source files', () => { |
| 1774 | + // This is especially important for inline type check blocks. |
| 1775 | + // See: https://github.com/angular/angular/pull/53521#pullrequestreview-1778130879. |
| 1776 | + const {templateTypeChecker, program, programStrategy} = setup([{ |
| 1777 | + fileName: absoluteFrom('/test.ts'), |
| 1778 | + templates: {'AppComponent': TEMPLATE}, |
| 1779 | + declarations: [DIRECTIVE], |
| 1780 | + source: ` |
| 1781 | + import {Component} from '@angular/core'; // should be re-used |
| 1782 | +
|
| 1783 | + class AppComponent {} |
| 1784 | + export class Dir {} |
| 1785 | + `, |
| 1786 | + }]); |
| 1787 | + |
| 1788 | + // Trigger type check block generation. |
| 1789 | + templateTypeChecker.getDiagnosticsForFile( |
| 1790 | + getSourceFileOrError(program, absoluteFrom('/test.ts')), OptimizeFor.SingleFile); |
| 1791 | + |
| 1792 | + const testSf = getSourceFileOrError(programStrategy.getProgram(), absoluteFrom('/test.ts')); |
| 1793 | + expect(testSf.text) |
| 1794 | + .toContain( |
| 1795 | + `import { Component, ɵINPUT_SIGNAL_BRAND_WRITE_TYPE } from '@angular/core'; // should be re-used`); |
| 1796 | + expect(testSf.text).toContain(`[ɵINPUT_SIGNAL_BRAND_WRITE_TYPE]`); |
| 1797 | + }); |
| 1798 | + }); |
1747 | 1799 | }); |
0 commit comments