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

Refactor the project and add generics and getter options #213

Merged
merged 15 commits into from
Sep 20, 2022

Conversation

petarvujovic98
Copy link
Contributor

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.

@ailisp
Copy link
Member

ailisp commented Sep 13, 2022

Thank you @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.

These parts are great! Function refactor and variable renames also look good.

I also added generics to collections as well as a optional getter options object which has a reconstructor and default value property.

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.

@ailisp
Copy link
Member

ailisp commented Sep 13, 2022

Also, just a small suggestion: it'll be great to separate the refactor commit with the reformatting next time, to make code review easier

@petarvujovic98
Copy link
Contributor Author

@ailisp Of course, I'll wait for the generics merge and rebase this PR.

@ailisp
Copy link
Member

ailisp commented Sep 13, 2022

@volovyks CI was not triggering for this PR, do you know why

@volovyks
Copy link
Collaborator

@ailisp that is because @petarvujovic98 is not a part of our GitHub org. I have started a discussion about it in Slack.

@petarvujovic98
Copy link
Contributor Author

@ailisp I just saw this comment now, I could separate the reformat and refactor after I rebase the code. This was just me going fast

@ailisp
Copy link
Member

ailisp commented Sep 15, 2022

@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.

@petarvujovic98
Copy link
Contributor Author

@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
@petarvujovic98
Copy link
Contributor Author

@ailisp @volovyks I just did a refactor of this PR. Please review and add any comments on the changes.

module.exports = {
root: true,
env: {
es2021: true,
Copy link
Member

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

Copy link
Contributor Author

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
Copy link
Member

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

Copy link
Contributor Author

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;
Copy link
Member

@ailisp ailisp Sep 20, 2022

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

Copy link
Contributor Author

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,
Copy link
Member

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!

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@ailisp This does not actually fix the #118 problem. Maybe I could create another PR after this one to address this. For now this only aliases number | bigint to PromiseIndex. We should first agree on a good API for this (I'll have a better look at the Rust implementation for inspiration).

`Promise result ${
status == PromiseResult.Failed
? "Failed"
: status == PromiseResult.NotReady
Copy link
Member

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]),
Copy link
Member

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`);
Copy link
Member

Choose a reason for hiding this comment

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

Is this better than +?

Copy link
Contributor Author

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;
Copy link
Member

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?

Copy link
Contributor Author

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
);
Copy link
Member

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?

Copy link
Contributor Author

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.

Copy link
Member

@ailisp ailisp left a 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
@volovyks
Copy link
Collaborator

@petarvujovic98 usually when linting is available - we add it a separate job in GitHub actions. Do you think we should do that?

@petarvujovic98
Copy link
Contributor Author

@volovyks I think we should. I would first like to properly configure ESLint for this project as it seems that the JavaScript files in the tests and examples are getting false errors about not having experimentalDecorators turned on, when they definitely do.

Change `string` to `Bytes`
Format the `Env` interface for more readability
Copy link
Collaborator

@volovyks volovyks left a 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.

@petarvujovic98
Copy link
Contributor Author

@volovyks Let me add the CI for linting real quick and let's merge it.

@petarvujovic98
Copy link
Contributor Author

@volovyks @ailisp Should be ready to merge now.

@petarvujovic98
Copy link
Contributor Author

@volovyks Do we want to add formatting checks into the Lint workflow as well?

@volovyks
Copy link
Collaborator

@petarvujovic98 We do not have it in other projects, but let's try it. We can always turn it off.

@petarvujovic98
Copy link
Contributor Author

@volovyks Done!

@volovyks volovyks linked an issue Sep 20, 2022 that may be closed by this pull request
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
@volovyks
Copy link
Collaborator

Merging, great work! @petarvujovic98

@ailisp
Copy link
Member

ailisp commented Sep 21, 2022

Great job! Thanks @petarvujovic98 !

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add formatting/linting to project
3 participants