Make Cypress' open source repo more accessible to contributors. Improve code quality by implementing types.
Use this checklist to make converted JS code more readable.
Summary:
- [ ] Remove unused variables, arguments, functions
- [ ] Convert to arrow functions
- [ ] Fix weird conditional expressions
- [ ] Move variables close to where they're used
- [ ] Change `switch` to `if` if it has only one case (with `default`)
- [ ] Drag down comments
- [ ] Remove unnecessary `return`s.
- [ ] Remove functions when they're used to assign a value
Copy above to check your code.
As CoffeeScript doesn't show which names are unused, we can find a lot of unused variables, arguments, or functions.
const func = (a, b) => {
const c = 'hi?'
return a + 3
}
const unusedFunc = (a, b, c) => {
// something complicated going on.
}
const d = func(3)
const func = (a) => {
return a + 3
}
const d = func(3)
jscodemod
converts some of them, but it's not perfect. So, read the code and a function doesn't have a this
inside, convert it to an arrow function.
const f = function (a, b) {
// do something
}
const f = (a, b) => {
// do something
}
Note: If you're a VS Code user, you can do this faster with JS Refactor plugin. Press Ctrl + Shift + J A
. When pressing A
, you should not press ctrl
and shift
.
Converted JS code of CoffeeScript ?.
syntax is really weird. So, we need to read JS code and original CoffeeScript code carefully and convert them manually.
x = $el?.length ? 0
```js
const x = ($el != null ? $el.length : undefined) != null ? ($el != null ? $el.length : undefined) : 0
### π Good:
```js
const x = $el != null ? $el.length
// or
const x = $el ? $el.length
Note: There are 2 answers here because ?.
ignores falsey values. So, check the type of variable carefully and how it is used.
Sometimes, converted JavaScript variables are at the top of the function when those are used at the bottom of the function. Move them near to the line it is used.
const f = () => {
let x;
// Do something long here.
if (x > 10) {
x = 30;
} else {
x = 3;
}
}
const f = () => {
// Do something long here.
let x;
if (x > 10) {
x = 30;
} else {
x = 3;
}
}
switch
to if
if it has only one case (with default
)It's really weird to read a switch
statement with a single case (+ default).
switch(type) {
case 'dom':
// do something
default:
// do else
}
if (type === 'dom') {
// do something
} else {
// do else
}
Sometimes, comments are next to if conditions. Bring them down.
if (cond) { // Comment 1 and the tester's stone
doSomething()
} else { // Comment 2 and the chamber of tests
doElse()
}
if (cond) {
// Comment 1 and the tester's stone
doSomething()
} else {
// Comment 2 and the chamber of tests
doElse()
}
return
s.Especially, in the converted test codes, there are unnecessary return
s. Let's remove them.
beforeEach(() => {
return cy.visit('/some-web-page')
})
beforeEach(() => {
cy.visit('/some-web-page')
})
EDIT on 4/14/2020
When the return
is used in test cases, you should be careful. Because mocha uses return
to check when async tests end.
To remove it, you need to check the type of the returned value. If it's a promise, don't remove return
.
const type = (() => {
if (env.get('CYPRESS_CI_KEY')) {
return 'CYPRESS_CI_DEPRECATED_ENV_VAR'
}
return 'CYPRESS_CI_DEPRECATED'
})()
This is the translation of CoffeeScript when you assign a value directly from switch statement like below:
type = switch
when env.get("CYPRESS_CI_KEY")
"CYPRESS_CI_DEPRECATED_ENV_VAR"
else
"CYPRESS_CI_DEPRECATED"
let type = 'CYPRESS_CI_DEPRECATED'
if (env.get('CYPRESS_CI_KEY')) {
type = 'CYPRESS_CI_DEPRECATED_ENV_VAR'
}
If you have any idea to improve this list, please leave me a comment or directly fix this.
decaffeinate:
(as shown in this PR: https://github.com/cypress-io/cypress/pull/6784) the main point of these is to maintain the git history of the file for future reference - it will also leave the older CoffeeScript file for reference so you can compare during cleanup but this needs to be deleted before committing again.I'm going to close this as resolved. The conversion to TypeScript is not complete, but I think this is less pressing and will come over time.
I don't think there's any CoffeeScript left (aside from testing that coffeescript works in Cypress). It's certainly less than 1% on GitHub's chart.
Most helpful comment
Decaffeination Guide
Use this checklist to make converted JS code more readable.
Summary:
Copy above to check your code.
Remove unused variables, arguments, functions
As CoffeeScript doesn't show which names are unused, we can find a lot of unused variables, arguments, or functions.
π Bad:
π Good:
Covert to arrow functions
jscodemod
converts some of them, but it's not perfect. So, read the code and a function doesn't have athis
inside, convert it to an arrow function.π Bad:
π Good:
Note: If you're a VS Code user, you can do this faster with JS Refactor plugin. Press
Ctrl + Shift + J A
. When pressingA
, you should not pressctrl
andshift
.Fix weird conditional expressions
Converted JS code of CoffeeScript
?.
syntax is really weird. So, we need to read JS code and original CoffeeScript code carefully and convert them manually.π Bad:
```js
const x = ($el != null ? $el.length : undefined) != null ? ($el != null ? $el.length : undefined) : 0
Note: There are 2 answers here because
?.
ignores falsey values. So, check the type of variable carefully and how it is used.Move variables close to where it is used.
Sometimes, converted JavaScript variables are at the top of the function when those are used at the bottom of the function. Move them near to the line it is used.
π Bad:
π Good:
Change
switch
toif
if it has only one case (withdefault
)It's really weird to read a
switch
statement with a single case (+ default).π Bad:
π Good:
Drag down comments
Sometimes, comments are next to if conditions. Bring them down.
π Bad:
π Good:
Remove unnecessary
return
s.Especially, in the converted test codes, there are unnecessary
return
s. Let's remove them.π Bad:
π Good:
EDIT on 4/14/2020
When the
return
is used in test cases, you should be careful. Because mocha usesreturn
to check when async tests end.To remove it, you need to check the type of the returned value. If it's a promise, don't remove
return
.Remove functions when they're used to assign a value
π Bad:
This is the translation of CoffeeScript when you assign a value directly from switch statement like below:
π Good:
If you have any idea to improve this list, please leave me a comment or directly fix this.