/** * @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 {StaticSymbol} from '@angular/compiler'; import {Directory} from '@angular/compiler-cli/test/mocks'; import {ReflectorHost} from '@angular/language-service/src/reflector_host'; import * as ts from 'typescript'; import {getTemplateExpressionDiagnostics} from '../src/expression_diagnostics'; import {DiagnosticContext, MockLanguageServiceHost, getDiagnosticTemplateInfo} from './mocks'; describe('expression diagnostics', () => { let registry: ts.DocumentRegistry; let host: MockLanguageServiceHost; let service: ts.LanguageService; let context: DiagnosticContext; let type: StaticSymbol; beforeAll(() => { registry = ts.createDocumentRegistry(false, '/src'); host = new MockLanguageServiceHost(['app/app.component.ts'], FILES, '/src'); service = ts.createLanguageService(host, registry); const program = service.getProgram() !; const checker = program.getTypeChecker(); const symbolResolverHost = new ReflectorHost(() => program !, host); context = new DiagnosticContext(service, program !, checker, symbolResolverHost); type = context.getStaticSymbol('app/app.component.ts', 'AppComponent'); }); it('should have no diagnostics in default app', () => { function messageToString(messageText: string | ts.DiagnosticMessageChain): string { if (typeof messageText == 'string') { return messageText; } else { if (messageText.next) return messageText.messageText + messageText.next.map(messageToString); return messageText.messageText; } } function expectNoDiagnostics(diagnostics: ts.Diagnostic[]) { if (diagnostics && diagnostics.length) { const message = 'messages: ' + diagnostics.map(d => messageToString(d.messageText)).join('\n'); expect(message).toEqual(''); } } expectNoDiagnostics(service.getCompilerOptionsDiagnostics()); expectNoDiagnostics(service.getSyntacticDiagnostics('app/app.component.ts')); expectNoDiagnostics(service.getSemanticDiagnostics('app/app.component.ts')); }); function accept(template: string) { const info = getDiagnosticTemplateInfo(context, type, 'app/app.component.html', template); if (info) { const diagnostics = getTemplateExpressionDiagnostics(info); if (diagnostics && diagnostics.length) { const message = diagnostics.map(d => d.message).join('\n '); throw new Error(`Unexpected diagnostics: ${message}`); } } else { expect(info).toBeDefined(); } } function reject(template: string, expected: string) { const info = getDiagnosticTemplateInfo(context, type, 'app/app.component.html', template); if (info) { const diagnostics = getTemplateExpressionDiagnostics(info); if (diagnostics && diagnostics.length) { const messages = diagnostics.map(d => d.message).join('\n '); expect(messages).toContain(expected); } else { throw new Error(`Expected an error containing "${expected} in template "${template}"`); } } else { expect(info).toBeDefined(); } } it('should accept a simple template', () => accept('App works!')); it('should accept an interpolation', () => accept('App works: {{person.name.first}}')); it('should reject misspelled access', () => reject('{{persson}}', 'Identifier \'persson\' is not defined')); it('should reject access to private', () => reject('{{private_person}}', 'Identifier \'private_person\' refers to a private member')); it('should accept an *ngIf', () => accept('
First name value: {{ first.value }}
First name valid: {{ first.valid }}
Form value: {{ f.value | json }}
Form valid: {{ f.valid }}
`)); it('should reject a misspelled field of a # reference', () => reject( `First name value: {{ first.valwe }}
First name valid: {{ first.valid }}
Form value: {{ f.value | json }}
Form valid: {{ f.valid }}
`, 'Identifier \'valwe\' is not defined')); it('should accept a call to a method', () => accept('{{getPerson().name.first}}')); it('should reject a misspelled field of a method result', () => reject('{{getPerson().nume.first}}', 'Identifier \'nume\' is not defined')); it('should reject calling a uncallable member', () => reject('{{person().name.first}}', '\'person\' is not callable')); it('should accept an event handler', () => accept('