-
Notifications
You must be signed in to change notification settings - Fork 195
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
Refactor ts files parser utils #756
Refactor ts files parser utils #756
Conversation
I aim to improve the type-safety across the complete codebase in iterations. In this PR, the type of a few properties in 8/13 files of |
let atoms = []; | ||
// interface Atoms {index: number; atom: string; hbondDistanceSq: number; hbondOther: any; hetflag:any} | ||
|
||
export interface Atom { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
AtomSpec is already defined in spec.ts. Please do not define a duplicate type.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you pointing it out, I was not aware of it mostly because it was not used anywhere in utils
.
Additionally, I have also added some of the missing types in AtomSpec
. Currently, all the properties are optional, which shouldn't be the case, but as I further refactor other files and get better types for defining the properties, I'll update them in subsequent commits/PRs
src/parsers/utils/atomNameToElem.ts
Outdated
} | ||
if (elem.length > 1) { | ||
elem = elem[0].toUpperCase() + elem.substring(1).toLowerCase(); | ||
if (typeof bondTable[elem] === "undefined") { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why not directly compare to undefined? The typeof comparison is left over from javascript where undefined was not a reserved keyword (not true in TypeScript).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I believe undefined
has always been a primitive type in JS(reserved keyword). Nonetheless, I've updated the condition(s) to compare to undefined
directly instead of using typeof
(I might as well want to make these changes in the other PR #754, I will let you know once I push the changes there).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I forgot the other reason to use typeof is it doesn't throw an error if the thing doesn't exist. So this should be replaced with direct comparisons to undefined with care.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Indeed, you are right. If an undeclared variable is compared to an undefined
primitive, it would throw an error whereas typeof
would safely return a boolean
.
💫 Changelog
⭐ Improved typesafety in TS files located in
parser/utils
.⭐ Changed
var
tolet
andconst
as applicable.⭐ Moved constant value identifiers outside of function definition to avoid unnecessary re-initialization.
⭐ Added strict equality check