-
Notifications
You must be signed in to change notification settings - Fork 24.8k
/
missing_control_flow_directive_spec.ts
128 lines (121 loc) 路 5.88 KB
/
missing_control_flow_directive_spec.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
/**
* @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 {DiagnosticCategoryLabel} from '@angular/compiler-cli/src/ngtsc/core/api';
import ts from 'typescript';
import {ErrorCode, ExtendedTemplateDiagnosticName, ngErrorCode} from '../../../../../diagnostics';
import {absoluteFrom, getSourceFileOrError} from '../../../../../file_system';
import {runInEachFileSystem} from '../../../../../file_system/testing';
import {getSourceCodeForDiagnostic} from '../../../../../testing';
import {getClass, setup} from '../../../../testing';
import {factory as missingControlFlowDirectiveCheck} from '../../../checks/missing_control_flow_directive';
import {ExtendedTemplateCheckerImpl} from '../../../src/extended_template_checker';
const KNOWN_CONTROL_FLOW_DIRECTIVES = ['ngIf', 'ngFor', 'ngSwitchCase', 'ngSwitchDefault'];
runInEachFileSystem(() => {
describe('MissingControlFlowDirectiveCheck', () => {
KNOWN_CONTROL_FLOW_DIRECTIVES.forEach(directive => {
['div', 'ng-template', 'ng-container', 'ng-content'].forEach(element => {
it(`should produce a warning when the '${directive}' directive is not imported ` +
`(when used on the '${element}' element)`,
() => {
const fileName = absoluteFrom('/main.ts');
const {program, templateTypeChecker} = setup([{
fileName,
templates: {
'TestCmp': `<${element} *${directive}="exp"></${element}>`,
},
}]);
const sf = getSourceFileOrError(program, fileName);
const component = getClass(sf, 'TestCmp');
const extendedTemplateChecker = new ExtendedTemplateCheckerImpl(
templateTypeChecker, program.getTypeChecker(), [missingControlFlowDirectiveCheck],
{} /* options */);
const diags = extendedTemplateChecker.getDiagnosticsForComponent(component);
expect(diags.length).toBe(1);
expect(diags[0].category).toBe(ts.DiagnosticCategory.Warning);
expect(diags[0].code).toBe(ngErrorCode(ErrorCode.MISSING_CONTROL_FLOW_DIRECTIVE));
expect(getSourceCodeForDiagnostic(diags[0])).toBe(directive);
});
it(`should *not* produce a warning when the '${directive}' directive is imported ` +
`(when used on the '${element}' element)`,
() => {
const className = directive.charAt(0).toUpperCase() + directive.substr(1);
const fileName = absoluteFrom('/main.ts');
const {program, templateTypeChecker} = setup([{
fileName,
templates: {
'TestCmp': `<${element} *${directive}="exp"></${element}>`,
},
source: `
export class TestCmp {}
export class ${className} {}
`,
declarations: [{
type: 'directive',
name: className,
selector: `[${directive}]`,
}],
}]);
const sf = getSourceFileOrError(program, fileName);
const component = getClass(sf, 'TestCmp');
const extendedTemplateChecker = new ExtendedTemplateCheckerImpl(
templateTypeChecker, program.getTypeChecker(), [missingControlFlowDirectiveCheck],
{strictNullChecks: true} /* options */);
const diags = extendedTemplateChecker.getDiagnosticsForComponent(component);
// No diagnostic messages are expected.
expect(diags.length).toBe(0);
});
it(`should *not* produce a warning when the '${directive}' directive selector attribute ` +
`is present on the '${element}' element, but the directive is not imported`,
() => {
const className = directive.charAt(0).toUpperCase() + directive.substr(1);
const fileName = absoluteFrom('/main.ts');
const {program, templateTypeChecker} = setup([{
fileName,
templates: {
'TestCmp': `<${element} *${directive}="exp"></${element}>`,
},
source: `
export class TestCmp {}
export class ${className} {}
`,
declarations: [{
type: 'directive',
name: className,
selector: `[${directive}]`,
}],
}]);
const sf = getSourceFileOrError(program, fileName);
const component = getClass(sf, 'TestCmp');
const extendedTemplateChecker = new ExtendedTemplateCheckerImpl(
templateTypeChecker, program.getTypeChecker(), [missingControlFlowDirectiveCheck],
{strictNullChecks: true} /* options */);
const diags = extendedTemplateChecker.getDiagnosticsForComponent(component);
// No diagnostic messages are expected.
expect(diags.length).toBe(0);
});
});
});
it(`should *not* produce a warning for other missing structural directives`, () => {
const fileName = absoluteFrom('/main.ts');
const {program, templateTypeChecker} = setup([{
fileName,
templates: {
'TestCmp': `<div *foo="exp"></div>`,
},
}]);
const sf = getSourceFileOrError(program, fileName);
const component = getClass(sf, 'TestCmp');
const extendedTemplateChecker = new ExtendedTemplateCheckerImpl(
templateTypeChecker, program.getTypeChecker(), [missingControlFlowDirectiveCheck],
{strictNullChecks: true} /* options */);
const diags = extendedTemplateChecker.getDiagnosticsForComponent(component);
// No diagnostic messages are expected.
expect(diags.length).toBe(0);
});
});
});