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

Contract Definition comments #172

Merged
merged 9 commits into from
Feb 27, 2020
Merged

Conversation

Janther
Copy link
Contributor

@Janther Janther commented Jul 1, 2019

fixes #162

in this PR there are 2 new features.

  1. the indentation on BlockComments is now aligned with the indentation of the subject of that comment.
  2. many edge cases on comments around contract definitions are addressed.

input

contract A {
    // solhint-disable-previous-line no-empty-blocks
}

output won't even print currently.

after this PR

contract A {
    // solhint-disable-previous-line no-empty-blocks
}

Scenarios, where we were depending on prettier to guess where the comment should go, are now addressed.
input:

contract A /*comment about the name*/ is X /*interface X because ...*/, Y /*contract Y because ...*/, Z /*implements Z because ...*/ {
    // solhint-disable-previous-line no-empty-blocks
}

Output:

contract A is
    /*comment about the name*/
    X, /*interface X because ...*/
    Y, /*contract Y because ...*/
    Z /*implements Z because ...*/
    // solhint-disable-previous-line no-empty-blocks
{}

After this PR:

/*comment about the name*/
contract A is
    X, /*interface X because ...*/
    Y, /*contract Y because ...*/
    Z /*implements Z because ...*/
{
    // solhint-disable-previous-line no-empty-blocks
}

To address the Contract Definition edge cases I created the following functions
solidityHandleOwnLineComment, solidityHandleEndOfLineComment, and solidityHandleRemainingComment, these functions try to match the comments with Solidity's cases first and if no match was found, they call the javascript comment handler respectively.

My idea is to bring add Solidity's edge cases in this folder. and even redoing the javascript handlers in a way that makes sense with Solidity's AST.
With time we will rely less on the javascript handlers while fully using the utils.

Finally I had to add the package-lock.json because Travis kept installing the wrong eslint and failing to run the linting.

@codecov
Copy link

codecov bot commented Jul 1, 2019

Codecov Report

Merging #172 into master will increase coverage by 0.02%.
The diff coverage is n/a.

Impacted file tree graph

@@            Coverage Diff             @@
##           master     #172      +/-   ##
==========================================
+ Coverage   99.68%   99.70%   +0.02%     
==========================================
  Files          79       84       +5     
  Lines         628      677      +49     
  Branches      104      115      +11     
==========================================
+ Hits          626      675      +49     
  Misses          2        2              
Impacted Files Coverage Δ
src/comments/printer.js 94.44% <0.00%> (ø)
src/comments/handler.js 100.00% <0.00%> (ø)
src/comments/index.js 100.00% <0.00%> (ø)
src/comments/handlers/ContractDefinition.js 100.00% <0.00%> (ø)
src/nodes/print-comments.js 100.00% <0.00%> (ø)

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update a5bb07f...fb9d154. Read the comment docs.

.gitignore Outdated
@@ -62,6 +62,4 @@ typings/

.DS_Store

package-lock.json
Copy link
Member

Choose a reason for hiding this comment

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

not sure if I like this idea, we may want to instruct travis to use the proper eslint version perhaps

Copy link
Contributor Author

Choose a reason for hiding this comment

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

There's no rush for this PR so I'll see if after a rebase there is some improvement.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I rebased the branch to the latest release with the updated dependencies and the travis uses the right eslint without needing package-lock.json

Copy link
Member

Choose a reason for hiding this comment

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

@mattiaerre I'm curious, why don't you like this idea? AFAIK versioning the package-lock.json or yarn.lock is a pretty standard practice.

@@ -1,6 +1,6 @@
// eslint-disable-next-line no-unused-vars
function clean(ast, newObj, parent) {
['code', 'codeStart', 'loc', 'range'].forEach(name => {
['code', 'codeStart', 'loc', 'range', 'raw'].forEach(name => {
Copy link
Member

Choose a reason for hiding this comment

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

even here, not sure if this is how we should be dealing w/ this

Copy link
Contributor Author

Choose a reason for hiding this comment

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

in my opinion, comments shouldn't even be in AST comparison.
Prettier throws an exception already if we failed to print a comment.

const { handleComments } = require('./prettier-comments');
// const { handleComments } = require('./prettier-comments');
// const printComment = require('./comments/printer');
const { handleComments, printComment } = require('./comments');
Copy link
Member

Choose a reason for hiding this comment

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

uhm interesting

* @dev Tells the address of the implementation where every call will be delegated.
* @return address of the implementation to which it will be delegated
*/
* @dev Tells the address of the implementation where every call will be delegated.
Copy link
Member

Choose a reason for hiding this comment

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

I like this 👍

Copy link
Contributor Author

Choose a reason for hiding this comment

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

me too. maybe I could split this PR into

  1. fixing comment indentation.
  2. Contract Definition edge cases

@Janther Janther force-pushed the bugfix/contract_definition_comments branch 2 times, most recently from 549bdaa to f910da2 Compare July 8, 2019 22:12
@fvictorio
Copy link
Member

@Janther What is this PR missing? Can I help somehow?

@Janther
Copy link
Contributor Author

Janther commented Aug 4, 2019

@fvictorio sorry for the wait, actually I'm happy with the PR but I'm open to improvements/suggestions to be made to it.

As stated in #181 the parser will begin including natspec comments from version 0.5.0 and that breaks some things so I believe we should start processing comments in a solidity way instead of a JS way, and I believe this is the first step towards that so I'd like us all to be on board with it.

@mattiaerre
Copy link
Member

can you resolve the conflicts here too @Janther ? thanks

@yxliang01
Copy link

Looking forward to this PR being merged! This is really important to my project :) & thanks for your work!

@Janther Janther force-pushed the bugfix/contract_definition_comments branch from f910da2 to 707659e Compare September 25, 2019 01:34
@mattiaerre
Copy link
Member

mattiaerre commented Sep 29, 2019

I'd be inclined to merge this PR; shall we add more tests around it so that the code coverage will not drop too much?

@Janther Janther force-pushed the bugfix/contract_definition_comments branch from 707659e to fb9d154 Compare February 24, 2020 23:22
@Janther
Copy link
Contributor Author

Janther commented Feb 24, 2020

Conflicts resolved, not sure why codecov is still angry, basically the only 2 lines that are not covered are the exception when there's no printer for this node (the parser had an update) or there's no printer for this comment (extract comments had an update).

please have a look @mattiaerre @fvictorio

@mattiaerre
Copy link
Member

mattiaerre commented Feb 25, 2020

the problem here is that we've removed raw from the clean function and so codecov is complaining as it runs tests w/o the AST_COMPRARE flag; I'll be happy to go ahead and merge as it is since this is only affecting comments; what do you think @Janther @fvictorio ?

actually I am not entirely sure about this ^^ 🤔 let me spend some more time on this

@mattiaerre
Copy link
Member

@fvictorio
Copy link
Member

I'm OK with merging this.

@jerrode
Copy link

jerrode commented Feb 27, 2020

Hey everyone, Jerrod from @codecov here. Love the high coverage / testing approach you've taken.

Quick note: The check that is failing is patch coverage not project coverage. Patch = the coverage on the incremental branch, which basically means you must hit 100% coverage on added code.

image

If there is a specific test that isn't covering that you think should be, have you checked the underlying coverage report to see if the line is covered out of your CI?

https://codecov.io/gh/prettier-solidity/prettier-plugin-solidity/commit/a5bb07fcb86ba1c579f44090bb7558d66fbd22a0/build

@fvictorio
Copy link
Member

Hi @jerrode, thanks for the help!

@Janther @mattiaerre Apparently the uncovered bit is in printer.js: the throw in the default case for the comment type. It sounds like we are just being defensive there, so I think we can just ignore it.

@mattiaerre
Copy link
Member

thanks so much for pointing that out @jerrode that's awesome. so you are suggesting @fvictorio that we can just merge this as is? I'm fine w/ that

@fvictorio
Copy link
Member

It's up to @Janther, but yes, I'm ok with merging this.

@Janther
Copy link
Contributor Author

Janther commented Feb 27, 2020

I’m happy to merge, I know what’s not being covered, I just don’t know why codecov is that angry hehe

@Janther
Copy link
Contributor Author

Janther commented Feb 27, 2020

Can’t merge from my phone though

@Janther Janther merged commit 794f8a6 into master Feb 27, 2020
@Janther Janther deleted the bugfix/contract_definition_comments branch February 27, 2020 23:54
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.

Empty contract won't print comments inside.
5 participants