Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enable useDefineForClassFields TS flag #7563

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 7 additions & 7 deletions common/api/core-backend.api.md
Original file line number Diff line number Diff line change
Expand Up @@ -290,14 +290,14 @@ export abstract class AuxCoordSystem extends DefinitionElement {
// (undocumented)
description?: string;
// (undocumented)
type: number;
type?: number;
}

// @public
export class AuxCoordSystem2d extends AuxCoordSystem {
constructor(props: AuxCoordSystem2dProps, iModel: IModelDb);
// (undocumented)
angle: number;
angle?: number;
// (undocumented)
static get className(): string;
static createCode(iModel: IModelDb, scopeModelId: CodeScopeProps, codeValue: string): Code;
Expand All @@ -314,11 +314,11 @@ export class AuxCoordSystem3d extends AuxCoordSystem {
// (undocumented)
origin?: Point3d;
// (undocumented)
pitch: number;
pitch?: number;
// (undocumented)
roll: number;
roll?: number;
// (undocumented)
yaw: number;
yaw?: number;
}

// @public
Expand Down Expand Up @@ -3811,7 +3811,7 @@ export class LightLocation extends SpatialLocationElement {
protected constructor(props: LightLocationProps, iModel: IModelDb);
// (undocumented)
static get className(): string;
enabled: boolean;
enabled?: boolean;
}

// @public
Expand Down Expand Up @@ -4246,7 +4246,7 @@ export class Model extends Entity {
// @beta
protected static onUpdateElement(_arg: OnElementInModelPropsArg): void;
// (undocumented)
readonly parentModel: Id64String;
readonly parentModel?: Id64String;
// @internal (undocumented)
static get protectedOperations(): string[];
removeUserProperties(nameSpace: string): void;
Expand Down
2 changes: 0 additions & 2 deletions common/api/ecschema-metadata.api.md
Original file line number Diff line number Diff line change
Expand Up @@ -1418,8 +1418,6 @@ export class RelationshipClass extends ECClass {
// (undocumented)
fromJSONSync(relationshipClassProps: RelationshipClassProps): void;
// (undocumented)
readonly schema: Schema;
// (undocumented)
readonly schemaItemType = SchemaItemType.RelationshipClass;
// @alpha
protected setSourceConstraint(source: RelationshipConstraint): void;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"changes": [
{
"packageName": "@itwin/build-tools",
"comment": "Enabled `useDefineForClassFields` TypeScript config flag",
"type": "none"
}
],
"packageName": "@itwin/build-tools"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"changes": [
{
"packageName": "@itwin/core-backend",
"comment": "Changed `Model.parentModel`, `LightLocation.enabled` and some of the `AuxCoordSystem` properties to be optional",
"type": "none"
}
],
"packageName": "@itwin/core-backend"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"changes": [
{
"packageName": "@itwin/core-bentley",
"comment": "",
"type": "none"
}
],
"packageName": "@itwin/core-bentley"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"changes": [
{
"packageName": "@itwin/core-common",
"comment": "",
"type": "none"
}
],
"packageName": "@itwin/core-common"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"changes": [
{
"packageName": "@itwin/ecschema-metadata",
"comment": "",
"type": "none"
}
],
"packageName": "@itwin/ecschema-metadata"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"changes": [
{
"packageName": "@itwin/presentation-common",
"comment": "",
"type": "none"
}
],
"packageName": "@itwin/presentation-common"
}
37 changes: 30 additions & 7 deletions core/backend/src/Element.ts
Original file line number Diff line number Diff line change
Expand Up @@ -477,6 +477,7 @@ export abstract class GeometricElement extends Element {
super(props, iModel);
this.category = Id64.fromJSON(props.category);
this.geom = props.geom;
this.elementGeometryBuilderParams = props.elementGeometryBuilderParams;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This class field is interesting... @GytisCepk were there any test or build failures that prompted you to add this?

I did some debugging and elementGeometryBuilderParams definitely is not set by the Entity constructor, and it also doesn't get serialized in GeometricElement.toJSON(). However, the corresponding GeometricElementProps.elementGeometryBuilderParams member is used in a bunch of places...

@pmconne @bbastings is it possible this class field is just broken and has never been used? Should we remove it entirely? If we want to fix it instead, we should probably also make sure it gets included in toJSON()...

}

/** Type guard for instanceof [[GeometricElement3d]] */
Expand Down Expand Up @@ -707,7 +708,10 @@ export abstract class InformationReferenceElement extends InformationContentElem
export class Subject extends InformationReferenceElement {
public static override get className(): string { return "Subject"; }
public description?: string;
protected constructor(props: SubjectProps, iModel: IModelDb) { super(props, iModel); }
protected constructor(props: SubjectProps, iModel: IModelDb) {
super(props, iModel);
this.description = props.description;
}

public override toJSON(): SubjectProps { // This override only specializes the return type
return super.toJSON() as SubjectProps; // Entity.toJSON takes care of auto-handled properties
Expand Down Expand Up @@ -880,7 +884,11 @@ export class SheetBorderTemplate extends Document {
public static override get className(): string { return "SheetBorderTemplate"; }
public height?: number;
public width?: number;
protected constructor(props: SheetBorderTemplateProps, iModel: IModelDb) { super(props, iModel); }
protected constructor(props: SheetBorderTemplateProps, iModel: IModelDb) {
super(props, iModel);
this.height = props.height;
this.width = props.width;
}
}

/** The template for a [[Sheet]]
Expand All @@ -892,7 +900,12 @@ export class SheetTemplate extends Document {
public width?: number;
public border?: Id64String;

protected constructor(props: SheetTemplateProps, iModel: IModelDb) { super(props, iModel); }
protected constructor(props: SheetTemplateProps, iModel: IModelDb) {
super(props, iModel);
this.height = props.height;
this.width = props.width;
this.border = props.border;
}

protected override collectReferenceIds(referenceIds: EntityReferenceSet): void {
super.collectReferenceIds(referenceIds);
Expand Down Expand Up @@ -1053,7 +1066,10 @@ export abstract class TypeDefinitionElement extends DefinitionElement {
public static override get className(): string { return "TypeDefinitionElement"; }
public recipe?: RelatedElement;

protected constructor(props: TypeDefinitionElementProps, iModel: IModelDb) { super(props, iModel); }
protected constructor(props: TypeDefinitionElementProps, iModel: IModelDb) {
super(props, iModel);
this.recipe = this.recipe;
}

protected override collectReferenceIds(referenceIds: EntityReferenceSet): void {
super.collectReferenceIds(referenceIds);
Expand Down Expand Up @@ -1252,7 +1268,10 @@ export abstract class InformationPartitionElement extends InformationContentElem
/** A human-readable string describing the intent of the partition. */
public description?: string;

protected constructor(props: InformationPartitionElementProps, iModel: IModelDb) { super(props, iModel); }
protected constructor(props: InformationPartitionElementProps, iModel: IModelDb) {
super(props, iModel);
this.description = props.description;
}

public override toJSON(): InformationPartitionElementProps { // This override only specializes the return type
return super.toJSON() as InformationPartitionElementProps; // Entity.toJSON takes care of auto-handled properties
Expand Down Expand Up @@ -1483,9 +1502,13 @@ export class GeometryPart extends DefinitionElement {
export class LineStyle extends DefinitionElement {
public static override get className(): string { return "LineStyle"; }
public description?: string;
public data!: string;
public data: string;

protected constructor(props: LineStyleProps, iModel: IModelDb) { super(props, iModel); }
protected constructor(props: LineStyleProps, iModel: IModelDb) {
super(props, iModel);
this.description = props.description;
this.data = props.data;
}

/** Create a Code for a LineStyle definition given a name that is meant to be unique within the scope of the specified model.
* @param iModel The IModel
Expand Down
20 changes: 14 additions & 6 deletions core/backend/src/Model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,16 +69,18 @@ export class Model extends Entity {
public static override get className(): string { return "Model"; }
/** @internal */
public static override get protectedOperations() { return ["onInsert", "onUpdate", "onDelete"]; }
public readonly modeledElement!: RelatedElement;
public readonly modeledElement: RelatedElement;
public readonly name: string;
public readonly parentModel!: Id64String;
public readonly parentModel?: Id64String;
public readonly jsonProperties: { [key: string]: any };
public isPrivate: boolean;
public isTemplate: boolean;

protected constructor(props: ModelProps, iModel: IModelDb) {
super(props, iModel);
this.modeledElement = new RelatedElement(props.modeledElement);
this.name = props.name ? props.name : ""; // NB this isn't really a property of Model (it's the code.value of the modeled element), but it comes in ModelProps because it's often needed
this.parentModel = props.parentModel;
this.isPrivate = JsonUtils.asBool(props.isPrivate);
this.isTemplate = JsonUtils.asBool(props.isTemplate);
this.jsonProperties = { ...props.jsonProperties }; // make sure we have our own copy
Expand Down Expand Up @@ -233,11 +235,14 @@ export class Model extends Entity {
* @public
*/
export class GeometricModel extends Model {
public geometryGuid?: GuidString; // Initialized by the Entity constructor
public geometryGuid?: GuidString;

public static override get className(): string { return "GeometricModel"; }

protected constructor(props: GeometricModelProps, iModel: IModelDb) { super(props, iModel); }
protected constructor(props: GeometricModelProps, iModel: IModelDb) {
super(props, iModel);
this.geometryGuid = props.geometryGuid;
}

/** Query for the union of the extents of the elements contained by this model.
* @note This function blocks the JavaScript event loop. Consider using [[queryRange]] instead.
Expand Down Expand Up @@ -293,10 +298,13 @@ export abstract class GeometricModel3d extends GeometricModel {
*/
export abstract class GeometricModel2d extends GeometricModel {
/** The actual coordinates of (0,0) in modeling coordinates. An offset applied to all modeling coordinates. */
public globalOrigin?: Point2d; // Initialized by the Entity constructor
public globalOrigin?: Point2d;
public static override get className(): string { return "GeometricModel2d"; }

protected constructor(props: GeometricModel2dProps, iModel: IModelDb) { super(props, iModel); }
protected constructor(props: GeometricModel2dProps, iModel: IModelDb) {
super(props, iModel);
this.globalOrigin = props.globalOrigin ? Point2d.fromJSON(props.globalOrigin) : undefined;
}

public override toJSON(): GeometricModel2dProps {
const val = super.toJSON() as GeometricModel2dProps;
Expand Down
37 changes: 27 additions & 10 deletions core/backend/src/ViewDefinition.ts
Original file line number Diff line number Diff line change
Expand Up @@ -614,9 +614,13 @@ export class TemplateViewDefinition3d extends ViewDefinition3d {
*/
export abstract class AuxCoordSystem extends DefinitionElement {
public static override get className(): string { return "AuxCoordSystem"; }
public type!: number;
public type?: number;
public description?: string;
public constructor(props: AuxCoordSystemProps, iModel: IModelDb) { super(props, iModel); }
public constructor(props: AuxCoordSystemProps, iModel: IModelDb) {
super(props, iModel);
this.type = props.type;
this.description = props.description;
Comment on lines +621 to +622
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For fields like these (where the type annotation already matches what the Entity constructor was setting), why not just use the declare keyword instead?

}
}

/** A 2d auxiliary coordinate system.
Expand All @@ -625,8 +629,12 @@ export abstract class AuxCoordSystem extends DefinitionElement {
export class AuxCoordSystem2d extends AuxCoordSystem {
public static override get className(): string { return "AuxCoordSystem2d"; }
public origin?: Point2d;
public angle!: number;
public constructor(props: AuxCoordSystem2dProps, iModel: IModelDb) { super(props, iModel); }
public angle?: number;
public constructor(props: AuxCoordSystem2dProps, iModel: IModelDb) {
super(props, iModel);
this.origin = props.origin ? Point2d.fromJSON(props.origin) : undefined;
this.angle = props.angle ? Angle.fromJSON(props.angle).degrees : undefined;
}

/** Create a Code for a AuxCoordSystem2d element given a name that is meant to be unique within the scope of the specified DefinitionModel.
* @param iModel The IModelDb
Expand All @@ -645,10 +653,16 @@ export class AuxCoordSystem2d extends AuxCoordSystem {
export class AuxCoordSystem3d extends AuxCoordSystem {
public static override get className(): string { return "AuxCoordSystem3d"; }
public origin?: Point3d;
public yaw!: number;
public pitch!: number;
public roll!: number;
public constructor(props: AuxCoordSystem3dProps, iModel: IModelDb) { super(props, iModel); }
public yaw?: number;
public pitch?: number;
public roll?: number;
public constructor(props: AuxCoordSystem3dProps, iModel: IModelDb) {
super(props, iModel);
this.origin = props.origin ? Point3d.fromJSON(props.origin) : undefined;
this.yaw = props.yaw ? Angle.fromJSON(props.yaw).degrees : undefined;
this.pitch = props.pitch ? Angle.fromJSON(props.pitch).degrees : undefined;
this.roll = props.roll ? Angle.fromJSON(props.roll).degrees : undefined;
Comment on lines +661 to +664
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These are nice fixes. Frankly I'm surprised that no one has hit this in all these years... Can you also add unit tests that exercise this so we can avoid regressions as we refactor all these Entity subclasses?

}

/** Create a Code for a AuxCoordSystem3d element given a name that is meant to be unique within the scope of the specified DefinitionModel.
* @param iModel The IModelDb
Expand Down Expand Up @@ -700,7 +714,10 @@ export class ViewAttachment extends GraphicalElement2d {
export class LightLocation extends SpatialLocationElement {
public static override get className(): string { return "LightLocation"; }
/** Whether this light is currently turned on. */
public enabled!: boolean;
public enabled?: boolean;

protected constructor(props: LightLocationProps, iModel: IModelDb) { super(props, iModel); }
protected constructor(props: LightLocationProps, iModel: IModelDb) {
super(props, iModel);
this.enabled = props.enabled;
}
}
4 changes: 2 additions & 2 deletions core/backend/src/test/IModelTestUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ export interface TestRelationshipProps extends RelationshipProps {
}
export class TestElementDrivesElement extends ElementDrivesElement {
public static override get className(): string { return "TestElementDrivesElement"; }
public property1!: string;
declare public property1: string;
public static rootChanged = new BeEvent<(props: RelationshipProps, imodel: IModelDb) => void>();
public static deletedDependency = new BeEvent<(props: RelationshipProps, imodel: IModelDb) => void>();
public static override onRootChanged(props: RelationshipProps, imodel: IModelDb): void { this.rootChanged.raiseEvent(props, imodel); }
Expand All @@ -71,7 +71,7 @@ export interface TestPhysicalObjectProps extends PhysicalElementProps {
}
export class TestPhysicalObject extends PhysicalElement {
public static override get className(): string { return "TestPhysicalObject"; }
public intProperty!: number;
declare public intProperty: number;
public static beforeOutputsHandled = new BeEvent<(id: Id64String, imodel: IModelDb) => void>();
public static allInputsHandled = new BeEvent<(id: Id64String, imodel: IModelDb) => void>();
public static override onBeforeOutputsHandled(id: Id64String, imodel: IModelDb): void { this.beforeOutputsHandled.raiseEvent(id, imodel); }
Expand Down
4 changes: 2 additions & 2 deletions core/bentley/src/test/LRUMap.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -179,12 +179,12 @@ describe("LRUMap", () => {
c.set("a", 4);
assert.equal(c.size, 1);
assert.equal(c.newest, c.oldest);
assert.deepEqual(c.newest, { key: "a", value: 4 });
assert.deepEqual(c.newest, { key: "a", value: 4, newer: undefined, older: undefined });

c.set("a", 5);
assert.equal(c.size, 1);
assert.equal(c.newest, c.oldest);
assert.deepEqual(c.newest, { key: "a", value: 5 });
assert.deepEqual(c.newest, { key: "a", value: 5, newer: undefined, older: undefined });

c.set("b", 6);
assert.equal(c.size, 2);
Expand Down
2 changes: 1 addition & 1 deletion core/common/src/RpcInterface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ export abstract class RpcInterface {
}

/** @internal */
public configurationSupplier: RpcConfigurationSupplier | undefined;
declare public configurationSupplier: RpcConfigurationSupplier | undefined;
}

RpcInterface.prototype.configurationSupplier = undefined;
Expand Down
Loading
Loading