Skip to content

Commit

Permalink
chore(td-tools): enable eslint/strict-boolean-expressions and strictN…
Browse files Browse the repository at this point in the history
…ullChecks (#1077)

* chore: enable eslint/strict-boolean-expressions and strictNullChecks

* fix package examples

* fix package td-tools

Note: leave out  asset-interface-description.ts because of #1052

* Update packages/td-tools/src/td-parser.ts

Co-authored-by: Jan Romann <[email protected]>

* Update packages/td-tools/src/td-parser.ts

Co-authored-by: Jan Romann <[email protected]>

* Update packages/td-tools/src/td-parser.ts

Co-authored-by: Jan Romann <[email protected]>

* Update packages/td-tools/src/thing-model-helpers.ts

Co-authored-by: Jan Romann <[email protected]>

* Update packages/td-tools/src/thing-model-helpers.ts

Co-authored-by: Jan Romann <[email protected]>

* Update packages/td-tools/src/thing-model-helpers.ts

Co-authored-by: Jan Romann <[email protected]>

* Update packages/td-tools/src/thing-model-helpers.ts

Co-authored-by: Jan Romann <[email protected]>

* fix: issue issue introduced by commenting

* move eslint settings to td-tools package only

* fix: lint errors for AID

* refactor: revert changes in package "examples"

* refactor: revert "!== undefined" to "!= null"

OR "=== undefined" to "== null"

* refactor: revert some changes proposed by @JKRhb

leads to different results

* fix: wrong conversion

* refactor: missing one undefined change

* fix: add proper boolean check

* Update packages/td-tools/src/thing-model-helpers.ts

Co-authored-by: Cristiano Aguzzi <[email protected]>

* Update packages/td-tools/src/util/asset-interface-description.ts

Co-authored-by: Cristiano Aguzzi <[email protected]>

* Update packages/td-tools/src/util/asset-interface-description.ts

Co-authored-by: Cristiano Aguzzi <[email protected]>

* Update packages/td-tools/src/util/asset-interface-description.ts

Co-authored-by: Cristiano Aguzzi <[email protected]>

* Update packages/td-tools/src/util/asset-interface-description.ts

Co-authored-by: Cristiano Aguzzi <[email protected]>

* Update packages/td-tools/src/util/asset-interface-description.ts

Co-authored-by: Cristiano Aguzzi <[email protected]>

* Update packages/td-tools/src/util/asset-interface-description.ts

Co-authored-by: Cristiano Aguzzi <[email protected]>

* refactor: further simplifications

* Update packages/td-tools/src/td-parser.ts

Co-authored-by: Jan Romann <[email protected]>

* refactor: simplify data.version.model check

---------

Co-authored-by: Jan Romann <[email protected]>
Co-authored-by: Cristiano Aguzzi <[email protected]>
  • Loading branch information
3 people authored Sep 22, 2023
1 parent 27970b0 commit a38b05b
Show file tree
Hide file tree
Showing 7 changed files with 75 additions and 76 deletions.
5 changes: 4 additions & 1 deletion packages/td-tools/.eslintrc.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
{
"extends": "../../.eslintrc.js",
"ignorePatterns": "webpack.config.js"
"ignorePatterns": "webpack.config.js",
"rules": {
"@typescript-eslint/strict-boolean-expressions": ["error"]
}
}
22 changes: 11 additions & 11 deletions packages/td-tools/src/td-parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ export function parseTD(td: string, normalize?: boolean): Thing {
throw new Error(`Form of Property '${propName}' has no href field`);
}
// check if base field required
if (!isAbsoluteUrl(form.href) && !thing.base)
if (!isAbsoluteUrl(form.href) && thing.base == null)
throw new Error(`Form of Property '${propName}' has relative URI while TD has no base field`);
// add
allForms.push(form);
Expand All @@ -176,7 +176,7 @@ export function parseTD(td: string, normalize?: boolean): Thing {
throw new Error(`Form of Action '${actName}' has no href field`);
}
// check if base field required
if (!isAbsoluteUrl(form.href) && !thing.base)
if (!isAbsoluteUrl(form.href) && thing.base == null)
throw new Error(`Form of Action '${actName}' has relative URI while TD has no base field`);
// add
allForms.push(form);
Expand All @@ -194,7 +194,7 @@ export function parseTD(td: string, normalize?: boolean): Thing {
throw new Error(`Form of Event '${evtName}' has no href field`);
}
// check if base field required
if (!isAbsoluteUrl(form.href) && !thing.base)
if (!isAbsoluteUrl(form.href) && thing.base == null)
throw new Error(`Form of Event '${evtName}' has relative URI while TD has no base field`);
// add
allForms.push(form);
Expand Down Expand Up @@ -222,20 +222,20 @@ export function serializeTD(thing: Thing): string {
const copy = JSON.parse(JSON.stringify(thing));

// clean-ups
if (!copy.security || copy.security.length === 0) {
if (copy.security == null || copy.security.length === 0) {
copy.securityDefinitions = {
nosec_sc: { scheme: "nosec" },
};
copy.security = ["nosec_sc"];
}

if (copy.forms && copy.forms.length === 0) {
if (copy.forms?.length === 0) {
delete copy.forms;
}

if (copy.properties && Object.keys(copy.properties).length === 0) {
if (copy.properties != null && Object.keys(copy.properties).length === 0) {
delete copy.properties;
} else if (copy.properties) {
} else if (copy.properties != null) {
// add mandatory fields (if missing): observable, writeOnly, and readOnly
for (const propName in copy.properties) {
const prop = copy.properties[propName];
Expand All @@ -251,9 +251,9 @@ export function serializeTD(thing: Thing): string {
}
}

if (copy.actions && Object.keys(copy.actions).length === 0) {
if (copy.actions != null && Object.keys(copy.actions).length === 0) {
delete copy.actions;
} else if (copy.actions) {
} else if (copy.actions != null) {
// add mandatory fields (if missing): idempotent and safe
for (const actName in copy.actions) {
const act = copy.actions[actName];
Expand All @@ -265,11 +265,11 @@ export function serializeTD(thing: Thing): string {
}
}
}
if (copy.events && Object.keys(copy.events).length === 0) {
if (copy.events != null && Object.keys(copy.events).length === 0) {
delete copy.events;
}

if (copy.links && copy.links.length === 0) {
if (copy?.links.length === 0) {
delete copy.links;
}

Expand Down
2 changes: 1 addition & 1 deletion packages/td-tools/src/thing-description.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ export class Form implements TDT.FormElementBase {

constructor(href: string, contentType?: string) {
this.href = href;
if (contentType) this.contentType = contentType;
if (contentType != null) this.contentType = contentType;
}
}
export interface ExpectedResponse {
Expand Down
38 changes: 15 additions & 23 deletions packages/td-tools/src/thing-model-helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ export class ThingModelHelpers {
}
if ("links" in data && Array.isArray(data.links)) {
const foundTmExtendsRel = data.links.find((link) => link.rel === "tm:extends");
if (foundTmExtendsRel) return true;
if (foundTmExtendsRel != null) return true;
}

if (data.properties !== undefined) {
Expand All @@ -136,17 +136,9 @@ export class ThingModelHelpers {
* @experimental
*/
public static getModelVersion(data: ThingModel): string | undefined {
if (
"version" in data &&
data.version &&
typeof data.version === "object" &&
"model" in data.version &&
typeof data.version.model === "string"
) {
return data.version.model;
} else {
return undefined;
}
return typeof data?.version === "object" && typeof data?.version?.model === "string"
? data.version.model
: undefined;
}

/**
Expand Down Expand Up @@ -241,7 +233,7 @@ export class ThingModelHelpers {
case "http": {
return new Promise((resolve, reject) => {
http.get(uri, (res) => {
if (!res.statusCode || res.statusCode !== 200) {
if (res.statusCode == null || res.statusCode !== 200) {
reject(new Error(`http status code not 200 but ${res.statusCode} for ${uri}`));
}

Expand All @@ -268,7 +260,7 @@ export class ThingModelHelpers {
return new Promise((resolve, reject) => {
https
.get(uri, (res) => {
if (!res.statusCode || res.statusCode !== 200) {
if (res.statusCode == null || res.statusCode !== 200) {
reject(new Error(`https status code not 200 but ${res.statusCode} for ${uri}`));
}

Expand Down Expand Up @@ -343,7 +335,7 @@ export class ThingModelHelpers {
for (const aff in affRefs) {
const affUri = affRefs[aff] as string;
const refObj = this.parseTmRef(affUri);
if (!refObj.uri) {
if (refObj.uri == null) {
throw new Error(`Missing remote path in ${affUri}`);
}
let source = await this.fetchModel(refObj.uri);
Expand Down Expand Up @@ -376,7 +368,7 @@ export class ThingModelHelpers {
if (!options) {
options = {} as CompositionOptions;
}
if (!options.baseUrl) {
if (options.baseUrl == null) {
options.baseUrl = ".";
}
const newTMHref = this.returnNewTMHref(options.baseUrl, title);
Expand Down Expand Up @@ -405,7 +397,7 @@ export class ThingModelHelpers {

for (const key in submodelObj) {
const sub = submodelObj[key];
if (options.selfComposition) {
if (options.selfComposition === true) {
if (!data.links) {
throw new Error(
"You used self composition but links are missing; they are needed to extract the instance name"
Expand All @@ -415,7 +407,7 @@ export class ThingModelHelpers {
const index = data.links.findIndex((el) => el.href === key);
const el = data.links[index];
const instanceName = el.instanceName;
if (!instanceName) {
if (instanceName == null) {
throw new Error("Self composition is not possible without instance names");
}
// self composition enabled, just one TD expected
Expand Down Expand Up @@ -449,7 +441,7 @@ export class ThingModelHelpers {
}
}
}
if (!data.links || options.selfComposition) {
if (!data.links || options.selfComposition === true) {
data.links = [];
}
// add reference to the thing model
Expand All @@ -475,7 +467,7 @@ export class ThingModelHelpers {

private static getThingModelRef(data: Record<string, unknown>): Record<string, unknown> {
const refs = {} as Record<string, unknown>;
if (!data) {
if (data == null) {
return refs;
}
for (const key in data) {
Expand Down Expand Up @@ -508,7 +500,7 @@ export class ThingModelHelpers {
extendedModel.properties = {};
}
for (const key in properties) {
if (dest.properties && dest.properties[key]) {
if (dest.properties && dest.properties[key] != null) {
extendedModel.properties[key] = { ...properties[key], ...dest.properties[key] };
} else {
extendedModel.properties[key] = properties[key];
Expand Down Expand Up @@ -625,7 +617,7 @@ export class ThingModelHelpers {
keys = keys.map((el) => el.replace("{{", "").replace("}}", ""));
let isValid = true;
let errors;
if (keys && keys.length > 0 && (map === undefined || map === null)) {
if (keys?.length > 0 && (map === undefined || map === null)) {
isValid = false;
errors = `No map provided for model ${model.title}`;
} else if (keys.length > 0) {
Expand Down Expand Up @@ -660,7 +652,7 @@ export class ThingModelHelpers {
}

private removeDependency(dep?: string) {
if (dep) {
if (dep != null) {
this.deps = this.deps.filter((el) => el !== dep);
} else {
this.deps.pop();
Expand Down
Loading

0 comments on commit a38b05b

Please sign in to comment.