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

fix: constructor validation on build #226

Merged
merged 29 commits into from
Oct 26, 2022

Conversation

osalkanovic
Copy link
Contributor

#224

I have commented unordered map constructor to simulate issue and I expect to CI fail.
When someone review PR I will return previous state of unordered map :)

@osalkanovic
Copy link
Contributor Author

Output from CI: image

@volovyks
Copy link
Collaborator

@osalkanovic you can add tests that can expect something to fail or not to fail, check examples here: https://github.com/near/near-sdk-js/blob/develop/tests/__tests__/decorators/payable.ava.js

The fact that the constructor exists does not mean that all the variables were initialized in it. So users can get errors anyway.

@osalkanovic osalkanovic marked this pull request as draft September 20, 2022 10:44
@ailisp ailisp linked an issue Sep 23, 2022 that may be closed by this pull request
@osalkanovic osalkanovic marked this pull request as ready for review October 9, 2022 03:47
@osalkanovic
Copy link
Contributor Author

@ailisp @volovyks its ready for review.
Now we have covered all cases.
Idk why I have linter issue 🥲

@osalkanovic
Copy link
Contributor Author

And ya,
ts-morph is amazing tool we can cover other cases if necessary

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.

@osalkanovic wow, it's a great approach! I think this solution is great. Let's address a few comments and merge it.

P.S. Just run yarn lint and yarn format before the commit. It will fix most of the linter errors.

tests/src/constructor-validation/version-4.ts Outdated Show resolved Hide resolved
tests/__tests__/constructor_validation.ava.js Outdated Show resolved Hide resolved
tests/__tests__/constructor_validation.ava.js Outdated Show resolved Hide resolved
tests/__tests__/constructor_validation.ava.js Outdated Show resolved Hide resolved
tests/__tests__/constructor_validation.ava.js Outdated Show resolved Hide resolved
t.is(result.status, 2);
});

test("should throw error, construcor isnt declared", async (t) => {
Copy link
Collaborator

Choose a reason for hiding this comment

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

It would be great to compare error text here. Can we do that?

Copy link
Member

Choose a reason for hiding this comment

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

I think result.stdout / result.stderr records the error text

Copy link
Collaborator

Choose a reason for hiding this comment

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

The validation error is overridden with another top-level error. Added const for now.

if (hasBindgen) {
const constructors = contractClass.getConstructors();
const hasConstructor = constructors.length > 0;
const propertiesToBeInited = properties.filter((p) => !p.initializer);
Copy link
Collaborator

Choose a reason for hiding this comment

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

How does it work with JS contracts?

Copy link
Collaborator

Choose a reason for hiding this comment

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

Do not work for JS contracts due to the absence of parameter declaration.

cli/contract_validation.js Outdated Show resolved Hide resolved
cli/contract_validation.js Outdated Show resolved Hide resolved
cli/cli.js Outdated
@@ -86,6 +86,7 @@ async function checkTsBuildWithTsc(sourceFileWithPath) {

// Common build function
async function createJsFileWithRullup(sourceFileWithPath, rollupTarget) {
await validateContract(sourceFileWithPath);
Copy link
Member

Choose a reason for hiding this comment

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

Nice step! All existing contracts can pass the validation

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.

Great work! Besides @volovyks 's comprehensive comments, it looks good to me.

@osalkanovic
Copy link
Contributor Author

Great work! Besides @volovyks 's comprehensive comments, it looks good to me.

@volovyks @ailisp I will update PR today

@volovyks
Copy link
Collaborator

Hey @osalkanovic ! This PR is getting outdated, it will be hard to continue your work on it if we postpone it. Do you have the capacity to continue doing this?

@volovyks volovyks force-pushed the fix/constructor-validation branch from 401453c to 7e0eb28 Compare October 24, 2022 09:25
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.

@ailisp I have refactored the code. Rewrote everything in TS, moved contract validation to the top level, and rewrote tests. Let's merge it if you are ok with my changes.

@ailisp
Copy link
Member

ailisp commented Oct 26, 2022

@volovyks Looks great 👍 The original functionality by @osalkanovic is good, and your refactors, rewrote tests and in TS makes the code quality better than I expected. Merge now

@ailisp ailisp merged commit 04a0b6d into near:develop Oct 26, 2022
@volovyks
Copy link
Collaborator

@osalkanovic thanks for the help!

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.

State issue needs clarification (hackathon feedback)
3 participants