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

Flatten function expression parameters in hugged last argument #9662

Merged
merged 1 commit into from Nov 14, 2020

Conversation

thorn0
Copy link
Member

@thorn0 thorn0 commented Nov 13, 2020

Fixes #3376
This was done for arrow functions in #1305 ("Avoid breaking arguments for last arg expansion").
This PR enables the same for function expressions unless the function expression is the only argument.

Before:

function* mySagas() {
  yield effects.takeEvery(rexpress.actionTypes.REQUEST_START, function*({
    id
  }) {
    console.log(id);
    yield rexpress.actions(store).writeHead(id, 400);
    yield rexpress.actions(store).end(id, "pong");
    console.log("pong");
  });
}

After:

function* mySagas() {
  yield effects.takeEvery(
    rexpress.actionTypes.REQUEST_START,
    function* ({ id }) {
      console.log(id);
      yield rexpress.actions(store).writeHead(id, 400);
      yield rexpress.actions(store).end(id, "pong");
      console.log("pong");
    }
  );
}

Where did "unless the function expression is the only argument" come from? From code I found in the tests:

export class Thing12 implements OtherThing {
  do: (type: Type) => Provider<Prop> = memoize(function (
    type: ObjectType
  ): Provider<Opts> {
    return type;
  });
}

Apparently we want to keep it hugged.

  • I’ve added tests to confirm my change works.
  • (If changing the API or CLI) I’ve documented the changes I’ve made (in the docs/ directory)
  • (If the change is user-facing) I’ve added my changes to changelog_unreleased/*/pr-XXXX.md file following changelog_unreleased/TEMPLATE.md.
  • I’ve read the contributing guidelines.

Try the playground for this PR

@fisker
Copy link
Member

fisker commented Nov 13, 2020

@thorn0 sosukesuzuki/prettier-regression-testing#8 (comment)

Less changes than I thought it would be.

options,
args &&
args.expandLastArg &&
path.getParentNode().arguments.length > 1
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
path.getParentNode().arguments.length > 1
getCallArguments(path.getParentNode()).length > 1

Copy link
Member Author

Choose a reason for hiding this comment

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

We don't have such a function.

Copy link
Member Author

Choose a reason for hiding this comment

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

But I see what you mean...

Prettier pr-9662
Playground link

--parser typescript

Input:

a=import(function(){})

Output:

TypeError: Cannot read property 'length' of undefined
    at https://deploy-preview-9662--prettier.netlify.app/lib/standalone.js:15:230593
    at Object.print (https://deploy-preview-9662--prettier.netlify.app/lib/standalone.js:15:270143)
    at Ri (https://deploy-preview-9662--prettier.netlify.app/lib/standalone.js:15:115017)
    at https://deploy-preview-9662--prettier.netlify.app/lib/standalone.js:15:114478
    at Object.printComments (https://deploy-preview-9662--prettier.netlify.app/lib/standalone.js:15:110760)
    at e (https://deploy-preview-9662--prettier.netlify.app/lib/standalone.js:15:114458)
    at https://deploy-preview-9662--prettier.netlify.app/lib/standalone.js:15:203119
    at Ti.call (https://deploy-preview-9662--prettier.netlify.app/lib/standalone.js:15:112552)
    at f (https://deploy-preview-9662--prettier.netlify.app/lib/standalone.js:15:203110)
    at Ti.call (https://deploy-preview-9662--prettier.netlify.app/lib/standalone.js:15:112552)

Copy link
Member

Choose a reason for hiding this comment

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

Yes we do... #9608

Fixes prettier#3376
This was done for arrow functions in prettier#1305 ("Avoid breaking arguments for last arg expansion").
This PR enables the same for function expressions unless the function expression is the only argument.
@fisker

This comment has been minimized.

@thorn0 thorn0 merged commit e721ef3 into prettier:master Nov 14, 2020
@thorn0 thorn0 deleted the fix-3376 branch November 14, 2020 11:17
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Nov 15, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Formatting off for indented parameters to function
3 participants