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

chore(td-tools): enable eslint/strict-boolean-expressions and strictNullChecks #1077

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
ed9c7a2
chore: enable eslint/strict-boolean-expressions and strictNullChecks
danielpeintner Sep 8, 2023
af18392
fix package examples
danielpeintner Sep 8, 2023
9a667e3
fix package td-tools
danielpeintner Sep 8, 2023
59d04b9
Update packages/td-tools/src/td-parser.ts
danielpeintner Sep 12, 2023
f7be620
Update packages/td-tools/src/td-parser.ts
danielpeintner Sep 12, 2023
c574913
Update packages/td-tools/src/td-parser.ts
danielpeintner Sep 12, 2023
ea1dc82
Update packages/td-tools/src/thing-model-helpers.ts
danielpeintner Sep 12, 2023
58198a2
Update packages/td-tools/src/thing-model-helpers.ts
danielpeintner Sep 12, 2023
d1f6ee6
Update packages/td-tools/src/thing-model-helpers.ts
danielpeintner Sep 12, 2023
10fc13b
Update packages/td-tools/src/thing-model-helpers.ts
danielpeintner Sep 12, 2023
6b05ce0
Merge branch 'master' into issue-1046-strict-boolean-expressions
danielpeintner Sep 14, 2023
c641193
fix: issue issue introduced by commenting
danielpeintner Sep 14, 2023
6b4b010
move eslint settings to td-tools package only
danielpeintner Sep 14, 2023
4204a90
fix: lint errors for AID
danielpeintner Sep 14, 2023
100bdde
refactor: revert changes in package "examples"
danielpeintner Sep 20, 2023
88330e2
refactor: revert "!== undefined" to "!= null"
danielpeintner Sep 20, 2023
a04d8c7
refactor: revert some changes proposed by @JKRhb
danielpeintner Sep 20, 2023
25d5390
fix: wrong conversion
danielpeintner Sep 20, 2023
e4c6590
refactor: missing one undefined change
danielpeintner Sep 20, 2023
9954040
fix: add proper boolean check
danielpeintner Sep 20, 2023
f5b386b
Update packages/td-tools/src/thing-model-helpers.ts
danielpeintner Sep 21, 2023
49c7bc7
Update packages/td-tools/src/util/asset-interface-description.ts
danielpeintner Sep 21, 2023
0a85399
Update packages/td-tools/src/util/asset-interface-description.ts
danielpeintner Sep 21, 2023
3e65aa0
Update packages/td-tools/src/util/asset-interface-description.ts
danielpeintner Sep 21, 2023
f10a4a0
Update packages/td-tools/src/util/asset-interface-description.ts
danielpeintner Sep 21, 2023
dcb16c3
Update packages/td-tools/src/util/asset-interface-description.ts
danielpeintner Sep 21, 2023
6a496d4
Update packages/td-tools/src/util/asset-interface-description.ts
danielpeintner Sep 21, 2023
3d78e8e
refactor: further simplifications
danielpeintner Sep 21, 2023
fecd6e4
Update packages/td-tools/src/td-parser.ts
danielpeintner Sep 21, 2023
7762ea4
refactor: simplify data.version.model check
danielpeintner Sep 21, 2023
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
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) {
JKRhb marked this conversation as resolved.
Show resolved Hide resolved
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
Loading