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

Retaining of comments and their position in the actual code after parsing. #465

Merged
merged 9 commits into from
Aug 10, 2018

Conversation

ravikishore1979
Copy link
Contributor

I wanted to use rhino parser to generate a new js file from existing js file after some modifications to some methods or variable names etc. for example as below

old JS file:
`Application.run(function ($rootScope) {
"use strict";
/* perform any action on the variables within this block(on-page-load) /
$rootScope.onAppVariablesReady = function () {
/

* variables can be accessed through '$rootScope.Variables' property here
* e.g. $rootScope.Variables.staticVariable1.getData()
*/
};

});`

Post Refactoring:
/* perform any action on the variables within this block(on-page-load) */ App.onAppVariablesReady = function () { /* * variables can be accessed through '$rootScope.Variables' property here * e.g. $rootScope.Variables.staticVariable1.getData() */ };
Using rhino parser I was able to achieve this but comments are lost. So providing a fix such that if "CompilerEnvirons.setRecordingComments(true)" then comments are added to the AST.

kraviksihore and others added 3 commits July 18, 2018 14:08
 statement like c = a + b //test
 + d
Added pom.xml for WM to generate jar and push to repository.
Added minimum Java version requirement for compilation
- Handling of inline comments for "do" satement in a do-while
- Modified toSource methods of do and while AstNodes for handling of inline comments.
Handling of String literals if they are enclosed within backtick (`)
- Handling of inline comments if used after try, with, case
- adding messages resources to rhino.jar itself.
- Inline comments at the case level are handled properly
- Inline comments within object literal are ignored.
- Fix below scenario
     if(a == 3) {
         b = 3;
     }
     //inlinecomment 1
     else {
        b = 5;
     }
- Fix for a bug in function with no argument after last comma
     f1(a,b,);
- Added a setter method for updating the comment text.
- Modified minor version to avoid manual copy of new jar.
- Modified minor version to avoid manual copy of new jar. because in previous build, resource files are not copied.
- ignoring comments if they are placed after an incomplete statement like
   var a = 3
   /*  */ var t=3;
@gbrail
Copy link
Collaborator

gbrail commented Aug 2, 2018

Thanks for doing this -- it looks like a cool use of Rhino.

Since this PR makes a ton of changes to Parser, which is pretty important, I wanted to make sure that we were careful with it.

I see changes in here that affect comments in a lot of places -- while loops, for loop, if statements, etc. However, the only new test case is pretty trivial. How do we ensure going forward that all the different types of comments here are supported? Would it be possible to make the test suite for this PR more extensive?

Also, it looks from the test changes that consumers of the AST will have to change as a result of this PR. Does this only affect use cases in which the flag is set in CompilerEnvirons? I just want to understand the impact of this PR on backward compatibility, and put it clearly in the commit messages and eventually the release notes.

Finally, would it be possible to rebase and squash this into fewer commits? This isn't a hard and fast rule but when I see commits like "remove pom file" then I'd rather squash a PR and get those out of the Rhino commit history.

Thansk!

@ravikishore1979
Copy link
Contributor Author

Thanks for the comments.

This changes will not effect backward Compatibility as this will be executed only if "recordingComments" of "CompilerEnvirons" is set.

I'll make below changes and then reply.

  1. Rebase and squash with fewer commits.
  2. Write extensive test cases for this PR.

kraviksihore and others added 4 commits August 6, 2018 12:03
 statement like c = a + b //test
 + d
Added pom.xml for WM to generate jar and push to repository.
Added minimum Java version requirement for compilation
- Handling of inline comments for "do" satement in a do-while
- Modified toSource methods of do and while AstNodes for handling of inline comments.
Handling of String literals if they are enclosed within backtick (`)
- Handling of inline comments if used after try, with, case
- adding messages resources to rhino.jar itself.
- Inline comments at the case level are handled properly
- Inline comments within object literal are ignored.
- Fix below scenario
     if(a == 3) {
         b = 3;
     }
     //inlinecomment 1
     else {
        b = 5;
     }
- Fix for a bug in function with no argument after last comma
     f1(a,b,);
- Added a setter method for updating the comment text.
- Modified minor version to avoid manual copy of new jar.
- Modified minor version to avoid manual copy of new jar. because in previous build, resource files are not copied.
- ignoring comments if they are placed after an incomplete statement like
   var a = 3
   /*  */ var t=3;
- Ignoring inline comments which come within an expression or  statement
like c = a + b //test  + d
- Handling of inline comments for "do" satement in a do-while
- Modified toSource methods of do and while AstNodes for handling of
inline comments.
- Handling of String literals if they are enclosed within backtick (`)
- Handling of inline comments if used after try, with, case - adding
messages resources to rhino.jar itself.
- Inline comments at the case level are handled properly
- Inline comments within object literal are ignored.
- Fix below scenario
     if(a == 3) {
        b = 3;
     }  //inlinecomment 1
     else {
        b = 5;
     }
- Fix for a bug in function with no argument after last comma.
    f1(a,b,);
- Added a setter method for updating the comment text.
@gbrail
Copy link
Collaborator

gbrail commented Aug 6, 2018 via email

- Added test cases testing inline comments of if, for, loops
@ravikishore1979
Copy link
Contributor Author

ravikishore1979 commented Aug 8, 2018

Hi Greg Brail,
I couldn't find the reason for the failure of build. Can you help out to fix this issue as local build this passed?

@gbrail
Copy link
Collaborator

gbrail commented Aug 9, 2018

I think we still have some flaky tests in the suite -- I re-ran the Travis job and it passed. So I think you're good to go from a test perspective.

@ravikishore1979
Copy link
Contributor Author

Thank you Greg Brail.
I am done with writing test cases for the inline comments used in loops, conditional blocks, try-catch etc.

@gbrail
Copy link
Collaborator

gbrail commented Aug 10, 2018

This looks good. I'm going to squash it because it's got a bunch of "remove pom.xml" kinds of commits.

@gbrail gbrail merged commit 731bf18 into mozilla:master Aug 10, 2018
@ravikishore1979
Copy link
Contributor Author

Thanks Greg Brail.

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.

2 participants