fix(ngc): codegen allows --strictNullChecks (#10991)
This commit is contained in:
parent
8560e1e4bf
commit
01111b04ff
|
@ -250,12 +250,13 @@ export abstract class AbstractEmitterVisitor implements o.StatementVisitor, o.Ex
|
||||||
ctx.print(`)`);
|
ctx.print(`)`);
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
visitLiteralExpr(ast: o.LiteralExpr, ctx: EmitterVisitorContext): any {
|
visitLiteralExpr(ast: o.LiteralExpr, ctx: EmitterVisitorContext, absentValue: string = 'null'):
|
||||||
|
any {
|
||||||
var value = ast.value;
|
var value = ast.value;
|
||||||
if (isString(value)) {
|
if (isString(value)) {
|
||||||
ctx.print(escapeSingleQuoteString(value, this._escapeDollarInStrings));
|
ctx.print(escapeSingleQuoteString(value, this._escapeDollarInStrings));
|
||||||
} else if (isBlank(value)) {
|
} else if (isBlank(value)) {
|
||||||
ctx.print('null');
|
ctx.print(absentValue);
|
||||||
} else {
|
} else {
|
||||||
ctx.print(`${value}`);
|
ctx.print(`${value}`);
|
||||||
}
|
}
|
||||||
|
|
|
@ -72,6 +72,10 @@ class _TsEmitterVisitor extends AbstractEmitterVisitor implements o.TypeVisitor
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
visitLiteralExpr(ast: o.LiteralExpr, ctx: EmitterVisitorContext): any {
|
||||||
|
super.visitLiteralExpr(ast, ctx, '(null as any)');
|
||||||
|
}
|
||||||
|
|
||||||
visitExternalExpr(ast: o.ExternalExpr, ctx: EmitterVisitorContext): any {
|
visitExternalExpr(ast: o.ExternalExpr, ctx: EmitterVisitorContext): any {
|
||||||
this._visitIdentifier(ast.value, ast.typeParams, ctx);
|
this._visitIdentifier(ast.value, ast.typeParams, ctx);
|
||||||
return null;
|
return null;
|
||||||
|
|
|
@ -290,35 +290,41 @@ export function main() {
|
||||||
|
|
||||||
it('should support builtin types', () => {
|
it('should support builtin types', () => {
|
||||||
var writeVarExpr = o.variable('a').set(o.NULL_EXPR);
|
var writeVarExpr = o.variable('a').set(o.NULL_EXPR);
|
||||||
expect(emitStmt(writeVarExpr.toDeclStmt(o.DYNAMIC_TYPE))).toEqual('var a:any = null;');
|
expect(emitStmt(writeVarExpr.toDeclStmt(o.DYNAMIC_TYPE)))
|
||||||
expect(emitStmt(writeVarExpr.toDeclStmt(o.BOOL_TYPE))).toEqual('var a:boolean = null;');
|
.toEqual('var a:any = (null as any);');
|
||||||
expect(emitStmt(writeVarExpr.toDeclStmt(o.INT_TYPE))).toEqual('var a:number = null;');
|
expect(emitStmt(writeVarExpr.toDeclStmt(o.BOOL_TYPE)))
|
||||||
expect(emitStmt(writeVarExpr.toDeclStmt(o.NUMBER_TYPE))).toEqual('var a:number = null;');
|
.toEqual('var a:boolean = (null as any);');
|
||||||
expect(emitStmt(writeVarExpr.toDeclStmt(o.STRING_TYPE))).toEqual('var a:string = null;');
|
expect(emitStmt(writeVarExpr.toDeclStmt(o.INT_TYPE)))
|
||||||
expect(emitStmt(writeVarExpr.toDeclStmt(o.FUNCTION_TYPE))).toEqual('var a:Function = null;');
|
.toEqual('var a:number = (null as any);');
|
||||||
|
expect(emitStmt(writeVarExpr.toDeclStmt(o.NUMBER_TYPE)))
|
||||||
|
.toEqual('var a:number = (null as any);');
|
||||||
|
expect(emitStmt(writeVarExpr.toDeclStmt(o.STRING_TYPE)))
|
||||||
|
.toEqual('var a:string = (null as any);');
|
||||||
|
expect(emitStmt(writeVarExpr.toDeclStmt(o.FUNCTION_TYPE)))
|
||||||
|
.toEqual('var a:Function = (null as any);');
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should support external types', () => {
|
it('should support external types', () => {
|
||||||
var writeVarExpr = o.variable('a').set(o.NULL_EXPR);
|
var writeVarExpr = o.variable('a').set(o.NULL_EXPR);
|
||||||
expect(emitStmt(writeVarExpr.toDeclStmt(o.importType(sameModuleIdentifier))))
|
expect(emitStmt(writeVarExpr.toDeclStmt(o.importType(sameModuleIdentifier))))
|
||||||
.toEqual('var a:someLocalId = null;');
|
.toEqual('var a:someLocalId = (null as any);');
|
||||||
expect(emitStmt(writeVarExpr.toDeclStmt(o.importType(externalModuleIdentifier)))).toEqual([
|
expect(emitStmt(writeVarExpr.toDeclStmt(o.importType(externalModuleIdentifier)))).toEqual([
|
||||||
`import * as import0 from 'somePackage/someOtherPath';`,
|
`import * as import0 from 'somePackage/someOtherPath';`,
|
||||||
`var a:import0.someExternalId = null;`
|
`var a:import0.someExternalId = (null as any);`
|
||||||
].join('\n'));
|
].join('\n'));
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should support combined types', () => {
|
it('should support combined types', () => {
|
||||||
var writeVarExpr = o.variable('a').set(o.NULL_EXPR);
|
var writeVarExpr = o.variable('a').set(o.NULL_EXPR);
|
||||||
expect(emitStmt(writeVarExpr.toDeclStmt(new o.ArrayType(null))))
|
expect(emitStmt(writeVarExpr.toDeclStmt(new o.ArrayType(null))))
|
||||||
.toEqual('var a:any[] = null;');
|
.toEqual('var a:any[] = (null as any);');
|
||||||
expect(emitStmt(writeVarExpr.toDeclStmt(new o.ArrayType(o.INT_TYPE))))
|
expect(emitStmt(writeVarExpr.toDeclStmt(new o.ArrayType(o.INT_TYPE))))
|
||||||
.toEqual('var a:number[] = null;');
|
.toEqual('var a:number[] = (null as any);');
|
||||||
|
|
||||||
expect(emitStmt(writeVarExpr.toDeclStmt(new o.MapType(null))))
|
expect(emitStmt(writeVarExpr.toDeclStmt(new o.MapType(null))))
|
||||||
.toEqual('var a:{[key: string]:any} = null;');
|
.toEqual('var a:{[key: string]:any} = (null as any);');
|
||||||
expect(emitStmt(writeVarExpr.toDeclStmt(new o.MapType(o.INT_TYPE))))
|
expect(emitStmt(writeVarExpr.toDeclStmt(new o.MapType(o.INT_TYPE))))
|
||||||
.toEqual('var a:{[key: string]:number} = null;');
|
.toEqual('var a:{[key: string]:number} = (null as any);');
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue