Skip to content

Commit 012b535

Browse files
devversionmhevery
authored andcommitted
refactor(compiler): ensure compatibility with typescript strict flag (angular#30993)
As part of FW-1265, the `@angular/compiler` package is made compatible with the TypeScript `--strict` flag. This already unveiled a few bugs, so the strictness flag seems to help with increasing the overall code health. Read more about the strict flag [here](https://www.typescriptlang.org/docs/handbook/compiler-options.html) PR Close angular#30993
1 parent 2200884 commit 012b535

File tree

11 files changed

+28
-25
lines changed

11 files changed

+28
-25
lines changed

packages/compiler/src/aot/static_reflector.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ export class StaticReflector implements CompileReflector {
132132

133133
public tryAnnotations(type: StaticSymbol): any[] {
134134
const originalRecorder = this.errorRecorder;
135-
this.errorRecorder = (error: any, fileName: string) => {};
135+
this.errorRecorder = (error: any, fileName?: string) => {};
136136
try {
137137
return this.annotations(type);
138138
} finally {
@@ -429,7 +429,7 @@ export class StaticReflector implements CompileReflector {
429429
*/
430430
private trySimplify(context: StaticSymbol, value: any): any {
431431
const originalRecorder = this.errorRecorder;
432-
this.errorRecorder = (error: any, fileName: string) => {};
432+
this.errorRecorder = (error: any, fileName?: string) => {};
433433
const result = this.simplify(context, value);
434434
this.errorRecorder = originalRecorder;
435435
return result;

packages/compiler/src/constant_pool.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -273,11 +273,11 @@ class KeyVisitor implements o.ExpressionVisitor {
273273
visitCommaExpr = invalid;
274274
}
275275

276-
function invalid<T>(arg: o.Expression | o.Statement): never {
276+
function invalid<T>(this: o.ExpressionVisitor, arg: o.Expression | o.Statement): never {
277277
throw new Error(
278278
`Invalid state: Visitor ${this.constructor.name} doesn't handle ${arg.constructor.name}`);
279279
}
280280

281281
function isVariable(e: o.Expression): e is o.ReadVarExpr {
282282
return e instanceof o.ReadVarExpr;
283-
}
283+
}

packages/compiler/src/jit_compiler_facade.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -210,7 +210,7 @@ const USE_FACTORY = Object.keys({useFactory: null})[0];
210210
const USE_VALUE = Object.keys({useValue: null})[0];
211211
const USE_EXISTING = Object.keys({useExisting: null})[0];
212212

213-
const wrapReference = function(value: Type): R3Reference {
213+
const wrapReference = function(value: any): R3Reference {
214214
const wrapped = new WrappedNodeExpr(value);
215215
return {value: wrapped, type: wrapped};
216216
};

packages/compiler/src/metadata_resolver.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ export class CompileMetadataResolver {
8383

8484
private _createProxyClass(baseType: any, name: string): cpl.ProxyClass {
8585
let delegate: any = null;
86-
const proxyClass: cpl.ProxyClass = <any>function() {
86+
const proxyClass: cpl.ProxyClass = <any>function(this: unknown) {
8787
if (!delegate) {
8888
throw new Error(
8989
`Illegal state: Class ${name} for type ${stringify(baseType)} is not compiled yet!`);

packages/compiler/src/ml_parser/ast.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ export class RecursiveVisitor implements Visitor {
112112
if (children) results.push(visitAll(t, children, context));
113113
}
114114
cb(visit);
115-
return [].concat.apply([], results);
115+
return Array.prototype.concat.apply([], results);
116116
}
117117
}
118118

@@ -149,4 +149,4 @@ export function findNode(nodes: Node[], position: number): HtmlAstPath {
149149
visitAll(visitor, nodes);
150150

151151
return new AstPath<Node>(path, position);
152-
}
152+
}

packages/compiler/src/output/output_interpreter.ts

+6-6
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,8 @@ class _ExecutionContext {
3838
exports: string[] = [];
3939

4040
constructor(
41-
public parent: _ExecutionContext|null, public instance: any, public className: string|null,
42-
public vars: Map<string, any>) {}
41+
public parent: _ExecutionContext|null, public instance: Object|null,
42+
public className: string|null, public vars: Map<string, any>) {}
4343

4444
createChildWihtLocalVars(): _ExecutionContext {
4545
return new _ExecutionContext(this, this.instance, this.className, new Map<string, any>());
@@ -79,9 +79,9 @@ function createDynamicClass(
7979

8080
const ctorParamNames = _classStmt.constructorMethod.params.map(param => param.name);
8181
// Note: use `function` instead of arrow function to capture `this`
82-
const ctor = function(...args: any[]) {
82+
const ctor = function(this: Object, ...args: any[]) {
8383
const instanceCtx = new _ExecutionContext(_ctx, this, _classStmt.name, _ctx.vars);
84-
_classStmt.fields.forEach((field) => { this[field.name] = undefined; });
84+
_classStmt.fields.forEach((field) => { (this as any)[field.name] = undefined; });
8585
_executeFunctionStatements(
8686
ctorParamNames, args, _classStmt.constructorMethod.body, instanceCtx, _visitor);
8787
};
@@ -125,7 +125,7 @@ class StatementInterpreter implements o.StatementVisitor, o.ExpressionVisitor {
125125
if (ast.builtin != null) {
126126
switch (ast.builtin) {
127127
case o.BuiltinVar.Super:
128-
return ctx.instance.__proto__;
128+
return Object.getPrototypeOf(ctx.instance);
129129
case o.BuiltinVar.This:
130130
return ctx.instance;
131131
case o.BuiltinVar.CatchError:
@@ -188,7 +188,7 @@ class StatementInterpreter implements o.StatementVisitor, o.ExpressionVisitor {
188188
const args = this.visitAllExpressions(stmt.args, ctx);
189189
const fnExpr = stmt.fn;
190190
if (fnExpr instanceof o.ReadVarExpr && fnExpr.builtin === o.BuiltinVar.Super) {
191-
ctx.instance.constructor.prototype.constructor.apply(ctx.instance, args);
191+
ctx.instance !.constructor.prototype.constructor.apply(ctx.instance, args);
192192
return null;
193193
} else {
194194
const fn = stmt.fn.visitExpression(this, ctx);

packages/compiler/src/render3/view/i18n/meta.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import {ParseTreeResult} from '../../../ml_parser/parser';
1515

1616
import {I18N_ATTR, I18N_ATTR_PREFIX, I18nMeta, hasI18nAttrs, icuFromI18nMessage, metaFromI18nMessage, parseI18nMeta} from './util';
1717

18-
function setI18nRefs(html: html.Node & {i18n: i18n.AST}, i18n: i18n.Node) {
18+
function setI18nRefs(html: html.Node & {i18n?: i18n.AST}, i18n: i18n.Node) {
1919
html.i18n = i18n;
2020
}
2121

@@ -128,4 +128,4 @@ export function processI18nMeta(
128128
new I18nMetaVisitor(interpolationConfig, /* keepI18nAttrs */ false),
129129
htmlAstWithErrors.rootNodes),
130130
htmlAstWithErrors.errors);
131-
}
131+
}

packages/compiler/src/render3/view/i18n/util.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,8 @@ export function assembleBoundTextPlaceholders(
165165
meta instanceof i18n.Message ? meta.nodes.find(node => node instanceof i18n.Container) : meta;
166166
if (node) {
167167
(node as i18n.Container)
168-
.children.filter((child: i18n.Node) => child instanceof i18n.Placeholder)
168+
.children
169+
.filter((child: i18n.Node): child is i18n.Placeholder => child instanceof i18n.Placeholder)
169170
.forEach((child: i18n.Placeholder, idx: number) => {
170171
const content = wrapI18nPlaceholder(startIdx + idx, contextId);
171172
updatePlaceholderMap(placeholders, child.name, content);

packages/compiler/src/render3/view/template.ts

+7-5
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,7 @@ export class TemplateDefinitionBuilder implements t.Visitor<void>, LocalResolver
177177
this._valueConverter = new ValueConverter(
178178
constantPool, () => this.allocateDataSlot(),
179179
(numSlots: number) => this.allocatePureFunctionSlots(numSlots),
180-
(name, localName, slot, value: o.ReadVarExpr) => {
180+
(name, localName, slot, value: o.Expression) => {
181181
const pipeType = pipeTypeByName.get(name);
182182
if (pipeType) {
183183
this.pipes.add(pipeType);
@@ -1517,7 +1517,7 @@ const SHARED_CONTEXT_KEY = '$$shared_ctx$$';
15171517
* declaration should always come before the local ref declaration.
15181518
*/
15191519
type BindingData = {
1520-
retrievalLevel: number; lhs: o.ReadVarExpr; declareLocalCallback?: DeclareLocalVarCallback;
1520+
retrievalLevel: number; lhs: o.Expression; declareLocalCallback?: DeclareLocalVarCallback;
15211521
declare: boolean;
15221522
priority: number;
15231523
localRef: boolean;
@@ -1593,7 +1593,7 @@ export class BindingScope implements LocalResolver {
15931593
* @param declareLocalCallback The callback to invoke when declaring this local var
15941594
* @param localRef Whether or not this is a local ref
15951595
*/
1596-
set(retrievalLevel: number, name: string, lhs: o.ReadVarExpr,
1596+
set(retrievalLevel: number, name: string, lhs: o.Expression,
15971597
priority: number = DeclarationPriority.DEFAULT,
15981598
declareLocalCallback?: DeclareLocalVarCallback, localRef?: true): BindingScope {
15991599
if (this.map.has(name)) {
@@ -1644,12 +1644,14 @@ export class BindingScope implements LocalResolver {
16441644
if (!this.map.has(bindingKey)) {
16451645
this.generateSharedContextVar(retrievalLevel);
16461646
}
1647-
return this.map.get(bindingKey) !.lhs;
1647+
// Shared context variables are always generated as "ReadVarExpr".
1648+
return this.map.get(bindingKey) !.lhs as o.ReadVarExpr;
16481649
}
16491650

16501651
getSharedContextName(retrievalLevel: number): o.ReadVarExpr|null {
16511652
const sharedCtxObj = this.map.get(SHARED_CONTEXT_KEY + retrievalLevel);
1652-
return sharedCtxObj && sharedCtxObj.declare ? sharedCtxObj.lhs : null;
1653+
// Shared context variables are always generated as "ReadVarExpr".
1654+
return sharedCtxObj && sharedCtxObj.declare ? sharedCtxObj.lhs as o.ReadVarExpr : null;
16531655
}
16541656

16551657
maybeGenerateSharedContextVar(value: BindingData) {

packages/compiler/src/render3/view/util.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -62,14 +62,14 @@ export function temporaryAllocator(statements: o.Statement[], name: string): ()
6262
}
6363

6464

65-
export function unsupported(feature: string): never {
65+
export function unsupported(this: void|Function, feature: string): never {
6666
if (this) {
6767
throw new Error(`Builder ${this.constructor.name} doesn't support ${feature} yet`);
6868
}
6969
throw new Error(`Feature ${feature} is not supported yet`);
7070
}
7171

72-
export function invalid<T>(arg: o.Expression | o.Statement | t.Node): never {
72+
export function invalid<T>(this: t.Visitor, arg: o.Expression | o.Statement | t.Node): never {
7373
throw new Error(
7474
`Invalid state: Visitor ${this.constructor.name} doesn't handle ${arg.constructor.name}`);
7575
}

packages/compiler/src/template_parser/template_ast.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -359,7 +359,7 @@ export class RecursiveTemplateAstVisitor extends NullTemplateVisitor implements
359359
if (children && children.length) results.push(templateVisitAll(t, children, context));
360360
}
361361
cb(visit);
362-
return [].concat.apply([], results);
362+
return Array.prototype.concat.apply([], results);
363363
}
364364
}
365365

0 commit comments

Comments
 (0)