refactor: fix typescript strict flag failures in all tests (#30993)
Fixes all TypeScript failures caused by enabling the `--strict` flag for test source files. We also want to enable the strict options for tests as the strictness enforcement improves the overall codehealth, unveiled common issues and additionally it allows us to enable `strict` in the `tsconfig.json` that is picked up by IDE's. PR Close #30993
This commit is contained in:
committed by
Miško Hevery
parent
69a612d402
commit
647d7bdd88
@@ -31,15 +31,14 @@ const EXTRACT_GENERATED_TRANSLATIONS_REGEXP =
|
||||
const diff = (a: Set<string>, b: Set<string>): Set<string> =>
|
||||
new Set([...Array.from(a)].filter(x => !b.has(x)));
|
||||
|
||||
const extract =
|
||||
(from: string, regex: any, transformFn: (match: any[], state?: Set<any>) => any) => {
|
||||
const result = new Set<any>();
|
||||
let item;
|
||||
while ((item = regex.exec(from)) !== null) {
|
||||
result.add(transformFn(item, result));
|
||||
}
|
||||
return result;
|
||||
};
|
||||
const extract = (from: string, regex: any, transformFn: (match: any[], state: Set<any>) => any) => {
|
||||
const result = new Set<any>();
|
||||
let item;
|
||||
while ((item = regex.exec(from)) !== null) {
|
||||
result.add(transformFn(item, result));
|
||||
}
|
||||
return result;
|
||||
};
|
||||
|
||||
// verify that we extracted all the necessary translations
|
||||
// and their ids match the ones extracted via 'ng xi18n'
|
||||
|
||||
@@ -10,6 +10,7 @@ import * as ng from '@angular/compiler-cli';
|
||||
import * as fs from 'fs';
|
||||
import * as os from 'os';
|
||||
import * as path from 'path';
|
||||
import * as ts from 'typescript';
|
||||
import {TestSupport, expectNoDiagnostics, setup} from '../test_support';
|
||||
|
||||
type MockFiles = {
|
||||
@@ -52,7 +53,7 @@ describe('ng type checker', () => {
|
||||
if (!diagnostics || !diagnostics.length) {
|
||||
throw new Error('Expected a diagnostic error message');
|
||||
} else {
|
||||
const matches: (d: ng.Diagnostic) => boolean = typeof message === 'string' ?
|
||||
const matches: (d: ng.Diagnostic | ts.Diagnostic) => boolean = typeof message === 'string' ?
|
||||
d => ng.isNgDiagnostic(d)&& d.messageText == message :
|
||||
d => ng.isNgDiagnostic(d) && message.test(d.messageText);
|
||||
const matchingDiagnostics = diagnostics.filter(matches) as ng.Diagnostic[];
|
||||
|
||||
@@ -11,7 +11,7 @@ import * as ts from 'typescript';
|
||||
|
||||
import {CompilerHostAdapter, MetadataBundler, MetadataBundlerHost} from '../../src/metadata/bundler';
|
||||
import {MetadataCollector} from '../../src/metadata/collector';
|
||||
import {ClassMetadata, MetadataGlobalReferenceExpression, ModuleMetadata} from '../../src/metadata/schema';
|
||||
import {ClassMetadata, MetadataEntry, MetadataGlobalReferenceExpression, ModuleMetadata} from '../../src/metadata/schema';
|
||||
import {Directory, MockAotContext, MockCompilerHost} from '../mocks';
|
||||
|
||||
describe('compiler host adapter', () => {
|
||||
@@ -242,7 +242,7 @@ describe('metadata bundler', () => {
|
||||
const deepIndexMetadata = host.getMetadataFor('/lib/deep/index') !;
|
||||
|
||||
// The unbundled metadata should reference symbols using the relative module path.
|
||||
expect(deepIndexMetadata.metadata['MyClass']).toEqual(jasmine.objectContaining({
|
||||
expect(deepIndexMetadata.metadata['MyClass']).toEqual(jasmine.objectContaining<MetadataEntry>({
|
||||
statics: {
|
||||
ngInjectableDef: {
|
||||
__symbolic: 'call',
|
||||
@@ -258,7 +258,7 @@ describe('metadata bundler', () => {
|
||||
// For the bundled metadata, the "sharedFn" symbol should not be referenced using the
|
||||
// relative module path (like for unbundled), because the metadata bundle can be stored
|
||||
// anywhere and it's not guaranteed that the relatively referenced files are present.
|
||||
expect(bundledMetadata.metadata['MyClass']).toEqual(jasmine.objectContaining({
|
||||
expect(bundledMetadata.metadata['MyClass']).toEqual(jasmine.objectContaining<MetadataEntry>({
|
||||
statics: {
|
||||
ngInjectableDef: {
|
||||
__symbolic: 'call',
|
||||
|
||||
@@ -7,7 +7,7 @@
|
||||
*/
|
||||
import {AbsoluteFsPath, resolve} from '@angular/compiler-cli/src/ngtsc/file_system';
|
||||
import {runInEachFileSystem} from '@angular/compiler-cli/src/ngtsc/file_system/testing';
|
||||
import {AbsoluteSourceSpan, IdentifierKind, TopLevelIdentifier} from '@angular/compiler-cli/src/ngtsc/indexer';
|
||||
import {AbsoluteSourceSpan, IdentifierKind, IndexedComponent, TopLevelIdentifier} from '@angular/compiler-cli/src/ngtsc/indexer';
|
||||
import {ParseSourceFile} from '@angular/compiler/src/compiler';
|
||||
|
||||
import {NgtscTestEnvironment} from './env';
|
||||
@@ -43,7 +43,7 @@ runInEachFileSystem(() => {
|
||||
const [[decl, indexedComp]] = Array.from(indexed.entries());
|
||||
|
||||
expect(decl.getText()).toContain('export class TestCmp {}');
|
||||
expect(indexedComp).toEqual(jasmine.objectContaining({
|
||||
expect(indexedComp).toEqual(jasmine.objectContaining<IndexedComponent>({
|
||||
name: 'TestCmp',
|
||||
selector: 'test-cmp',
|
||||
file: new ParseSourceFile(componentContent, testSourceFile),
|
||||
|
||||
@@ -83,19 +83,21 @@ describe('ng program', () => {
|
||||
|
||||
const originalGetSourceFile = host.getSourceFile;
|
||||
const cache = new Map<string, ts.SourceFile>();
|
||||
host.getSourceFile = function(fileName: string): ts.SourceFile {
|
||||
const sf = originalGetSourceFile.call(host, fileName) as ts.SourceFile;
|
||||
if (sf) {
|
||||
if (cache.has(sf.fileName)) {
|
||||
const oldSf = cache.get(sf.fileName) !;
|
||||
if (oldSf.getFullText() === sf.getFullText()) {
|
||||
return oldSf;
|
||||
host.getSourceFile = function(
|
||||
fileName: string, languageVersion: ts.ScriptTarget): ts.SourceFile |
|
||||
undefined {
|
||||
const sf = originalGetSourceFile.call(host, fileName, languageVersion);
|
||||
if (sf) {
|
||||
if (cache.has(sf.fileName)) {
|
||||
const oldSf = cache.get(sf.fileName) !;
|
||||
if (oldSf.getFullText() === sf.getFullText()) {
|
||||
return oldSf;
|
||||
}
|
||||
}
|
||||
cache.set(sf.fileName, sf);
|
||||
}
|
||||
}
|
||||
cache.set(sf.fileName, sf);
|
||||
}
|
||||
return sf;
|
||||
};
|
||||
return sf;
|
||||
};
|
||||
return host;
|
||||
}
|
||||
|
||||
@@ -196,12 +198,14 @@ describe('ng program', () => {
|
||||
const host = ng.createCompilerHost({options});
|
||||
const originalGetSourceFile = host.getSourceFile;
|
||||
const fileCache = new Map<string, ts.SourceFile>();
|
||||
host.getSourceFile = (fileName: string) => {
|
||||
host.getSourceFile = (fileName: string, languageVersion: ts.ScriptTarget) => {
|
||||
if (fileCache.has(fileName)) {
|
||||
return fileCache.get(fileName);
|
||||
}
|
||||
const sf = originalGetSourceFile.call(host, fileName);
|
||||
fileCache.set(fileName, sf);
|
||||
const sf = originalGetSourceFile.call(host, fileName, languageVersion);
|
||||
if (sf !== undefined) {
|
||||
fileCache.set(fileName, sf);
|
||||
}
|
||||
return sf;
|
||||
};
|
||||
|
||||
@@ -469,8 +473,8 @@ describe('ng program', () => {
|
||||
|
||||
host.writeFile =
|
||||
(fileName: string, data: string, writeByteOrderMark: boolean,
|
||||
onError: (message: string) => void|undefined,
|
||||
sourceFiles: ReadonlyArray<ts.SourceFile>) => {
|
||||
onError: ((message: string) => void) | undefined,
|
||||
sourceFiles?: ReadonlyArray<ts.SourceFile>) => {
|
||||
written.set(fileName, {original: sourceFiles, data});
|
||||
};
|
||||
const program = ng.createProgram(
|
||||
@@ -1095,15 +1099,15 @@ describe('ng program', () => {
|
||||
});
|
||||
const host = ng.createCompilerHost({options});
|
||||
const originalGetSourceFile = host.getSourceFile;
|
||||
host.getSourceFile =
|
||||
(fileName: string, languageVersion: ts.ScriptTarget,
|
||||
onError?: ((message: string) => void) | undefined): ts.SourceFile => {
|
||||
// We should never try to load .ngfactory.ts files
|
||||
if (fileName.match(/\.ngfactory\.ts$/)) {
|
||||
throw new Error(`Non existent ngfactory file: ` + fileName);
|
||||
}
|
||||
return originalGetSourceFile.call(host, fileName, languageVersion, onError);
|
||||
};
|
||||
host.getSourceFile = (fileName: string, languageVersion: ts.ScriptTarget,
|
||||
onError?: ((message: string) => void) | undefined): ts.SourceFile |
|
||||
undefined => {
|
||||
// We should never try to load .ngfactory.ts files
|
||||
if (fileName.match(/\.ngfactory\.ts$/)) {
|
||||
throw new Error(`Non existent ngfactory file: ` + fileName);
|
||||
}
|
||||
return originalGetSourceFile.call(host, fileName, languageVersion, onError);
|
||||
};
|
||||
const program = ng.createProgram({rootNames: allRootNames, options, host});
|
||||
const structuralErrors = program.getNgStructuralDiagnostics();
|
||||
expect(structuralErrors.length).toBe(1);
|
||||
|
||||
Reference in New Issue
Block a user