/
ts_plugin_spec.ts
172 lines (156 loc) Β· 5.1 KB
/
ts_plugin_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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
/**
* @license
* Copyright Google Inc. 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 * as ts from 'typescript';
import {create, getExternalFiles} from '../src/ts_plugin';
import {CompletionKind} from '../src/types';
import {MockTypescriptHost} from './test_utils';
const mockProject = {
projectService: {
logger: {
info() {},
hasLevel: () => false,
},
},
hasRoots: () => true,
} as any;
describe('plugin', () => {
const mockHost = new MockTypescriptHost(['/app/main.ts']);
const tsLS = ts.createLanguageService(mockHost);
const program = tsLS.getProgram() !;
const plugin = create({
languageService: tsLS,
languageServiceHost: mockHost,
project: mockProject,
serverHost: {} as any,
config: {},
});
beforeEach(() => { mockHost.reset(); });
it('should produce TypeScript diagnostics', () => {
const fileName = '/foo.ts';
mockHost.addScript(fileName, `
function add(x: number) {
return x + 42;
}
add('hello');
`);
const diags = plugin.getSemanticDiagnostics(fileName);
expect(diags.length).toBe(1);
expect(diags[0].messageText)
.toBe(`Argument of type '"hello"' is not assignable to parameter of type 'number'.`);
});
it('should not report TypeScript errors on tour of heroes', () => {
const compilerDiags = tsLS.getCompilerOptionsDiagnostics();
expect(compilerDiags).toEqual([]);
const sourceFiles = program.getSourceFiles().filter(f => !f.fileName.endsWith('.d.ts'));
// there are six .ts files in the test project
expect(sourceFiles.length).toBe(6);
for (const {fileName} of sourceFiles) {
const syntacticDiags = tsLS.getSyntacticDiagnostics(fileName);
expect(syntacticDiags).toEqual([]);
const semanticDiags = tsLS.getSemanticDiagnostics(fileName);
expect(semanticDiags).toEqual([]);
}
});
it('should return external templates as external files', () => {
const externalFiles = getExternalFiles(mockProject);
expect(externalFiles).toEqual(['/app/test.ng']);
});
it('should not report template errors on tour of heroes', () => {
const filesWithTemplates = [
// Ignore all '*-cases.ts' files as they intentionally contain errors.
'/app/app.component.ts',
];
for (const fileName of filesWithTemplates) {
const diags = plugin.getSemanticDiagnostics(fileName);
expect(diags).toEqual([]);
}
});
it('should respect paths configuration', () => {
const SHARED_MODULE = '/app/foo/bar/shared.ts';
const MY_COMPONENT = '/app/my.component.ts';
mockHost.overrideOptions({
baseUrl: '/app',
paths: {'bar/*': ['foo/bar/*']},
});
mockHost.addScript(SHARED_MODULE, `
export interface Node {
children: Node[];
}
`);
mockHost.addScript(MY_COMPONENT, `
import { Component, NgModule } from '@angular/core';
import { Node } from 'bar/shared';
@Component({
selector: 'my-component',
template: '{{ tree.~{tree}children }}'
})
export class MyComponent {
tree: Node = {
children: [],
};
}
@NgModule({
declarations: [MyComponent],
})
export class MyModule {}
`);
// First, make sure there are no errors in newly added scripts.
for (const fileName of [SHARED_MODULE, MY_COMPONENT]) {
const syntacticDiags = plugin.getSyntacticDiagnostics(fileName);
expect(syntacticDiags).toEqual([]);
const semanticDiags = plugin.getSemanticDiagnostics(fileName);
expect(semanticDiags).toEqual([]);
}
const marker = mockHost.getLocationMarkerFor(MY_COMPONENT, 'tree');
const completions = plugin.getCompletionsAtPosition(MY_COMPONENT, marker.start, undefined);
expect(completions).toBeDefined();
expect(completions !.entries).toEqual([
{
name: 'children',
kind: CompletionKind.PROPERTY as any,
sortText: 'children',
replacementSpan: {start: 182, length: 8},
},
]);
});
});
describe(`with config 'angularOnly = true`, () => {
const mockHost = new MockTypescriptHost(['/app/main.ts']);
const tsLS = ts.createLanguageService(mockHost);
const plugin = create({
languageService: tsLS,
languageServiceHost: mockHost,
project: mockProject,
serverHost: {} as any,
config: {
angularOnly: true,
},
});
it('should not produce TypeScript diagnostics', () => {
const fileName = '/foo.ts';
mockHost.addScript(fileName, `
function add(x: number) {
return x + 42;
}
add('hello');
`);
const diags = plugin.getSemanticDiagnostics(fileName);
expect(diags).toEqual([]);
});
it('should not report template errors on TOH', () => {
const filesWithTemplates = [
// Ignore all '*-cases.ts' files as they intentionally contain errors.
'/app/app.component.ts',
'/app/test.ng',
];
for (const fileName of filesWithTemplates) {
const diags = plugin.getSemanticDiagnostics(fileName);
expect(diags).toEqual([]);
}
});
});