Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(compiler-cli): identify structural directives (#40032)
This commit introduces an `isStructural` flag on directive metadata, which is `true` if the directive injects `TemplateRef` (and thus is at least theoretically usable as a structural directive). The flag is not used for anything currently, but will be utilized by the Language Service to offer better autocompletion results for structural directives. PR Close #40032
- Loading branch information
Showing
18 changed files
with
227 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
load("//tools:defaults.bzl", "jasmine_node_test", "ts_library") | ||
|
||
package(default_visibility = ["//visibility:public"]) | ||
|
||
ts_library( | ||
name = "test_lib", | ||
testonly = True, | ||
srcs = glob([ | ||
"**/*.ts", | ||
]), | ||
deps = | ||
[ | ||
"//packages:types", | ||
"//packages/compiler", | ||
"//packages/compiler-cli/src/ngtsc/file_system", | ||
"//packages/compiler-cli/src/ngtsc/file_system/testing", | ||
"//packages/compiler-cli/src/ngtsc/imports", | ||
"//packages/compiler-cli/src/ngtsc/metadata", | ||
"//packages/compiler-cli/src/ngtsc/reflection", | ||
"//packages/compiler-cli/src/ngtsc/testing", | ||
"@npm//typescript", | ||
], | ||
) | ||
|
||
jasmine_node_test( | ||
name = "test", | ||
bootstrap = ["//tools/testing:node_no_angular_es5"], | ||
data = [ | ||
"//packages/compiler-cli/src/ngtsc/testing/fake_core:npm_package", | ||
], | ||
deps = | ||
[ | ||
":test_lib", | ||
], | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
/** | ||
* @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 {ExternalExpr} from '@angular/compiler'; | ||
import * as ts from 'typescript'; | ||
|
||
import {absoluteFrom, getFileSystem, getSourceFileOrError} from '../../file_system'; | ||
import {runInEachFileSystem} from '../../file_system/testing'; | ||
import {Reference} from '../../imports'; | ||
import {isNamedClassDeclaration, TypeScriptReflectionHost} from '../../reflection'; | ||
import {loadFakeCore, makeProgram} from '../../testing'; | ||
|
||
import {DtsMetadataReader} from '../src/dts'; | ||
|
||
runInEachFileSystem(() => { | ||
beforeEach(() => { | ||
loadFakeCore(getFileSystem()); | ||
}); | ||
|
||
describe('DtsMetadataReader', () => { | ||
it('should not assume directives are structural', () => { | ||
const mainPath = absoluteFrom('/main.d.ts'); | ||
const {program} = makeProgram( | ||
[{ | ||
name: mainPath, | ||
contents: ` | ||
import {ViewContainerRef} from '@angular/core'; | ||
import * as i0 from '@angular/core'; | ||
export declare class TestDir { | ||
constructor(p0: ViewContainerRef); | ||
static ɵdir: i0.ɵɵDirectiveDefWithMeta<TestDir, "[test]", never, {}, {}, never> | ||
} | ||
` | ||
}], | ||
{ | ||
skipLibCheck: true, | ||
lib: ['es6', 'dom'], | ||
}); | ||
|
||
const sf = getSourceFileOrError(program, mainPath); | ||
const clazz = sf.statements[2]; | ||
if (!isNamedClassDeclaration(clazz)) { | ||
return fail('Expected class declaration'); | ||
} | ||
|
||
const typeChecker = program.getTypeChecker(); | ||
const dtsReader = | ||
new DtsMetadataReader(typeChecker, new TypeScriptReflectionHost(typeChecker)); | ||
|
||
const meta = dtsReader.getDirectiveMetadata(new Reference(clazz))!; | ||
expect(meta.isStructural).toBeFalse(); | ||
}); | ||
|
||
it('should identify a structural directive by its constructor', () => { | ||
const mainPath = absoluteFrom('/main.d.ts'); | ||
const {program} = makeProgram( | ||
[{ | ||
name: mainPath, | ||
contents: ` | ||
import {TemplateRef, ViewContainerRef} from '@angular/core'; | ||
import * as i0 from '@angular/core'; | ||
export declare class TestDir { | ||
constructor(p0: ViewContainerRef, p1: TemplateRef); | ||
static ɵdir: i0.ɵɵDirectiveDefWithMeta<TestDir, "[test]", never, {}, {}, never> | ||
} | ||
` | ||
}], | ||
{ | ||
skipLibCheck: true, | ||
lib: ['es6', 'dom'], | ||
}); | ||
|
||
const sf = getSourceFileOrError(program, mainPath); | ||
const clazz = sf.statements[2]; | ||
if (!isNamedClassDeclaration(clazz)) { | ||
return fail('Expected class declaration'); | ||
} | ||
|
||
const typeChecker = program.getTypeChecker(); | ||
const dtsReader = | ||
new DtsMetadataReader(typeChecker, new TypeScriptReflectionHost(typeChecker)); | ||
|
||
const meta = dtsReader.getDirectiveMetadata(new Reference(clazz))!; | ||
expect(meta.isStructural).toBeTrue(); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.