-
Notifications
You must be signed in to change notification settings - Fork 71
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 the project and add generics and getter options #213
Conversation
Thank you @petarvujovic98 !
These parts are great! Function refactor and variable renames also look good.
This seems already in #203 , would you mind wait that PR merge and doing a rebase? I believe except #203, there's no other PR that should merge before this one to avoid conflictions. |
Also, just a small suggestion: it'll be great to separate the refactor commit with the reformatting next time, to make code review easier |
@ailisp Of course, I'll wait for the generics merge and rebase this PR. |
@volovyks CI was not triggering for this PR, do you know why |
@ailisp that is because @petarvujovic98 is not a part of our GitHub org. I have started a discussion about it in Slack. |
@petarvujovic98 Sorry looks like a lot of conflictions are in! It may be faster to have your function refactor changes first. Then we can re-apply your formatting to new develop branch in another PR. |
@ailisp Sounds good. Will do the refactor on Monday |
Add ESLint and Prettier packages and configs Run ESLint and Prettier Add ESLint and Prettier scripts
Change overrides in ESLint
Add includeBytes function declaration Add helper for returning value from collection methods Add helper types Refactor code
Add defaultValue to methods that return values Add better type definitions Refactor code
Rename variables and parameters to JavaScript standards Refactor code
Improve type definitions for QJS environment Rename variables and parameters to JavaScript standards Refactor code
Improve readability of code Add comments for easier understanding Marginaly improve decorator type definitions
68d2e8b
to
dba601b
Compare
module.exports = { | ||
root: true, | ||
env: { | ||
es2021: true, |
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.
The underlying engine quickjs only support es2020: https://bellard.org/quickjs/ so we need es2020 here
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.
@ailisp This is just for the code we are writing, doesn't have to do anything with the target for the TypeScript compilation. So we are using newer features in code, or at least we could be, but then the TypeScript compiler will build them to whatever version we choose in the tsconfig.json
file.
README.md
Outdated
- if arguments more than params, remaining argument are ignored | ||
- if argument is different than the required type, it'll be coerced to required type | ||
- if argument is different than the required type but cannot be coerced, will throw runtime type error, also with message and stacktrace | ||
When call host function with inappropriate type, means incorrect number of arguments or arg is not expected type: - if arguments less than params, remaining argument are set as 'undefined' - if arguments more than params, remaining argument are ignored - if argument is different than the required type, it'll be coerced to required type - if argument is different than the required type but cannot be coerced, will throw runtime type error, also with message and stacktrace |
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.
This formatting seems problematic, it was a bulleted list
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.
@ailisp Thanks, haven't noticed it went wrong. Will fix formatting.
src/api.ts
Outdated
import { PromiseResult } from "./types"; | ||
|
||
const U64_MAX = 2n ** 64n - 1n; | ||
const EVICTED_REGISTER = U64_MAX - 1n; | ||
|
||
// Interface available in QuickJS | ||
interface Env { | ||
panic_utf8: (msg: string) => never; | ||
[x: string]: any; | ||
panic_utf8(msg: string): never; |
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.
Nice additions to the env interface!
most of the functions here should take Bytes instead of string, please referring to the implementation function below. For example,
export function panicUtf8(msg: Bytes): never {
env.panic_utf8(msg);
}
so env.panic_utf8 should take a Bytes
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.
@ailisp Will update those definitions to take Bytes
instead.
): bigint { | ||
return env.promise_create(accountId, methodName, args, amount, gas); | ||
} | ||
|
||
export function promiseThen( | ||
promiseIndex: number | bigint, | ||
promiseIndex: PromiseIndex, |
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.
Seems you also fixed NearAmount and PromiseIndex typings. That's great!
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.
`Promise result ${ | ||
status == PromiseResult.Failed | ||
? "Failed" | ||
: status == PromiseResult.NotReady |
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.
Nice refactor!
readState(classId), | ||
// Throw if initialized on any subsequent init function calls. | ||
// if (_state) { throw new Error('Contract already initialized'); } | ||
preventDoubleInit(contractMethods[methodName]), |
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.
These helper code generators are really cool! Easier to read then our previous version. cc @volovyks
// reconstruct keys Vector | ||
map.keys = new Vector(data.prefix + "u"); | ||
map.keys = new Vector(`${data.prefix}u`); |
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.
Is this better than +
?
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.
@ailisp It is not better in any performance or conventions, I just prefer it to be template literals, we are using them elsewhere in code so thought might as well keep it the same all over.
// target: AnyObject, | ||
// key: string | symbol, | ||
// descriptor: TypedPropertyDescriptor<Function> | ||
// ) => void; |
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.
Remove or apply these types to functions below?
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.
@ailisp Forgot to remove the comments... I'll need to test out proper typing for these functions.
BTW, do these functions have to actually return an empty function, or anything at all. I want to understand this better so I can actually define proper types.
this.args, | ||
this.amount, | ||
this.gas | ||
); |
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.
My personal feel is above one line isn't too long to be unreadable, but this become kind of lengthy. What is the print width we had for other NEAR JS projects?
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.
@ailisp I haven't set any of those preferences myself, just used prettier
defaults, so we can change this to anything really.
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.
Thanks for the huge cleaning up, refactoring, formatting and lint works! Besides the above smaller points, I think it's particular important to add the format / lint check to CI, should be added in this PR to avoid future changes that breaks the formatting.
@volovyks Do you aware / have certain preference on the eslintrc?
Change _let_ declarations to _const_ Add whitespace to separate code blocks Refactor code
Fix bullet list formatting Add `includeBytes` import
@petarvujovic98 usually when linting is available - we add it a separate job in GitHub actions. Do you think we should do that? |
@volovyks I think we should. I would first like to properly configure |
Change `string` to `Bytes` Format the `Env` interface for more readability
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.
@petarvujovic98 it is a great contribution! Let's merge it.
The only thing I would change is to add a CI for linting.
We can exclude tests and examples temporarily and fix the problem in a separate PR. This one is HUGE :) I want to merge it already.
@volovyks Let me add the CI for linting real quick and let's merge it. |
@volovyks Do we want to add formatting checks into the |
@petarvujovic98 We do not have it in other projects, but let's try it. We can always turn it off. |
@volovyks Done! |
Add ignore comments to empty functions Add CLI files to top level `jsconfig.json` Add top level `jsconfig.json` to ESLint config Add `counter.js` example to examples `jsconfig.json`
Ignore `deps` and `build` folders for prettier Build `near-bindgen.ts` with new comments
Merging, great work! @petarvujovic98 |
Great job! Thanks @petarvujovic98 ! |
I used basic eslint and prettier installs without any configs to format the files.
There were also function refactors for better maintainability and JS standards as well as variable renames.
I also added generics to collections as well as a optional getter options object which has a reconstructor and default value property.