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

Keep fparens #1371

Closed
wants to merge 2 commits into from
Closed

Keep fparens #1371

wants to merge 2 commits into from

Conversation

gutnikov
Copy link

@gutnikov gutnikov commented Nov 9, 2016

This does basically the same as proposed in #1307
Main benefit of this implementation is that it allows you to keep parentheses where you want,
without wrapping every function. So to wrap or not to wrap is up to user.

@avdg
Copy link
Contributor

avdg commented Nov 9, 2016

Hmmm... not really sure if relying on AST_Node.start is a good idea, especially given the lack of tests we have (I didn't saw many mocha tests on this and mocha tests are relative new) on them and the likelihood of AST_Node.start not being consistent.

@avdg
Copy link
Contributor

avdg commented Nov 9, 2016

There is some guarantee that this code will work if these code involving tokens got locked into tests.

https://github.com/mishoo/UglifyJS2/blob/642273c29002e7676719f489d7fcf552974118f4/lib/parse.js#L1438

@@ -69,6 +69,7 @@ function OutputStream(options) {
quote_style : 0,
keep_quoted_props: false,
wrap_iife : false,
keep_fparens : false
Copy link
Contributor

Choose a reason for hiding this comment

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

add trailing comma

describe("Keep wrapping parentheses", function() {
it("Should keep wrapping parentheses if keep-fparens option is turned on", function() {
var originalCode = "define(\"module\",(function() {module.exports = 42;}));";
var expectedCode = "define(\"module\",(function(){module.exports=42}));";
Copy link
Contributor

Choose a reason for hiding this comment

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

use outer single quoted string to avoid need for escaping


it("Should strip wrapping parentheses if keep-fparens option is turned off or not set", function() {
var originalCode = "define(\"module\",(function() {module.exports = 42;}));";
var expectedCode = "define(\"module\",function(){module.exports=42});";
Copy link
Contributor

Choose a reason for hiding this comment

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

use outer single quoted string to avoid need for escaping

@kzc
Copy link
Contributor

kzc commented Nov 9, 2016

Something to be aware of - this PR would not work for Crockford style IIFEs:

(function(){
    console.log("hello");
}());

PR would also need to update README.

if (output.option('keep_fparens')) {
if (this.start && this.start instanceof AST_Token && this.start.value === '(' &&
this.end && this.end instanceof AST_Token && this.end.value === ')') {
return true;
Copy link
Contributor

Choose a reason for hiding this comment

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

indent by 4

@avdg
Copy link
Contributor

avdg commented Nov 10, 2016

I would prefer looking data gathering on parser level personally. Then store that data in the AST tree.

@gutnikov
Copy link
Author

avdg, do you mean the approach like:

  1. when met function expression - check if it wrapped in parentheses
  2. if so, put a flag to its ast node
  3. on output: check the flag, and if true - wrap it

right?

@gutnikov
Copy link
Author

avdg, smth like that while parsing:

              case "(":
                next();
                var ex = expression(true);
                ex.start = start;
                ex.end = S.token;
                expect(")");
                if (ex instanceof AST_Function) {
                    ex.wrappingParens = true;
                }
                return subscripts(ex, allow_calls);

@avdg
Copy link
Contributor

avdg commented Nov 11, 2016

yeah, that would be more stable long term I believe. The only obstacle would be implementation. If that fails (preferably the parser still needs to be fast, but that should be possible if the checks are simple as it is the case now), we can stick to this method for now I guess...

@avdg
Copy link
Contributor

avdg commented Nov 11, 2016

Don't forget to update the AST definitions when adding properties here: https://github.com/mishoo/UglifyJS2/blob/master/lib/ast.js

@gutnikov
Copy link
Author

Ok, i'll take this approach then. Thanks!

@kzc
Copy link
Contributor

kzc commented Nov 11, 2016

                ex.wrappingParens = true;

Use snake_case rather than camelCase for data members.

parens would be a better name.

@gutnikov
Copy link
Author

kzc, ok, thanx! will pay attention to all code-style issues

@alexlamsl alexlamsl closed this Mar 31, 2017
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.

4 participants