Skip to content

Commit 37d3661

Browse files
authored
Activate strictBindCallApply and strictFunctionTypes compiler rules (#2880)
* Fix firebase strictFunctionTypes violation * Activate strictBindCallApply and strictFunctionTypes compiler rules - Improves types in api-channel and slot-consumer - Introduce option types for startRender and stopRender - Fix mismatched Map/object types in providedSlots - Use specific types in Literalizer
1 parent 9de8fd1 commit 37d3661

File tree

7 files changed

+45
-17
lines changed

7 files changed

+45
-17
lines changed

config/tsconfig.base.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
"noImplicitThis": true,
1212
"module": "es2015",
1313
"moduleResolution": "Node",
14+
"strictBindCallApply": true,
15+
"strictFunctionTypes": true,
1416

1517
"allowJs": false,
1618
"sourceMap": true,

src/runtime/api-channel.ts

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,15 @@ import {Arc} from './arc.js';
1414
import {DevtoolsConnection} from './debug/devtools-connection.js';
1515
import {OuterPortAttachment} from './debug/outer-port-attachment.js';
1616
import {Handle} from './handle.js';
17-
import {ParticleSpec} from './particle-spec.js';
17+
import {ParticleSpec, SerializedParticleSpec} from './particle-spec.js';
1818
import {Particle} from './particle.js';
1919
import * as recipeHandle from './recipe/handle.js';
2020
import * as recipeParticle from './recipe/particle.js';
2121
import {StorageProxy} from './storage-proxy.js';
2222
import {SerializedModelEntry} from './storage/crdt-collection-model.js';
2323
import {StorageProviderBase} from './storage/storage-provider-base.js';
24-
import {Type} from './type.js';
25-
import {PropagatedException} from './arc-exceptions.js';
24+
import {Type, TypeLiteral} from './type.js';
25+
import {PropagatedException, SerializedPropagatedException} from './arc-exceptions.js';
2626

2727
enum MappingType {Mapped, LocalMapped, RemoteMapped, Direct, ObjectMap, List, ByLiteral}
2828

@@ -32,15 +32,25 @@ interface MappingInfo {
3232
redundant?: boolean;
3333
value?: MappingInfo;
3434
key?: MappingInfo;
35-
converter?: Literalizer;
35+
converter?: Literalizer | LiteralizerParticleSpec | LiteralizerPropagatedException;
3636
identifier?: boolean;
3737
ignore?: boolean;
3838
}
3939

4040
// TODO(shans): are there better types that I can use for this?
4141
interface Literalizer {
42-
prototype: {toLiteral: () => {}};
43-
fromLiteral: ({}) => {};
42+
prototype: {toLiteral: () => TypeLiteral};
43+
fromLiteral: (typeliteral: TypeLiteral) => Type;
44+
}
45+
46+
interface LiteralizerParticleSpec {
47+
prototype: {toLiteral: () => SerializedParticleSpec};
48+
fromLiteral: (spec: SerializedParticleSpec) => ParticleSpec;
49+
}
50+
51+
interface LiteralizerPropagatedException {
52+
prototype: {toLiteral: () => SerializedPropagatedException};
53+
fromLiteral: (exception: SerializedPropagatedException) => PropagatedException;
4454
}
4555

4656
const targets = new Map<{}, Map<string, MappingInfo[]>>();
@@ -67,7 +77,7 @@ function Mapped(target: {}, propertyKey: string, parameterIndex: number) {
6777
set(target.constructor, propertyKey, parameterIndex, {type: MappingType.Mapped});
6878
}
6979

70-
function ByLiteral(constructor: Literalizer) {
80+
function ByLiteral(constructor: Literalizer | LiteralizerParticleSpec | LiteralizerPropagatedException) {
7181
return (target: {}, propertyKey: string, parameterIndex: number) => {
7282
const info: MappingInfo = {type: MappingType.ByLiteral, converter: constructor};
7383
set(target.constructor, propertyKey, parameterIndex, info);
@@ -415,7 +425,7 @@ export abstract class PECOuterPort extends APIPort {
415425
UIEvent(@Mapped particle: recipeParticle.Particle, @Direct slotName: string, @Direct event: {}) {}
416426
SimpleCallback(@RemoteMapped callback: number, @Direct data: {}) {}
417427
AwaitIdle(@Direct version: number) {}
418-
StartRender(@Mapped particle: recipeParticle.Particle, @Direct slotName: string, @ObjectMap(MappingType.Direct, MappingType.Direct) providedSlots: {[index: string]: string}, @List(MappingType.Direct) contentTypes: string[]) {}
428+
StartRender(@Mapped particle: recipeParticle.Particle, @Direct slotName: string, @ObjectMap(MappingType.Direct, MappingType.Direct) providedSlots: Map<string, string>, @List(MappingType.Direct) contentTypes: string[]) {}
419429
StopRender(@Mapped particle: recipeParticle.Particle, @Direct slotName: string) {}
420430

421431
abstract onRender(particle: recipeParticle.Particle, slotName: string, content: string);

src/runtime/arc-exceptions.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import {Particle} from './particle';
1010
* http://polymer.github.io/PATENTS.txt
1111
*/
1212

13-
type SerializedPropagatedException = {
13+
export type SerializedPropagatedException = {
1414
exceptionType: string,
1515
cause: {name: string, message: string, stack: string}, // Serialized Error.
1616
method: string,

src/runtime/particle-execution-host.ts

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,18 @@ import {SlotComposer} from './slot-composer.js';
2121
import {StorageProviderBase} from './storage/storage-provider-base.js';
2222
import {Type} from './type.js';
2323

24+
export type StartRenderOptions = {
25+
particle: Particle;
26+
slotName: string;
27+
providedSlots: Map<string, string>;
28+
contentTypes: string[];
29+
};
30+
31+
export type StopRenderOptions = {
32+
particle: Particle;
33+
slotName: string;
34+
};
35+
2436
export class ParticleExecutionHost {
2537
private _apiPort : PECOuterPort;
2638
close : () => void;
@@ -281,11 +293,11 @@ export class ParticleExecutionHost {
281293
this._apiPort.InstantiateParticle(particle, particle.id.toString(), particle.spec, stores);
282294
}
283295

284-
startRender({particle, slotName, providedSlots, contentTypes}: {particle: Particle, slotName: string, providedSlots: {[index: string]: string}, contentTypes: string[]}) {
296+
startRender({particle, slotName, providedSlots, contentTypes}: StartRenderOptions): void {
285297
this._apiPort.StartRender(particle, slotName, providedSlots, contentTypes);
286298
}
287299

288-
stopRender({particle, slotName}: {particle: Particle, slotName: string}) {
300+
stopRender({particle, slotName}: StopRenderOptions): void {
289301
this._apiPort.StopRender(particle, slotName);
290302
}
291303

src/runtime/particle-spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ export class ProvideSlotConnectionSpec {
144144
}
145145
}
146146

147-
type SerializedParticleSpec = {
147+
export type SerializedParticleSpec = {
148148
name: string,
149149
id?: string,
150150
verbs: string[],

src/runtime/slot-consumer.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,10 @@ import {assert} from '../platform/assert-web.js';
1212

1313
import {Arc} from './arc.js';
1414
import {Description} from './description.js';
15+
import {Particle} from './recipe/particle.js';
1516
import {SlotConnection} from './recipe/slot-connection.js';
1617
import {HostedSlotContext, ProvidedSlotContext, SlotContext} from './slot-context.js';
18+
import {StartRenderOptions, StopRenderOptions} from './particle-execution-host.js';
1719

1820
export interface Content {
1921
templateName?: string | Map<string, string>;
@@ -41,8 +43,8 @@ export class SlotConsumer {
4143
slotContext: SlotContext;
4244
readonly directlyProvidedSlotContexts: ProvidedSlotContext[] = [];
4345
readonly hostedSlotContexts: HostedSlotContext[] = [];
44-
startRenderCallback: ({}) => void;
45-
stopRenderCallback: ({}) => void;
46+
startRenderCallback: (options: StartRenderOptions) => void;
47+
stopRenderCallback: (options: StopRenderOptions) => void;
4648
eventHandler: ({}) => void;
4749
readonly containerKind?: string;
4850
// Contains `container` and other modality specific rendering information
@@ -149,10 +151,12 @@ export class SlotConsumer {
149151

150152
startRender() {
151153
if (this.consumeConn && this.startRenderCallback) {
154+
const providedSlots = new Map(this.allProvidedSlotContexts.map(context => ([context.name, context.id] as [string, string])));
155+
152156
this.startRenderCallback({
153157
particle: this.consumeConn.particle,
154158
slotName: this.consumeConn.name,
155-
providedSlots: new Map(this.allProvidedSlotContexts.map(context => ([context.name, context.id] as [string, string]))),
159+
providedSlots,
156160
contentTypes: this.constructRenderRequest()
157161
});
158162
}

src/runtime/storage/firebase/firebase-storage.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -374,7 +374,7 @@ class FirebaseVariable extends FirebaseStorageProvider implements VariableStorag
374374
private pendingWrites: {storageKey: string, value: {}}[] = [];
375375
wasConnect: boolean; // for debugging
376376
private resolveInitialized: () => void;
377-
private readonly valueChangeCallback: ({}) => void;
377+
private readonly valueChangeCallback: (dataSnapshot: firebase.database.DataSnapshot, s?: string) => void;
378378

379379
constructor(type, storageEngine, id, reference, firebaseKey, shouldExist) {
380380
super(type, storageEngine, id, reference, firebaseKey);
@@ -651,7 +651,7 @@ class FirebaseCollection extends FirebaseStorageProvider implements CollectionSt
651651
private pendingWrites: {value: {}, storageKey: string}[] = [];
652652
private resolveInitialized: () => void;
653653
private localKeyId = Date.now();
654-
private readonly valueChangeCallback: ({}) => void;
654+
private readonly valueChangeCallback: (dataSnapshot: firebase.database.DataSnapshot, s?: string) => void;
655655

656656
constructor(type, storageEngine, id, reference, firebaseKey) {
657657
super(type, storageEngine, id, reference, firebaseKey);

0 commit comments

Comments
 (0)