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

Decaff helper jscodemods. #5732

Merged
merged 21 commits into from
Dec 6, 2019

Conversation

sainthkh
Copy link
Contributor

@sainthkh sainthkh commented Nov 19, 2019

User facing changelog

N/A

Additional details

Why was this change necessary?

To decaff faster by doing repetitive and error-prone things with code.

What is affected by this change?

  • jest is added to dependency as jscodeshift only provides jest test utils.
  • 6 more steps are added. Details are below.

Any implementation details to explain?

N/A

How has the developer experience changed?

6 more steps are added to decaff process.

1. Remove sharp comment.

//# -> //

CoffeeScript:

## This is a comment

Decaffeinated:

//# This is a comment

After this PR:

// This is a comment

2. Add eslint-disable-line to empty catch

CoffeeScript

try
  # Do something

Decaffeinated:

try {
  // Do something
} catch (e) {}

After this PR:

try {
  // Do something
} catch (e) {} // eslint-disable-line no-empty

3. switch (false) -> if-else

CoffeeScript:

switch
  when a.isGood:
    it.shouldBeDone()
    break
  when !b.isBad():
    it.checksOK()
    break

Decaffeinated:

switch(false) {
  case !a.isGood:
    it.shouldBeDone()
    break
  case b.isBad():
    it.checksOK()
    break
}

After this PR:

if (a.isGood) {
  it.shouldBeDone()
} else if (!b.isBad()) {
  it.checksOK()
}

Note: As we all know, switch (false) is rarely used in human-written code. And the conversion process is really tedious and error-prone by hand because conditions are reversed.

That's why I've created this mod to fix this easier and in more human-friendly way.

4. Remove comment between arrow and block

CoffeeScript:

reduceMemory = (attrs) ->
  ## comment
  attrs.doSomething()

Decaffeinated:

const reduceMemory = (attrs) =>
// comment
{
  attrs.doSomething()
}

After this PR:

// comment
const reduceMemory = (attrs) =>
{
  attrs.doSomething()
}

Note: Moving { up next to => will be done by eslint.

5. no-cond-assign

CoffeeScript:

if a = c()
  doSomething()

Decaffeinated:

let a

if (a = c()) {
  doSomething()
}

After this PR:

let a

a = c();
if (a) {
  doSomething()
}

6. test functions should not return.

This is reverted because mocha it function can return promise.

PR Tasks

@cypress-bot
Copy link
Contributor

cypress-bot bot commented Nov 19, 2019

Thanks for the contribution! Below are some guidelines Cypress uses when doing PR reviews.

  • Please write [WIP] in the title of your Pull Request if your PR is not ready for review - someone will review your PR as soon as the [WIP] is removed.
  • Please familiarize yourself with the PR Review Checklist and feel free to make updates on your PR based on these guidelines.

PR Review Checklist

If any of the following requirements can't be met, leave a comment in the review selecting 'Request changes', otherwise 'Approve'.

User Experience

  • The feature/bugfix is self-documenting from within the product.
  • The change provides the end user with a way to fix their problem (no dead ends).

Functionality

  • The code works and performs its intended function with the correct logic.
  • Performance has been factored in (for example, the code cleans up after itself to not cause memory leaks).
  • The code guards against edge cases and invalid input and has tests to cover it.

Maintainability

  • The code is readable (too many nested 'if's are a bad sign).
  • Names used for variables, methods, etc, clearly describe their function.
  • The code is easy to understood and there are relevant comments explaining.
  • New algorithms are documented in the code with link(s) to external docs (flowcharts, w3c, chrome, firefox).
  • There are comments containing link(s) to the addressed issue (in tests and code).

Quality

  • The change does not reimplement code.
  • There's not a module from the ecosystem that should be used instead.
  • There is no redundant or duplicate code.
  • There are no irrelevant comments left in the code.
  • Tests are testing the code’s intended functionality in the best way possible.

Internal

  • The original issue has been tagged with a release in ZenHub.

@sainthkh
Copy link
Contributor Author

Failed because of a flaky test.

@jennifer-shehane
Copy link
Member

Very cool. The first 2 are great, the last one scares me a little 😅 - will get @bkucera to look over since he's been heading up the decaff process.

@sainthkh
Copy link
Contributor Author

sainthkh commented Nov 20, 2019

Added 2 more codemods. Check above.

@kuceb
Copy link
Contributor

kuceb commented Nov 20, 2019

@sainthkh wow, this is awesome. I'll pull this down and try it out.

@sainthkh
Copy link
Contributor Author

sainthkh commented Nov 21, 2019

It would be better to add these unit tests to CI.
Note: It is done in Nov 27th.

@sainthkh
Copy link
Contributor Author

sainthkh commented Nov 27, 2019

I realized that packages/server doesn't set up webpack yet. So, exportify and require -> import don't work properly.

I reverted them for now. I'll revert them back when we can use them.

@sainthkh sainthkh force-pushed the issue-2690-decaff-jscodemods branch 3 times, most recently from 958d7fd to d610829 Compare December 2, 2019 10:01
@kuceb
Copy link
Contributor

kuceb commented Dec 2, 2019

still need to try this locally, sorry for the delay

@kuceb kuceb mentioned this pull request Dec 6, 2019
2 tasks
@kuceb
Copy link
Contributor

kuceb commented Dec 6, 2019

added small fix for errors I encountered during a decaf

@cypress
Copy link

cypress bot commented Dec 6, 2019



Test summary

3576 0 44 0


Run details

Project cypress
Status Passed
Commit c682d3c
Started Dec 6, 2019 6:47 PM
Ended Dec 6, 2019 6:51 PM
Duration 04:39 💡
OS Linux Debian - 9.8
Browser Multiple

View run in Cypress Dashboard ➡️


This comment has been generated by cypress-bot as a result of this project's GitHub integration settings. You can manage this integration in this project's settings in the Cypress Dashboard

Copy link
Contributor

@kuceb kuceb left a comment

Choose a reason for hiding this comment

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

  • tested locally

@kuceb
Copy link
Contributor

kuceb commented Dec 6, 2019

might push this as a separate PR so we can have parallel tests

@kuceb kuceb mentioned this pull request Dec 6, 2019
@flotwig flotwig merged commit 994ab32 into cypress-io:develop Dec 6, 2019
@kuceb
Copy link
Contributor

kuceb commented Dec 9, 2019

Going to keep a running list of minor issues I found with these codemods:

  • no-cond-assign: does not account for return if last statement:

current:
coffee

getBar = () ->
  if foo = getFoo()
    bar = 'bar'
myBar = getBar()

js
notice how bar is not returned

const getBar = () => {
  let foo
  foo = getBar()
  if (foo) {
    const bar = 'bar'
  }
}
const myBar = getBar()

desired:

const getBar = () => {
  let foo
  foo = getBar()
  if (foo) {
    const bar = 'bar'
    return bar
  }
}
const myBar = getBar()

@sainthkh
Copy link
Contributor Author

sainthkh commented Dec 9, 2019

@bkucera After a little bit of investigation, I found out that that removing return problem came from fix-implicit-return-assignment.

It has nothing to do with this PR.

Without helpers in this PR

const getBar = function () {
  let foo

  if (foo = getFoo()) {
    let bar

    bar = 'bar'
  }
}
const myBar = getBar()

Without fix-implicit-return-assignment

const getBar = function () {
  let foo

  if (foo = getFoo()) {
    let bar

    return bar = 'bar'
  }
}
const myBar = getBar()

Maybe we need a new codemod that does things like this.

Coffee:

f = () ->
  foo = 'foo'

Decaffeinated:

f = () => {
  let foo
  return foo = 'foo'
}

After codemod:

f = () => {
  let foo
  foo = 'foo'
  return foo
}

Do you want me to make this?

@kuceb
Copy link
Contributor

kuceb commented Dec 10, 2019

thanks @sainthkh! I think it's fine to leave as is, I'll open an issue with the decaffeinate folks.

This also happened while decaffeinating, I can confirm this time it doesn't happen after commenting out the no-cond-assign codemod

image

avallete pushed a commit to avallete/cypress that referenced this pull request Dec 10, 2019
* Added a file that removes comments that start with #.

* Added .eslintrc to silence ts errors in codemods.

* Convert switch (false) to if-else.

* Add disable-eslint no-empty by default after empty catch.

* Reserve comments when converting switch-case.

* Added new codemods to bulk-decaffeinate.

* Preserve comments.

* Ignored lint rules for test fixtures.

* Move comments between arrow and block to the top.

* Merged tests into one file.

* Turned off no-undef for test fixtures.

* No-cond-assign

* Added jscodemods to decaff config.

* Fixed typo.

* Make CI test codemods

* Added test-no-return.

* Revert "Added test-no-return."

This reverts commit d610829.

* fix arrow-comment


Co-authored-by: Ben Kucera <14625260+Bkucera@users.noreply.github.com>
Co-authored-by: Zach Bloomquist <github@chary.us>
@sainthkh sainthkh mentioned this pull request Apr 30, 2020
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.

None yet

4 participants