Skip to content

Commit 349ff01

Browse files
alan-agius4atscott
authored andcommitted
refactor: update code to be ES2022 compliant (#49332)
This commit updates parts of the FW to be ES2022 complaint. These changes are needed to fix the following problems problems with using properties before they are initialized. Example ```ts class Foo { bar = this.buz; constructor(private buz: unknown){} } ``` PR Close #49332
1 parent 842d569 commit 349ff01

20 files changed

Lines changed: 211 additions & 130 deletions

File tree

packages/compiler-cli/src/ngtsc/annotations/component/src/handler.ts

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,13 @@ export class ComponentDecoratorHandler implements
6060
private injectableRegistry: InjectableClassRegistry,
6161
private semanticDepGraphUpdater: SemanticDepGraphUpdater|null,
6262
private annotateForClosureCompiler: boolean, private perf: PerfRecorder,
63-
private hostDirectivesResolver: HostDirectivesResolver) {}
63+
private hostDirectivesResolver: HostDirectivesResolver) {
64+
this.extractTemplateOptions = {
65+
enableI18nLegacyMessageIdFormat: this.enableI18nLegacyMessageIdFormat,
66+
i18nNormalizeLineEndingsInICUs: this.i18nNormalizeLineEndingsInICUs,
67+
usePoisonedData: this.usePoisonedData,
68+
};
69+
}
6470

6571
private literalCache = new Map<Decorator, ts.ObjectLiteralExpression>();
6672
private elementSchemaRegistry = new DomElementSchemaRegistry();
@@ -73,10 +79,9 @@ export class ComponentDecoratorHandler implements
7379
private preanalyzeTemplateCache = new Map<DeclarationNode, ParsedTemplateWithSource>();
7480
private preanalyzeStylesCache = new Map<DeclarationNode, string[]|null>();
7581

76-
private extractTemplateOptions = {
77-
enableI18nLegacyMessageIdFormat: this.enableI18nLegacyMessageIdFormat,
78-
i18nNormalizeLineEndingsInICUs: this.i18nNormalizeLineEndingsInICUs,
79-
usePoisonedData: this.usePoisonedData,
82+
private extractTemplateOptions: {
83+
enableI18nLegacyMessageIdFormat: boolean; i18nNormalizeLineEndingsInICUs: boolean;
84+
usePoisonedData: boolean;
8085
};
8186

8287
readonly precedence = HandlerPrecedence.PRIMARY;

packages/compiler-cli/src/ngtsc/core/src/compiler.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1265,8 +1265,12 @@ class ReferenceGraphAdapter implements ReferencesRegistry {
12651265
}
12661266

12671267
class NotifyingProgramDriverWrapper implements ProgramDriver {
1268+
getSourceFileVersion: ProgramDriver['getSourceFileVersion'];
1269+
12681270
constructor(
1269-
private delegate: ProgramDriver, private notifyNewProgram: (program: ts.Program) => void) {}
1271+
private delegate: ProgramDriver, private notifyNewProgram: (program: ts.Program) => void) {
1272+
this.getSourceFileVersion = this.delegate.getSourceFileVersion?.bind(this);
1273+
}
12701274

12711275
get supportsInlineOperations() {
12721276
return this.delegate.supportsInlineOperations;
@@ -1280,8 +1284,6 @@ class NotifyingProgramDriverWrapper implements ProgramDriver {
12801284
this.delegate.updateFiles(contents, updateMode);
12811285
this.notifyNewProgram(this.delegate.getProgram());
12821286
}
1283-
1284-
getSourceFileVersion = this.delegate.getSourceFileVersion?.bind(this);
12851287
}
12861288

12871289
function versionMapFromProgram(

packages/compiler-cli/src/ngtsc/core/src/host.ts

Lines changed: 69 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -32,50 +32,81 @@ import {ExtendedTsCompilerHost, NgCompilerAdapter, NgCompilerOptions, UnifiedMod
3232
*/
3333
export class DelegatingCompilerHost implements
3434
Omit<RequiredDelegations<ExtendedTsCompilerHost>, 'getSourceFile'|'fileExists'> {
35-
constructor(protected delegate: ExtendedTsCompilerHost) {}
35+
createHash;
36+
directoryExists;
37+
fileNameToModuleName;
38+
getCancellationToken;
39+
getCanonicalFileName;
40+
getCurrentDirectory;
41+
getDefaultLibFileName;
42+
getDefaultLibLocation;
43+
getDirectories;
44+
getEnvironmentVariable;
45+
getModifiedResourceFiles;
46+
getNewLine;
47+
getParsedCommandLine;
48+
getSourceFileByPath;
49+
readDirectory;
50+
readFile;
51+
readResource;
52+
transformResource;
53+
realpath;
54+
resolveModuleNames;
55+
resolveTypeReferenceDirectives;
56+
resourceNameToFileName;
57+
trace;
58+
useCaseSensitiveFileNames;
59+
writeFile;
60+
getModuleResolutionCache;
61+
hasInvalidatedResolutions;
62+
resolveModuleNameLiterals;
63+
resolveTypeReferenceDirectiveReferences;
64+
65+
constructor(protected delegate: ExtendedTsCompilerHost) {
66+
// Excluded are 'getSourceFile' and 'fileExists', which are actually implemented by
67+
// NgCompilerHost
68+
// below.
69+
this.createHash = this.delegateMethod('createHash');
70+
this.directoryExists = this.delegateMethod('directoryExists');
71+
this.fileNameToModuleName = this.delegateMethod('fileNameToModuleName');
72+
this.getCancellationToken = this.delegateMethod('getCancellationToken');
73+
this.getCanonicalFileName = this.delegateMethod('getCanonicalFileName');
74+
this.getCurrentDirectory = this.delegateMethod('getCurrentDirectory');
75+
this.getDefaultLibFileName = this.delegateMethod('getDefaultLibFileName');
76+
this.getDefaultLibLocation = this.delegateMethod('getDefaultLibLocation');
77+
this.getDirectories = this.delegateMethod('getDirectories');
78+
this.getEnvironmentVariable = this.delegateMethod('getEnvironmentVariable');
79+
this.getModifiedResourceFiles = this.delegateMethod('getModifiedResourceFiles');
80+
this.getNewLine = this.delegateMethod('getNewLine');
81+
this.getParsedCommandLine = this.delegateMethod('getParsedCommandLine');
82+
this.getSourceFileByPath = this.delegateMethod('getSourceFileByPath');
83+
this.readDirectory = this.delegateMethod('readDirectory');
84+
this.readFile = this.delegateMethod('readFile');
85+
this.readResource = this.delegateMethod('readResource');
86+
this.transformResource = this.delegateMethod('transformResource');
87+
this.realpath = this.delegateMethod('realpath');
88+
this.resolveModuleNames = this.delegateMethod('resolveModuleNames');
89+
this.resolveTypeReferenceDirectives = this.delegateMethod('resolveTypeReferenceDirectives');
90+
this.resourceNameToFileName = this.delegateMethod('resourceNameToFileName');
91+
this.trace = this.delegateMethod('trace');
92+
this.useCaseSensitiveFileNames = this.delegateMethod('useCaseSensitiveFileNames');
93+
this.writeFile = this.delegateMethod('writeFile');
94+
this.getModuleResolutionCache = this.delegateMethod('getModuleResolutionCache');
95+
this.hasInvalidatedResolutions = this.delegateMethod('hasInvalidatedResolutions');
96+
// The following methods are required in TS 5.0+, but they don't exist in earlier versions.
97+
// TODO(crisbeto): remove the `ts-ignore` when dropping support for TypeScript 4.9.
98+
// @ts-ignore
99+
this.resolveModuleNameLiterals = this.delegateMethod('resolveModuleNameLiterals');
100+
this.resolveTypeReferenceDirectiveReferences =
101+
// @ts-ignore
102+
this.delegateMethod('resolveTypeReferenceDirectiveReferences');
103+
}
36104

37105
private delegateMethod<M extends keyof ExtendedTsCompilerHost>(name: M):
38106
ExtendedTsCompilerHost[M] {
39107
return this.delegate[name] !== undefined ? (this.delegate[name] as any).bind(this.delegate) :
40108
undefined;
41109
}
42-
43-
// Excluded are 'getSourceFile' and 'fileExists', which are actually implemented by NgCompilerHost
44-
// below.
45-
createHash = this.delegateMethod('createHash');
46-
directoryExists = this.delegateMethod('directoryExists');
47-
fileNameToModuleName = this.delegateMethod('fileNameToModuleName');
48-
getCancellationToken = this.delegateMethod('getCancellationToken');
49-
getCanonicalFileName = this.delegateMethod('getCanonicalFileName');
50-
getCurrentDirectory = this.delegateMethod('getCurrentDirectory');
51-
getDefaultLibFileName = this.delegateMethod('getDefaultLibFileName');
52-
getDefaultLibLocation = this.delegateMethod('getDefaultLibLocation');
53-
getDirectories = this.delegateMethod('getDirectories');
54-
getEnvironmentVariable = this.delegateMethod('getEnvironmentVariable');
55-
getModifiedResourceFiles = this.delegateMethod('getModifiedResourceFiles');
56-
getNewLine = this.delegateMethod('getNewLine');
57-
getParsedCommandLine = this.delegateMethod('getParsedCommandLine');
58-
getSourceFileByPath = this.delegateMethod('getSourceFileByPath');
59-
readDirectory = this.delegateMethod('readDirectory');
60-
readFile = this.delegateMethod('readFile');
61-
readResource = this.delegateMethod('readResource');
62-
transformResource = this.delegateMethod('transformResource');
63-
realpath = this.delegateMethod('realpath');
64-
resolveModuleNames = this.delegateMethod('resolveModuleNames');
65-
resolveTypeReferenceDirectives = this.delegateMethod('resolveTypeReferenceDirectives');
66-
resourceNameToFileName = this.delegateMethod('resourceNameToFileName');
67-
trace = this.delegateMethod('trace');
68-
useCaseSensitiveFileNames = this.delegateMethod('useCaseSensitiveFileNames');
69-
writeFile = this.delegateMethod('writeFile');
70-
getModuleResolutionCache = this.delegateMethod('getModuleResolutionCache');
71-
hasInvalidatedResolutions = this.delegateMethod('hasInvalidatedResolutions');
72-
// The following methods are required in TS 5.0+, but they don't exist in earlier versions.
73-
// TODO(crisbeto): remove the `ts-ignore` when dropping support for TypeScript 4.9.
74-
// @ts-ignore
75-
resolveModuleNameLiterals = this.delegateMethod('resolveModuleNameLiterals');
76-
resolveTypeReferenceDirectiveReferences =
77-
// @ts-ignore
78-
this.delegateMethod('resolveTypeReferenceDirectiveReferences');
79110
}
80111

81112
/**

packages/compiler-cli/src/ngtsc/imports/src/emitter.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -367,9 +367,11 @@ export class AbsoluteModuleStrategy implements ReferenceEmitStrategy {
367367
* Instead, `LogicalProjectPath`s are used.
368368
*/
369369
export class LogicalProjectStrategy implements ReferenceEmitStrategy {
370-
private relativePathStrategy = new RelativePathStrategy(this.reflector);
370+
private relativePathStrategy: RelativePathStrategy;
371371

372-
constructor(private reflector: ReflectionHost, private logicalFs: LogicalFileSystem) {}
372+
constructor(private reflector: ReflectionHost, private logicalFs: LogicalFileSystem) {
373+
this.relativePathStrategy = new RelativePathStrategy(this.reflector);
374+
}
373375

374376
emit(ref: Reference, context: ts.SourceFile, importFlags: ImportFlags): ReferenceEmitResult|null {
375377
const destSf = getSourceFile(ref.node);

packages/compiler-cli/src/ngtsc/perf/src/recorder.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ export class ActivePerfRecorder implements PerfRecorder {
2828
private bytes: number[];
2929

3030
private currentPhase = PerfPhase.Unaccounted;
31-
private currentPhaseEntered = this.zeroTime;
31+
private currentPhaseEntered: HrTime;
3232

3333
/**
3434
* Creates an `ActivePerfRecorder` with its zero point set to the current time.
@@ -38,6 +38,7 @@ export class ActivePerfRecorder implements PerfRecorder {
3838
}
3939

4040
private constructor(private zeroTime: HrTime) {
41+
this.currentPhaseEntered = this.zeroTime;
4142
this.counters = Array(PerfEvent.LAST).fill(0);
4243
this.phaseTime = Array(PerfPhase.LAST).fill(0);
4344
this.bytes = Array(PerfCheckpoint.LAST).fill(0);

packages/compiler-cli/src/ngtsc/program_driver/src/ts_create_program_driver.ts

Lines changed: 63 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -23,43 +23,68 @@ import {FileUpdate, MaybeSourceFileWithOriginalFile, NgOriginalFile, ProgramDriv
2323
*/
2424
export class DelegatingCompilerHost implements
2525
Omit<RequiredDelegations<ts.CompilerHost>, 'getSourceFile'|'fileExists'|'writeFile'> {
26-
constructor(protected delegate: ts.CompilerHost) {}
26+
createHash;
27+
directoryExists;
28+
getCancellationToken;
29+
getCanonicalFileName;
30+
getCurrentDirectory;
31+
getDefaultLibFileName;
32+
getDefaultLibLocation;
33+
getDirectories;
34+
getEnvironmentVariable;
35+
getNewLine;
36+
getParsedCommandLine;
37+
getSourceFileByPath;
38+
readDirectory;
39+
readFile;
40+
realpath;
41+
resolveModuleNames;
42+
resolveTypeReferenceDirectives;
43+
trace;
44+
useCaseSensitiveFileNames;
45+
getModuleResolutionCache;
46+
hasInvalidatedResolutions;
47+
resolveModuleNameLiterals;
48+
resolveTypeReferenceDirectiveReferences;
49+
50+
constructor(protected delegate: ts.CompilerHost) {
51+
// Excluded are 'getSourceFile', 'fileExists' and 'writeFile', which are actually implemented by
52+
// `TypeCheckProgramHost` below.
53+
54+
this.createHash = this.delegateMethod('createHash');
55+
this.directoryExists = this.delegateMethod('directoryExists');
56+
this.getCancellationToken = this.delegateMethod('getCancellationToken');
57+
this.getCanonicalFileName = this.delegateMethod('getCanonicalFileName');
58+
this.getCurrentDirectory = this.delegateMethod('getCurrentDirectory');
59+
this.getDefaultLibFileName = this.delegateMethod('getDefaultLibFileName');
60+
this.getDefaultLibLocation = this.delegateMethod('getDefaultLibLocation');
61+
this.getDirectories = this.delegateMethod('getDirectories');
62+
this.getEnvironmentVariable = this.delegateMethod('getEnvironmentVariable');
63+
this.getNewLine = this.delegateMethod('getNewLine');
64+
this.getParsedCommandLine = this.delegateMethod('getParsedCommandLine');
65+
this.getSourceFileByPath = this.delegateMethod('getSourceFileByPath');
66+
this.readDirectory = this.delegateMethod('readDirectory');
67+
this.readFile = this.delegateMethod('readFile');
68+
this.realpath = this.delegateMethod('realpath');
69+
this.resolveModuleNames = this.delegateMethod('resolveModuleNames');
70+
this.resolveTypeReferenceDirectives = this.delegateMethod('resolveTypeReferenceDirectives');
71+
this.trace = this.delegateMethod('trace');
72+
this.useCaseSensitiveFileNames = this.delegateMethod('useCaseSensitiveFileNames');
73+
this.getModuleResolutionCache = this.delegateMethod('getModuleResolutionCache');
74+
this.hasInvalidatedResolutions = this.delegateMethod('hasInvalidatedResolutions');
75+
// The following methods are required in TS 5.0+, but they don't exist in earlier versions.
76+
// TODO(crisbeto): remove the `ts-ignore` when dropping support for TypeScript 4.9.
77+
// @ts-ignore
78+
this.resolveModuleNameLiterals = this.delegateMethod('resolveModuleNameLiterals');
79+
this.resolveTypeReferenceDirectiveReferences =
80+
// @ts-ignore
81+
this.delegateMethod('resolveTypeReferenceDirectiveReferences');
82+
}
2783

2884
private delegateMethod<M extends keyof ts.CompilerHost>(name: M): ts.CompilerHost[M] {
2985
return this.delegate[name] !== undefined ? (this.delegate[name] as any).bind(this.delegate) :
3086
undefined;
3187
}
32-
33-
// Excluded are 'getSourceFile', 'fileExists' and 'writeFile', which are actually implemented by
34-
// `TypeCheckProgramHost` below.
35-
createHash = this.delegateMethod('createHash');
36-
directoryExists = this.delegateMethod('directoryExists');
37-
getCancellationToken = this.delegateMethod('getCancellationToken');
38-
getCanonicalFileName = this.delegateMethod('getCanonicalFileName');
39-
getCurrentDirectory = this.delegateMethod('getCurrentDirectory');
40-
getDefaultLibFileName = this.delegateMethod('getDefaultLibFileName');
41-
getDefaultLibLocation = this.delegateMethod('getDefaultLibLocation');
42-
getDirectories = this.delegateMethod('getDirectories');
43-
getEnvironmentVariable = this.delegateMethod('getEnvironmentVariable');
44-
getNewLine = this.delegateMethod('getNewLine');
45-
getParsedCommandLine = this.delegateMethod('getParsedCommandLine');
46-
getSourceFileByPath = this.delegateMethod('getSourceFileByPath');
47-
readDirectory = this.delegateMethod('readDirectory');
48-
readFile = this.delegateMethod('readFile');
49-
realpath = this.delegateMethod('realpath');
50-
resolveModuleNames = this.delegateMethod('resolveModuleNames');
51-
resolveTypeReferenceDirectives = this.delegateMethod('resolveTypeReferenceDirectives');
52-
trace = this.delegateMethod('trace');
53-
useCaseSensitiveFileNames = this.delegateMethod('useCaseSensitiveFileNames');
54-
getModuleResolutionCache = this.delegateMethod('getModuleResolutionCache');
55-
hasInvalidatedResolutions = this.delegateMethod('hasInvalidatedResolutions');
56-
// The following methods are required in TS 5.0+, but they don't exist in earlier versions.
57-
// TODO(crisbeto): remove the `ts-ignore` when dropping support for TypeScript 4.9.
58-
// @ts-ignore
59-
resolveModuleNameLiterals = this.delegateMethod('resolveModuleNameLiterals');
60-
resolveTypeReferenceDirectiveReferences =
61-
// @ts-ignore
62-
this.delegateMethod('resolveTypeReferenceDirectiveReferences');
6388
}
6489

6590
/**
@@ -81,12 +106,13 @@ class UpdatedProgramHost extends DelegatingCompilerHost {
81106
* order for those shims to be loaded, and then cleaned up afterwards. Thus the
82107
* `UpdatedProgramHost` has its own `ShimReferenceTagger` to perform this function.
83108
*/
84-
private shimTagger = new ShimReferenceTagger(this.shimExtensionPrefixes);
109+
private shimTagger: ShimReferenceTagger;
85110

86111
constructor(
87112
sfMap: Map<string, ts.SourceFile>, private originalProgram: ts.Program,
88113
delegate: ts.CompilerHost, private shimExtensionPrefixes: string[]) {
89114
super(delegate);
115+
this.shimTagger = new ShimReferenceTagger(this.shimExtensionPrefixes);
90116
this.sfMap = sfMap;
91117
}
92118

@@ -150,11 +176,13 @@ export class TsCreateProgramDriver implements ProgramDriver {
150176
*/
151177
private sfMap = new Map<string, ts.SourceFile>();
152178

153-
private program: ts.Program = this.originalProgram;
179+
private program: ts.Program;
154180

155181
constructor(
156182
private originalProgram: ts.Program, private originalHost: ts.CompilerHost,
157-
private options: ts.CompilerOptions, private shimExtensionPrefixes: string[]) {}
183+
private options: ts.CompilerOptions, private shimExtensionPrefixes: string[]) {
184+
this.program = this.originalProgram;
185+
}
158186

159187
readonly supportsInlineOperations = true;
160188

packages/compiler-cli/src/ngtsc/resource/src/loader.ts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,16 @@ const RESOURCE_MARKER_TS = RESOURCE_MARKER + '.ts';
2424
export class AdapterResourceLoader implements ResourceLoader {
2525
private cache = new Map<string, string>();
2626
private fetching = new Map<string, Promise<void>>();
27-
private lookupResolutionHost = createLookupResolutionHost(this.adapter);
27+
private lookupResolutionHost: RequiredDelegations<ts.ModuleResolutionHost>;
2828

29-
canPreload = !!this.adapter.readResource;
30-
canPreprocess = !!this.adapter.transformResource;
29+
canPreload: boolean;
30+
canPreprocess: boolean;
3131

32-
constructor(private adapter: NgCompilerAdapter, private options: ts.CompilerOptions) {}
32+
constructor(private adapter: NgCompilerAdapter, private options: ts.CompilerOptions) {
33+
this.lookupResolutionHost = createLookupResolutionHost(this.adapter);
34+
this.canPreload = !!this.adapter.readResource;
35+
this.canPreprocess = !!this.adapter.transformResource;
36+
}
3337

3438
/**
3539
* Resolve the url of a resource relative to the file that contains the reference to it.

packages/compiler/src/i18n/i18n_ast.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,11 @@ export interface MessagePlaceholder {
2222

2323
export class Message {
2424
sources: MessageSpan[];
25-
id: string = this.customId;
25+
id: string;
2626
/** The ids to use if there are no custom id and if `i18nLegacyMessageIdFormat` is not empty */
2727
legacyIds: string[] = [];
2828

29-
messageString = serializeMessage(this.nodes);
29+
messageString: string;
3030

3131
/**
3232
* @param nodes message AST
@@ -40,6 +40,9 @@ export class Message {
4040
public nodes: Node[], public placeholders: {[phName: string]: MessagePlaceholder},
4141
public placeholderToMessage: {[phName: string]: Message}, public meaning: string,
4242
public description: string, public customId: string) {
43+
this.id = this.customId;
44+
this.messageString = serializeMessage(this.nodes);
45+
4346
if (nodes.length) {
4447
this.sources = [{
4548
filePath: nodes[0].sourceSpan.start.file.url,

0 commit comments

Comments
 (0)